An Opensim version for llHTTPResponse

From OpenSimulator

Revision as of 10:45, 23 November 2010 by Fritigern (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A Opensim version for llHTTPResponse

As we know, until now we receive a very nice message for llHTTPResponse :D, but to make a prim communicate to another, i've made this method:

Client

// This script is to do a request on the primitive url on the description and return it's response.
key url_request;
 
 string myurl; //This is only valid for internal Requests/Responses, for calling from other grids, use the simulator ip:port where it shows something like 2K3S-***-***-***:port, but mantain the rest.
 
integer listen_u;
 
string prepare_post_body(list l)
{
    integer i;
    string body;
    integer len=llGetListLength(l) & 0xFFFE; // make it even
    for (i=0;i<len;i+=2)
    {
        string varname=llList2String(l,i);
        string varvalue=llList2String(l,i + 1);
        if (i>0) body+="&";
        body+=llEscapeURL(varname)+"="+llEscapeURL(varvalue);
    }
    return body;
}
 
list Postdata2List(string data)
{
    return llParseString2List(data, "&", "");
}
 
string Httpdata2String(string data)
{
    list d = llParseString2List(data, "=", "");
    return llUnescapeURL(llList2String(d, 1));
}
 
key FastHttpRequest(string url, list meta, list body)
{
    return llHTTPRequest(url, meta, prepare_post_body(body));
} 
 
default
{
 
    state_entry()
    {
        url_request = llRequestURL();
    }
    http_request(key id, string method, string body)
    {
        if (url_request == id)
        {
            url_request = "";
            if (method == "URL_REQUEST_GRANTED")
            {
                llSetText("MyURL> " + body, <1,.5,0>, 1.0);
                myurl = body;
                state run;
            }
            else if (method == "URL_REQUEST_DENIED")
            {
                llSay(0, "Something went wrong, no url. " + body);
            }
        }
        else
        {
            llSay(0, "Received> " + body);
        }
    }
}
 
state run
{
    state_entry()
    {
        llSay(0, "Running, touch for request to my description.");
        listen_u = llListen(-1, "", "", "");
        llListen(1, "", "", "reset");
    }
 
    listen(integer c, string n, key k, string m)
    {
        if(c == -1)
        {
            llListenRemove(listen_u);
            llSetObjectDesc(m);
        }
        if(c == 1 && m == "reset")
        {
            llResetScript();
        }
    }
 
    touch_start(integer i)
    {
        FastHttpRequest(llGetObjectDesc(), [HTTP_METHOD, "POST",HTTP_MIMETYPE, "application/x-www-form-urlencoded"],["MYURL",myurl,"MESSAGE","Hello, my name is " + llGetObjectName() + "."]);
    }
 
    http_request(key id, string method, string body)
    {
        if (url_request == id)
        {
            url_request = "";
            if (method == "URL_REQUEST_GRANTED")
            {
                llSetText("MyURL> " + body, <1,.5,0>, 1.0);
                myurl = body;
            }
            else if (method == "URL_REQUEST_DENIED")
            {
                llSay(0, "Something went wrong, no url. " + body);
            }
        }
        else
        {
            list body_i = Postdata2List(body);
            string url = Httpdata2String(llList2String(body_i,0));
            string data = Httpdata2String(llList2String(body_i,1));
            llSay(0, "Received> " + data);
        }
    }
}

Server

key url_request;
 
 string myurl;//This is only valid for internal Requests/Responses, for calling from other grids, use the simulator ip:port where it shows something like 2K3S-***-***-***:port, but mantain the rest.
 
string prepare_post_body(list l)
{
    integer i;
    string body;
    integer len=llGetListLength(l) & 0xFFFE; // make it even
    for (i=0;i<len;i+=2)
    {
        string varname=llList2String(l,i);
        string varvalue=llList2String(l,i + 1);
        if (i>0) body+="&";
        body+=llEscapeURL(varname)+"="+llEscapeURL(varvalue);
    }
    return body;
} 
 
list Postdata2List(string data)
{
    return llParseString2List(data, "&", "");
}
 
string Httpdata2String(string data)
{
    list d = llParseString2List(data, "=", "");
    return llUnescapeURL(llList2String(d, 1));
}
 
key FastHttpRequest(string url, list meta, list body)
{
    return llHTTPRequest(url, meta, prepare_post_body(body));
} 
 
default
{
    state_entry()
    {
        url_request = llRequestURL();
    }
    touch_start(integer i)
    {
//        llSay(0, "URL: " + myurl);
//        
//        llSay(-1, myurl);
 
        llSetText(" ", <0,0,0>, 0.0);
 
        llResetScript();
    }
    http_request(key id, string method, string body)
    {
        if (url_request == id)
        {
            url_request = "";
            if (method == "URL_REQUEST_GRANTED")
            {
                llSay(0,"New URL: " + body);
 
                llSay(-1, body);
                myurl = body;
            }
            else if (method == "URL_REQUEST_DENIED")
            {
                llSay(0, "Something went wrong, no url. " + body);
            }
        }
        else
        {
//            llSay(0, body);
            list body_i = Postdata2List(body);
            string url = Httpdata2String(llList2String(body_i,0));
            string data = Httpdata2String(llList2String(body_i,1));
            llSetText("FROM: " + url + "\nDATA: " + data, <1,.5,0>, 1.0);
//            llSay(0, "FROM: " + url + "\nDATA: " + data);
//            llHTTPResponse(id, 200, body);
            llSay(0, "Response to " + url);
            FastHttpRequest(url, [HTTP_METHOD, "POST",HTTP_MIMETYPE, "application/x-www-form-urlencoded"], ["MYURL", myurl, "MESSAGE", "You sent me: \""+data+"\""]);
            llSay(0, "Sent!");
        }
    }
}

To receive a response, you need to send in the body this parameter: MYURL and in value add the object url, but if you need, try to change the value of url to the ip:port of region, sometimes it may be strange. Is it, easy? ;D

Will keep updating until told otherwise :-)

Personal tools
General
About This Wiki