User:Darien Caldwell/HTTP-DNS

From Second Life Wiki
Jump to navigation Jump to search

Dynamic DNS service for HTTP-IN via Google App Engine:

NOTE: While the original version of this code did not provide true DNS service, it has since been updated to do so. Please read the original thread to understand the evolution of the code.

http://forums-archive.secondlife.com/54/33/323981/1.html

The API Calls

Once it's installed you can start using it:

Adds a new service named [NAME] with the HTTP-IN url [URL]. The URL must be converted to a string using llEscapeURL() first.

if the URL is added, returns the response 'Added', or 'Found' if the service
already exists. 

Removes the given service named [NAME].

Returns 'Removed' if successful, or 'None' if the service wasn't found.

Updates the service named [NAME] with the HTTP-IN url [URL].

The URL must be converted to a string using llEscapeURL() first.
If the URL is updated the response 'Updated' is returned.
If the service doesn't exist, a new service is added and 'Added' is returned.

Retrieves the url of the given service named [NAME].

Returns the URL if the service is found, or 'None' if the service wasn't found.
the returned URL must be converted to a URL using llUnescapeURL()

Lists the available services currently stored.

Returns a Comma seperated list of service names, ending with the word 'END'.
if no services are defined, 'Empty' is returned.

Using the DNS URL

Once a service has been entered, you can use the serivce name in the URL to do automatic redirection.

Example:

You establish a service named 'intro1'

The URL for the service is: http://sim3015.aditi.lindenlab.com:12046/cap/3ff4f3f2-ea08-76c1-cef6-a22b4a573a7c

By Navigating to http://yourappname.appspot.com/intro1 The page will be automatically redirected to the service URL:

http://sim3015.aditi.lindenlab.com:12046/cap/3ff4f3f2-ea08-76c1-cef6-a22b4a573a7c

If the service name used in the URL is invalid, 'None' is returned.

<lsl>

string url = ""; string service_name = "my_service"; key URL_KEY;


setup() {

   llReleaseURL(url);
   URL_KEY=llRequestURL();

}

default {

   state_entry()
   {
       llSetObjectName("HTTP Server");
       setup();
   }
   
   on_rez(integer n)
   {
       setup();
   }
   changed(integer c)
   {
       if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
       {
           setup();
       }
   }
   //Response to our HTTPRequest
   http_response(key id, integer status, list meta, string body)
   {
       
       if (status == 415) // The remote server did reply to your request but the Content-Type of the reply (such as XML, JSON, Atom, RSS, PLS) 
                          // is not recognised by the LL server and so is not passed back to the script. 
                          // You can assume that 415 means the server heard your request and did reply. 
       {
           llInstantMessage(llGetOwner(),"Special Error Status 415 Encountered.");
           return;
       }
       else if (status == 499) // Besides the usual HTTP status codes, SL implements a special status code 499. 
                               // This code isn't generated by the remote web server but by SL's servers, it can indicate: 
                               // Request timeout (60 seconds)   --  SSL failure  --  A space was present in the url (escape your URL with llEscapeURL). 
       {
           llInstantMessage(llGetOwner(),"Special Error Status 499 Encountered.");
           return;
       }
       else if (status == 502)  // The proxy server received an invalid response from an upstream server. 
                                // This error occurs when you send an llHTTPRequest to an object in-world, and it does not reply with an llHTTPResponse. 
       {
           llInstantMessage(llGetOwner(),"Special Error Status 502 Encountered.");
           return;
       }
       
       if (body=="Updated") llOwnerSay("DNS updated with Server Address.");
       else  llOwnerSay("Response code:"+(string)status+" Body:"+body);  // Some other arbitrary response
   }
   http_request(key id, string method, string body)
   {
       if (id==URL_KEY)
       {
           if (method == URL_REQUEST_GRANTED)
           {
               url = body;
               llHTTPRequest("http://yourappname.appspot.com/?type=add&name=" + service_name + "&url=" + llEscapeURL(url),[],"");
           }
           else
           {
               url="";
               llOwnerSay("Error: HTTP-IN URL Request Denied.");
           }
       }
       else
       {
           // Some other Request
           llOwnerSay("Got:"+body);
           string pathInfoHeader = llGetHTTPHeader(id, "x-path-info");
           llOwnerSay("Path:"+pathInfoHeader);
           string query = llGetHTTPHeader(id, "x-query-string");
           llOwnerSay("Query:"+query);
       }
       
       // should always respond to prevent timeouts.
       llHTTPResponse(id,200,"ACK"); 
   }

} </lsl>