User:Void Singer/Teacup

From Second Life Wiki
Jump to: navigation, search

Teacup/Saucer

Teacup/Saucer
What is it?


Teapot is an open source webserver front end for LSL's HTTP-in + MOAP functionality. It builds on the work of several other people including but not limited to, Kelly Linden, Torley Linden, Tali Rosca, Vegas Silverweb, and special mentions to Kate and/or Edelman Linden. The idea is to make it easy to serve web content from within SL, with a minimum of work or understanding, in the most standards compliant and flexible way possible.

How does it work?


Externally, when Teacup starts, it gets a URL and creates a Micro HTML page, which it bootstraps onto the prim face with a "data:" URN. Once loaded, that page requests a JavaScript library, which loads normally. Once loaded, the library bootstraps the specially prepared ".tsp" HTML content onto the current page, and captures all links targeting other ".tsp" files, so that it can do the same when they are requested. Internally the server broadcasts a message when it gets a page request, and any installed File Service scripts can reply with content, or the server will simply report back that it doesn't have the file requested if it doesn't receive a reply in a set time.

Why all the tea jokes?


Well I happen to like steam punk stuff, and it has a sort of pseudo-Victorian-era feel to it, but in all truth it was sparked by a bit of geek humor. There was an old april fool's joke passed around as an official sounding memo about creating a protocol for controlling coffee pots with HTTP (back before they actually started making net enabled appliances) and part of that proposal was that teapots should return an error response 418 "I'm a teapot" if you tried to make it brew coffee... since these servers are small, and 418 is safe to use internally because it wouldn't normally be encountered, I figured hey, Teapot --> Teacup --> Victorian Imagery --> Steam Punk --> teacup that is it's own teapot --> SIP --> many different types of tea --> Success!

... And thus was born the SIP, Teacup server, and Red Tea File Service
(developers are encouraged to keep the theme, but it's not a requirement)

Return to top

Quick Start

So you don't want to read a bunch of tech babble, you just want to make a website in a prim right? Well I can sympathize so here are some (mostly) easy steps to get you up and running fast...

  1. Follow the instructions on This Page or choose a File Service from the Compatible File Services and Extensions and follow those instructions.
  2. Optional: Copy the text in the grey box below Code: Saucer into a script named "Saucer", save it, and drop it in your rezzed box
  3. Optional: Copy the text in the grey box below Code: Tea Strainer into a script named "Tea_Strainer", save it, and drop it in your rezzed box
  4. Copy the text in the grey box below Code: Teacup into a script named "Teacup v0.3.1", save it, and drop it in your rezzed box
  5. Click on the top of your box if your webpage isn't already showing.
    • At this point you may want to rotate and/or resize your box.
    • If you like, you have my express permission to download (right click, "save as") and edit (with whichever image editor you prefer) the Teacup.png file to use as the default texture of your website prim.
  6. Show it off to your friends with MOAP enabled viewers


If you run into any problems, simply start a thread with the problem you are having here, and I or some other technical wizard will assist you.

Return to top

Code

Teacup


  • Save in a MONO script named "Teacup v0.3.1"

<lsl>/*( Teacup Server v0.3.1 )*/


/*//-- Teacup Boot Strap Variables --//*/
//-- Server Default page start (always loads)

string gStrTBS1xx = "data:text/html;charset:utf-8,<html>\n";

//-- Head Section start: loads with URL_REQUEST_GRANTED

string gStrTBS1a1 = " <head>\n <base href='";

//-- Head Section end & Address intsertion point: loads with URL_REQUEST_GRANTED

string gStrTBS1a2 = "'/>\n <script type='text/javascript' defer='1' src='teacup.js'></script>\n </head>\n";

//-- Server Default message (always loads)
string gStrTBS2xx = " <body>\n

Teacup Server

\n
\n

If you are reading this, the server is working

\n";
//-- Default Error message: loads with URL_REQUEST_GRANTED
string gStrTBS2a1 = "

(But the File Service may not be)

\n";
//-- URL Failure MessageL included for URL_REQUEST_DENIED
string gStrTBS2b1 = "

(But the region failed to provide a URL)

\n";
//-- Server Default Footer start (always loads)
string gStrTBS3xx = "
\n

Teacup/Saucer v0.3
<a href='secondlife:///app/agent/";

//-- Server Default Footer end & Contact Owner key insertion point (always loads)
string gStrTBS4xx = "/about'>Contact Owner?</a>

\n </body>\n</html>";
/*//-- Pending Request Variables --//*/

list gLstPndKey; //-- Key queue (pages waiting to be served) list gLstPndTim; //-- Timeout Value queue (Request Timeout) //-- These (hopefully) prevent multiple responses if SL sends a timeout.


default{

   state_entry(){ //-- Request URL on start up
       llRequestURL();
   }
   
   on_rez( integer vIntBgn ){ //-- Clear pending and request new URL on rez
       gLstPndKey = gLstPndTim = [];
       llRequestURL();
   }
   
   changed( integer vBitChg ){ //-- Clear pending and re-request URL on region change/restart
if ((CHANGED_REGION_START

Protocols

This section is not required reading for users, it is really only of use to scripters looking understand or extend it.

Server Protocols


Outgoing Messages


The server has only two outgoing message formats. The first is general advertisement, and can be used by any File Service or extension to advertise information about itself. <lsl>llMessageLinked( LINK_SET, TEACUP_MESSAGE, "Message", NULL_KEY );</lsl> Where "Message" is anything the script wants to advertise The server only sends one such message "Teacup URL Changed", and ignores messages sent by other scripts using this format.

When this message is sent, The current Address of the Server is placed in the prims hover text, other scripts may use the following to retrieve it<lsl>string vStrAddress = llList2String( llGetLinkPrimitiveParams( SERVER_LINK_NUMBER, [PRIM_TEXT] ), 0 );</lsl>

The second Outgoing Message is a file request and has the format <lsl>llMessageLinked( LINK_SET, TEACUP_MESSAGE, REQUEST_PAGE, REQUEST_KEY );</lsl>

  • TEACUP_MESSAGE: 418
  • REQUEST_PAGE: the format is "<page name>?<search string>#<post data>]
    • Ex: "index.tsp?#" <-- file has no search string or post data
    • the page name can be parsed with<lsl>string vStrPageName = llList2String( llParseStringKeepNulls( REQUEST_PAGE, [], ["?","#"] ), 0 );</lsl>
  • REQUEST_KEY: the return key for the requested data.

valid Server Requests can be detected by checking that REQUEST_KEY is a valid key, and that TEACUP_MESSAGE is 418.

Incoming Messages


The server expects it will receive a return message in the following format for any file request made... <lsl>llMessageLinked( REQUEST_SOURCE, RESPONSE_CODE, FILE_TEXT, REQUEST_KEY ); //-- sent from the File Service</lsl>

  • REQUEST_SOURCE: The link number of the Teacup server that sent the request
  • RESPONSE_CODE: the follow standard HTTP Response codes are currently recognized
    • 100 "Continue": The server ignores this, make sure you send a normal response code as a follow up (see Saucer for details)
    • 200 "Success": Basic "yes we have it, here you go". anything that returns data can return this
    • 201 "Created": File was created for this request. probably best if the file was created for the request
    • 202 "Accepted": Request acknowledged/received (content optional) probably best for commands that generate in world actions
    • 204 "No Content": We got the request/data, but don't need to return anything. probably best for form data, FILE_TEXT should be blank.
    • 404 "Not Found": The file does not exist. Avoid using this unless you are reasonably sure the file isn't being served from another script.
  • FILE_TEXT: text of the file that was requested.
  • REQUEST_KEY: server request key for this file.
Any return should be done within 20 seconds or it's results will be ignored (Mostly an LSL limitation)

Special Ranges and limitations


To promote predictable data flows within a scripted object, The following are "Official" limits for scripts to be included as compatible resources
  1. The 0xx Range is off limits and may not be used
  2. All normal HTTP Response codes and ranges (1xx-5xx) are reserved... do not use them unless they make sense or the server can actually send them properly.
  3. the Range of 6xx codes is reserved for use by File System scripts, limited to no more than 10 per range per script and will be registed by request, first (working script) come, first served. if you reasonably need more than 10, use the 8xx-9xx range
    • the following 6xx codes are are reserved at this time
      • Saucer
        • 600 "File Removed" (sent with a file name and NULL_KEY)
        • 601 "File Added" (sent with a file name and NULL_KEY)
  4. 7xx codes are reserved for future expansion
  5. 8xx-9xx are unregulated, use at your own risk
  6. All Codes must be below 1000 (this is to guarantee safe data transfer for other unrelated scripts)
To Promote Maximum Stability and Flexibility
  1. Any Script that resides in the server prim should expect link messages of up to 40KiB
    • File Service Scripts should attempt to send files 30KiB or smaller
  2. File Service Scripts should not broadcast any link message larger than 4KiB using LINK_THIS
    • Responses to requests should be targeted to the Server prim that requested the data, for larger data sizes (optimally for ALL responses)
  3. Scripts residing outside of the server prim should expect link messages with up to 4KiB of data (the max the server is able to request with)

^ Return to top

Teacup Server Page (".tsp") Format


Teacup serves a modified html page called a Teacup Server Page (".tsp"), to overcome limitations in the LSL HTTP-in behavior.

if a requested filename ends with ".tsp" it needs to conform to a specific format that is similar but not identical to the contents of a normal html webpage. To convert an html page into a tsp page, the follow actions must take place (an example of all of them may be found in the User:Void_Singer/Red Tea File Service
  • The file name should end with ".tsp"
  • The contents are limited to what's valid inside a normal html "body" tag.
  • It also requires the following minor changes, made in order:
    1. all backslash characters must be converted to double backslashes (may be applied on a line by line basis)
    2. all single quote characters must be prefixed with a backslash (may be applied on a line by line basis)
    3. all line breaks should be converted to "\n" literals (may be applied on a line by line basis)
    4. The page should be prefixed with "vTea='" and suffixed with "';" (applies only to the whole page)
      • eg:
         file_string = "vTea='" + file_string + "';";
Return to top

Notes

Compatible File Systems and Extensions


  • Red Tea - A very simple SIP back end File Service for a starting point.
  • Saucer - Optional Fast 404 Extension for preventing long timeouts for missing/bad pages
  • Tea Strainer - Optional Troubleshooting Monitor for checking messages being sent and received.

Known Bugs


  • inline script/style elements are not evaluated at load time (inline attributes should work normally)
    • images have their own events, and can be used to trigger page load javascript
  • document/body onload events are not triggered for ".tsp" loads
    • images have their own events, and can be used to trigger page load javascript
  • Fails to load directly to external browsers (VWR-25555)
    • v2 internal browsers will only load it from the shared media bar "Load URL in browser" button (VWR-25554)
      • Workaround for v2 is to load it via the shared media button, to the internal browser, then cut and paste into an external browser (should work with everything but IE)

Requested Feature Upgrades


  • Add support per page javascript and css
    • considering a method to jumpstart on page script tags
  • Trigger at least the normal load events manually
    • may not be necessary if I the jumpstart method works
  • Find a simple way to serve base64 encoded content such as images or sounds.
    • NEED ASSISTANCE: I can (probably) do it the same way we load pages, but this makes it hard for page writers.
    • considering a doubletap method where the server js library parses img tags for failures and bootstraps them with a modified name
  • Find a convenient way to set title on per page basis... probably use the page name
    • Accepted: Page titles should match the internal file name minus the ".tsp" extension

Change Log / Old Versions


Most Recent Changes are at the top, old version links below.

  • Version will be considered stable when it reaches 1.0
  • Teacup v0.3.1
    • Reabsorbed the bootstrap html page into this script and limited it's size for compatibility due to feedback
    • Moved Saucer to it's own page, as an option resource
    • Removed dead touch code due to incompatibility with llLoadURL (VWR-25554 and VWR-25555)
    • Added handling for POST data. Page request format should be stable now.
  • Teacup/Saucer v0.3
    • Server Test Page moved to saucer file availability handler
    • Server javascript page loader library (Teacup.js) moved to File Service System
      • Users should have less trouble editing the file now and can include their own functions in the same file
    • Sitewide CSS supported via "teacup.css"
    • Saucer Script now handles page availability for quick 404 returns to speed page loading.
    • Site Title is pulled from the object name
    • Site address is discoverable by object scripts in the (hidden) prim hovertext
    • Server now runs from any prim, not just the root.
  • Teacup v0.2
    • Initial Public Release

NOTE TO WIKI EDITORS


I have kept this page as a sub page of my user page to denote that it should NOT be edited except to correct errors, add confirmed bugs, or add related resources.
The intent is to ensure executive control is in one person's hands to retain a cohesive vision of it's development.
If you have an improvement suggestion, or request, please use the discussion page.

Return to top

Questions or Comments?

Feel free to leave me a note on the discussion page.