-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.py
415 lines (325 loc) · 14.2 KB
/
headers.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# -*- coding: utf-8 -*-
# To you alone oh, The Father of Jesus, our Lord. I give Glory. Forever and
# Ever, AAAAAAMEN
import time
from fs import FileSystem
class Header():
"""
This handles the calculation of the header variables.
Apart from the computeResponse function, every one of the functions
are returning a string and only a string
The computeResponse output should be a byte of-course
"""
def __init__(self, parent_folder, url):
super.__self__
self.parent_folder = parent_folder
self.host = url
self.port = 0
self.content_type = ""
self.request_method = ''
self.requested_file = ''
self.requested_body = ''
self._encoding = 'UTF-8'
self._extension = ''
self.Files = FileSystem(self.parent_folder, self.host)
self._content_length = 0
self.raw_headers = ""
self.status_code = 0
self.status_str = ""
self.headerPair = {}
self.status_stat = {
200: 'OK', 300: 'NOT FOUND', 301: 'MOVED PERMANENTLY',
302: 'FOUND', 303: 'SEE OTHER', 304: 'NOT MODIFIED',
307: 'Temporary Redirect',
308: 'Permanent Redirect',
400: 'Bad Request',
401: 'Unathourized',
402: 'Payment Required',
403: 'Forbidden',
404: 'NOT FOUND',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Payload Too Large',
414: 'URI Too Long',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
418: "I'm a teapot",
421: 'Misdirected Request',
422: 'Unprocessable Entity',
423: 'Locked',
424: 'Failed Dependency',
425: 'Too Early',
426: 'Upgrade Required',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
451: 'Unavailable For Legal Reasons',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
510: 'Not Extended',
511: 'Network Authentication Required'}
self.send_headers = {'Server': 'Peter (Python/3.7)',
'X-Frame-Options': 'SAMEORIGIN',
'Accept-Ranges': 'bytes',
'Content-Length': '0',
'Keep-Alive': 'timeout=5, max=99',
'Connection': 'Keep-Alive',
'Content-Type': 'text/html'}
self.data = ''
self._extMap = {'html': 'text/html', 'htm': 'text/html',
'php': 'text/html', 'css': 'text/css',
'py': 'text/html',
'js': 'application/javascript',
'json': 'application/json',
'png': 'image/png', 'jpeg': 'image/jpeg',
'gif': 'image/gif', 'svg': 'image/svg+xml',
'tiff': 'image/tiff', 'aces': 'image/aces',
'avci': 'image/avci', 'avcs': 'image/avcs',
'bmp': 'image/bmp', 'cgm': 'image/cgm',
'dicom-rle': 'image/dicom-rle',
'emf': 'image/emf', 'example': 'image/example',
'fits': 'image/fits', 'g3fax': 'image/g3fax',
'heic': 'image/heic',
'heic-sequence': 'image/heic-sequence',
'heif': 'image/heif',
'heif-sequence': 'image/heif-sequence',
'hej2k': 'image/hej2k', 'hsj2': 'image/hsj2',
'ief': 'image/ief', 'jls': 'image/jls',
'jp2': 'image/jp2', 'jph': 'image/jph',
'jphc': 'image/jphc', 'jpm': 'image/jpm',
'jpx': 'image/jpx', 'jxr': 'image/jxr',
'jxrA': 'image/jxrA', 'jxrS': 'image/jxrS',
'jxs': 'image/jxs', 'jxsc': 'image/jxsc',
'jxsi': 'image/jxsi', 'jxss': 'image/jxss',
'ktx': 'image/ktx', 'naplps': 'image/naplps',
'prs.btif': 'image/prs.btif',
'prs.pti': 'image/prs.pti',
'pwg-raster': 'image/pwg-raster',
't38': 'image/t38', 'tiff-fx': 'image/tiff-fx',
'wmf': 'image/wmf', 'ico': 'image/ico'}
self.functions = {'Host': self._getHost, 'X-Powered-By': self._powered,
'User-Agent': self._getUserAgent,
'Content-Type': self._getContentType,
'Cookie': self._getCookies}
self.cookies = {}
self.cookie_str = ""
self.user_agent_str = ""
def computeResponse(self):
"""
Computes the response to be sent back to the browser
"""
string = ""
# cookies = {'phpmyadmin': {'phpMyAdmin': "onesdfk", "expires": "Fri,
# 25-May-2018 09:46:00 GMT", "Max-Age": "2592000",
# "path": "/phk/jhkl/"},
# 'user-1': {"user-1": "Jesus", "path": "/path/about/",
# "expires": "Fri, 25-May-2018 09:46:00 GMT"}}
if self.requested_file == '':
return ''
# calculation of the data the we will be sending
self.Files = FileSystem(self.parent_folder, self.host)
self.Files.request_method = self.request_method
self.Files.content_type = self.content_type
self._extension = self.Files._file_extension
self.Files.post_data = self.requested_body
self.Files.cookies = self.cookies
self.Files.cookie_str = self.cookie_str
self.Files.user_agent_str = self.user_agent_str
self.Files.search(self.requested_file)
# All variables
self.data = self.Files.data
self._encoding = self.Files.encoding
self._content_length = self.Files.contentLength
self._extension = self.Files._file_extension
self._contentType()
self.send_headers['Content-Length'] = str(self._contentLength())
self.status_code = self.Files.status_code
self.status_str = self.Files.status_str
# status code
string += self._status(self.status_str, self.status_code)
# the actual date this whole event was completed
string += self._date()
# *** Coming from PHP ***
self.send_headers.update(self.Files.additional_head_str)
# Header calculator
for header in self.send_headers:
string += header
string += ': '
string += self.send_headers[header]
string += '\r\n'
if header == 'X-Powered-By':
string += self.add_set_cookies()
# this kinda ends the response header
string += '\r\n'
# ----
if type(self.data) == str:
total = bytes(string + self.data, self._encoding)
else:
total = bytes(string, self._encoding) + self.data
return total
def getRequest(self, header):
"""
Breaks the req header down to key value pairs and then send them
to be processed by their corresponding functions.
"""
if header == '':
return 1
# break
splited = header.split(b'\r\n\r\n', 1)
# This is the request body that came
# if it was a post we will use it
self.raw_headers = str(splited[0], 'ascii')
if len(splited) > 1:
self.requested_body = splited[-1]
else:
self.requested_body = ''
# clear it to save ram
splited.clear()
# Break into individual lines
lines = self.raw_headers.split('\r\n')
# This the request either get or post
# It does not follow the pairing protocol of the rest
self._getFile(lines[0])
# Break into key-value pairs
for pair in lines:
# for now we are breaking with ': ' to escpace
# the port no. eg. localhost':'9999
splits = pair.split(": ")
# if it was a key-value pair
if len(splits) > 1:
# make it a part of the header pair dict
self.headerPair[splits[0]] = splits[1]
# loop through the functions we have set and declared
for func in self.functions:
# key exist in the headers that was sent by client
if func in self.headerPair:
# find its corresponding function and set it to a new variable
function = self.functions[func]
# run the function with the self and required values
# This means, every corresponding function must strictly
# accept a single value
function(self.headerPair[func])
def _getContentType(self, content_type_str):
self.content_type = content_type_str
def _getHost(self, hostname_str):
"""
Gets Host and its port
"""
# for now just put everything as hostname
# later we break it
self.host = hostname_str
def _getUserAgent(self, user_agent_str):
self.user_agent_str = user_agent_str
def _getFile(self, req_str):
"""
"""
# strip the http protocol off
parsed = req_str[:-8]
# Strip it by space to avoid escaping the forward-slash
# There will be three entries
# the last will just be empty
splits = parsed.split(' ')
# the request method (eg. GET or POST)
self.request_method = splits[0]
# to avoid an index error
if len(splits) > 1:
# the file requested for
self.requested_file = splits[1]
def _getCookies(self, cookie_str):
"""
Breaks the cookie string into individual cookies
And store them
"""
# split them in main entries
self.cookie_str = cookie_str
splits = cookie_str.split('; ')
for pair in splits:
# split into key-value pairs
pairs = pair.split('=')
# Add the key-value pairs to the cookies variable
self.cookies[pairs[0]] = pairs[1]
def _status(self, status=None, digit=None):
string = 'HTTP/1.1 '
if status:
string += status + "\r\n"
return string
elif digit:
string += str(digit) + " " + self.status_stat[digit] + "\r\n"
return string
def _date(self):
string_time = "Date: "
string_time += time.strftime('%a, %d %b %Y %H:%M:%S %Z')
return string_time + "\r\n"
def _contentLength(self):
if self._content_length:
return self._content_length
else:
if type(self.data) == bytes:
self._content_length = len(self.data)
else:
ddata = bytes(self.data, self._encoding)
# len of data from outside
self._content_length = len(ddata)
"""string = 'Content-Length: '
# Now we are just continuing with the content length string
if 'Content-Disposition' in self.send_headers:
string = ''
else:
string += str(self._content_length) + '\r\n'"""
return self._content_length
def _cookie(self, cookies=None):
# set string to empty
string = ""
if cookies:
# cookie's name in cookies multi-dimensional array
for name in cookies:
string += "Set-Cookie: "
# set actual cookie as a "cookie": {}
cookie = cookies[name]
# each value that has been listed
for val in cookie:
string += val + "=" + str(cookie[val]) + "; "
# add the httponly
string += "HttpOnly\r\n"
return string
def _powered(self, statement):
string = 'X-Powered-By: ' + statement
return string + "\r\n"
def add_set_cookies(self):
string = ""
for l in self.Files.additional_set_cookie:
string += l + "\r\n"
return string
def _contentType(self):
# If content type has already been set
if self.Files.mime_type:
self.send_headers['Content-Type'] = self.Files.mime_type
if self.send_headers['Content-Type'] == 'text/html':
self.send_headers['Content-Type'] += '; charset=' + self._encoding
# find the extension in the extension map
elif self._extension in self._extMap:
# add the corresponding format to the string
self.send_headers['Content-Type'] = self._extMap[self._extension]
# if its a css file
elif self._extension in ['css', 'js']:
pass
elif 'image' in self._extMap[self._extension]:
pass
else:
# it is not a css file
self.send_headers['Content-Type'] += '; charset=' + self._encoding
return