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-27 23:38:49 +00:00
|
|
|
def print_request(self, spec, showreq, showresp, explain, hexdump, ignorecodes, ignoretimeout, 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.
|
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
spec: A request specification
|
2012-09-25 22:12:30 +00:00
|
|
|
showreq: Print requests
|
|
|
|
showresp: Print responses
|
|
|
|
explain: Print request explanation
|
|
|
|
hexdump: When printing requests or responses, use hex dump output
|
2012-09-26 21:44:25 +00:00
|
|
|
ignorecodes: Sequence of return codes to ignore
|
2012-09-26 22:56:06 +00:00
|
|
|
|
|
|
|
Returns True if we have a non-ignored response.
|
2012-06-29 22:51:13 +00:00
|
|
|
"""
|
2012-09-26 02:25:39 +00:00
|
|
|
try:
|
|
|
|
r = rparse.parse_request(self.settings, spec)
|
|
|
|
except rparse.ParseException, v:
|
|
|
|
print >> fp, "Error parsing request spec: %s"%v.msg
|
|
|
|
print >> fp, v.marked()
|
|
|
|
return
|
|
|
|
except rparse.FileAccessDenied, v:
|
|
|
|
print >> fp, "File access error: %s"%v
|
|
|
|
return
|
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
resp, req = None, None
|
2012-09-26 02:25:39 +00:00
|
|
|
if showreq:
|
|
|
|
self.wfile.start_log()
|
|
|
|
try:
|
|
|
|
req = r.serve(self.wfile, None, self.host)
|
|
|
|
self.wfile.flush()
|
|
|
|
if showresp:
|
|
|
|
self.rfile.start_log()
|
|
|
|
resp = http.read_response(self.rfile, r.method, None)
|
|
|
|
except http.HttpError, v:
|
|
|
|
print >> fp, "<< HTTP Error:", v.msg
|
|
|
|
except tcp.NetLibTimeout:
|
2012-09-27 23:38:49 +00:00
|
|
|
if ignoretimeout:
|
|
|
|
return
|
2012-09-26 02:25:39 +00:00
|
|
|
print >> fp, "<<", "Timeout"
|
|
|
|
except tcp.NetLibDisconnect: # pragma: nocover
|
|
|
|
print >> fp, "<<", "Disconnect"
|
2012-09-26 21:44:25 +00:00
|
|
|
|
|
|
|
if req:
|
|
|
|
if ignorecodes and resp and resp[1] in ignorecodes:
|
|
|
|
return
|
|
|
|
if explain:
|
|
|
|
print >> fp, ">>", req["method"], repr(req["path"])
|
|
|
|
for a in req["actions"]:
|
|
|
|
print >> fp, "\t",
|
|
|
|
for x in a:
|
|
|
|
print >> fp, x,
|
|
|
|
print >> fp
|
|
|
|
if showreq:
|
|
|
|
self._show(fp, ">> Request", self.wfile.get_log(), hexdump)
|
|
|
|
|
|
|
|
if showresp:
|
|
|
|
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
|
|
|
else:
|
|
|
|
if resp:
|
|
|
|
self._show_summary(fp, *resp)
|
2012-09-26 22:56:06 +00:00
|
|
|
return True
|