mitmproxy/libpathod/pathod.py

60 lines
2.0 KiB
Python
Raw Normal View History

2012-06-19 22:51:37 +00:00
import urllib
2012-06-19 04:57:57 +00:00
from netlib import tcp, protocol, odict, wsgi
2012-06-19 22:51:37 +00:00
import version, app, rparse
2012-06-19 04:57:57 +00:00
2012-06-19 01:23:07 +00:00
class PathodHandler(tcp.BaseHandler):
def handle(self):
2012-06-19 22:59:38 +00:00
if self.server.ssloptions:
self.convert_to_ssl(
self.server.ssloptions["certfile"],
self.server.ssloptions["keyfile"],
)
2012-06-19 01:23:07 +00:00
line = self.rfile.readline()
if line == "\r\n" or line == "\n": # Possible leftover from previous message
line = self.rfile.readline()
if line == "":
return None
2012-04-29 21:46:49 +00:00
2012-06-19 01:23:07 +00:00
method, path, httpversion = protocol.parse_init_http(line)
2012-06-19 22:51:37 +00:00
if path.startswith(self.server.prefix):
spec = urllib.unquote(path)[len(self.server.prefix):]
try:
presp = rparse.parse({}, spec)
except rparse.ParseException, v:
presp = rparse.InternalResponse(
800,
"Error parsing response spec: %s\n"%v.msg + v.marked()
)
presp.serve(self.wfile)
self.finish()
return
2012-06-19 01:23:07 +00:00
headers = odict.ODictCaseless(protocol.read_headers(self.rfile))
content = protocol.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, None
)
2012-06-19 04:57:57 +00:00
cc = wsgi.ClientConn(self.client_address)
req = wsgi.Request(cc, "http", method, path, headers, content)
sn = self.connection.getsockname()
app = wsgi.WSGIAdaptor(
self.server.app,
sn[0],
self.server.port,
version.NAMEVERSION
)
app.serve(req, self.wfile)
2012-06-19 01:23:07 +00:00
class Pathod(tcp.TCPServer):
2012-06-19 22:59:38 +00:00
def __init__(self, addr, ssloptions=None, prefix="/p/"):
2012-06-19 01:23:07 +00:00
tcp.TCPServer.__init__(self, addr)
2012-06-19 22:59:38 +00:00
self.ssloptions = ssloptions
2012-06-19 22:51:37 +00:00
self.prefix = prefix
2012-06-19 04:57:57 +00:00
self.app = app.app
self.app.config["pathod"] = self
def handle_connection(self, request, client_address):
PathodHandler(request, client_address, self)