2012-06-27 04:15:22 +00:00
|
|
|
import urllib, threading, re, logging, socket, sys
|
2012-06-23 02:06:54 +00:00
|
|
|
from netlib import tcp, http, odict, wsgi
|
2012-06-19 22:51:37 +00:00
|
|
|
import version, app, rparse
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2012-06-24 09:40:31 +00:00
|
|
|
|
2012-06-24 04:38:32 +00:00
|
|
|
class PathodError(Exception): pass
|
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-06-19 01:23:07 +00:00
|
|
|
class PathodHandler(tcp.BaseHandler):
|
2012-06-26 02:49:40 +00:00
|
|
|
wbufsize = 0
|
2012-06-25 22:15:11 +00:00
|
|
|
sni = None
|
2012-06-26 03:36:59 +00:00
|
|
|
def debug(self, s):
|
|
|
|
logging.debug("%s:%s: %s"%(self.client_address[0], self.client_address[1], str(s)))
|
|
|
|
|
2012-07-19 23:40:37 +00:00
|
|
|
def info(self, s):
|
|
|
|
logging.info("%s:%s: %s"%(self.client_address[0], self.client_address[1], str(s)))
|
|
|
|
|
2012-06-25 22:15:11 +00:00
|
|
|
def handle_sni(self, connection):
|
|
|
|
self.sni = connection.get_servername()
|
|
|
|
|
2012-07-21 08:50:41 +00:00
|
|
|
def handle_request(self):
|
|
|
|
"""
|
|
|
|
Returns True if handling should continue.
|
|
|
|
"""
|
|
|
|
line = self.rfile.readline()
|
|
|
|
if line == "\r\n" or line == "\n": # Possible leftover from previous message
|
|
|
|
line = self.rfile.readline()
|
|
|
|
if line == "":
|
|
|
|
return
|
|
|
|
|
|
|
|
parts = http.parse_init_http(line)
|
|
|
|
if not parts:
|
2012-07-22 00:40:27 +00:00
|
|
|
s = "Invalid first line: %s"%repr(line)
|
2012-07-21 08:50:41 +00:00
|
|
|
self.info(s)
|
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
|
|
|
type = "error",
|
|
|
|
msg = s
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
method, path, httpversion = parts
|
|
|
|
|
|
|
|
headers = http.read_headers(self.rfile)
|
2012-07-22 00:30:10 +00:00
|
|
|
try:
|
|
|
|
content = http.read_http_body_request(
|
|
|
|
self.rfile, self.wfile, headers, httpversion, None
|
|
|
|
)
|
|
|
|
except http.HttpError, s:
|
|
|
|
s = str(s)
|
|
|
|
self.info(s)
|
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
|
|
|
type = "error",
|
|
|
|
msg = s
|
2012-07-21 08:50:41 +00:00
|
|
|
)
|
2012-07-22 00:30:10 +00:00
|
|
|
)
|
|
|
|
return
|
2012-07-21 08:50:41 +00:00
|
|
|
|
|
|
|
crafted = None
|
|
|
|
for i in self.server.anchors:
|
|
|
|
if i[0].match(path):
|
|
|
|
crafted = i[1]
|
|
|
|
|
|
|
|
if not crafted and path.startswith(self.server.prefix):
|
|
|
|
spec = urllib.unquote(path)[len(self.server.prefix):]
|
|
|
|
try:
|
|
|
|
crafted = rparse.parse_response(self.server.request_settings, spec)
|
|
|
|
except rparse.ParseException, v:
|
|
|
|
crafted = rparse.InternalResponse(
|
|
|
|
800,
|
|
|
|
"Error parsing response spec: %s\n"%v.msg + v.marked()
|
|
|
|
)
|
2012-07-22 11:37:46 +00:00
|
|
|
except rparse.FileAccessDenied:
|
|
|
|
crafted = rparse.InternalResponse(
|
|
|
|
800,
|
|
|
|
"Access Denied"
|
|
|
|
)
|
2012-07-21 08:50:41 +00:00
|
|
|
|
|
|
|
request_log = dict(
|
|
|
|
path = path,
|
|
|
|
method = method,
|
|
|
|
headers = headers.lst,
|
|
|
|
sni = self.sni,
|
|
|
|
remote_address = self.client_address,
|
|
|
|
httpversion = httpversion,
|
|
|
|
)
|
|
|
|
if crafted:
|
|
|
|
response_log = crafted.serve(self.wfile)
|
|
|
|
if response_log["disconnect"]:
|
|
|
|
return
|
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
|
|
|
type = "crafted",
|
|
|
|
request=request_log,
|
|
|
|
response=response_log
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
self.debug("%s %s"%(method, path))
|
|
|
|
return True
|
|
|
|
|
2012-06-16 19:57:24 +00:00
|
|
|
def handle(self):
|
2012-06-19 22:59:38 +00:00
|
|
|
if self.server.ssloptions:
|
2012-06-25 23:03:35 +00:00
|
|
|
try:
|
|
|
|
self.convert_to_ssl(
|
|
|
|
self.server.ssloptions["certfile"],
|
|
|
|
self.server.ssloptions["keyfile"],
|
|
|
|
)
|
|
|
|
except tcp.NetLibError, v:
|
2012-07-20 01:21:33 +00:00
|
|
|
s = str(v)
|
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
|
|
|
type = "error",
|
|
|
|
msg = s
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.info(s)
|
2012-07-21 08:50:41 +00:00
|
|
|
return
|
2012-06-19 22:59:38 +00:00
|
|
|
|
2012-06-24 23:34:29 +00:00
|
|
|
while not self.finished:
|
2012-07-21 08:50:41 +00:00
|
|
|
try:
|
|
|
|
if not self.handle_request():
|
|
|
|
return
|
|
|
|
except tcp.NetLibDisconnect:
|
|
|
|
self.info("Disconnect")
|
2012-07-19 23:40:37 +00:00
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
|
|
|
type = "error",
|
2012-07-21 08:50:41 +00:00
|
|
|
msg = "Disconnect"
|
2012-07-19 23:40:37 +00:00
|
|
|
)
|
|
|
|
)
|
2012-07-21 08:50:41 +00:00
|
|
|
return
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-06-19 01:23:07 +00:00
|
|
|
class Pathod(tcp.TCPServer):
|
2012-06-21 02:29:49 +00:00
|
|
|
LOGBUF = 500
|
|
|
|
def __init__(self, addr, ssloptions=None, prefix="/p/", staticdir=None, anchors=None):
|
2012-06-24 04:20:50 +00:00
|
|
|
"""
|
|
|
|
addr: (address, port) tuple. If port is 0, a free port will be
|
|
|
|
automatically chosen.
|
|
|
|
ssloptions: a dictionary containing certfile and keyfile specifications.
|
2012-06-24 04:38:32 +00:00
|
|
|
prefix: string specifying the prefix at which to anchor response generation.
|
2012-06-24 04:20:50 +00:00
|
|
|
staticdir: path to a directory of static resources, or None.
|
|
|
|
anchors: A list of (regex, spec) tuples, or None.
|
|
|
|
"""
|
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-24 03:07:45 +00:00
|
|
|
self.staticdir = staticdir
|
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
|
2012-06-21 02:29:49 +00:00
|
|
|
self.log = []
|
|
|
|
self.logid = 0
|
2012-06-24 04:20:50 +00:00
|
|
|
self.anchors = []
|
|
|
|
if anchors:
|
|
|
|
for i in anchors:
|
2012-06-24 04:38:32 +00:00
|
|
|
try:
|
|
|
|
arex = re.compile(i[0])
|
|
|
|
except re.error:
|
|
|
|
raise PathodError("Invalid regex in anchor: %s"%i[0])
|
|
|
|
try:
|
2012-06-24 05:01:04 +00:00
|
|
|
aresp = rparse.parse_response(self.request_settings, i[1])
|
2012-06-24 04:38:32 +00:00
|
|
|
except rparse.ParseException, v:
|
|
|
|
raise PathodError("Invalid page spec in anchor: '%s', %s"%(i[1], str(v)))
|
2012-06-24 04:20:50 +00:00
|
|
|
self.anchors.append((arex, aresp))
|
2012-06-07 04:35:54 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
@property
|
|
|
|
def request_settings(self):
|
2012-06-24 03:07:45 +00:00
|
|
|
return dict(
|
|
|
|
staticdir = self.staticdir
|
|
|
|
)
|
2012-06-21 04:54:49 +00:00
|
|
|
|
2012-06-16 19:57:24 +00:00
|
|
|
def handle_connection(self, request, client_address):
|
2012-06-24 23:22:44 +00:00
|
|
|
h = PathodHandler(request, client_address, self)
|
2012-07-19 23:40:37 +00:00
|
|
|
h.handle()
|
2012-06-24 23:22:44 +00:00
|
|
|
h.finish()
|
2012-06-21 02:29:49 +00:00
|
|
|
|
|
|
|
def add_log(self, d):
|
|
|
|
lock = threading.Lock()
|
|
|
|
with lock:
|
|
|
|
d["id"] = self.logid
|
|
|
|
self.log.insert(0, d)
|
|
|
|
if len(self.log) > self.LOGBUF:
|
|
|
|
self.log.pop()
|
|
|
|
self.logid += 1
|
|
|
|
return d["id"]
|
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
lock = threading.Lock()
|
|
|
|
with lock:
|
|
|
|
self.log = []
|
|
|
|
|
|
|
|
def log_by_id(self, id):
|
|
|
|
for i in self.log:
|
|
|
|
if i["id"] == id:
|
|
|
|
return i
|
|
|
|
|
|
|
|
def get_log(self):
|
|
|
|
return self.log
|