summaryrefslogtreecommitdiff
path: root/tko/jsonp_fetcher.cgi
diff options
context:
space:
mode:
authorshoward <showard@592f7852-d20e-0410-864c-8624ca9c26a4>2009-03-27 20:55:16 +0000
committershoward <showard@592f7852-d20e-0410-864c-8624ca9c26a4>2009-03-27 20:55:16 +0000
commitab4d4003903891a5cc66bd28941770656bfd8bf6 (patch)
tree920d654c386d29db1851981b7b37dccfe6400074 /tko/jsonp_fetcher.cgi
parentd0ec4aa6a8a06673dc40ed9bdb22ccfd9cbbe3c3 (diff)
generic JSON-RPC implementation using JSONP (JSON with Padding). the central part of this change consists of:
* refactored JsonRpcProxy to extract all XmlHttpRequest-specific logic into a new XhrHttpRequest subclass, and made JsonRpcProxy abstract * introduced new PaddedJsonRpcProxy subclass of XmlHttpRequest that uses JSONP instead of XHR * added new handle_jsonp_rpc_request() method to rpc_handler.py, to handle JSONP requests on the server side This enables the entire frontend (either AFE or TKO) to operate via JSONP instead of XHR. I didn't make them do that now, since there's no reason, but it will be critical when we go to make components embeddable in other pages (on other domains). Other changes here include: * made TestDetailView use PaddedJsonRpcProxy (it previous had its own custom JSONP logic, which was all moved into PaddedJsonRpcProxy). * made retrieve_logs.cgi and jsonp_fetcher.cgi support JSONP requests, so that log fetching requests could go through the shared JsonRpcProxy codepath. retrieve_logs.cgi still supports the old GET params interface for backwards compatibility (at least old TKO still uses it, and possible other scripts). Signed-off-by: Steve Howard <showard@google.com> git-svn-id: svn://test.kernel.org/autotest/trunk@2943 592f7852-d20e-0410-864c-8624ca9c26a4
Diffstat (limited to 'tko/jsonp_fetcher.cgi')
-rw-r--r--tko/jsonp_fetcher.cgi29
1 files changed, 17 insertions, 12 deletions
diff --git a/tko/jsonp_fetcher.cgi b/tko/jsonp_fetcher.cgi
index 981c61cc..75ac84aa 100644
--- a/tko/jsonp_fetcher.cgi
+++ b/tko/jsonp_fetcher.cgi
@@ -1,6 +1,8 @@
#!/usr/bin/python
import cgi, urllib2
+import common
+from autotest_lib.frontend.afe.json_rpc import serviceHandler
script = """\
Content-Type: text/javascript
@@ -8,21 +10,24 @@ Content-Type: text/javascript
%(callback)s(%(result)s);
"""
-form = cgi.FieldStorage()
-path = form['path'].value
+class LogFileNotFound(Exception):
+ pass
+
+form = cgi.FieldStorage(keep_blank_values=True)
+encoded_request = form['request'].value
callback = form['callback'].value
+request = serviceHandler.ServiceHandler.translateRequest(encoded_request)
+parameters = request['params'][0]
+path = parameters['path']
+
+result, error = None, None
try:
file_contents = urllib2.urlopen('http://localhost' + path).read()
- # escape backslashes, double-quotes, newlines, and carriage returns -- all
- # would mess up a Javascript string literal
- escaped_contents = file_contents.replace(
- '\\', r'\\').replace(
- '"', r'\"').replace(
- '\n', r'\n').replace(
- '\r', r'\r')
- result = '{"contents" : "%s"}' % escaped_contents
+ result = file_contents
except urllib2.HTTPError:
- result = '{"error" : "File not found"}'
+ error = LogFileNotFound('%s not found' % path)
-print script % dict(callback=callback, result=result)
+encoded_result = serviceHandler.ServiceHandler.translateResult(result, error,
+ None, None)
+print script % dict(callback=callback, result=encoded_result)