server.py
334 lines
| 11.3 KiB
| text/x-python
|
PythonLexer
|
r2391 | # hgweb/server.py - The standalone hg web server. | ||
|
r2355 | # | ||
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | ||||
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
|
r2355 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2355 | |||
|
r27046 | from __future__ import absolute_import | ||
import errno | ||||
import os | ||||
import socket | ||||
import sys | ||||
import traceback | ||||
from ..i18n import _ | ||||
from .. import ( | ||||
error, | ||||
|
r30639 | pycompat, | ||
|
r27046 | util, | ||
) | ||||
|
r29566 | httpservermod = util.httpserver | ||
|
r29433 | socketserver = util.socketserver | ||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r27046 | from . import ( | ||
common, | ||||
) | ||||
|
r2355 | |||
def _splitURI(uri): | ||||
|
r17427 | """Return path and query that has been split from uri | ||
|
r2355 | |||
Just like CGI environment, the path is unquoted, the query is | ||||
not. | ||||
""" | ||||
if '?' in uri: | ||||
path, query = uri.split('?', 1) | ||||
else: | ||||
path, query = uri, '' | ||||
|
r28883 | return urlreq.unquote(path), query | ||
|
r2355 | |||
|
r2506 | class _error_logger(object): | ||
def __init__(self, handler): | ||||
self.handler = handler | ||||
def flush(self): | ||||
pass | ||||
|
r3130 | def write(self, str): | ||
|
r2506 | self.writelines(str.split('\n')) | ||
|
r3130 | def writelines(self, seq): | ||
|
r2506 | for msg in seq: | ||
self.handler.log_error("HG error: %s", msg) | ||||
|
r29566 | class _httprequesthandler(httpservermod.basehttprequesthandler): | ||
|
r4870 | |||
url_scheme = 'http' | ||||
|
r4957 | |||
|
r12783 | @staticmethod | ||
|
r29553 | def preparehttpserver(httpserver, ui): | ||
|
r12783 | """Prepare .socket of new HTTPServer instance""" | ||
|
r2355 | def __init__(self, *args, **kargs): | ||
|
r34513 | self.protocol_version = r'HTTP/1.1' | ||
|
r29566 | httpservermod.basehttprequesthandler.__init__(self, *args, **kargs) | ||
|
r2355 | |||
|
r5549 | def _log_any(self, fp, format, *args): | ||
fp.write("%s - - [%s] %s\n" % (self.client_address[0], | ||||
self.log_date_time_string(), | ||||
format % args)) | ||||
fp.flush() | ||||
|
r2355 | def log_error(self, format, *args): | ||
|
r5549 | self._log_any(self.server.errorlog, format, *args) | ||
|
r2355 | |||
def log_message(self, format, *args): | ||||
|
r5549 | self._log_any(self.server.accesslog, format, *args) | ||
|
r2355 | |||
|
r14093 | def log_request(self, code='-', size='-'): | ||
|
r19877 | xheaders = [] | ||
if util.safehasattr(self, 'headers'): | ||||
xheaders = [h for h in self.headers.items() | ||||
if h[0].startswith('x-')] | ||||
|
r14093 | self.log_message('"%s" %s %s%s', | ||
self.requestline, str(code), str(size), | ||||
''.join([' %s:%s' % h for h in sorted(xheaders)])) | ||||
|
r4860 | def do_write(self): | ||
try: | ||||
self.do_hgweb() | ||||
|
r25660 | except socket.error as inst: | ||
|
r4860 | if inst[0] != errno.EPIPE: | ||
raise | ||||
|
r2355 | def do_POST(self): | ||
try: | ||||
|
r4860 | self.do_write() | ||
|
r13443 | except Exception: | ||
|
r4015 | self._start_response("500 Internal Server Error", []) | ||
self._write("Internal Server Error") | ||||
|
r23409 | self._done() | ||
|
r4015 | tb = "".join(traceback.format_exception(*sys.exc_info())) | ||
|
r8663 | self.log_error("Exception happened during processing " | ||
"request '%s':\n%s", self.path, tb) | ||||
|
r2355 | |||
def do_GET(self): | ||||
self.do_POST() | ||||
def do_hgweb(self): | ||||
|
r5835 | path, query = _splitURI(self.path) | ||
|
r2355 | |||
env = {} | ||||
|
r34513 | env[r'GATEWAY_INTERFACE'] = r'CGI/1.1' | ||
env[r'REQUEST_METHOD'] = self.command | ||||
env[r'SERVER_NAME'] = self.server.server_name | ||||
env[r'SERVER_PORT'] = str(self.server.server_port) | ||||
env[r'REQUEST_URI'] = self.path | ||||
env[r'SCRIPT_NAME'] = self.server.prefix | ||||
env[r'PATH_INFO'] = path[len(self.server.prefix):] | ||||
env[r'REMOTE_HOST'] = self.client_address[0] | ||||
env[r'REMOTE_ADDR'] = self.client_address[0] | ||||
|
r2355 | if query: | ||
|
r34513 | env[r'QUERY_STRING'] = query | ||
|
r2355 | |||
if self.headers.typeheader is None: | ||||
|
r34513 | env[r'CONTENT_TYPE'] = self.headers.type | ||
|
r2355 | else: | ||
|
r34513 | env[r'CONTENT_TYPE'] = self.headers.typeheader | ||
|
r2355 | length = self.headers.getheader('content-length') | ||
if length: | ||||
|
r34513 | env[r'CONTENT_LENGTH'] = length | ||
|
r4633 | for header in [h for h in self.headers.keys() | ||
|
r2505 | if h not in ('content-type', 'content-length')]: | ||
|
r34513 | hkey = r'HTTP_' + header.replace(r'-', r'_').upper() | ||
hval = self.headers.get(header) | ||||
hval = hval.replace(r'\n', r'').strip() | ||||
|
r2505 | if hval: | ||
env[hkey] = hval | ||||
|
r34513 | env[r'SERVER_PROTOCOL'] = self.request_version | ||
env[r'wsgi.version'] = (1, 0) | ||||
env[r'wsgi.url_scheme'] = self.url_scheme | ||||
if env.get(r'HTTP_EXPECT', '').lower() == '100-continue': | ||||
|
r13570 | self.rfile = common.continuereader(self.rfile, self.wfile.write) | ||
|
r34513 | env[r'wsgi.input'] = self.rfile | ||
env[r'wsgi.errors'] = _error_logger(self) | ||||
env[r'wsgi.multithread'] = isinstance(self.server, | ||||
|
r29433 | socketserver.ThreadingMixIn) | ||
|
r34513 | env[r'wsgi.multiprocess'] = isinstance(self.server, | ||
|
r29433 | socketserver.ForkingMixIn) | ||
|
r34513 | env[r'wsgi.run_once'] = 0 | ||
|
r2355 | |||
|
r2506 | self.saved_status = None | ||
self.saved_headers = [] | ||||
self.sent_headers = False | ||||
|
r2508 | self.length = None | ||
|
r18354 | self._chunked = None | ||
|
r6784 | for chunk in self.server.application(env, self._start_response): | ||
self._write(chunk) | ||||
|
r18349 | if not self.sent_headers: | ||
self.send_headers() | ||||
|
r18354 | self._done() | ||
|
r2506 | |||
def send_headers(self): | ||||
if not self.saved_status: | ||||
|
r8663 | raise AssertionError("Sending headers before " | ||
"start_response() called") | ||||
|
r2506 | saved_status = self.saved_status.split(None, 1) | ||
saved_status[0] = int(saved_status[0]) | ||||
self.send_response(*saved_status) | ||||
|
r18354 | self.length = None | ||
self._chunked = False | ||||
|
r2506 | for h in self.saved_headers: | ||
self.send_header(*h) | ||||
|
r2508 | if h[0].lower() == 'content-length': | ||
self.length = int(h[1]) | ||||
|
r18380 | if (self.length is None and | ||
saved_status[0] != common.HTTP_NOT_MODIFIED): | ||||
|
r18354 | self._chunked = (not self.close_connection and | ||
self.request_version == "HTTP/1.1") | ||||
if self._chunked: | ||||
self.send_header('Transfer-Encoding', 'chunked') | ||||
else: | ||||
self.send_header('Connection', 'close') | ||||
|
r2506 | self.end_headers() | ||
self.sent_headers = True | ||||
def _start_response(self, http_status, headers, exc_info=None): | ||||
code, msg = http_status.split(None, 1) | ||||
code = int(code) | ||||
self.saved_status = http_status | ||||
|
r2508 | bad_headers = ('connection', 'transfer-encoding') | ||
|
r4633 | self.saved_headers = [h for h in headers | ||
if h[0].lower() not in bad_headers] | ||||
|
r2506 | return self._write | ||
def _write(self, data): | ||||
if not self.saved_status: | ||||
raise AssertionError("data written before start_response() called") | ||||
elif not self.sent_headers: | ||||
self.send_headers() | ||||
|
r2508 | if self.length is not None: | ||
if len(data) > self.length: | ||||
|
r8663 | raise AssertionError("Content-length header sent, but more " | ||
"bytes than specified are being written.") | ||||
|
r2508 | self.length = self.length - len(data) | ||
|
r18354 | elif self._chunked and data: | ||
data = '%x\r\n%s\r\n' % (len(data), data) | ||||
|
r2506 | self.wfile.write(data) | ||
self.wfile.flush() | ||||
|
r2355 | |||
|
r18354 | def _done(self): | ||
if self._chunked: | ||||
self.wfile.write('0\r\n\r\n') | ||||
self.wfile.flush() | ||||
|
r12784 | class _httprequesthandlerssl(_httprequesthandler): | ||
|
r26202 | """HTTPS handler based on Python's ssl module""" | ||
|
r12784 | |||
url_scheme = 'https' | ||||
@staticmethod | ||||
|
r29553 | def preparehttpserver(httpserver, ui): | ||
|
r12784 | try: | ||
|
r29555 | from .. import sslutil | ||
sslutil.modernssl | ||||
|
r12784 | except ImportError: | ||
|
r26587 | raise error.Abort(_("SSL support is unavailable")) | ||
|
r29553 | |||
certfile = ui.config('web', 'certificate') | ||||
|
r29555 | |||
# These config options are currently only meant for testing. Use | ||||
# at your own risk. | ||||
cafile = ui.config('devel', 'servercafile') | ||||
reqcert = ui.configbool('devel', 'serverrequirecert') | ||||
httpserver.socket = sslutil.wrapserversocket(httpserver.socket, | ||||
ui, | ||||
certfile=certfile, | ||||
cafile=cafile, | ||||
requireclientcert=reqcert) | ||||
|
r12784 | |||
def setup(self): | ||||
self.connection = self.request | ||||
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) | ||||
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) | ||||
|
r10639 | try: | ||
|
r27046 | import threading | ||
threading.activeCount() # silence pyflakes and bypass demandimport | ||||
|
r29433 | _mixin = socketserver.ThreadingMixIn | ||
|
r10639 | except ImportError: | ||
|
r14957 | if util.safehasattr(os, "fork"): | ||
|
r29433 | _mixin = socketserver.ForkingMixIn | ||
|
r10639 | else: | ||
|
r14764 | class _mixin(object): | ||
|
r10639 | pass | ||
|
r10643 | def openlog(opt, default): | ||
if opt and opt != '-': | ||||
return open(opt, 'a') | ||||
return default | ||||
|
r30082 | class MercurialHTTPServer(_mixin, httpservermod.httpserver, object): | ||
|
r10643 | |||
# SO_REUSEADDR has broken semantics on windows | ||||
|
r30639 | if pycompat.osname == 'nt': | ||
|
r10643 | allow_reuse_address = 0 | ||
def __init__(self, ui, app, addr, handler, **kwargs): | ||||
|
r29566 | httpservermod.httpserver.__init__(self, addr, handler, **kwargs) | ||
|
r10643 | self.daemon_threads = True | ||
self.application = app | ||||
|
r2355 | |||
|
r29553 | handler.preparehttpserver(self, ui) | ||
|
r10643 | |||
|
r34240 | prefix = ui.config('web', 'prefix') | ||
|
r10643 | if prefix: | ||
prefix = '/' + prefix.strip('/') | ||||
self.prefix = prefix | ||||
|
r34224 | alog = openlog(ui.config('web', 'accesslog'), ui.fout) | ||
|
r34237 | elog = openlog(ui.config('web', 'errorlog'), ui.ferr) | ||
|
r10643 | self.accesslog = alog | ||
self.errorlog = elog | ||||
self.addr, self.port = self.socket.getsockname()[0:2] | ||||
self.fqaddr = socket.getfqdn(addr[0]) | ||||
class IPv6HTTPServer(MercurialHTTPServer): | ||||
address_family = getattr(socket, 'AF_INET6', None) | ||||
def __init__(self, *args, **kwargs): | ||||
if self.address_family is None: | ||||
raise error.RepoError(_('IPv6 is not available on this system')) | ||||
super(IPv6HTTPServer, self).__init__(*args, **kwargs) | ||||
|
r10644 | def create_server(ui, app): | ||
|
r2355 | |||
|
r10644 | if ui.config('web', 'certificate'): | ||
|
r26848 | handler = _httprequesthandlerssl | ||
|
r4860 | else: | ||
|
r12783 | handler = _httprequesthandler | ||
|
r4860 | |||
|
r10644 | if ui.configbool('web', 'ipv6'): | ||
|
r10641 | cls = IPv6HTTPServer | ||
else: | ||||
cls = MercurialHTTPServer | ||||
|
r8224 | # ugly hack due to python issue5853 (for threaded use) | ||
|
r20357 | try: | ||
import mimetypes | ||||
mimetypes.init() | ||||
except UnicodeDecodeError: | ||||
# Python 2.x's mimetypes module attempts to decode strings | ||||
# from Windows' ANSI APIs as ascii (fail), then re-encode them | ||||
# as ascii (clown fail), because the default Python Unicode | ||||
# codec is hardcoded as ascii. | ||||
|
r20529 | sys.argv # unwrap demand-loader so that reload() works | ||
|
r20357 | reload(sys) # resurrect sys.setdefaultencoding() | ||
oldenc = sys.getdefaultencoding() | ||||
sys.setdefaultencoding("latin1") # or any full 8-bit encoding | ||||
mimetypes.init() | ||||
sys.setdefaultencoding(oldenc) | ||||
|
r8224 | |||
|
r34225 | address = ui.config('web', 'address') | ||
|
r34239 | port = util.getport(ui.config('web', 'port')) | ||
|
r3628 | try: | ||
|
r10644 | return cls(ui, app, (address, port), handler) | ||
|
r25660 | except socket.error as inst: | ||
|
r26587 | raise error.Abort(_("cannot start server at '%s:%d': %s") | ||
|
r6262 | % (address, port, inst.args[1])) | ||