|
From: <wgh...@us...> - 2009-09-02 08:48:41
|
Revision: 23684
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=23684&view=rev
Author: wghassan
Date: 2009-09-02 08:48:31 +0000 (Wed, 02 Sep 2009)
Log Message:
-----------
switch to using xss.js for script tag ajax
Modified Paths:
--------------
pkg/trunk/sandbox/web/rosweb2/src/rosweb.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/nodes.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/status.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topic.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topics.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/db_webui.py
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/nodes.cs
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/status.cs
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topic.cs
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topics.cs
Added Paths:
-----------
pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/rosfooter.cs
Modified: pkg/trunk/sandbox/web/rosweb2/src/rosweb.py
===================================================================
--- pkg/trunk/sandbox/web/rosweb2/src/rosweb.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/rosweb2/src/rosweb.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -366,7 +366,8 @@
try:
self.send_response(retcode)
if callback:
- buf = callback + "(" + buf + ");"
+ #buf = callback + "(" + buf + "); var _xsajax$transport_status=200;"
+ buf = callback + "=" + buf + ";\nvar _xsajax$transport_status=200;"
self.send_header('Content-Type', 'text/javascript')
else:
self.send_header('Content-Type', 'application/json')
@@ -386,7 +387,8 @@
self.send_header('Content-Length', str(len(buf)))
- self.send_header('Connection', 'keep-alive')
+ if self.headers.get("Connection", '').lower() == "keep-alive":
+ self.send_header('Connection', 'keep-alive')
self.end_headers()
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/nodes.py
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/nodes.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/nodes.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -25,7 +25,7 @@
pass
def display(self, hdf):
- return
+ db_webui.grabTopics(hdf, ["/topics"])
def run(context):
return MyPage(context, pagename="nodes", nologin=1)
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/status.py
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/status.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/status.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -24,10 +24,10 @@
class MyPage(MBPage.MBPage):
def setup(self, hdf):
- self.db = db_webui.initSchema()
+ pass
def display(self, hdf):
- pass
+ db_webui.grabTopics(hdf, ["/rosout", "/users"])
def run(context):
return MyPage(context, pagename="status", nologin=1)
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topic.py
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topic.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topic.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -30,6 +30,8 @@
topic = hdf.getValue("Query.topic", "")
hdf.setValue("CGI.cur.topic", topic)
+ db_webui.grabTopics(hdf, ["/topics", topic, "/battery_state", "/power_board_state", "/app_status"])
+
def run(context):
return MyPage(context, pagename="topic", nologin=1)
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topics.py
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topics.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/cgibin/topics.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -22,10 +22,10 @@
class MyPage(MBPage.MBPage):
def setup(self, hdf):
- self.db = db_webui.initSchema()
-
+ pass
+
def display(self, hdf):
- return
+ db_webui.grabTopics(hdf, ["/topics"])
def run(context):
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/db_webui.py
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/db_webui.py 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/db_webui.py 2009-09-02 08:48:31 UTC (rev 23684)
@@ -21,6 +21,21 @@
from launchman import app
+import urllib
+
+def grabTopics(hdf, topics):
+ topics = topics + ["/battery_state", "/power_board_state", "/app_status"]
+ topicList = []
+ for topic in topics: topicList.append("topic=%s" % topic)
+ topics = string.join(topicList, "&")
+
+ url = "http://localhost:8080/ros/receive?since=0&" + topics
+ fp = urllib.urlopen(url)
+ body = fp.read()
+ fp.close()
+
+ hdf.setValue("CGI.cur.messages", body)
+
def path2taskid(path):
p = []
package = None
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 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/ros.js 2009-09-02 08:48:31 UTC (rev 23684)
@@ -71,17 +71,16 @@
receive: function(json_result) {
try {
- this.lastTime = json_result.since;
this.evalMessages(json_result);
} catch (e) {
ros_debug("Error with evalMessages.");
}
- setTimeout("gPump.pump();", 200);
+ setTimeout("gPump.pump();", 100);
},
evalMessages: function(json_result) {
// ros_debug("evalMessages()");
-
+ this.lastTime = json_result.since;
for(var i=0; i<json_result.msgs.length; i++) {
var msgEnv = json_result.msgs[i];
// ros_debug(msgEnv.topic);
@@ -132,7 +131,7 @@
});
},
- service_call: function(service_name, parameterList) {
+ service_call_xss: function(service_name, parameterList) {
var parameters = {args: parameterList};
var uri = this.urlprefix + "/service/" + service_name;
@@ -140,17 +139,53 @@
uri = uri + "?callback=gPump.receive_service&json=" + Object.toJSON(parameterList);
getDataFromServer("_ros_service_pump", uri);
},
+
+
+ service_call: function(service_name, parameterList) {
+ var parameters = {args: parameterList};
+
+ var uri = this.urlprefix + "/service/" + service_name;
+ uri = uri + "?callback=gPump.receive_service&json=" + Object.toJSON(parameterList);
+ var parameters = {json: Object.toJSON(parameterList)};
+ new Ajax.Request(uri, {parameters: parameters, crossSite: true, method: 'get'});
+ },
+
receive_server: function(msg) {
alert('receive_server');
},
- pump: function() {
+ pump_xs: function() {
var uri = this.urlprefix + "/receive?callback=gPump.receive&since=" + this.lastTime;
this.topicListeners.each(function(pair) {uri = uri + "&topic=" + pair.key; });
getDataFromServer("_ros_pump", uri);
},
+ pump: function() {
+ var uri = this.urlprefix + "/receive?callback=gMessage&since=" + this.lastTime;
+ this.topicListeners.each(function(pair) {
+ uri = uri + "&topic=" + pair.key;
+ });
+
+ var obj = this;
+ new Ajax.Request(uri, {
+ method: 'get',
+ crossSite: true,
+ onSuccess: function(transport) {
+ try {
+ var json = gMessage;
+ obj.evalMessages(json);
+ obj.pump();
+ } catch(e) {
+ obj.pump();
+ }
+ },
+ onFailure: function(transport) {
+ obj.pump();
+ }
+ });
+ },
+
pump_old: function() {
var uri = this.urlprefix + "/receive?since=" + this.lastTime;
this.topicListeners.each(function(pair) {
@@ -164,9 +199,8 @@
onSuccess: function(transport) {
try {
var json = transport.responseText.evalJSON();
- obj.lastTime = json.since;
+ obj.evalMessages(json);
obj.pump();
- obj.evalMessages(json);
} catch(e) {
obj.pump();
}
@@ -185,7 +219,9 @@
gPump = new MessagePump("http://" + window.location.hostname + ":8080" + prefix);
gPump.setupWidgets();
- //setTimeout("gPump.pump();", 10);
+ if(window.gMessage) {
+ gPump.evalMessages(window.gMessage);
+ }
gPump.pump();
}
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/jslib/xss.js 2009-09-02 08:48:31 UTC (rev 23684)
@@ -1,4 +1,10 @@
scriptTransport = Class.create();
+//
+//Written by Thierry Schellenbach
+//http://www.mellowmorning.com/2007/10/25/introducing-a-cross-site-ajax-plugin-for-prototype/
+//Version 0.8.0 for Prototype 1.5.0
+//Developed for www.commenthub.com
+//
//modeled after XmlHttpRequest http://en.wikipedia.org/wiki/XMLHttpRequest
//functions open, send (setRequestHeader) - variable readyState, status
//
@@ -7,6 +13,14 @@
// * 2 = sent - send() has been called, headers and status are available.
// * 3 = receiving - Downloading, responseText holds partial data.
// * 4 = loaded - Finished.
+//
+// for which prototype does this:
+// ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']
+// unfortunately the onreadystatechange only works for the last 3, because
+// prototype 1.5.0 assigns it too late, for our usage and prevents status 1
+// Prototype uses a timer, which in tests lead onSuccess to occur before onLoading
+// We use respondToReadyState to make a direct instruction and bypass the filter
+//
//TODO:
//Removal of <script> nodes?
@@ -23,11 +37,12 @@
if (method != 'GET')
alert('Method should be set to GET when using cross site ajax');
this.readyState = 1;
+ /* little hack to get around the late assignment of onreadystatechange */
+ this.respondToReadyState(1);
this.onreadystatechange();
this.url = url;
this.userAgent = navigator.userAgent.toLowerCase();
this.setBrowser();
- this.prepareGetScriptXS();
}
scriptTransport.prototype.send = function(body) {
@@ -51,25 +66,24 @@
};
}
-scriptTransport.prototype.prepareGetScriptXS = function() {
- if (this.browser.safari || this.browser.konqueror) {
- _xsajax$node = [];
- _xsajax$nodes = 0;
+scriptTransport.prototype.callback = function() {
+ try{
+ this.status = _xsajax$transport_status;
+ } catch(e) {
+ return;
+ //to prevent people from writing code, which is not cross browser compatible
}
-}
-
-scriptTransport.prototype.callback = function() {
- this.status = (_xsajax$transport_status) ? _xsajax$transport_status : 200;
this.readyState = 4;
this.onreadystatechange();
+ _xsajax$transport_status = null;
}
scriptTransport.prototype.getScriptXS = function() {
/* determine arguments */
var arg = {
- 'url': null
- };
+ 'url': null
+ }
arg.url = arguments[0];
/* generate <script> node */
@@ -77,13 +91,10 @@
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;
+ /* FF and Opera properly support onload. MSIE has its own implementation. Safari and Konqueror need some polling */
if (this.browser.msie) {
-
+
function mybind(obj) {
temp = function() {
if (this.readyState == "complete" || this.readyState == "loaded") {
@@ -101,24 +112,23 @@
to "loaded". So, we check for both here... */
this.node.onreadystatechange = mybind(this);
- } else if (this.browser.safari || this.browser.konqueror) {
+ } else if (this.browser.safari || this.browser.konqueror) {
+ this.timepassed = 0;
/* 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
- };
+ _any_ events at all, so we need to use some primitive polling */
+ this.checkTimer = setInterval(function()
+ {
+ this.timepassed = this.timepassed+100;
+ if(typeof(eval(_xsajax$transport_status)) != 'undefined' && eval(_xsajax$transport_status) != null)
+ {
+ this.callback();
+ clearInterval(this.checkTimer);
+ }
+ if(this.timepassed > 20000)
+ clearInterval(this.checkTimer);
+ }.bind(this),100);
} else {
+
/* Firefox, Opera and other reasonable browsers can
use the regular "onload" event... */
this.node.onload = this.callback.bind(this);
@@ -130,18 +140,6 @@
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);
- }
-
}
//
@@ -152,14 +150,36 @@
}
scriptTransport.prototype.onreadystatechange = function() {
}
+scriptTransport.prototype.respondToReadyState = 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
+ initialize: function(url, options) {
+ this.options = {
+ method: 'post',
+ asynchronous: true,
+ contentType: 'application/x-www-form-urlencoded',
+ encoding: 'UTF-8',
+ parameters: '',
+ evalJSON: true,
+ evalJS: true
+ };
+ Object.extend(this.options, options || { });
+
+ this.options.method = this.options.method.toLowerCase();
+
+ if (Object.isString(this.options.parameters))
+ this.options.parameters = this.options.parameters.toQueryParams();
+ else if (Object.isHash(this.options.parameters))
+ this.options.parameters = this.options.parameters.toObject();
+
+ this.transport = (!this.options.crossSite) ? Ajax.getTransport() : new scriptTransport;
+ this.options.asynchronous = (!this.options.crossSite) ? this.options.asynchronous : false;
+ //turns of the timed onLoad executer
+ this.transport.respondToReadyState = this.respondToReadyState.bind(this);
+ this.request(url);
+ }
+ });
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/nodes.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/nodes.cs 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/nodes.cs 2009-09-02 08:48:31 UTC (rev 23684)
@@ -14,5 +14,6 @@
<br>
+<?cs include:"rosfooter.cs"?>
</body>
</html>
Added: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/rosfooter.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/rosfooter.cs (rev 0)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/rosfooter.cs 2009-09-02 08:48:31 UTC (rev 23684)
@@ -0,0 +1,4 @@
+<script type="text/javascript">
+gMessage = <?cs var:CGI.cur.messages ?>;
+</script>
+
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/status.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/status.cs 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/status.cs 2009-09-02 08:48:31 UTC (rev 23684)
@@ -31,5 +31,6 @@
+<?cs include:"rosfooter.cs"?>
</body>
</html>
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topic.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topic.cs 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topic.cs 2009-09-02 08:48:31 UTC (rev 23684)
@@ -13,8 +13,7 @@
<div style="border: 2px solid white; font-size: 10pt; font-family: courier; height: 40em; width: 60em;" objtype=ScrollingTextWidget topic="<?cs var:CGI.cur.topic ?>"></div><br>
<br>
-<div id=ErrorDiv></div>
-
+<?cs include:"rosfooter.cs"?>
</body>
</html>
Modified: pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topics.cs
===================================================================
--- pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topics.cs 2009-09-02 08:35:18 UTC (rev 23683)
+++ pkg/trunk/sandbox/web/webui/src/webui/mod/webui/templates/topics.cs 2009-09-02 08:48:31 UTC (rev 23684)
@@ -1,7 +1,7 @@
<html>
<head>
<?cs include:"includes.cs" ?>
-</head>
+ </head>
<body onload="ros_handleOnLoad('/ros')">
<?cs include:"header.cs" ?>
@@ -21,5 +21,6 @@
<br>
+<?cs include:"rosfooter.cs"?>
</body>
</html>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|