Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Viewer 2 WebServing_Prim  

WebServing_Prim

This is an extention of the Bootstrapping HTML on a prim idea.

Category: Viewer 2
By : Tali Rosca
Created: 2010-09-02 Edited: 2010-09-02
Worlds: Second Life

the Zip file

Download all files for WebServing_Prim
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. WebServing_Prim_1.lsl
1 list pages;
2 list content;
3 integer indexPage = 0;
4
5 key url_request;
6 string baseUrl;
7
8 loadPages()
9 {
10 // Could be loaded from notecard, and/or possibly fetched from storage prims through link messages:
11 // For now, the links are a little cumbersome to write, requiring the full javascript: notation.
12 // (Remember to escape the single-quotes since the pages will be merged into a JavaScript string.)
13 // This could obviously be made prettier with some string pre-processing, and likewise, a system for
14 // variable includes, <% %>-style, could be programmed. For now, though, just serve HTML pages.
15 pages = [];
16 content = [];
17
18 pages += "index";
19 content += "<ul><li><a href=\"javascript:l(\\'page1\\')\">Page 1</a></li><li><a href=\"javascript:l(\\'page2\\')\">Page 2</a></li></ul>";
20
21 pages += "page1";
22 content += "<h1>Page 1</h1><p>This is page 1.<br/><a href=\"javascript:l(\\'\\')\">Return to the index</a></p>";
23
24 pages += "page2";
25 content += "<h1>Page 2</h1><p>And this is page 2.<br/><a href=\"javascript:l(\\'\\')\">Return to the index</a></p>";
26
27 pages += "css";
28 content += "body {font-family:Verdana,Arial;font-size:12px;margin:8px;padding:0px;color: #000000;}a {font-weight: bold;text-decoration: none;color: #880000;}a:hover {color: #ee5555;}h1 {float: left;padding-right: 50px;margin-top: 0px;margin-bottom: 10px;font-size: 20px;border-bottom: 1px solid;}p {clear:both}";
29 }
30
31 // Default state is responsible for getting the URL, running a timer until it manages to get one.
32 default
33 {
35 {
36 loadPages();
37
38 if(llGetFreeURLs()<1)
39 llOwnerSay("No URLs available. Something else is using a lot of URLs.");
40 else
41 {
42 // Defensively releasing. Should be a no-op, but prevents a leak if we somehow get here while the server doesn't think it has released.
43 llReleaseURL(baseUrl);
44 url_request = llRequestURL();
45 }
47 }
48
49 http_request(key id, string method, string body)
50 {
51 // We need the URL, so only register success, and let the timer retry otherwise:
52 if(url_request == id && method == URL_REQUEST_GRANTED)
53 {
54 url_request = "";
55
56 baseUrl = body;
57 state running;
58 }
59 }
60
61 timer()
62 {
63 llReleaseURL(baseUrl);
64 url_request = llRequestURL();
65 }
66
68 {
70 }
71 }
72
73 // The main state, handling the HTTP requests.
74 state running
75 {
77 {
78 // Re-acquire URL:
79 state default;
80 }
81
82 changed(integer change)
83 {
85 {
86 state default;
87 }
88 }
89
91 {
92 // Set up the base HTML page:
93
94 string cssString = "";
95 if(llListFindList(pages, ["css"]) > -1) cssString = "<link rel=\"stylesheet\" href=\""+baseUrl+"/css\"/>";
96
97 // This "seeds" the page with a JSON/P-based JavaScript to merge in loaded pages in the body tag.
98 string dataUrl = "data:text/html," + llEscapeURL("<html><head>"+cssString+"<script>var s;function w(h){body.innerHTML=h;if(s)head.removeChild(s);s=null}function l(a){if(s)head.removeChild(s);s=document.createElement('script');s.src='"+baseUrl+"/'+a+'?callback=w';head.appendChild(s);}</script></head><body onload=\"head=document.getElementsByTagName('head')[0];body=document.getElementsByTagName('body')[0];l('')\"></body></html>");
99
100 //llOwnerSay(dataUrl);
101 //llOwnerSay("Length: "+ (string)llStringLength(dataUrl));
102
103 llSetPrimMediaParams(0, [
104 PRIM_MEDIA_CONTROLS, PRIM_MEDIA_CONTROLS_MINI,
105 PRIM_MEDIA_PERMS_CONTROL, 0,
106 PRIM_MEDIA_PERMS_INTERACT, PRIM_MEDIA_PERM_ANYONE,
107 PRIM_MEDIA_AUTO_SCALE, TRUE,
108 PRIM_MEDIA_AUTO_PLAY, TRUE,
109 PRIM_MEDIA_AUTO_ZOOM, FALSE,
110 PRIM_MEDIA_FIRST_CLICK_INTERACT, TRUE,
111 PRIM_MEDIA_CURRENT_URL, dataUrl]);
112 }
113
114 http_request(key id, string method, string body)
115 {
116 string page = llGetSubString(llGetHTTPHeader(id, "x-path-info"), 1, -1);
117 string query = "&"+llGetHTTPHeader(id, "x-query-string")+"&";
118 integer pos = llSubStringIndex(query, "&callback=");
119 string callback = "";
120 if(pos >-1)
121 {
122 callback = llGetSubString(query, pos + 10, - 1);
123 callback = llGetSubString(callback, 0, llSubStringIndex(callback, "&") - 1);
124 }
125
126 //llOwnerSay("Page hit: "+page+ ", callback: " + callback);
127
128 integer p = llListFindList(pages, [page]);
129 if(p == -1) p = indexPage;
130
131 string c = llList2String(content, p);
132
133 if(callback != "")
134 llHTTPResponse(id, 200, callback+"('"+c+"');");
135 else
136 llHTTPResponse(id, 200, c);
137 }
138 }

Back to the Best Free Tools in Second Life and OpenSim.