"""
+ debuglevel = 0
server_version = "SimpleHTTP/" + __version__
def on_query(self, client, reqtype, *args):
self.client = client
self.hr = args[0]
- print "on_query", reqtype, repr(self.hr.headers), str(self.hr.headers)
+ if self.debuglevel > 0:
+ print "on_query", reqtype, repr(self.hr.headers), \
+ str(self.hr.headers)
session = self.client.session
p = self.proxies.get(session, None)
if not p:
try:
# send command
req = "%s %s %s\n" % (reqtype, self.hr.path, "HTTP/1.1")
- print "req", req
+ if self.debuglevel > 0:
+ print "req", req
yield p.ss.write(req)
conntype = self.hr.headers.get('Connection', "")
# send headers
hdrs = str(self.hr.headers)
- print "hdrs", hdrs
+ if self.debuglevel > 0:
+ print "hdrs", hdrs
yield p.ss.write(hdrs)
yield p.ss.write('\r\n')
max_chunk_size = 10*1024*1024
size_remaining = int(self.hr.headers["content-length"])
L = []
- print "size_remaining", size_remaining
+ if self.debuglevel > 0:
+ print "size_remaining", size_remaining
while size_remaining:
chunk_size = min(size_remaining, max_chunk_size)
data = self.hr.rfile.read(chunk_size)
- print "proxy rfile read", repr(data)
+ if self.debuglevel > 0:
+ print "proxy rfile read", repr(data)
yield multitask.send(p.sock, data)
size_remaining -= len(data)
try:
while 1:
line = (yield p.ss.readline())
- print "reading from proxy", repr(line)
+ if self.debuglevel > 0:
+ print "reading from proxy", repr(line)
res += line
if line in ['\n', '\r\n']:
break
except StopIteration:
- if httpd._debug: print "proxy read stopiter"
+ if self.debuglevel > 0:
+ print "proxy read stopiter"
# TODO: close connection
except:
- if httpd._debug:
+ if self.debuglevel > 0:
print 'proxy read error', \
(traceback and traceback.print_exc() or None)
# TODO: close connection
# Examine the headers and look for a Connection directive
respheaders = mimetools.Message(f, 0)
- print "response headers", str(respheaders)
+ if self.debuglevel > 0:
+ print "response headers", str(respheaders)
remote = self.client.remote
rcooks = httpd.process_cookies(respheaders, remote, "Set-Cookie", False)
rcooks['session'] = self.hr.response_cookies['session'].value # nooo
rcooks['session']['expires'] = \
self.hr.response_cookies['session']['expires']
self.hr.response_cookies = rcooks
- print "rcooks", str(rcooks)
+ if self.debuglevel > 0:
+ print "rcooks", str(rcooks)
# override connection: keep-alive hack
#responseline = responseline.split(" ")
self.hr.close_connection = 0
# write rest of data
- print "writing to client body"
+ if self.debuglevel > 0:
+ print "writing to client body"
yield self.client.writeMessage("\r\n")
if respheaders.has_key('content-length'):
while size_remaining:
chunk_size = min(size_remaining, max_chunk_size)
data = (yield p.ss.read(chunk_size))
- print "reading from proxy expecting", size_remaining, repr(data)
+ if self.debuglevel > 0:
+ print "reading from proxy expecting", \
+ size_remaining, repr(data)
yield self.client.writeMessage(data)
size_remaining -= len(data)
else:
data = (yield p.ss.read(1024))
except httpd.ConnectionClosed:
break
- print "reading from proxy", repr(data)
+ if self.debuglevel > 0:
+ print "reading from proxy", repr(data)
if data == '':
break
yield self.client.writeMessage(data)
if not keepalive: #self.hr.close_connection:
- print 'proxy wants client to close_connection'
+ if self.debuglevel > 0:
+ print 'proxy wants client to close_connection'
try:
yield self.client.connectionClosed()
raise httpd.ConnectionClosed
except httpd.ConnectionClosed:
- print 'close_connection done'
+ if self.debuglevel > 0:
+ print 'close_connection done'
pass
p.serving = False
# there's nothing there to talk to.
self.client.removeConnection()
self.proxies.pop(session)
+
except:
- print traceback.print_exc()
+ if self.debuglevel > 0:
+ print traceback.print_exc()
raise StopIteration
def process_cookies(headers, remote, cookie_key="Cookie", add_sess=True):
ch = headers.getheaders(cookie_key)
- print "messageReceived cookieheaders=", '; '.join(ch)
+ if _debug:
+ print "messageReceived cookieheaders=", '; '.join(ch)
res = []
for c in ch:
c = c.split(";")
has_sess = False
response_cookies = SimpleCookie()
for c in res:
- print "found cookie", repr(c)
+ if _debug:
+ print "found cookie", repr(c)
name, value = c.split("=")
response_cookies[name] = value
#response_cookies[name]['path'] = "/"
def parseRequests(self):
'''Parses complete messages until connection closed. Raises ConnectionLost exception.'''
- print "parseRequests start", repr(self)
+ if _debug:
+ print "parseRequests start", repr(self)
self.hr = MultitaskHTTPRequestHandler(self.stream, self.remote, None)
self.hr.close_connection = 1
self.cookies = CookieJar()
# prepare reading the request so that when it's handed
# over to "standard" HTTPRequestHandler, the data's already
# there.
- print "parseRequests"
+ if _debug:
+ print "parseRequests"
try:
readok = (yield multitask.readable(self.stream.sock, 5000))
except select.error:
print "select error: connection closed"
raise ConnectionClosed
- print "readok", readok
- print
+ if _debug:
+ print "readok", readok
+ print
raw_requestline = (yield self.stream.readline())
if _debug: print "parseRequests, line", raw_requestline
if not raw_requestline:
pos = 0
self.hr.rfile.truncate(0)
self.hr.rfile.write(data)
- print "parseRequests write after"
+ if _debug:
+ print "parseRequests write after"
self.hr.rfile.seek(pos)
- print "parseRequests seek after"
+ if _debug:
+ print "parseRequests seek after"
if not self.hr.parse_request():
raise ConnectionClosed
- print "parseRequests parse_req after"
- print "parseRequest headers", repr(self.hr.headers), str(self.hr.headers)
+ if _debug:
+ print "parseRequests parse_req after"
+ print "parseRequest headers", repr(self.hr.headers), str(self.hr.headers)
try:
yield self.messageReceived(self.hr)
except ConnectionClosed:
if data is None:
# just in case TCP socket is not closed, close it.
try:
- print "stream closing"
- print self.stream
+ if _debug:
+ print "stream closing"
+ print self.stream
yield self.stream.close()
- print "stream closed"
+ if _debug:
+ print "stream closed"
except: pass
break
yield client.rejectConnection(reason='Unsupported encoding ' + str(client.objectEncoding) + '. Please use NetConnection.defaultObjectEncoding=ObjectEncoding.AMF0')
yield client.connectionClosed()
else:
- print "cookies", str(msg.response_cookies)
+ if _debug:
+ print "cookies", str(msg.response_cookies)
session = msg.response_cookies['session'].value
client.session = session
name = msg.path
- print "serverlistener", name
+ if _debug:
+ print "serverlistener", name
if '*' not in self.apps and name not in self.apps:
yield client.rejectConnection(reason='Application not found: ' + name)
else: # create application instance as needed and add in our list
if _debug: print 'name=', name, 'name in apps', str(name in self.apps)
app = self.apps[name] if name in self.apps else self.apps['*'] # application class
- print "clients", self.clients.keys()
+ if _debug:
+ print "clients", self.clients.keys()
if session in self.clients:
inst = self.clients[session][0]
else:
msg.server = inst # whew! just in time!
try:
methodname = "on%s" % msg.command
- print methodname, dir(inst)
+ if _debug:
+ print methodname, dir(inst)
method = getattr(inst, methodname, None)
result = method(client, msg)
close_connection = msg.close_connection
- print "close connection", close_connection
+ if _debug:
+ print "close connection", close_connection
except:
if _debug: traceback.print_exc()
yield client.rejectConnection(reason='Exception on %s' % methodname)
print 'close_connection done'
pass
else:
- print "result", result
+ if _debug:
+ print "result", result
yield client.rejectConnection(reason='Rejected in onConnect')
except StopIteration: raise
except: