-
Notifications
You must be signed in to change notification settings - Fork 0
/
responder.py
374 lines (307 loc) · 10.6 KB
/
responder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import mimetypes
import sys
import os
if sys.version_info.major == 2:
import urllib2 as urllib
urllib.request = urllib
elif sys.version_info.major == 3:
import urllib
import urllib.request
# Constants
# Files are transferred in chunks of this size.
# A larger size will probably be faster, but
# it will also use more memory.
BUFFER_SIZE = 1024 * 8
# Internal globals
# Each element must be a dict of resource keys referencing the response
# handlers for those resources.
# Response handlers should call set_response() on the response to
# set the response code, and set_content() to set the content type.
response_handlers = {"GET":{}, "POST":{}, "DELETE":{}}
# Filters are functions that take a request and return a response
# handler if the request matches or None otherwise. Filters should
# be very fast, to minimize response latency.
#
# Filters are primarily intended to capture malicious requests,
# so that they can be handled differently from legitimate traffic.
#
# Filters should be used carefully, both to minimize response
# latency and to avoid false positives that could negtively impact
# the experience of legitimate users.
response_filters = []
# Functions
# These will populate the response handlers for requests.
# Currently, other request types can be added manually by adding the
# appropriate request type to response_handers and populating it with
# a dict.
def initGet(handlers):
global response_handlers
response_handlers["GET"] = handlers.index
def initPost(handlers):
global response_handlers
response_handlers["POST"] = handlers.index
# These will add handlers for the appropriate request methods.
# Use these with care, as new handlers can overwrite old ones
# if there are naming conflicts.
def addGet(handlers):
global response_handlers
response_handlers["GET"] = \
dict(list(response_handlers["GET"].items()) + list(handlers.index.items()))
def addPost(handlers):
global response_handlers
response_handlers["POST"] = \
dict(list(response_handlers["POST"].items()) + list(handlers.index.items()))
def addDelete(handlers):
global response_handlers
response_handlers["DELETE"] = \
dict(list(response_handlers["DELETE"].items()) + list(handlers.index.items()))
def addFilter(filters):
global response_filters
response_filters = response_filters + filters.index
def filter_request(request):
global response_filters
triggered_filter = None
for filter in response_filters:
triggered_filter = filter(request)
if triggered_filter is not None:
return triggered_filter
# Creates and returns the appropriate response type for the request
def getresponse(request):
global response_handlers
# Get the resource name
resource = request.path.strip("/").split("?", 1)[0].split("/", 1)[0]
# Filter request
triggered_filter = filter_request(request)
if triggered_filter is not None:
return triggered_filter(request)
# Check if the first path element is a valid resource
if resource in response_handlers[request.command]:
# If so, generate a resource response
# Note that resources will shadow directories with the
# same name.
return ResourceResponse(request)
else:
# Otherwise assume it is a file request
return FileResponse(request)
# Class definitions
# Do not use this abstract class. Instead inherit or use one of the
# other included responses.
class Response(object):
# request = BaseHTTPServer.RequestHandler
def __init__(self, request):
self.request = request
self.response = 200 # Default value
self.content = 'text/html' # Default value
self.extra_headers = {}
def set_response(self, code):
if code in self.request.responses:
self.response = code
else:
# Throw some kind of exception here
pass
def set_content(self, contenttype):
self.content = contenttype
def set_extra_header(self, header, value):
self.extra_headers[header] = value
# Override this!
# This should send the headers and write the data
# to the output stream (self.request.wfile.write())
def send(self):
pass
# Send error page
# send() function should return directly after calling this,
# to avoid trying to send anything else
# Set reponse code before calling this
def send_error(self):
error_text = self.request.responses[self.response][0]
data = "<!DOCTYPE html>" # HTML5
data += "<html><head><title>"
data += error_text
data += "</title></head>"
data += "<body>"
data += "<pre>"
data += str(self.response) + ": " + error_text
data += "</pre>"
data += "</body>"
data += "</html>"
# Send HTTP header data
self.request.send_response(self.response)
self.request.send_header('Content-Type', 'text/html')
self.request.send_header('Content-Length', len(data))
for k, v in self.extra_headers.items():
self.request.send_header(k, v)
self.request.end_headers()
# Send the data
self.request.wfile.write(bytes(data, encoding="utf-8"))
# For resource handling (not for file handling)
class ResourceResponse(Response):
# Instance variables
# resource = the first element of the path (for "/piano/xyz",
# this would be "piano")
# query = the query string, parsed into a dictionary if possible
# subpath = any elements of the path beyond the first
def __init__(self, request):
# Call parent constructor
super(ResourceResponse, self).__init__(request)
self.cookie = {}
# Parse the URL path
# Split off the query string
if "?" in self.request.path:
self.resource, self.query = \
self.request.path.strip("/").split("?", 1)
# Split the query string first at & and then split
# expressions at = and put the result into a dictionary
try:
self.query = {urllib.parse.unquote_plus(key):urllib.parse.unquote_plus(value)
for key, value in [element.split("=")
for element in self.query.split("&")]}
except Exception as e:
# We will assume that the malformed query
# string was intentional, and we will let
# the application handle it
self.query = urllib.parse.unquote_plus(self.query)
else:
self.resource = self.request.path.strip("/")
self.query = None
# Extract the base resource from the URL
# Whatever is left is put in subpath for the user to manage
if "/" in self.resource:
self.resource, self.subpath = \
self.resource.split("/", 1)
else:
self.subpath = None
# If this is a POST request, let's kindly parse the data if it is
# form data.
if request.command == "POST" and \
"Content-Type" in request.headers and \
"application/x-www-form-urlencoded" in request.headers["Content-Type"]:
try:
clength = int(request.headers["Content-Length"])
self.postquery = request.rfile.read(clength)
# Turn the form data into a dictionary
self.postquery = {key:value
for key, value in [element.split("=")
for element in self.postquery.split("&")]}
except:
pass
# If the POST data is JSON data, we will read it into
# a string for the application.
elif request.command == "POST" and \
"Content-Type" in request.headers and \
"application/json" in request.headers["Content-Type"]:
clength = int(request.headers["Content-Length"])
self.postjson = request.rfile.read(clength)
# Ok, so we will handle cookies too...
if "Cookie" in request.headers:
request.cookie = request.headers["Cookie"]
try:
request.cookie = {key:value
for key, value in [element.split("=")
for element in request.cookie.split("; ")]}
except:
pass
# This should generate the data, then send the header and then the data
def send(self):
global response_handlers
data = ""
try:
data = response_handlers[self.request.command][self.resource](self)
except KeyError:
self.set_response(404)
self.send_error()
return
except Exception as e:
print(e)
self.set_response(500)
self.send_error()
return
# Send HTTP header data
self.request.send_response(self.response)
self.request.send_header('Content-Type', self.content)
self.request.send_header('Content-Length', len(data))
for k, v in self.extra_headers.items():
self.request.send_header(k, v)
self._send_cookies()
self.request.end_headers()
# Send the data
if type(data) == str:
self.request.wfile.write(bytes(data, encoding="UTF-8"))
else:
self.request.wfile.write(data)
def _send_cookies(self):
for k, v in self.cookie.items():
cookie = str(k) + "=" + str(v)
self.request.send_header('Set-Cookie', cookie)
# For file handling
class FileResponse(Response):
# Instance variables
# path = the cleaned file path
def __init__(self, request):
# Call parent constructor
super(FileResponse, self).__init__(request)
cwd = os.getcwd()
# Trim any query string
self.path = cwd + urllib.request.url2pathname(os.path.normpath(self.request.path.split("?", 1)[0]))
# Find the MIME type and set the content
(self.content, _) = mimetypes.guess_type(self.path)
if not self.content:
self.content = "application/octet-stream"
# Send the file, or
def send(self):
file_size = 0
try:
file_size = os.path.getsize(self.path)
except OSError as e:
data = ""
if e.errno == 2: # No such file...
self.set_response(404)
self.send_error()
else: # errno: 13, Permission denied
print(e)
self.set_response(403)
self.send_error()
# We will consider anything that is not 404 to be
# Permission denied, even if it is some other
# reason. This will help avoid revealing information
# that could be used to compromise security.
return
except Exception as e:
print(e)
# If it is not an OSError, it is probably an
# internal server error
self.set_response(500)
self.send_error()
return
file = None
try:
file = open(self.path, 'rb')
# Send HTTP header data
self.request.send_response(self.response)
self.request.send_header('Content-Type', self.content + "; charset=utf-8")
self.request.send_header('Content-Length', file_size)
self.request.end_headers()
# Send the file data in chunks of BUFFER_SIZE
buffer = file.read(BUFFER_SIZE)
while buffer:
self.request.wfile.write(buffer)
buffer = file.read(BUFFER_SIZE)
except IOError as e:
data = ""
if e.errno == 2: # No such file...
self.set_response(404)
self.send_error()
else: # errno: 13, Permission denied
print(e)
self.set_response(403)
self.send_error()
except Exception as e:
print(e)
# We have already checked to make sure the file
# exists and is readable. If we fail now, it is
# probably something else.
self.set_response(500)
self.send_error()
return
finally:
if file:
file.close()