Support Python scripts in test/www/.
Rather than add all the special URLs we'll ever want to run-tests.py,
the embedded HTTP server now supports scripts in test/www/. If you
try to load http://localhost:9180/path and test/www/path doesn't exist
but test/www/path.py does, then test/www/path.py is loaded as a module.
That module must export one function, handle_request(), which is called
to produce the response.
handle_request() has the same semantics as
SimpleHTTPRequestHandler.send_head(). That is, it takes one argument,
the SimpleHTTPRequestHandler object, conventionally named 'req'.
It should call the send_response(), send_header(), and end_headers()
methods of that object as appropriate. And it should return a readable
filelike whose contents are taken as the body of the response.
If either module import or handle_request() throws any exception, the
HTTP response will be a 500 Internal Server Error with body provided
by cgitb.
Ongoing work on issue #12439.
2014-09-12 23:43:29 +04:00
|
|
|
import json
|
|
|
|
import urlparse
|
|
|
|
import cStringIO as StringIO
|
|
|
|
|
|
|
|
def handle_request(req):
|
|
|
|
url = urlparse.urlparse(req.path)
|
|
|
|
headers = {}
|
|
|
|
for name, value in req.headers.items():
|
|
|
|
headers[name] = value.rstrip()
|
|
|
|
|
|
|
|
d = dict(
|
|
|
|
command = req.command,
|
|
|
|
version = req.protocol_version,
|
|
|
|
origin = req.client_address,
|
|
|
|
url = req.path,
|
|
|
|
path = url.path,
|
|
|
|
params = url.params,
|
|
|
|
query = url.query,
|
|
|
|
fragment = url.fragment,
|
|
|
|
headers = headers
|
|
|
|
)
|
|
|
|
body = json.dumps(d, indent=2) + '\n'
|
|
|
|
|
|
|
|
req.send_response(200)
|
|
|
|
req.send_header('Content-Type', 'application/json')
|
|
|
|
req.send_header('Content-Length', str(len(body)))
|
|
|
|
req.end_headers()
|
2014-09-17 00:56:27 +04:00
|
|
|
return StringIO.StringIO(body)
|