Integrated Web-Server

There are cases where you want to submit commands to Corrade outside of Second Life. Corrade bundles its own HTTP web-server that can be used for that purpose. The internal web-server can be activated by setting the configuration parameters in the configuration file:

<HTTPServer>
    <Enable>1</Enable>
    <!--
        The prefixes that the HTTP server should listen on. 
    -->
    <Prefixes>
        <Prefix>http://+:8080/</Prefix>
    </Prefixes>
</HTTPServer>

In the example above, the HTTP server is activated and Corrade listens on the http://+:8080/ prefix. This means that it will bind to any adapter address (+) on port 8080. The + token stands for any IP address, and you are free to restrict that to anything, for example http://93.168.34.122:8080/ will listen on the address 93.168.34.122 and port 8080.

After Corrade binds successfully to the prefix, you can send commands to the bound address using any scripting or programming language you want. Corrade will then reply with the result of the command. For example, the following perl script is meant to retrieve Corrade's current balance:

#!/usr/bin/perl -w
 
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::Escape;
 
# Set this accordingly
$CORRADE_PORT = '8080';
$CORRADE_IP = '93.168.34.122';
$CORRADE_GROUP = uri_escape('My Group');
$CORRADE_GROUP_PASSWORD = uri_escape('mypassword');
 
$ua = LWP::UserAgent->new;
$res = $ua->post("http://$CORRADE_IP:$CORRADE_PORT/",
      Content =>
      "command=getbalance&group=$CORRADE_GROUP&password=$CORRADE_GROUP_PASSWORD"
);
 
if($res -> is_success) {
      print $res->content."\n";
      exit;
}
print "Error: ".$res->status_line."\n";

Note that we have built the POST data manually in the perl example. This is because Corrade expects a percent (%)-encoded string of key-value data pairs while LWP::UserAgent encodes data using (+). This may not be applicable to other languages: as long as key-value data is properly escaped, the HTTP request will return the result as a HTTP response.

The perl script will post the data to Corrade's IP address and retrieve the result. In this example, it will print out something like the following:

command=getbalance&balance=0&success=True&group=My%20Group

Corrade's integrated web-server is useful if you want, for example, to create a web-frontend and retrieve and manipulate entities in Second Life from outside of Second Life instead of relying on in-world objects to send the command using LSL. You are free, of course, to include the callback key in out-of-world script which will make Corrade send the result both to the script and the URL you may have specified.

Here is a PHP script using curl that sends a request to Corrade's internal HTTP Server to return the current balance and then prints out the outcome:

###########################################################################
## Contributed by Katolinochka                                           ##
###########################################################################
# Set this to Corrade's HTTP Server URL
$URL = 'http://71.131.38.211:8080';
 
# This constructs the command as an array of key-value pairs.
$params = array(
    'command' => 'getbalance',
    'group' => 'My group',
    'password' => 'mypassword'
);
 
# We now escape each key and value: this is very important, because the 
# data we send to Corrade may contain the '&' or '=' characters (we don't 
# in this example though but this will not hurt anyone).
array_walk($params,
    function(&$value, $key) {
        $value = rawurlencode($key)."=".rawurlencode($value);
    }
);
$postvars = implode('&', $params);
 
# Set the options, send the request and then display the outcome
if ($curl = curl_init()) {
    curl_setopt($curl, CURLOPT_URL, $URL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
    $out = curl_exec($curl);
    echo $out;
    curl_close($curl);
}

The script assumes that Corrade's HTTP server runs on port 8080 and is listening on the address 71.131.38.211. To use the script, just save it to a file, change the $URL variable suitably and then run the script through the php command.

Compression

Corrade's internal web-server also supports compressing the output using an algorithm that can be configured in Corrade's configuration file. The functionality is quite simple: after making the POST request, Corrade returns data with the content-type set to whatever algorithm you chose in the configuration file. Most programming languages handle compressed output very well and efficiently. For example, taking our previous request and changing the perl script such that compressed output is supported, we would write the script as:

#!/usr/bin/perl -w
 
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::Escape;
 
# Set this accordingly
$CORRADE_PORT = '8080';
$CORRADE_IP = '71.131.38.211';
$CORRADE_GROUP = uri_escape('My Group');
$CORRADE_GROUP_PASSWORD = uri_escape('mypassword');
 
$ua = LWP::UserAgent->new;
$can_accept = HTTP::Message::decodable;
$res = $ua->post("http://$CORRADE_IP:$CORRADE_PORT/",
      Content =>
      "command=getbalance&group=$CORRADE_GROUP&password=$CORRADE_GROUP_PASSWORD",
      'Accept-Encoding' => $can_accept
);
 
if($res -> is_success) {
      print $res->decoded_content."\n";
      exit;
}
print "Error: ".$res->status_line."\n";

which will then make the perl script print out the result from Corrade while transparently decompressing the output.

Using HTTPs instead of HTTP

For Windows, please see the HTTPs for .NET HttpListener tutorial in order to make Corrade listen on https instead of http. For Unix, please see the HTTPs for mono HttpListener.

Index


secondlife/scripted_agents/corrade/tutorials/integrated_web-server.txt ยท Last modified: 2023/09/27 10:05 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.