-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·367 lines (308 loc) · 10.3 KB
/
server.py
File metadata and controls
executable file
·367 lines (308 loc) · 10.3 KB
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
#!/usr/bin/env python3
from http.server import HTTPServer, BaseHTTPRequestHandler
from threading import Lock
from datetime import datetime
from util import Stack
import pkgutil
import sys
import logging
import json
############
# config ###
PORT = 8080
DEBUG = False
PLUGINDIR = "endpoints"
# Endpoint URL options
IGNORE_DOUBLE_SLASH = False # not implemented yet
CASE_SENSITIVE = False # not implemented yet
############
############
usage = "Usage: {} [OPTIONS]\n\n" \
"OPTIONS:\n" \
" --help\n" \
" --port PORT\n" \
" --debug\n" \
" --yttest YOUTUBELINK\n" \
"".format(sys.argv[0])
class ParseError(Exception):
pass
class MethodError(Exception):
pass
class Error:
def __init__(self, plugin, msg, ts=None):
self.msg = msg
self.plugin = plugin
self.timestamp = None
if ts is None:
self.timestamp = datetime.now()
def serializable(self):
return {
"plugin": self.plugin.name,
"msg": self.msg,
"timestamp": self.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
}
def to_json(self):
return json.dumps(self.serializable())
class Endpoint:
"""
Endpoint to be subclassed and registered with the server.
Methods that are called on appropriate request:
do_GET(requesthandler),
do_POST(requesthandler),
do_HEAD(requesthandler),
do_PUT(requesthandler)
If a method is not present, the request is refused (TODO 403 or 404 maybe).
"""
def __init__(self, path):
self.path = sanitize_path(path)
self.pathlist = self.path.split("/") # todo property structure
class RESTServer(HTTPServer):
def __init__(self, config, **kwargs):
addr = ("", config["port"])
super().__init__(addr, RequestHandler, **kwargs)
self.endpoints = []
self.plugins = []
self.load_plugins()
self._endpoints_to_register = None
self._errors = Stack()
self._error_lock = Lock()
logging.info("Running server.")
try:
self.serve_forever()
except KeyboardInterrupt:
self.shutdown()
def load_plugins(self):
# import
for el in pkgutil.iter_modules([PLUGINDIR]):
plugin = el[1]
try:
p = pkgutil.importlib.import_module("{}.{}".format(PLUGINDIR, plugin))
except Exception as e:
logging.error("Unable to load plugin: {} ({})".format(plugin, e))
continue
else:
self.plugins.append(p)
# load
failed = []
for i in range(len(self.plugins)):
module = self.plugins[i]
try:
self._endpoints_to_register = []
plugin = module.Plugin(self)
logging.info("Loaded Plugin: {}".format(plugin.name))
except (AttributeError, TypeError, Exception) as e:
failed.append(module)
logging.error("Unable to load plugin: {} ({})".format(module, e))
continue
self.plugins[i] = plugin
for endpoint in self._endpoints_to_register:
self.endpoints.append((endpoint, plugin))
logging.info("Registered endpoint: {}".format(endpoint.path))
self._endpoints_to_register = None
for el in failed:
self.plugins.remove(el)
def match_endpoints(self, path):
"""
Finds the endpoint that matches best with path.
If path is "/a/b/c", it matches "/a/b" better than "/a". It does not match "/b".
:param path: path that is to be matched
:return: endpoint object that matches; None if no match is found
"""
path = sanitize_path(path).split("/")
candidates = []
for el in self.endpoints:
candidates.append(el[0])
matches = []
todel = []
# comparison loop
for i in range(len(path)):
if not candidates:
break
for el in candidates:
if len(el.pathlist) == i:
matches.append(el)
todel.append(el)
elif el.pathlist[i] != path[i]:
todel.append(el)
elif i == len(el.pathlist) - 1:
matches.append(el)
for el in todel:
candidates.remove(el)
todel = []
if not matches:
return None
# get best match
best = matches[0]
for el in matches:
if len(el.pathlist) > len(best.pathlist):
best = el
return best
def register_endpoint(self, endpoint):
"""
Registers and endpoint.
:param endpoint: Endpoint object
"""
if self._endpoints_to_register is None:
logging.error("Endpoints must be registered in the Plugin constructor ({})".format(endpoint.path))
return
if endpoint not in self.endpoints and endpoint not in self._endpoints_to_register:
self._endpoints_to_register.append(endpoint)
else:
logging.error("Endpoint already registered: {}".format(endpoint.path))
def report_error(self, endpoint, msg, timestamp=None):
"""
Reports an error to the server error stack.
:param endpoint: Endpoint object the error occured in.
:param msg: Error message.
:param timestamp: Error timestamp; uses now if ommited.
:return:
"""
error = Error(endpoint, msg, timestamp)
self._error_lock.acquire()
self._errors.push(error)
self._error_lock.release()
def consume_errors(self):
"""
Generator for all errors on the server error stack. Errors are removed from the stack (popped).
"""
self._error_lock.acquire()
while True:
try:
yield self._errors.pop()
except IndexError:
break
self._error_lock.release()
def consume_error(self):
"""
Pops one error from the server error stack (read and remove).
:return: Last error
"""
self._error_lock.acquire()
r = self._errors.pop()
self._error_lock.release()
return r
def has_error(self):
return not self._errors.is_empty()
def last_error(self):
"""
Reads the last error from the error stack. Does not remove it.
:return: Last error
"""
self._error_lock.acquire()
r = self._errors.top()
self._error_lock.release()
return r
def shutdown(self):
for plugin in self.plugins:
try:
plugin.shutdown()
except AttributeError:
logging.info("Plugin {} has no shutdown method.".format(plugin))
pass
except Exception as e:
logging.error("Plugin {} failed to shut down ({})".format(plugin, e))
logging.info("Shutting down.")
try:
self.shutdown()
except Exception as e:
logging.error("Clean shutdown failed ({})".format(e))
class RequestHandler(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.ep = None
self._route = None
super().__init__(*args, **kwargs)
@property
def route(self):
if self._route is not None:
return self._route
if self.ep is None:
return None
assert(self.path.startswith(self.ep.path))
return self.path[len(self.ep.path):]
def do_method(self, method):
logging.debug("Incoming {} on {}".format(method, self.path))
self.ep = self.server.match_endpoints(self.path)
if self.ep is None:
logging.info("No matching endpoint for {} found, sending 404".format(self.path))
self.send_response(404) # Not found
self.end_headers()
return
try:
logging.debug("Sending request to endpoint {}".format(self.ep.path))
if method == "GET":
self.ep.do_GET(self)
elif method == "POST":
self.ep.do_POST(self)
elif method == "PUT":
self.ep.do_PUT(self)
elif method == "HEAD":
self.ep.do_HEAD(self)
else:
raise MethodError
except MethodError:
logging.debug("Endpoint {} does not support method {}, sending 405".format(self.ep.path, method))
self.send_response(405) # Method not allowed
self.end_headers()
def do_POST(self):
self.do_method("POST")
def do_GET(self):
self.do_method("GET")
def do_PUT(self):
self.do_method("PUT")
def do_HEAD(self):
self.do_method("HEAD")
def sanitize_path(path):
"""
Removes trailing / and adds leading /; e.g. "/endpoint/path"
:param path: path to sanitize
:return: sanitized path
"""
if not path.startswith("/"):
path = "/" + path
if path.endswith("/"):
path = path[:-1]
return path
def parse_args(args):
config = {
"help": False,
"port": PORT,
"debug": DEBUG,
"yttest": False,
"yttestlink": None,
}
i = 1
while i < len(args):
if args[i] == "--port":
try:
config["port"] = int(args[i+1])
except KeyError:
raise IndexError("Port not specified.")
except ValueError:
raise ParseError("{} is not a valid port number.".format(args[i+1]))
i += 1
elif args[i] == "--debug":
config["debug"] = True
elif args[i] == "--help":
config["help"] = True
elif args[i] == "--yttest":
config["yttest"] = True
try:
config["yttestlink"] = args[i+1]
except IndexError:
raise ParseError("Youtube link not specified.")
i += 1
i += 1
return config
def main(args):
config = parse_args(args)
if config["help"]:
print(usage)
return
elif config["debug"]:
logging.basicConfig(level=logging.DEBUG)
logging.debug("Loglevel: Debug")
else:
logging.basicConfig(level=logging.WARNING)
RESTServer(config)
if __name__ == "__main__":
main(sys.argv)