]> git.pld-linux.org Git - projects/pld-builder.new.git/blob - PLD_Builder/request_handler_server.py
- add styles to page (minified version of www.pld-linux.org styles)
[projects/pld-builder.new.git] / PLD_Builder / request_handler_server.py
1 #!/usr/bin/python
2
3 import socket
4 import string
5 import cgi
6 import time
7 import log
8 import sys
9 import traceback
10 import os
11 from config import config, init_conf
12
13 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
14
15 import request_handler
16 import path
17
18 class MyHandler(BaseHTTPRequestHandler):
19
20         def do_GET(self):
21                 self.send_error(401);
22
23         def do_POST(self):
24                 global rootnode
25                 try:
26                         length = int(self.headers.getheader('content-length'))
27                         ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
28                         if ctype != 'application/x-www-form-urlencoded':
29                                 log.error("request_handler_server: [%s]: 401 Unauthorized" % self.client_address[0])
30                                 self.send_error(401)
31                                 self.end_headers()
32                                 return
33
34                         query = self.rfile.read(length)
35
36                         filename = self.headers.getheader('x-filename')
37
38                         if not request_handler.handle_request_main(query, filename = filename):
39                                 error = log.last_log();
40                                 log.error("request_handler_server: [%s]: handle_request_main(..., %s) failed" % (self.client_address[0], filename))
41                                 self.send_error(500, "%s: request failed. %s" % (filename, error))
42                                 self.end_headers()
43                                 return
44
45                         self.send_response(200)
46                         self.end_headers()
47
48                 except Exception, e:
49                         self.send_error(500, "%s: %s" % (filename, e))
50                         self.end_headers()
51                         log.error("request_handler_server: [%s]: exception: %s\n%s" % (self.client_address[0], e, traceback.format_exc()))
52                         raise
53                         pass
54
55 def write_css():
56         css_file = path.www_dir + "/style.css"
57         if os.path.exists(css_file):
58                 return
59
60         # css from www.pld-linux.org wiki theme, using css usage firebug plugin to cleanup
61         css = """
62 html {
63         background-color: white;
64         color: #5e5e5e;
65         font-family: Tahoma, Arial, Lucida Grande, sans-serif;
66         font-size: 0.75em;
67         line-height: 1.25em;
68 }
69
70 a {
71         text-decoration: underline;
72         color: #00f;
73 }
74
75 a:hover {
76         color: #00c;
77 }
78
79 @media screen, projection {
80         html {
81                 background-color: #f3efe3;
82         }
83
84         body {
85                 position: relative;
86         }
87 }
88 @media print {
89         a {
90                 background-color: inherit;
91                 color: inherit;
92         }
93 }
94
95 @media projection {
96         html { line-height: 1.8em; }
97         body, b, a, p { font-size: 22pt; }
98 }
99 """
100         old_umask = os.umask(0022)
101         f = open(css_file, "w")
102         f.write(css)
103         f.close()
104         os.umask(old_umask)
105
106 def main():
107         write_css();
108         socket.setdefaulttimeout(30)
109         try:
110                 init_conf()
111                 host = ""
112                 port = config.request_handler_server_port
113
114                 try:
115                         server = HTTPServer((host, port), MyHandler)
116                 except Exception, e:
117                         log.notice("request_handler_server: can't start server on [%s:%d]: %s" % (host, port, e))
118                         print >> sys.stderr, "ERROR: Can't start server on [%s:%d]: %s" % (host, port, e)
119                         sys.exit(1)
120
121                 log.notice('request_handler_server: started on [%s:%d]...' % (host, port))
122                 server.serve_forever()
123         except KeyboardInterrupt:
124                 log.notice('request_handler_server: ^C received, shutting down server')
125                 server.socket.close()
126
127 if __name__ == '__main__':
128         main()
129
This page took 0.347479 seconds and 4 git commands to generate.