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-10-04 21:30:32 +00:00
|
|
|
import language, utils
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
class PathocError(Exception): pass
|
|
|
|
|
|
|
|
|
2013-02-26 20:07:16 +00:00
|
|
|
class Response:
|
|
|
|
def __init__(self, httpversion, status_code, msg, headers, content):
|
|
|
|
self.httpversion, self.status_code, self.msg = httpversion, status_code, msg
|
|
|
|
self.headers, self.content = headers, content
|
|
|
|
|
2013-03-02 03:57:00 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "Response(%s - %s)"%(self.status_code, self.msg)
|
|
|
|
|
2012-06-24 09:10:10 +00:00
|
|
|
class Pathoc(tcp.TCPClient):
|
2014-02-27 05:33:48 +00:00
|
|
|
def __init__(self, address, ssl=None, sni=None, sslversion=1, clientcert=None, ciphers=None):
|
2014-01-28 18:28:20 +00:00
|
|
|
tcp.TCPClient.__init__(self, address)
|
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
|
|
|
)
|
2013-01-03 21:37:26 +00:00
|
|
|
self.ssl, self.sni = ssl, sni
|
2013-01-20 09:37:43 +00:00
|
|
|
self.clientcert = clientcert
|
2014-02-27 05:33:48 +00:00
|
|
|
self.sslversion = utils.SSLVERSIONS[sslversion]
|
|
|
|
self.ciphers = ciphers
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2013-12-15 05:42:58 +00:00
|
|
|
def http_connect(self, connect_to):
|
|
|
|
self.wfile.write(
|
2013-01-05 07:29:46 +00:00
|
|
|
'CONNECT %s:%s HTTP/1.1\r\n'%tuple(connect_to) +
|
|
|
|
'\r\n'
|
|
|
|
)
|
2013-12-15 05:42:58 +00:00
|
|
|
self.wfile.flush()
|
|
|
|
l = self.rfile.readline()
|
2013-03-02 03:57:00 +00:00
|
|
|
if not l:
|
|
|
|
raise PathocError("Proxy CONNECT failed")
|
|
|
|
parsed = http.parse_response_line(l)
|
|
|
|
if not parsed[1] == 200:
|
|
|
|
raise PathocError("Proxy CONNECT failed: %s - %s"%(parsed[1], parsed[2]))
|
2014-01-19 05:20:01 +00:00
|
|
|
http.read_headers(self.rfile)
|
2013-01-05 07:29:46 +00:00
|
|
|
|
|
|
|
def connect(self, connect_to=None):
|
|
|
|
"""
|
|
|
|
connect_to: A (host, port) tuple, which will be connected to with an
|
|
|
|
HTTP CONNECT request.
|
|
|
|
"""
|
2013-01-03 21:37:26 +00:00
|
|
|
tcp.TCPClient.connect(self)
|
2013-01-05 07:29:46 +00:00
|
|
|
if connect_to:
|
2013-12-15 05:42:58 +00:00
|
|
|
self.http_connect(connect_to)
|
2013-01-03 21:37:26 +00:00
|
|
|
if self.ssl:
|
|
|
|
try:
|
2014-02-27 05:33:48 +00:00
|
|
|
self.convert_to_ssl(
|
|
|
|
sni=self.sni,
|
|
|
|
cert=self.clientcert,
|
|
|
|
method=self.sslversion,
|
|
|
|
cipher_list = self.ciphers
|
|
|
|
)
|
2013-01-03 21:37:26 +00:00
|
|
|
except tcp.NetLibError, v:
|
|
|
|
raise PathocError(str(v))
|
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-10-04 21:30:32 +00:00
|
|
|
May raise language.ParseException, netlib.http.HttpError or
|
|
|
|
language.FileAccessDenied.
|
2012-06-26 05:28:07 +00:00
|
|
|
"""
|
2012-10-04 21:30:32 +00:00
|
|
|
r = language.parse_request(self.settings, spec)
|
2014-01-28 18:28:20 +00:00
|
|
|
language.serve(r, self.wfile, self.settings, self.address.host)
|
2012-06-24 09:10:10 +00:00
|
|
|
self.wfile.flush()
|
2013-02-26 20:07:16 +00:00
|
|
|
return Response(*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:
|
2012-10-04 21:30:32 +00:00
|
|
|
r = language.parse_request(self.settings, spec)
|
|
|
|
except language.ParseException, v:
|
2012-09-26 02:25:39 +00:00
|
|
|
print >> fp, "Error parsing request spec: %s"%v.msg
|
|
|
|
print >> fp, v.marked()
|
|
|
|
return
|
2012-10-04 21:30:32 +00:00
|
|
|
except language.FileAccessDenied, v:
|
2012-09-26 02:25:39 +00:00
|
|
|
print >> fp, "File access error: %s"%v
|
|
|
|
return
|
|
|
|
|
2012-10-30 22:23:53 +00:00
|
|
|
if explain:
|
2014-01-28 18:28:20 +00:00
|
|
|
r = r.freeze(self.settings, self.address.host)
|
2012-10-30 22:23:53 +00:00
|
|
|
|
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()
|
2012-10-04 21:30:32 +00:00
|
|
|
if showresp:
|
|
|
|
self.rfile.start_log()
|
2012-09-26 02:25:39 +00:00
|
|
|
try:
|
2014-01-28 18:28:20 +00:00
|
|
|
req = language.serve(r, self.wfile, self.settings, self.address.host)
|
2012-09-26 02:25:39 +00:00
|
|
|
self.wfile.flush()
|
|
|
|
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
|
2012-10-30 22:23:53 +00:00
|
|
|
if explain:
|
|
|
|
print >> fp, ">> Spec:", r.spec()
|
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
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
|