|
From: <wgh...@us...> - 2009-09-02 07:14:27
|
Revision: 23679
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=23679&view=rev
Author: wghassan
Date: 2009-09-02 07:14:20 +0000 (Wed, 02 Sep 2009)
Log Message:
-----------
added compression and keep-alive to the rosweb
Modified Paths:
--------------
pkg/trunk/sandbox/web/rosweb2/src/rosweb.py
pkg/trunk/sandbox/web/sample_application/templates/index.cs
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/pr2_widgets.js
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/includes.cs
Added Paths:
-----------
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js
Modified: pkg/trunk/sandbox/web/rosweb2/src/rosweb.py
===================================================================
--- pkg/trunk/sandbox/web/rosweb2/src/rosweb.py 2009-09-02 06:53:44 UTC (rev 23678)
+++ pkg/trunk/sandbox/web/rosweb2/src/rosweb.py 2009-09-02 07:14:20 UTC (rev 23679)
@@ -43,6 +43,7 @@
import sys
import threading
import time
+import gzip
import traceback
import BaseHTTPServer
import logging
@@ -369,9 +370,26 @@
self.send_header('Content-Type', 'text/javascript')
else:
self.send_header('Content-Type', 'application/json')
+
+ if len(buf) > 500:
+ acceptEncoding = self.headers.get('Accept-Encoding', '')
+ if acceptEncoding.find('gzip') != -1:
+ zbuf = cStringIO.StringIO()
+ zfile = gzip.GzipFile(None, "wb", 9, zbuf)
+ zfile.write(buf)
+ zfile.close()
+
+ nbuf = len(buf)
+ buf = zbuf.getvalue()
+ self.send_header('Content-encoding', 'gzip')
+ print "%d/%d" % (len(buf), nbuf)
+
self.send_header('Content-Length', str(len(buf)))
+
+ self.send_header('Connection', 'keep-alive')
self.end_headers()
+
self.wfile.write(buf)
except socket.error, (ecode, reason):
if ecode == 32:
Modified: pkg/trunk/sandbox/web/sample_application/templates/index.cs
===================================================================
--- pkg/trunk/sandbox/web/sample_application/templates/index.cs 2009-09-02 06:53:44 UTC (rev 23678)
+++ pkg/trunk/sandbox/web/sample_application/templates/index.cs 2009-09-02 07:14:20 UTC (rev 23679)
@@ -17,7 +17,7 @@
<table width=80% align=center>
<tr>
<td>
-<div style="align: center; border: 1px solid black; font-size: 10pt; font-family: courier; height: 40em; width: 60em;" objtype=ScrollingTextWidget topic="/chatter"></div><br>
+<div style="align: center; border: 1px solid black; font-size: 10pt; font-family: courier; height: 40em; width: 60em;" objtype=TerminalTextWidget topic="/chatter" key="data"></div><br>
</td>
</table>
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/pr2_widgets.js
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/pr2_widgets.js 2009-09-02 06:53:44 UTC (rev 23678)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/pr2_widgets.js 2009-09-02 07:14:20 UTC (rev 23679)
@@ -40,7 +40,7 @@
else height = parseInt(height);
this.percent = 0;
- this.draw();
+ //this.draw();
},
receive: function(topic, msg) {
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js 2009-09-02 06:53:44 UTC (rev 23678)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js 2009-09-02 07:14:20 UTC (rev 23679)
@@ -24,8 +24,9 @@
var oScript = document.getElementById(id);
var head = document.getElementsByTagName("head").item(0);
if (oScript) {
- // Destory object
+ // Destroy object
head.removeChild(oScript);
+ delete oScript;
}
// Create object
oScript = document.createElement("script");
@@ -45,6 +46,8 @@
this.topicListeners = new Hash();
this.widgets = [];
this.http_request = [];
+
+ this.totalReceivedBytes = 0;
},
registerWidget: function(widget) {
@@ -73,7 +76,7 @@
} catch (e) {
ros_debug("Error with evalMessages.");
}
- setTimeout("gPump.pump();", 500);
+ setTimeout("gPump.pump();", 200);
},
evalMessages: function(json_result) {
Added: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js (rev 0)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js 2009-09-02 07:14:20 UTC (rev 23679)
@@ -0,0 +1,165 @@
+scriptTransport = Class.create();
+//modeled after XmlHttpRequest http://en.wikipedia.org/wiki/XMLHttpRequest
+//functions open, send (setRequestHeader) - variable readyState, status
+//
+// * 0 = uninitialized - open() has not yet been called.
+// * 1 = open - send() has not yet been called.
+// * 2 = sent - send() has been called, headers and status are available.
+// * 3 = receiving - Downloading, responseText holds partial data.
+// * 4 = loaded - Finished.
+
+//TODO:
+//Removal of <script> nodes?
+
+//
+//------------------------------ initialize, open and send ------------------------------------------------------
+//
+
+scriptTransport.prototype.initialize = function() {
+ this.readyState = 0;
+}
+
+scriptTransport.prototype.open = function(method, url, asynchronous) {
+ if (method != 'GET')
+ alert('Method should be set to GET when using cross site ajax');
+ this.readyState = 1;
+ this.onreadystatechange();
+ this.url = url;
+ this.userAgent = navigator.userAgent.toLowerCase();
+ this.setBrowser();
+ this.prepareGetScriptXS();
+}
+
+scriptTransport.prototype.send = function(body) {
+ this.readyState = 2;
+ this.onreadystatechange();
+ this.getScriptXS(this.url);
+}
+
+//
+//------------------------------ actually do the request: setBrowser, prepareGetScriptXS, callback, getScriptXS ----------
+//
+
+scriptTransport.prototype.setBrowser = function(body) {
+ scriptTransport.prototype.browser = {
+ version: (this.userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
+ safari: /webkit/.test(this.userAgent),
+ opera: /opera/.test(this.userAgent),
+ msie: /msie/.test(this.userAgent) && !/opera/.test(this.userAgent),
+ mozilla: /mozilla/.test(this.userAgent) && !/(compatible|webkit)/.test(this.userAgent),
+ konqueror: this.userAgent.match(/konqueror/i)
+ };
+}
+
+scriptTransport.prototype.prepareGetScriptXS = function() {
+ if (this.browser.safari || this.browser.konqueror) {
+ _xsajax$node = [];
+ _xsajax$nodes = 0;
+ }
+}
+
+scriptTransport.prototype.callback = function() {
+ this.status = (_xsajax$transport_status) ? _xsajax$transport_status : 200;
+ this.readyState = 4;
+ this.onreadystatechange();
+}
+
+scriptTransport.prototype.getScriptXS = function() {
+
+ /* determine arguments */
+ var arg = {
+ 'url': null
+ };
+ arg.url = arguments[0];
+
+ /* generate <script> node */
+ this.node = document.createElement('SCRIPT');
+ this.node.type = 'text/javascript';
+ this.node.src = arg.url;
+
+ /* optionally apply event handler to <script> node for
+ garbage collecting <script> node after loading and/or
+ calling a custom callback function */
+ var node_helper = null;
+
+ if (this.browser.msie) {
+
+ function mybind(obj) {
+ temp = function() {
+ if (this.readyState == "complete" || this.readyState == "loaded") {
+ return obj.callback.call(obj);
+ }
+ };
+ return temp;
+ }
+ /* MSIE doesn't support the "onload" event on
+ <script> nodes, but it at least supports an
+ "onreadystatechange" event instead. But notice:
+ according to the MSDN documentation we would have
+ to look for the state "complete", but in practice
+ for <script> the state transitions from "loading"
+ to "loaded". So, we check for both here... */
+ this.node.onreadystatechange = mybind(this);
+
+ } else if (this.browser.safari || this.browser.konqueror) {
+ /* Safari/WebKit and Konqueror/KHTML do not emit
+ _any_ events at all, but we can exploit the fact
+ that dynamically generated <script> DOM nodes
+ are executed in sequence (although the scripts
+ theirself are still loaded in parallel) */
+ _xsajax$nodes++;
+
+ var helper = 'var ctx = _xsajax$node[' + _xsajax$nodes + '];' + 'ctx.callback.call(ctx.node);' + 'setTimeout(function () {' + ' ctx.node_helper.parentNode.removeChild(ctx.node_helper);' + '}, 100);';
+ node_helper = document.createElement('SCRIPT');
+ node_helper.type = 'text/javascript';
+ node_helper.appendChild(document.createTextNode(helper));
+ _xsajax$node[_xsajax$nodes] = {
+ callback: this.callback.bind(this),
+ node: this.node,
+ node_helper: node_helper
+ };
+ } else {
+ /* Firefox, Opera and other reasonable browsers can
+ use the regular "onload" event... */
+ this.node.onload = this.callback.bind(this);
+ }
+
+ /* inject <script> node into <head> of document */
+ this.readyState = 3;
+ this.onreadystatechange();
+ var head = document.getElementsByTagName('HEAD')[0];
+ head.appendChild(this.node);
+
+ /* optionally inject helper <script> node into <head>
+ (Notice: we have to use a strange indirection via
+ setTimeout() to insert this second <script> node here or
+ at least Konqueror (and perhaps also Safari) for unknown
+ reasons will not execute the first <script> node at all) */
+ if (node_helper !== null) {
+ setTimeout(function() {
+ var head = document.getElementsByTagName('HEAD')[0];
+ head.appendChild(node_helper);
+ }, 100);
+ }
+
+}
+
+//
+//------------------------------ Don't complain when these are called: setRequestHeader and onreadystatechange ----------
+//
+
+scriptTransport.prototype.setRequestHeader = function() {
+}
+scriptTransport.prototype.onreadystatechange = function() {
+}
+
+//
+//------------------------------- Extend prototype a bit -----------------------
+//
+Ajax.Request.prototype = Object.extend(Ajax.Request.prototype,{
+ initialize: function(url, options) {
+ this.setOptions(options);
+ this.transport = (!this.options.crossSite) ? Ajax.getTransport() : new scriptTransport;
+ this.request(url);
+ }
+});
\ No newline at end of file
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/includes.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/includes.cs 2009-09-02 06:53:44 UTC (rev 23678)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/includes.cs 2009-09-02 07:14:20 UTC (rev 23679)
@@ -3,6 +3,7 @@
<title>RosWeb: <?cs var:CGI.ServerName?></title>
<script type="text/javascript" src="<?cs var:CGI.ScriptName?>/webui/jslib/prototype.js"></script>
+<script type="text/javascript" src="<?cs var:CGI.ScriptName?>/webui/jslib/xss.js"></script>
<script type="text/javascript" src="<?cs var:CGI.ScriptName?>/webui/jslib/ros.js"></script>
<script type="text/javascript" src="<?cs var:CGI.ScriptName?>/webui/jslib/ros_toolbar.js"></script>
<script type="text/javascript" src="<?cs var:CGI.ScriptName?>/webui/jslib/pr2_graph.js"></script>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|