User:Opensource Obscure/http/gridurl-sample

From Second Life Wiki
Jump to navigation Jump to search

WARNING

This may be out-of-date, broken, whatever...if it's broken and you know what you're doing, fix it.

Sample script for persistent URL using Gridurl

Grid URL Persister (gridurl.appspot.com) was created by Latif Khalifa to make work with http-in easier. Here's a sample script that shows how to use Gridurl.

// Go to http://gridurl.appspot.com/random and copy the UUID 
// that will be generated, then paste it below. 
string gridurl_key="CHANGE-ME"; 

// This message will be seen by users on the web page after 
// they send their message, so you may want to customize it:
string risposta = "Thanks! Please use the button in your browser to go back."; 

// ---------------------------------------------------
string gridurl_indirizzo = "http://gridurl.appspot.com/go/";
string baseurl = "http://gridurl.appspot.com/reg?service=";
key mykey;
string url;
string query;
key requestid;

setup()
{
    url = "";
    llRequestURL();
}


// risponde via HTTP
send_response(key id, string body)
{
    llHTTPResponse(id, 200, risposta);
}

// http://gridurl.appspot.com/
update_gridurl(string testo)
{
    query = gridurl_key + "&url=" + llEscapeURL(testo) + "/";
    requestid = llHTTPRequest(
    baseurl + query,
    [HTTP_METHOD,"GET", HTTP_MIMETYPE,"application/x-www-form-urlencoded"],
    "");    
}


action(string query)
{
    if(query == "luce")
    {
        llSetPrimitiveParams([PRIM_GLOW, ALL_SIDES, 0.1]);
        llSetPrimitiveParams([PRIM_GLOW, ALL_SIDES, 0.0]);        
    }
    // DEBUG:
    // llSay(0, query);
}

default
{
    state_entry()
    {
        mykey = llGetOwner();            
        setup();        
    }
    
    on_rez(integer n) 
    {
        setup(); 
    }

    changed(integer c)
    {
        if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
        {
            setup();
        }
    }


// scatta in risposta a setup() cioe' alla richiesta di un nuovo URL  
    http_request(key id, string method, string body)
    {
        // ci e' stato correttamente assegnato un nuovo URL
        if (method == URL_REQUEST_GRANTED)
        {
            url = body;
            // DEBUG:            
            // llOwnerSay(" nuovo url = " + url);                   
            update_gridurl(url);            
        }
        
        else if (method == URL_REQUEST_DENIED)
        {
            llInstantMessage(mykey, "Something went wrong, no url. " + body);
        }
        // lo script e' stato richiamato (da un browser, cliccando su un link...)
        else if (method == "GET")
        {
                string query = llGetHTTPHeader(id, "x-query-string"); 
                // DEBUG:
                // llInstantMessage(mykey, "query = " + query);       
                action(query);
                llHTTPResponse(id,200,"OK");
        }
        else
        {
            llHTTPResponse(id,405,"Unsupported method.");
        }
    }


// comunico gli URL dello script
    touch_start(integer total_number)
    {
        if(gridurl_key != "CHANGE-ME")
        {
            llInstantMessage(mykey, "Click to test: " 
            + gridurl_indirizzo
            + gridurl_key
            + "?luce"); 
        }
        else
        {
            llSay(0, "You didn't set the Gridurl key yet. Go to http://gridurl.appspot.com/random and copy the UUID that will be generated, then paste it at the begin of this script."); 
        }
    }    
   
}