]> git.pld-linux.org Git - projects/pld-builder.new.git/blob - PLD_Builder/request_handler_server.py
- a bit smaller padding
[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         # skip if file exists and code is not newer
58         if os.path.exists(css_file) and os.stat(__file__).st_mtime < os.stat(css_file).st_mtime:
59                 return
60
61         # css from www.pld-linux.org wiki theme, using css usage firebug plugin to cleanup
62         css = """
63 html {
64         background-color: white;
65         color: #5e5e5e;
66         font-family: Tahoma, Arial, Lucida Grande, sans-serif;
67         font-size: 0.75em;
68         line-height: 1.25em;
69 }
70
71 a {
72         text-decoration: underline;
73         color: #00f;
74 }
75
76 a:hover {
77         color: #00c;
78 }
79
80 @media screen, projection {
81         html {
82                 background-color: #f3efe3;
83         }
84
85         body {
86                 position: relative;
87         }
88
89         div {
90                 background-color: white;
91                 margin: 10px 0px;
92                 padding: 2px;
93         }
94         div > a {
95                 font-weight: bold;
96                 color: #5e5e5e;
97         }
98         div > a:hover {
99                 color: #5e5e5e;
100         }
101         div:target {
102                 background-color: #ffffcc;
103                 color: black;
104         }
105 }
106 @media print {
107         a {
108                 background-color: inherit;
109                 color: inherit;
110         }
111 }
112
113 @media projection {
114         html { line-height: 1.8em; }
115         body, b, a, p { font-size: 22pt; }
116 }
117 """
118         old_umask = os.umask(0022)
119         f = open(css_file, "w")
120         f.write(css)
121         f.close()
122         os.umask(old_umask)
123
124 def main():
125         write_css();
126         socket.setdefaulttimeout(30)
127         try:
128                 init_conf()
129                 host = ""
130                 port = config.request_handler_server_port
131
132                 try:
133                         server = HTTPServer((host, port), MyHandler)
134                 except Exception, e:
135                         log.notice("request_handler_server: can't start server on [%s:%d]: %s" % (host, port, e))
136                         print >> sys.stderr, "ERROR: Can't start server on [%s:%d]: %s" % (host, port, e)
137                         sys.exit(1)
138
139                 log.notice('request_handler_server: started on [%s:%d]...' % (host, port))
140                 server.serve_forever()
141         except KeyboardInterrupt:
142                 log.notice('request_handler_server: ^C received, shutting down server')
143                 server.socket.close()
144
145 if __name__ == '__main__':
146         main()
147
This page took 0.076852 seconds and 3 git commands to generate.