summaryrefslogtreecommitdiff
path: root/standalone.py
diff options
context:
space:
mode:
authormartin.von.loewis <devnull@localhost>2010-01-01 19:51:35 +0000
committermartin.von.loewis <devnull@localhost>2010-01-01 19:51:35 +0000
commit71feddb4ca5705572ff309544e1d19e9bda94b3b (patch)
treea7e55bfa3f727f88ef505def1b2a08605d7255ae /standalone.py
parentf41e56d495c714fd3c7c8a721af4ffef5d760110 (diff)
downloaddecorator-71feddb4ca5705572ff309544e1d19e9bda94b3b.tar.gz
Add standalone server.
Diffstat (limited to 'standalone.py')
-rw-r--r--standalone.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/standalone.py b/standalone.py
new file mode 100644
index 0000000..c860b5b
--- /dev/null
+++ b/standalone.py
@@ -0,0 +1,86 @@
+#!/usr/bin/python
+import config, webui, BaseHTTPServer, urllib
+
+class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ config = config.Config("config.ini")
+
+ def set_content_type(self, content_type):
+ self.send_header('Content-Type', content_type)
+
+ def run(self):
+ for scriptname in ('/mirrors', '/simple', '/pypi'):
+ if self.path.startswith(scriptname):
+ rest = self.path[len(scriptname):]
+ break
+ else:
+ # invalid URL
+ return
+
+ i = rest.rfind('?')
+ if i >= 0:
+ rest, query = rest[:i], rest[i+1:]
+ else:
+ query = ''
+
+ env = {}
+ env['SERVER_SOFTWARE'] = self.version_string()
+ env['SERVER_NAME'] = self.server.server_name
+ env['GATEWAY_INTERFACE'] = 'CGI/1.1'
+ env['SERVER_PROTOCOL'] = self.protocol_version
+ env['SERVER_PORT'] = str(self.server.server_port)
+ env['REQUEST_METHOD'] = self.command
+ uqrest = urllib.unquote(rest)
+ env['PATH_INFO'] = uqrest
+ # env['PATH_TRANSLATED'] = self.translate_path(uqrest)
+ env['SCRIPT_NAME'] = scriptname
+ if query:
+ env['QUERY_STRING'] = query
+ host = self.address_string()
+ if host != self.client_address[0]:
+ env['REMOTE_HOST'] = host
+ env['REMOTE_ADDR'] = self.client_address[0]
+ authorization = self.headers.getheader("authorization")
+ if authorization:
+ env['HTTP_CGI_AUTHORIZATION'] = authorization
+ authorization = authorization.split()
+ if len(authorization) == 2:
+ import base64, binascii
+ env['AUTH_TYPE'] = authorization[0]
+ if authorization[0].lower() == "basic":
+ try:
+ authorization = base64.decodestring(authorization[1])
+ except binascii.Error:
+ pass
+ else:
+ authorization = authorization.split(':')
+ if len(authorization) == 2:
+ env['REMOTE_USER'] = authorization[0]
+ if self.headers.typeheader is None:
+ env['CONTENT_TYPE'] = self.headers.type
+ else:
+ env['CONTENT_TYPE'] = self.headers.typeheader
+ length = self.headers.getheader('content-length')
+ if length:
+ env['CONTENT_LENGTH'] = length
+ referer = self.headers.getheader('referer')
+ if referer:
+ env['HTTP_REFERER'] = referer
+ accept = []
+ for line in self.headers.getallmatchingheaders('accept'):
+ if line[:1] in "\t\n\r ":
+ accept.append(line.strip())
+ else:
+ accept = accept + line[7:].split(',')
+ env['HTTP_ACCEPT'] = ','.join(accept)
+ ua = self.headers.getheader('user-agent')
+ if ua:
+ env['HTTP_USER_AGENT'] = ua
+ co = filter(None, self.headers.getheaders('cookie'))
+ if co:
+ env['HTTP_COOKIE'] = ', '.join(co)
+
+ webui.WebUI(self, env).run()
+ do_GET = do_POST = run
+
+httpd = BaseHTTPServer.HTTPServer(('',8000), RequestHandler)
+httpd.serve_forever()