12 from config import config, init_conf
14 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
16 import request_handler
19 class MyHandler(BaseHTTPRequestHandler):
27 length = int(self.headers.getheader('content-length'))
28 ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
29 if ctype != 'application/x-www-form-urlencoded':
30 log.error("request_handler_server: [%s]: 401 Unauthorized" % self.client_address[0])
35 query = self.rfile.read(length)
37 filename = self.headers.getheader('x-filename')
39 if not request_handler.handle_request_main(query, filename = filename):
40 error = log.last_log();
41 log.error("request_handler_server: [%s]: handle_request_main(..., %s) failed" % (self.client_address[0], filename))
42 self.send_error(500, "%s: request failed. %s" % (filename, error))
46 self.send_response(200)
50 self.send_error(500, "%s: %s" % (filename, e))
52 log.error("request_handler_server: [%s]: exception: %s\n%s" % (self.client_address[0], e, traceback.format_exc()))
57 css_src = os.path.join(os.path.dirname(__file__), 'style.css')
58 css_file = path.www_dir + "/style.css"
59 # skip if file exists and code is not newer
60 if os.path.exists(css_file) and os.stat(css_src).st_mtime < os.stat(css_file).st_mtime:
63 old_umask = os.umask(0022)
64 r = open(css_src, 'r')
65 f = open(css_file, "w")
72 js_src = os.path.join(os.path.dirname(__file__), 'script.js')
73 js_file = path.www_dir + '/script.js'
74 # skip if file exists and code is not newer
75 if os.path.exists(js_file) and os.stat(js_src).st_mtime < os.stat(js_file).st_mtime:
78 old_umask = os.umask(0022)
80 f = open(js_file, 'w')
89 socket.setdefaulttimeout(30)
93 port = config.request_handler_server_port
94 srv_ssl = config.request_handler_server_ssl
97 server = HTTPServer((host, port), MyHandler)
99 server.socket = ssl.wrap_socket (server.socket,
100 keyfile = config.request_handler_server_ssl_key,
101 certfile = config.request_handler_server_ssl_cert,
102 ca_certs = config.request_handler_server_ssl_cacert,
105 log.notice("request_handler_server: can't start server on [%s:%d], ssl=%s: %s" % (host, port, str(srv_ssl), e))
106 print >> sys.stderr, "ERROR: Can't start server on [%s:%d], ssl=%s: %s" % (host, port, str(srv_ssl), e)
109 log.notice('request_handler_server: started on [%s:%d], ssl=%s...' % (host, port, str(srv_ssl)))
110 server.serve_forever()
111 except KeyboardInterrupt:
112 log.notice('request_handler_server: ^C received, shutting down server')
113 server.socket.close()
115 if __name__ == '__main__':