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-09-25 22:38:47 +00:00
|
|
|
import netlib.utils
|
2012-07-23 12:00:55 +00:00
|
|
|
import rparse, utils
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
class PathocError(Exception): pass
|
|
|
|
|
|
|
|
|
|
|
|
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-09-25 23:07:22 +00:00
|
|
|
def _show_summary(self, fp, httpversion, code, msg, headers, content):
|
|
|
|
print >> fp, "<< %s %s: %s bytes"%(code, utils.xrepr(msg), len(content))
|
|
|
|
|
|
|
|
def _show(self, fp, header, data, hexdump):
|
|
|
|
if hexdump:
|
|
|
|
print >> fp, "%s (hex dump):"%header
|
|
|
|
for line in netlib.utils.hexdump(data):
|
|
|
|
print >> fp, "\t%s %s %s"%line
|
|
|
|
else:
|
|
|
|
print >> fp, "%s (unprintables escaped):"%header
|
|
|
|
print >> fp, netlib.utils.cleanBin(data)
|
|
|
|
|
2012-09-25 22:12:30 +00:00
|
|
|
def print_requests(self, reqs, showreq, showresp, explain, hexdump, fp=sys.stdout):
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
|
|
|
Performs a series of requests, and prints results to the specified
|
2012-09-25 22:12:30 +00:00
|
|
|
file descriptor.
|
|
|
|
|
|
|
|
reqs: A sequence of request specifications
|
|
|
|
showreq: Print requests
|
|
|
|
showresp: Print responses
|
|
|
|
explain: Print request explanation
|
|
|
|
hexdump: When printing requests or responses, use hex dump output
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
|
|
|
for i in reqs:
|
|
|
|
try:
|
2012-07-22 11:37:46 +00:00
|
|
|
r = rparse.parse_request(self.settings, i)
|
2012-09-25 22:38:47 +00:00
|
|
|
if showreq:
|
|
|
|
self.wfile.start_log()
|
2012-07-24 09:38:28 +00:00
|
|
|
req = r.serve(self.wfile, None, self.host)
|
2012-09-25 22:12:30 +00:00
|
|
|
if explain:
|
2012-09-25 22:38:47 +00:00
|
|
|
print >> fp, ">> ", 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-09-25 22:38:47 +00:00
|
|
|
if showreq:
|
2012-09-25 23:07:22 +00:00
|
|
|
self._show(fp, ">> Request", self.wfile.get_log(), hexdump)
|
2012-07-21 08:20:37 +00:00
|
|
|
self.wfile.flush()
|
2012-09-25 23:07:22 +00:00
|
|
|
if showresp:
|
|
|
|
self.rfile.start_log()
|
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-09-25 22:12:30 +00:00
|
|
|
print >> fp, "<< HTTP Error:", v.msg
|
2012-09-25 23:07:22 +00:00
|
|
|
if showresp:
|
|
|
|
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
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-09-25 23:07:22 +00:00
|
|
|
if showresp:
|
|
|
|
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
2012-07-30 01:52:40 +00:00
|
|
|
return
|
|
|
|
except tcp.NetLibDisconnect: # pragma: nocover
|
2012-07-29 23:58:29 +00:00
|
|
|
print >> fp, "<<", "Disconnect"
|
2012-07-30 01:52:40 +00:00
|
|
|
return
|
2012-06-29 22:51:13 +00:00
|
|
|
else:
|
2012-09-25 22:12:30 +00:00
|
|
|
if showresp:
|
2012-09-25 23:07:22 +00:00
|
|
|
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
2012-07-21 04:19:44 +00:00
|
|
|
else:
|
2012-09-25 23:07:22 +00:00
|
|
|
self._show_summary(fp, *resp)
|