Difference between revisions of "LlHTTPResponse"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(27 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{LSL_Function|func_id
{{LSL_Function
|mode=request
|inject-2={{Issues/SCR-396}}
|func_id=348|func_sleep=0.0|func_energy=10.0|mode
|func=llHTTPResponse
|func=llHTTPResponse
|func_sleep=0.2
|p1_type=key|p1_subtype=handle|p1_name=request_id|p1_desc=A valid HTTP request key.
|func_energy=10.0
|p2_type=integer|p2_name=status|p2_desc={{Wikipedia|List_of_HTTP_status_codes|HTTP Status}} (200, 400, 404, etc)|p2_hover=HTTP Status (200, 400, 404, etc)
|func_desc=Sends a reply to an HTTP request.
|sort=HTTPResponse
|p1_type=key|p1_name=request_id|p1_desc=A valid HTTP request key.
|p2_type=list|p2_name=parameters|p2_desc=HTTP response parameters in the format<br/>[param1, value1, param2, value2, . . . paramN, valueN]
|p3_type=string|p3_name=body|p3_desc=Contents of the response.
|p3_type=string|p3_name=body|p3_desc=Contents of the response.
|func_footnote=The response need not be made inside the [[http_request]] event but if it does not happen in a timely fashion the request will time out (within 25 seconds).
|func_desc=Responds to {{LSLP|request_id}} with {{LSLP|status}} and {{LSLP|body}}.
|return_text
|spec
|caveats=* This call must be made by the script containing the [[http_request]] event where the {{LSLP|request_id}} was received.
* There is no limit, other than script size, to the amount of data that can be sent by this function.
** [[llHTTPRequest]] can truncate the response length in ''receiving'' scripts. Be aware when using them together for prim-to-prim communications.
* The response by default has <code>{{String|content-type: text/plain}}</code>. Use [[llSetContentType]] to optionally return a different type, like <code>{{String|text/html}}</code>.
|constants
|constants
|caveats
|examples=
|examples=<pre>
<source lang="lsl2">
string url;
 
default
default
{
{
     http_request(key request_id, string method, string path_info, list parameters, string body)
     changed(integer change)
     {
     {
         // Loop Back Request Data
         if (change & (CHANGED_REGION_START | CHANGED_REGION | CHANGED_TELEPORT))
         llHTTPResponse(request_id, parameters, body);
            llResetScript();
    }
 
    state_entry()
    {
        llRequestURL();
    }
 
    touch_start(integer num_detected)
    {
    //  PUBLIC_CHANNEL has the integer value 0
        if (url != "")
            llSay(PUBLIC_CHANNEL, "URL: " + url);
    }
 
    http_request(key id, string method, string body)
    {
    //  http://en.wikipedia.org/wiki/Create,_read,_update_and_delete
        list CRUDmethods = ["GET", "POST", "PUT", "DELETE"];
    // it's bit-wise NOT ( ~ ) !!!
        integer isAllowedMethod = ~llListFindList(CRUDmethods, [method]);
 
        if (isAllowedMethod)
         {
            llHTTPResponse(id, 200, "Body of request below:\n" + body);
        }
        else if (method == URL_REQUEST_GRANTED)
        {
        //  don't forget the trailing slash
            url = body + "/";
 
            llOwnerSay("URL: " + url);
        }
        else if (method == URL_REQUEST_DENIED)
        {
            llOwnerSay("Something went wrong, no URL.\n" + body);
        }
        else
        {
            llOwnerSay("Ummm... I have no idea what SL just did. Method=\""+method+"\"\n" + body);
        }
     }
     }
}
}
</pre>
</source>
|helpers
|helpers
|also_header
|also_constants=
|also_events={{LSL DefineRow||[[http_request]]}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_TEXT}}|3={{#var:comment}}}}
{{LSL DefineRow||[[http_server]]}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_HTML}}|3={{#var:comment}}}}
|also_functions={{LSL DefineRow||[[llHTTPServer]]|For opening an llHTTPServer.}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_XML}}|3={{#var:comment}}}}
{{LSL DefineRow||[[llHTTPServerRemove]]|For removing the current HTTP server from a script.}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_XHTML}}|3={{#var:comment}}}}
{{LSL DefineRow||[[llGetHTTPServerURL]]|For retrieving the current HTTP server URL.}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_ATOM}}|3={{#var:comment}}}}
{{LSL DefineRow||[[llEscapeURL]]}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_JSON}}|3={{#var:comment}}}}
{{LSL DefineRow||[[llUnescapeURL]]}}
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_LLSD}}|3={{#var:comment}}}}
|also_footer
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_FORM}}|3={{#var:comment}}}}
|notes=This function is used to reply to HTTP requests received via the [[http_request]] event.<br /><br />Zero Linden's Office Hours Discussion On Incoming HTTP: [https://wiki.secondlife.com/wiki/User:Zero_Linden/Office_Hours/Discussion#Incoming_HTTP]<br />PJIRA feature request at [http://jira.secondlife.com/browse/SVC-913 SVC-913]<br />Please go vote if this feature is important to you.
{{LSL DefineRow|1=|2={{LSL Const|CONTENT_TYPE_RSS}}|3={{#var:comment}}}}
|cat1=
|also_functions=
{{LSL DefineRow||[[llGetFreeURLs]]}}
{{LSL DefineRow||[[llRequestURL]]}}
{{LSL DefineRow||[[llRequestSecureURL]]}}
{{LSL DefineRow||[[llReleaseURL]]}}
{{LSL DefineRow||[[llGetHTTPHeader]]}}
{{LSL DefineRow||[[llSetContentType]]}}
|also_tests
|also_events=
{{LSL DefineRow||[[http_request]]}}
{{LSL DefineRow||[[http_response]]}}
|also_articles=
{{LSL DefineRow||[[LSL http server]]}}
|notes
|deepnotes=
|history=
*{{SVN|1836|rev=112899 |trunk=*|anchor=file22|ver=|ser=}}
|cat1=HTTP
|cat2=HTTP/Server
|cat3
|cat4
}}
}}

Latest revision as of 10:30, 22 January 2015

Summary

Function: llHTTPResponse( key request_id, integer status, string body );
0.0 Forced Delay
10.0 Energy

Responds to request_id with status and body.

• key request_id A valid HTTP request key.
• integer status "Wikipedia logo"HTTP Status (200, 400, 404, etc)
• string body Contents of the response.

The response need not be made inside the http_request event but if it does not happen in a timely fashion the request will time out (within 25 seconds).

Caveats

  • This call must be made by the script containing the http_request event where the request_id was received.
  • There is no limit, other than script size, to the amount of data that can be sent by this function.
    • llHTTPRequest can truncate the response length in receiving scripts. Be aware when using them together for prim-to-prim communications.
  • The response by default has "content-type: text/plain". Use llSetContentType to optionally return a different type, like "text/html".

Examples

string url;

default
{
    changed(integer change)
    {
        if (change & (CHANGED_REGION_START | CHANGED_REGION | CHANGED_TELEPORT))
            llResetScript();
    }

    state_entry()
    {
        llRequestURL();
    }

    touch_start(integer num_detected)
    {
    //  PUBLIC_CHANNEL has the integer value 0
        if (url != "")
            llSay(PUBLIC_CHANNEL, "URL: " + url);
    }

    http_request(key id, string method, string body)
    {
    //  http://en.wikipedia.org/wiki/Create,_read,_update_and_delete
        list CRUDmethods = ["GET", "POST", "PUT", "DELETE"];
    //  it's bit-wise NOT ( ~ ) !!!
        integer isAllowedMethod = ~llListFindList(CRUDmethods, [method]);

        if (isAllowedMethod)
        {
            llHTTPResponse(id, 200, "Body of request below:\n" + body);
        }
        else if (method == URL_REQUEST_GRANTED)
        {
        //  don't forget the trailing slash
            url = body + "/";

            llOwnerSay("URL: " + url);
        }
        else if (method == URL_REQUEST_DENIED)
        {
            llOwnerSay("Something went wrong, no URL.\n" + body);
        }
        else
        {
            llOwnerSay("Ummm... I have no idea what SL just did. Method=\""+method+"\"\n" + body);
        }
    }
}

Deep Notes

History

Signature

function void llHTTPResponse( key request_id, integer status, string body );