LSL HTTP server/examples

From Second Life Wiki
Jump to navigation Jump to search

Hello World!

Classic example, the smallest http_request script possible. <lsl> default {

   state_entry()
   {
       llRequestURL();
   }
   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           llSay(0,"URL: " + body);
       }
       else if (method == "GET")
       {
           llHTTPResponse(id,200,"Hello World!");
       }
   }

} </lsl> A slightly more robust version: <lsl> default {

   state_entry()
   {
       llRequestURL();
   }
   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           llSay(0,"URL: " + body);
       }
       else if (method == URL_REQUEST_DENIED)
       {
           llSay(0, "Something went wrong, no url. " + body);
       }
       else if (method == "GET")
       {
           llHTTPResponse(id,200,"Hello World!");
       }
       else
       {
           llHTTPResponse(id,405,"Unsupported Method");
       }
   }

} </lsl>

Visitor List

A list of residents within sensor range of the server.
Notes:

  • This includes a method for handling multiple requests while waiting for asynchronous data requests to come back.

<lsl>integer scanning = FALSE;

list requests;

send_response(string body) {

   integer j;
   for (j = 0; j < llGetListLength(requests); ++j)
   {
       llHTTPResponse(llList2Key(requests,j), 200, body);
   }
   requests = [];

}

default {

   state_entry()
   {
       llRequestURL();
   }

   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           llSay(0,"URL: " + body);
       }
       else if (method == URL_REQUEST_DENIED)
       {
           llSay(0, "Something went wrong, no url. " + body);
       }
       else if (method == "GET")
       {
           if (!scanning)
           {
               llSensor("",NULL_KEY,AGENT,96,PI);
               scanning = TRUE;
           }
           
           requests += [id];
       }
       else
       {
           llHTTPResponse(id,405,"Unsupported method.");
       }
   }

   no_sensor()
   {
       send_response("There is no one here.");
       scanning = FALSE;
   }

   sensor(integer n)
   {
       string output;
       if (n < 16) output = "There are " + (string)n + " avatars nearby:";
       else output = "There are at least 16 avatars nearby:";
       
       integer i;
       for (i = 0;i<n;++i)
       {
           output += "\n\t" + llDetectedName(i);
       }
       
       send_response(output);
       scanning = FALSE;
   }

} </lsl>

Url Persistence / Visitor Counter

A more complete 'hello world', always has an url and keeps a visitor counter. <lsl> string url; integer hits;

setup() {

   llSetObjectName("HTTP Server");
   url = "";
   llRequestURL();
   hits = (integer)llGetObjectDesc();
   llSetText((string)hits + " visitors.",<1,1,0>,1);

}

default {

   state_entry() { setup(); }
   on_rez(integer n) { setup(); }
   
   changed(integer c)
   {
       if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
       {
           setup();
       }
   }
   
   touch_start(integer n)
   {
       llSay(0,"My url is: " + url);
   }

   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           url = body;
       }
       else if (method == URL_REQUEST_DENIED)
       {
           llSay(0, "Something went wrong, no url. " + body);
       }
       else if (method == "GET")
       {
           ++hits;
           llSetObjectDesc((string)hits);
           llSetText((string)hits + " visitors.",<1,1,0>,1);
           llHTTPResponse(id,200,"Hello!  You are visitor " + (string)hits + ".");
       }
       else
       {
           llHTTPResponse(id,405,"Method unsupported");
       }
   }

} </lsl>


Perl Example Using POST

Here are a pair of scripts to provide another example. The first script is the LSL code to receive the information on the grid. The second script is a PERL script that is used on the outside server to contact the code on the grid. <lsl>

key requestURL;

default {

   state_entry() {
       requestURL = llRequestURL();     // Request that an URL be assigned to me.
   }
   
   
    http_request(key id, string method, string body) {
       
       if ((method == URL_REQUEST_GRANTED) && (id == requestURL) ){
           // An URL has been assigned to me.
           llOwnerSay("Obtained URL: " + body);
           requestURL = NULL_KEY;
       }
       
       
       else if ((method == URL_REQUEST_DENIED) && (id == requestURL)) {
           // I could not obtain a URL
           llOwnerSay("There was a problem, and an URL was not assigned: " + body);
           requestURL = NULL_KEY;
       }
       else if (method == "POST") {
           // An incoming message was received.
           llOwnerSay("Received information from the outside: " + body);
           llHTTPResponse(id,200,"Thank you for calling. All of our operators are busy.");
       }


       else {
           // An incoming message has come in using a method that has not been anticipated.
           llHTTPResponse(id,405,"Unsupported Method");
       }


   }


}

</lsl>

<lsl>

  1. !/usr/bin/perl
  2. Routine to contact a script on the SL Grid using http server

use URI; use LWP; require HTTP::Request; use strict;


  1. Set the URL manually....

my $url = 'http://sim3015.aditi.lindenlab.com:12046/cap/2e83b2ce-65aa-9c44-5d1c-e64f48a222e2';

  1. #################################################
  2. Routine to send the information to the prim
  3. submitInformation($url,$information)

sub submitInformation{

   # First get the arguments.
   my $url = shift;
   my $information = shift;
   # Create the objects to be used to send out the HTTP request
   my $browser = LWP::UserAgent->new;
   my $request = HTTP::Request->new(POST => $url);
   # Set the encoding method and specify the information to be sent.
   $request->content_type('application/x-www-form-urlencoded');
   $request->content($information);
   # Send the request.
   my $response = $browser->request($request);
   # Return the information sent back
   return($response->content);

}



my $info = submitInformation

   ($url,'id=244195d6-c9b7-4fd6-9229-c3a8b2e60e81&name=M Linden');

print $info,"\n";



</lsl>