-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyhttp.py
278 lines (245 loc) · 8.6 KB
/
myhttp.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
from __future__ import annotations
'''
Serves through a (super) simplified version of http protocol.
Warning: Running this may expose your computer to attacks.
Don't run this.
'''
from abc import ABC, abstractmethod
from threading import Thread
from queue import Queue, Empty
from socket import socket as Socket, timeout, gethostname, gethostbyname
import logging
from typing import List
from mythread import Safe
__all__ = [
'BadRequest', 'ClientShutdown', 'Request',
'OneServer', 'Server', 'Intent', 'log', 'logging',
'respond', 'myLogger',
]
class BadRequest(BaseException):
pass
class ClientShutdown(BaseException):
pass
class HandlerCloseConnection(BaseException):
pass
logging.basicConfig(format='%(asctime)s %(message)s',
filename = 'log.log')
logging.root.setLevel(logging.NOTSET)
class MyLogger:
def __init__(self):
self.verbose = True
def log(self, *args, sep = ' ', end = '\n', flush = False, level = logging.INFO):
text = sep.join([str(x) for x in args]) + end
if self.verbose:
print(text, flush = flush, end = '')
logging.log(level, text)
myLogger = MyLogger()
log = myLogger.log
class Intent:
pass
class DeregisterOneServer(Intent):
def __init__(self, oneServer):
self.oneServer = oneServer
class Request:
def __init__(self, command: str, target: str, http_version):
self.command = command
self.target = target
self.http_version = http_version
self.options = {}
self.body = ''
def add(self, kw, value):
self.options[kw] = value
def get(self, kw):
return self.options[kw]
def __str__(self):
if self.command == 'POST':
return self.command + ' ' + self.target + ' ' + self.body
else:
return self.command + ' ' + self.target
def parseHead(text: str):
whats_bad = ''
try:
lines = text.split('\r\n')
whats_bad = lines[0]
request = Request(*lines[0].split(' '))
for line in lines[1:]:
whats_bad = line
kw, value = line.split(':', 1)
kw = kw.strip(' ')
value = value.strip(' ')
request.add(kw, value)
return request
except Exception:
log('Bad line:', whats_bad, level = logging.ERROR)
raise BadRequest
def respond(socket: Socket, data: bytes):
response = '''HTTP/1.1 200 OK\r
Content-Length: %d\r
Content-Type: text/html\r\n\r\n''' % len(data)
socket.send(response.encode())
socket.send(data)
class OneServer(Thread, ABC):
'''
Subclass this class and override:
handle(request) where request is a Request object
request_filter is a list of request that you don't wanna log
`close()`
'''
request_filter = []
def __init__(self, addr, socket: Socket, parent: Server):
'''
You shouldn't override this. OneServer doesn't need any
runtime state. Keep-alive should not be abused.
'''
Thread.__init__(self)
self.addr = addr
self.socket = socket
socket.settimeout(.4)
self.parent = parent
self.parentQueue = parent.queue
self.queue = Queue()
self._go_on = Safe(True)
def close(self):
self._go_on.set(False)
def respond(self, data, do_log = True):
respond(self.socket, data)
if do_log:
if len(data) < 50:
log(self, data.decode())
@abstractmethod
def handle(self, request) -> bool:
# Override this
# return True to allow re-using keep-alive connection
respond(self.socket, b'''<html>What a shame.
The programmer didn't override the request handler. </html>''')
raise NotImplemented
def __repr__(self):
return repr(self.addr)
def run(self):
log(self, 'service begins. ')
chunk = b''
try:
while self._go_on.get():
try:
recved = self.socket.recv(4096)
if recved == b'':
raise ClientShutdown
else:
chunk += recved
while b'\r\n\r\n' in chunk:
bytes_head, chunk = chunk.split(b'\r\n\r\n', 1)
request = parseHead(bytes_head.decode())
if request.command == 'POST':
content_len = int(request.get('Content-Length'))
if len(chunk) >= content_len:
bytes_body = chunk[:content_len]
chunk = chunk[content_len:]
request.body = bytes_body.decode()
else:
chunk = b'\r\n\r\n'.join([bytes_head, chunk])
break
do_log = True
for filter in self.request_filter:
if filter in request.target:
do_log = False
break
if do_log:
log(self, 'Request', request)
if not self.handle(request):
raise HandlerCloseConnection
except timeout:
pass
# self.close() called
except (ClientShutdown, ConnectionAbortedError, ConnectionResetError):
log(self, 'client shutdown')
except HandlerCloseConnection:
log(self, 'handler closes connection')
finally:
self.parentQueue.put(DeregisterOneServer(self))
self.socket.close()
log(self, 'Thread has stopped. ')
class Server(Thread, ABC):
'''
Subclass this class and override:
handleQueue()
interval()
onConnect()
`close()`
'''
def __init__(
self, my_OneServer=OneServer, name='', port=80,
listen=1, accept_timeout=.5, max_connections=4*32,
):
# Pass in your subclassed OneServer
Thread.__init__(self)
self.queue = Queue()
self.OneServer = my_OneServer
self.listen = listen
self.socket = Socket()
self.socket.bind((name, port))
self.socket.settimeout(accept_timeout)
self._go_on = Safe(True)
self.oneServers: List[OneServer] = []
self.max_connection = Safe(max_connections)
self.showing_max_waring = False
def setMaxConnection(self, number):
self.max_connection.set(number)
def getMaxConnection(self):
return self.max_connection.get()
@abstractmethod
def interval(self):
'''
Override this.
'''
pass
@abstractmethod
def handleQueue(self, intent):
'''
Override this.
'''
pass
def __handleQueue(self, intent):
if type(intent) is DeregisterOneServer:
self.oneServers.remove(intent.oneServer)
else:
self.handleQueue(intent)
def close(self):
if self.is_alive():
with self._go_on:
self._go_on.value = False
#self.join() public method
@abstractmethod
def onConnect(self, addr):
pass # to override.
def run(self):
self.socket.listen(self.listen)
log('listening at', gethostbyname(gethostname()), '...')
while self._go_on.get():
if len(self.oneServers) >= self.getMaxConnection():
if not self.showing_max_waring:
log('Max connection reached. ')
self.showing_max_waring = True
else:
if self.showing_max_waring:
log("Max connection isn't reached anymore. ")
self.showing_max_waring = False
try:
socket, addr = self.socket.accept()
log(addr, 'Accepted. ')
self.onConnect(addr)
oneServer = self.OneServer(addr, socket, self)
self.oneServers.append(oneServer)
oneServer.start()
except timeout:
pass
try:
while self._go_on.get():
self.__handleQueue(self.queue.get_nowait())
except Empty:
pass
self.interval()
self.socket.close()
log('Closing', len(self.oneServers), 'oneServers.')
for oneServer in self.oneServers:
oneServer.close()
log('Server thread has stopped. ')