2012-07-22 11:37:46 +00:00
|
|
|
import sys, os
|
2012-06-24 09:10:10 +00:00
|
|
|
from netlib import tcp, http
|
2012-07-23 12:00:55 +00:00
|
|
|
import rparse, utils
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
class PathocError(Exception): pass
|
|
|
|
|
|
|
|
|
2012-06-24 10:59:20 +00:00
|
|
|
def print_short(fp, httpversion, code, msg, headers, content):
|
2012-07-23 12:00:55 +00:00
|
|
|
print >> fp, "<< %s %s: %s bytes"%(code, utils.xrepr(msg), len(content))
|
2012-06-24 10:59:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_full(fp, httpversion, code, msg, headers, content):
|
2012-07-23 12:00:55 +00:00
|
|
|
print >> fp, "<< HTTP%s/%s %s %s"%(httpversion[0], httpversion[1], code, utils.xrepr(msg))
|
|
|
|
print >> fp, utils.escape_unprintables(str(headers))
|
|
|
|
print >> fp, utils.escape_unprintables(content)
|
2012-06-24 10:59:20 +00:00
|
|
|
|
|
|
|
|
2012-06-24 09:10:10 +00:00
|
|
|
class Pathoc(tcp.TCPClient):
|
2012-06-25 22:15:11 +00:00
|
|
|
def __init__(self, host, port):
|
2012-06-29 22:51:13 +00:00
|
|
|
tcp.TCPClient.__init__(self, host, port)
|
2012-07-22 11:37:46 +00:00
|
|
|
self.settings = dict(
|
|
|
|
staticdir = os.getcwd(),
|
2012-07-24 09:38:28 +00:00
|
|
|
unconstrained_file_access = True,
|
2012-07-22 11:37:46 +00:00
|
|
|
)
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
def request(self, spec):
|
2012-06-26 05:28:07 +00:00
|
|
|
"""
|
|
|
|
Return an (httpversion, code, msg, headers, content) tuple.
|
2012-06-29 22:51:13 +00:00
|
|
|
|
2012-07-22 11:37:46 +00:00
|
|
|
May raise rparse.ParseException, netlib.http.HttpError or
|
|
|
|
rparse.FileAccessDenied.
|
2012-06-26 05:28:07 +00:00
|
|
|
"""
|
2012-07-22 11:37:46 +00:00
|
|
|
r = rparse.parse_request(self.settings, spec)
|
2012-07-24 09:38:28 +00:00
|
|
|
ret = r.serve(self.wfile, None, self.host)
|
2012-06-24 09:10:10 +00:00
|
|
|
self.wfile.flush()
|
2012-06-24 10:59:20 +00:00
|
|
|
return http.read_response(self.rfile, r.method, None)
|
2012-06-29 22:51:13 +00:00
|
|
|
|
2012-07-21 08:20:37 +00:00
|
|
|
def print_requests(self, reqs, respdump, reqdump, fp=sys.stdout):
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
|
|
|
Performs a series of requests, and prints results to the specified
|
|
|
|
file pointer.
|
|
|
|
"""
|
|
|
|
for i in reqs:
|
|
|
|
try:
|
2012-07-22 11:37:46 +00:00
|
|
|
r = rparse.parse_request(self.settings, i)
|
2012-07-24 09:38:28 +00:00
|
|
|
req = r.serve(self.wfile, None, self.host)
|
2012-07-21 08:20:37 +00:00
|
|
|
if reqdump:
|
2012-07-22 03:26:05 +00:00
|
|
|
print >> fp, "\n>>", req["method"], repr(req["path"])
|
2012-07-21 08:20:37 +00:00
|
|
|
for a in req["actions"]:
|
|
|
|
print >> fp, "\t",
|
|
|
|
for x in a:
|
2012-07-21 08:50:41 +00:00
|
|
|
print >> fp, x,
|
|
|
|
print >> fp
|
2012-07-21 08:20:37 +00:00
|
|
|
self.wfile.flush()
|
2012-07-21 08:50:41 +00:00
|
|
|
resp = http.read_response(self.rfile, r.method, None)
|
2012-06-29 22:51:13 +00:00
|
|
|
except rparse.ParseException, v:
|
|
|
|
print >> fp, "Error parsing request spec: %s"%v.msg
|
|
|
|
print >> fp, v.marked()
|
|
|
|
return
|
2012-07-22 11:37:46 +00:00
|
|
|
except rparse.FileAccessDenied, v:
|
|
|
|
print >> fp, "File access error: %s"%v
|
|
|
|
return
|
2012-06-29 22:51:13 +00:00
|
|
|
except http.HttpError, v:
|
2012-07-21 08:50:41 +00:00
|
|
|
print >> fp, "<<", v.msg
|
2012-06-29 22:51:13 +00:00
|
|
|
return
|
2012-07-21 04:19:44 +00:00
|
|
|
except tcp.NetLibTimeout:
|
2012-07-21 08:50:41 +00:00
|
|
|
print >> fp, "<<", "Timeout"
|
2012-06-29 22:51:13 +00:00
|
|
|
else:
|
2012-07-21 08:20:37 +00:00
|
|
|
if respdump:
|
|
|
|
print_full(fp, *resp)
|
2012-07-21 04:19:44 +00:00
|
|
|
else:
|
2012-07-21 08:20:37 +00:00
|
|
|
print_short(fp, *resp)
|