Difference between revisions of "VirtualID URLMap (Broken)"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 174: Line 174:


Back to [[LSL_Library]]
Back to [[LSL_Library]]
{{#vardefine:sort|VirtualID URL map/redirect HTTP-in}}{{LSLC|Library}}{{LSLC|Examples}}
{{LSLC|Tutorials}}

Revision as of 18:05, 24 July 2009

Apez Corp is please to offer a free URL redirection service: VirtualID URlMap - tailored for use with HTTP-in.

It uses a very simple REST-based API and is implemented using Amazon's cloud computing services (AWS).


It simply allows you to map a user-defined alias (a subdomain of .obj.virtualid.info) to a target URL (http[s]). For example, you might map http://my-counter.obj.virtualid.info to your script's HTTP-in URL and keep it updated. An alias can be setup and updated with a single GET (or POST) request.


First, we'll show the simplest way to use it - save the details for the docs.


To create a new alias (or update an existing one), simply make a HTTP request like this in your script:

llHTTPRequest("http://api.virtualid.info/?op=set&a=my-visitors&t=http://sim3015.aditi.lindenlab.com:12046/cap/3ff4f3f2-ea08-76c1-cef6-a22b4a573a7c", [], "");

The part after op= is the operation - set in this case, which is used to create or update a mapping between an alias and a target URL. The alias is the subdomain used to access the object before the .obj.virtualid.info part.

Obviously, the string after a= is the alias - which you can choose. Note that they're global - so you'll need to choose one nobody else has used (alphanumeric or "-" characters, can't start with a digit or "-").

Finally, the string after t= is the target URL - where you wish the alias to be redirected.


Now, when you access http://my-visitors.obj.virtualid.info the service issues a standard HTTP 302 Redirect code to the accesing client. If that is a browser, for example, it will automatically and instantly access the target URL. (Note that if you're using this for inter-object communication in-world, be aware that llHTTPRequest() doesn't handle 302 Redirect, so you'll need to access the location and make an additional request to the supplied target)

Once you've created an alias the first time, you 'own' it and are the only one that can set it to a different value until either you delete it or it expires (see below for details). The default expiry is 30days from the last time is was updated.

There are several other parameters, but that's about it to get started! Copy and paste the example script text below to try it out.


Please refer to http://wiki.apez.biz/Development for full documentation to all the extra parameters and features.

--

The following example script maintains a static alias URL for an object's HTTP service which just returns some simple information about the object when accessed. It serves as a simple usage example.

<lsl> // Apez Corp Example LSL script // This script is hereby placed in the public domain. // // {"obj":"Apez DevKit", "ver":"2.3", "src":"devkit/Apez-urlmap-example.lsl"} // $Revision$


// // This is an example of how to use the Apez URL mapping Affiliate Services REST API // The URLmap service is convenient for having a static URL that can be updated in response // to a changing target URL - such as those generated by the http_request event. // For details see http://wiki.apez.biz/Development


// This script creates a HTTP server for this object and maps a static alias which it // keeps up-to-date so that the object can always be accessed via the alias URL


key url_req_key; key map_req_id; string url; string alias; string aliasurl; integer alias_last_updated; // unixtime integer expiry_secs; integer touch_count = 0;


// obtain a URL setup() {

   // Request a HTTP URL for this object
   url_req_key = llRequestURL();    

}


// setup URL alias from <alias>.obj.virtualid.info -> targetURL setAlias(string alias, string targetURL) {

   // every alias has an expiry period (in seconds), which cannot be greater than 3 months.
   //  The script needs to update it before then to renew the period
   expiry_secs = 48*60*60; // only going to ask for 48hours for this example
   llSetTimerEvent(expiry_secs-1); // set URL again before it expires
   
   map_req_id = llHTTPRequest("http://api.virtualid.info/?op=set&a="+alias
                             +"&t="+llEscapeURL(targetURL)+"&exp="+(string)expiry_secs, [], "");

}


default {

   state_entry()
   {
       setup();
   }
   on_rez(integer passed)
   {
       setup();
   }  
   
   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           url = body;
           llOwnerSay("SL assigned us the URL: " + url);
           
           // the user-defined alias/subdomain we want to map to this object
           //  must be a string of alphanumeric characters, can't start with a digit and must be <64 chars in length
           // (for this example we append the first few chars of the object key to make it different for each tester)
           alias = "myalias"+llGetSubString((string)llGetKey(),0,2);
           aliasurl = alias+".obj.virtualid.info";
           
           llOwnerSay("Requesting URL alias \""+aliasurl+"\" ...");
           
           setAlias(alias, url);
                       
       }
       else if (method == URL_REQUEST_DENIED)
       {
           llSay(0, "Something went wrong, no url. " + body);
       }
       else if (method == "GET")
       {
           string rsp = "Hello from object "+llGetObjectName()+"!\n"
                         +" owner: "+llKey2Name(llGetOwner())+"\n"
                         +" key: "+(string)llGetKey()+"\n"
                         +" touch count since reset: "+(string)touch_count;
           
           llHTTPResponse(id,200,rsp);
       }
       else
       {
           llHTTPResponse(id,405,"Unsupported Method");
       }
   }
   http_response(key request_id, integer status, list metadata, string body)
   {
       if (request_id = map_req_id)
       {
           llOwnerSay("Apez URLmap service said:"+body+" (status "+(string)status+")");
           
           if ((status >= 200) && (status < 300)) { // success
               llOwnerSay("You can now access this object via the URL http://"+aliasurl+"  (try it in your browser!)");
               alias_last_updated = llGetUnixTime();
           }
       }
   }
   changed(integer c)
   {
       if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
       {
           setup();
       }
   }
   
   timer()
   {
       if (llGetUnixTime() - alias_last_updated > expiry_secs)
           setAlias(alias, url);
   }
   
   touch(integer num_detected)
   {
       touch_count++;
   }
 

} </lsl>

Back to LSL_Library