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
|
#!/usr/bin/python3
import socket
import string
import cgi
import time
import log
import ssl
import sys
import traceback
import os
from config import config, init_conf
from http.server import BaseHTTPRequestHandler, HTTPServer
import request_handler
import path
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_error(401);
def do_POST(self):
global rootnode
try:
length = int(self.headers.get('content-length'))
filename = self.headers.get('x-filename')
if not length:
log.error("request_handler_server: [%s]: 401 Unauthorized" % self.client_address[0])
self.send_error(401)
self.end_headers()
return
query = self.rfile.read(length)
if not request_handler.handle_request_main(query, filename = filename):
error = log.last_log();
log.error("request_handler_server: [%s]: handle_request_main(..., %s) failed" % (self.client_address[0], filename))
self.send_error(500, "%s: request failed. %s" % (filename, error))
self.end_headers()
return
self.send_response(200)
self.end_headers()
except Exception as e:
self.send_error(500, "%s: %s" % (filename, e))
self.end_headers()
log.error("request_handler_server: [%s]: exception: %s\n%s" % (self.client_address[0], e, traceback.format_exc()))
raise
pass
def write_css():
css_src = os.path.join(os.path.dirname(__file__), 'style.css')
css_file = path.www_dir + "/style.css"
# skip if file exists and code is not newer
if os.path.exists(css_file) and os.stat(css_src).st_mtime < os.stat(css_file).st_mtime:
return
old_umask = os.umask(0o0022)
r = open(css_src, 'r')
f = open(css_file, "w")
f.write(r.read())
f.close()
r.close()
os.umask(old_umask)
def write_js():
js_src = os.path.join(os.path.dirname(__file__), 'script.js')
js_file = path.www_dir + '/script.js'
# skip if file exists and code is not newer
if os.path.exists(js_file) and os.stat(js_src).st_mtime < os.stat(js_file).st_mtime:
return
old_umask = os.umask(0o0022)
r = open(js_src, 'r')
f = open(js_file, 'w')
f.write(r.read())
f.close()
r.close()
os.umask(old_umask)
def main(srv_ssl=False):
write_css();
write_js();
socket.setdefaulttimeout(30)
try:
init_conf()
host = ""
port = config.request_handler_server_port
if srv_ssl:
port = config.request_handler_server_ssl_port
try:
server = HTTPServer((host, port), MyHandler)
if srv_ssl:
server.socket = ssl.wrap_socket (server.socket,
keyfile = config.request_handler_server_ssl_key,
certfile = config.request_handler_server_ssl_cert,
ca_certs = "/etc/certs/ca-certificates.crt",
server_side=True)
except Exception as e:
log.notice("request_handler_server: can't start server on [%s:%d], ssl=%s: %s" % (host, port, str(srv_ssl), e))
print >> sys.stderr, "ERROR: Can't start server on [%s:%d], ssl=%s: %s" % (host, port, str(srv_ssl), e)
sys.exit(1)
log.notice('request_handler_server: started on [%s:%d], ssl=%s...' % (host, port, str(srv_ssl)))
server.serve_forever()
except KeyboardInterrupt:
log.notice('request_handler_server: ^C received, shutting down server')
server.socket.close()
if __name__ == '__main__':
srv_ssl = False
if len(sys.argv) == 2 and sys.argv[1] == "ssl":
srv_ssl = True
main(srv_ssl)
|