1
# Written by Bram Cohen
2
# see LICENSE.txt for license information
4
from cStringIO import StringIO
7
from gzip import GzipFile
11
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
13
months = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
14
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
17
def __init__(self, handler, connection):
18
self.handler = handler
19
self.connection = connection
23
self.donereading = False
24
self.next_func = self.read_type
27
return self.connection.get_ip()
29
def data_came_in(self, data):
30
if self.donereading or self.next_func is None:
35
i = self.buf.index('\n')
39
self.buf = self.buf[i+1:]
40
self.next_func = self.next_func(val)
43
if self.next_func is None or self.closed:
46
def read_type(self, data):
47
self.header = data.strip()
50
self.command, self.path, garbage = words
53
self.command, self.path = words
55
if self.command != 'GET':
59
if self.command not in ('HEAD', 'GET'):
62
return self.read_header
64
def read_header(self, data):
67
self.donereading = True
68
# check for Accept-Encoding: header, pick a
69
if self.headers.has_key('accept-encoding'):
70
ae = self.headers['accept-encoding']
72
print "Got Accept-Encoding: " + ae + "\n"
74
#identity assumed if no header
76
# this eventually needs to support multple acceptable types
77
# q-values and all that fancy HTTP crap
78
# for now assume we're only communicating with our own client
79
if ae.find('gzip') != -1:
80
self.encoding = 'gzip'
83
self.encoding = 'identity'
84
r = self.handler.getfunc(self, self.path, self.headers)
92
self.headers[data[:i].strip().lower()] = data[i+1:].strip()
94
print data[:i].strip() + ": " + data[i+1:].strip()
95
return self.read_header
97
def answer(self, (responsecode, responsestring, headers, data)):
100
if self.encoding == 'gzip':
101
#transform data using gzip compression
102
#this is nasty but i'm unsure of a better way at the moment
103
compressed = StringIO()
104
gz = GzipFile(fileobj = compressed, mode = 'wb', compresslevel = 9)
108
cdata = compressed.read()
110
if len(cdata) >= len(data):
111
self.encoding = 'identity'
114
print "Compressed: %i Uncompressed: %i\n" % (len(cdata),len(data))
116
headers['Content-Encoding'] = 'gzip'
118
# i'm abusing the identd field here, but this should be ok
119
if self.encoding == 'identity':
122
ident = self.encoding
124
referer = self.headers.get('referer','-')
125
useragent = self.headers.get('user-agent','-')
126
year, month, day, hour, minute, second, a, b, c = time.localtime(time.time())
127
print '%s %s %s [%02d/%3s/%04d:%02d:%02d:%02d] "%s" %i %i "%s" "%s"' % (
128
self.connection.get_ip(), ident, username, day, months[month], year, hour,
129
minute, second, self.header, responsecode, len(data), referer, useragent)
131
if t - self.handler.lastflush > self.handler.minflush:
132
self.handler.lastflush = t
137
r.write('HTTP/1.0 ' + str(responsecode) + ' ' +
138
responsestring + '\r\n')
140
headers['Content-Length'] = len(data)
141
for key, value in headers.items():
142
r.write(key + ': ' + str(value) + '\r\n')
144
if self.command != 'HEAD':
146
self.connection.write(r.getvalue())
147
if self.connection.is_flushed():
148
self.connection.shutdown(1)
151
def __init__(self, getfunc, minflush):
152
self.connections = {}
153
self.getfunc = getfunc
154
self.minflush = minflush
155
self.lastflush = time.time()
157
def external_connection_made(self, connection):
158
self.connections[connection] = HTTPConnection(self, connection)
160
def connection_flushed(self, connection):
161
if self.connections[connection].done:
162
connection.shutdown(1)
164
def connection_lost(self, connection):
165
ec = self.connections[connection]
169
del self.connections[connection]
171
def data_came_in(self, connection, data):
172
c = self.connections[connection]
173
if not c.data_came_in(data) and not c.closed:
174
c.connection.shutdown(1)