server.py
367 lines
| 12.6 KiB
| text/x-python
|
PythonLexer
Eric Hopper
|
r2391 | # hgweb/server.py - The standalone hg web server. | ||
Eric Hopper
|
r2355 | # | ||
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | ||||
Thomas Arendsen Hein
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
Eric Hopper
|
r2355 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Eric Hopper
|
r2355 | |||
Yuya Nishihara
|
r27046 | from __future__ import absolute_import | ||
import errno | ||||
import os | ||||
import socket | ||||
import sys | ||||
import traceback | ||||
Gregory Szorc
|
r36821 | import wsgiref.validate | ||
Yuya Nishihara
|
r27046 | |||
from ..i18n import _ | ||||
from .. import ( | ||||
Augie Fackler
|
r34707 | encoding, | ||
Yuya Nishihara
|
r27046 | error, | ||
Pulkit Goyal
|
r30639 | pycompat, | ||
Yuya Nishihara
|
r27046 | util, | ||
) | ||||
Pulkit Goyal
|
r29566 | httpservermod = util.httpserver | ||
Pulkit Goyal
|
r29433 | socketserver = util.socketserver | ||
timeless
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
Yuya Nishihara
|
r27046 | from . import ( | ||
common, | ||||
) | ||||
Eric Hopper
|
r2355 | |||
def _splitURI(uri): | ||||
Mads Kiilerich
|
r17427 | """Return path and query that has been split from uri | ||
Eric Hopper
|
r2355 | |||
Just like CGI environment, the path is unquoted, the query is | ||||
not. | ||||
""" | ||||
Augie Fackler
|
r34701 | if r'?' in uri: | ||
path, query = uri.split(r'?', 1) | ||||
Eric Hopper
|
r2355 | else: | ||
Augie Fackler
|
r34701 | path, query = uri, r'' | ||
timeless
|
r28883 | return urlreq.unquote(path), query | ||
Eric Hopper
|
r2355 | |||
Eric Hopper
|
r2506 | class _error_logger(object): | ||
def __init__(self, handler): | ||||
self.handler = handler | ||||
def flush(self): | ||||
pass | ||||
Benoit Boissinot
|
r3130 | def write(self, str): | ||
Eric Hopper
|
r2506 | self.writelines(str.split('\n')) | ||
Benoit Boissinot
|
r3130 | def writelines(self, seq): | ||
Eric Hopper
|
r2506 | for msg in seq: | ||
self.handler.log_error("HG error: %s", msg) | ||||
Pulkit Goyal
|
r29566 | class _httprequesthandler(httpservermod.basehttprequesthandler): | ||
Wesley J. Landaker
|
r4870 | |||
url_scheme = 'http' | ||||
Thomas Arendsen Hein
|
r4957 | |||
Mads Kiilerich
|
r12783 | @staticmethod | ||
Gregory Szorc
|
r29553 | def preparehttpserver(httpserver, ui): | ||
Mads Kiilerich
|
r12783 | """Prepare .socket of new HTTPServer instance""" | ||
Eric Hopper
|
r2355 | def __init__(self, *args, **kargs): | ||
Augie Fackler
|
r34513 | self.protocol_version = r'HTTP/1.1' | ||
Pulkit Goyal
|
r29566 | httpservermod.basehttprequesthandler.__init__(self, *args, **kargs) | ||
Eric Hopper
|
r2355 | |||
Patrick Mezard
|
r5549 | def _log_any(self, fp, format, *args): | ||
Augie Fackler
|
r34707 | fp.write(pycompat.sysbytes( | ||
r"%s - - [%s] %s" % (self.client_address[0], | ||||
self.log_date_time_string(), | ||||
format % args)) + '\n') | ||||
Patrick Mezard
|
r5549 | fp.flush() | ||
Eric Hopper
|
r2355 | def log_error(self, format, *args): | ||
Patrick Mezard
|
r5549 | self._log_any(self.server.errorlog, format, *args) | ||
Eric Hopper
|
r2355 | |||
def log_message(self, format, *args): | ||||
Patrick Mezard
|
r5549 | self._log_any(self.server.accesslog, format, *args) | ||
Eric Hopper
|
r2355 | |||
Augie Fackler
|
r34707 | def log_request(self, code=r'-', size=r'-'): | ||
David Soria Parra
|
r19877 | xheaders = [] | ||
if util.safehasattr(self, 'headers'): | ||||
xheaders = [h for h in self.headers.items() | ||||
Augie Fackler
|
r34707 | if h[0].startswith(r'x-')] | ||
self.log_message(r'"%s" %s %s%s', | ||||
Steven Brown
|
r14093 | self.requestline, str(code), str(size), | ||
Augie Fackler
|
r34707 | r''.join([r' %s:%s' % h for h in sorted(xheaders)])) | ||
Steven Brown
|
r14093 | |||
Brendan Cully
|
r4860 | def do_write(self): | ||
try: | ||||
self.do_hgweb() | ||||
Gregory Szorc
|
r25660 | except socket.error as inst: | ||
Brendan Cully
|
r4860 | if inst[0] != errno.EPIPE: | ||
raise | ||||
Eric Hopper
|
r2355 | def do_POST(self): | ||
try: | ||||
Brendan Cully
|
r4860 | self.do_write() | ||
Mads Kiilerich
|
r13443 | except Exception: | ||
Thomas Arendsen Hein
|
r4015 | self._start_response("500 Internal Server Error", []) | ||
self._write("Internal Server Error") | ||||
Gregory Szorc
|
r23409 | self._done() | ||
Augie Fackler
|
r34707 | tb = r"".join(traceback.format_exception(*sys.exc_info())) | ||
# We need a native-string newline to poke in the log | ||||
# message, because we won't get a newline when using an | ||||
# r-string. This is the easy way out. | ||||
newline = chr(10) | ||||
self.log_error(r"Exception happened during processing " | ||||
r"request '%s':%s%s", self.path, newline, tb) | ||||
Eric Hopper
|
r2355 | |||
Matt Harbison
|
r37165 | def do_PUT(self): | ||
self.do_POST() | ||||
Eric Hopper
|
r2355 | def do_GET(self): | ||
self.do_POST() | ||||
def do_hgweb(self): | ||||
Augie Fackler
|
r34721 | self.sent_headers = False | ||
Michele Cella
|
r5835 | path, query = _splitURI(self.path) | ||
Eric Hopper
|
r2355 | |||
Matt Harbison
|
r37288 | # Ensure the slicing of path below is valid | ||
if (path != self.server.prefix | ||||
and not path.startswith(self.server.prefix + b'/')): | ||||
self._start_response(common.statusmessage(404), []) | ||||
self._write("Not Found") | ||||
self._done() | ||||
return | ||||
Eric Hopper
|
r2355 | env = {} | ||
Augie Fackler
|
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 | ||||
Gregory Szorc
|
r36820 | env[r'SCRIPT_NAME'] = pycompat.sysstr(self.server.prefix) | ||
env[r'PATH_INFO'] = pycompat.sysstr(path[len(self.server.prefix):]) | ||||
Augie Fackler
|
r34513 | env[r'REMOTE_HOST'] = self.client_address[0] | ||
env[r'REMOTE_ADDR'] = self.client_address[0] | ||||
Gregory Szorc
|
r36821 | env[r'QUERY_STRING'] = query or r'' | ||
Eric Hopper
|
r2355 | |||
Augie Fackler
|
r34720 | if pycompat.ispy3: | ||
if self.headers.get_content_type() is None: | ||||
env[r'CONTENT_TYPE'] = self.headers.get_default_type() | ||||
else: | ||||
env[r'CONTENT_TYPE'] = self.headers.get_content_type() | ||||
Augie Fackler
|
r37609 | length = self.headers.get(r'content-length') | ||
Augie Fackler
|
r34720 | else: | ||
Augie Fackler
|
r34719 | if self.headers.typeheader is None: | ||
env[r'CONTENT_TYPE'] = self.headers.type | ||||
else: | ||||
env[r'CONTENT_TYPE'] = self.headers.typeheader | ||||
Augie Fackler
|
r37609 | length = self.headers.getheader(r'content-length') | ||
Eric Hopper
|
r2355 | if length: | ||
Augie Fackler
|
r34513 | env[r'CONTENT_LENGTH'] = length | ||
Thomas Arendsen Hein
|
r4633 | for header in [h for h in self.headers.keys() | ||
Augie Fackler
|
r37609 | if h not in (r'content-type', r'content-length')]: | ||
Augie Fackler
|
r34513 | hkey = r'HTTP_' + header.replace(r'-', r'_').upper() | ||
hval = self.headers.get(header) | ||||
hval = hval.replace(r'\n', r'').strip() | ||||
Eric Hopper
|
r2505 | if hval: | ||
env[hkey] = hval | ||||
Augie Fackler
|
r34513 | env[r'SERVER_PROTOCOL'] = self.request_version | ||
env[r'wsgi.version'] = (1, 0) | ||||
Gregory Szorc
|
r36820 | env[r'wsgi.url_scheme'] = pycompat.sysstr(self.url_scheme) | ||
Augie Fackler
|
r34513 | if env.get(r'HTTP_EXPECT', '').lower() == '100-continue': | ||
Augie Fackler
|
r13570 | self.rfile = common.continuereader(self.rfile, self.wfile.write) | ||
Augie Fackler
|
r34513 | env[r'wsgi.input'] = self.rfile | ||
env[r'wsgi.errors'] = _error_logger(self) | ||||
env[r'wsgi.multithread'] = isinstance(self.server, | ||||
Pulkit Goyal
|
r29433 | socketserver.ThreadingMixIn) | ||
Augie Fackler
|
r34513 | env[r'wsgi.multiprocess'] = isinstance(self.server, | ||
Pulkit Goyal
|
r29433 | socketserver.ForkingMixIn) | ||
Augie Fackler
|
r34513 | env[r'wsgi.run_once'] = 0 | ||
Eric Hopper
|
r2355 | |||
Gregory Szorc
|
r36821 | wsgiref.validate.check_environ(env) | ||
Eric Hopper
|
r2506 | self.saved_status = None | ||
self.saved_headers = [] | ||||
Eric Hopper
|
r2508 | self.length = None | ||
Mads Kiilerich
|
r18354 | self._chunked = None | ||
Dirkjan Ochtman
|
r6784 | for chunk in self.server.application(env, self._start_response): | ||
self._write(chunk) | ||||
Mads Kiilerich
|
r18349 | if not self.sent_headers: | ||
self.send_headers() | ||||
Mads Kiilerich
|
r18354 | self._done() | ||
Eric Hopper
|
r2506 | |||
def send_headers(self): | ||||
if not self.saved_status: | ||||
Martin Geisler
|
r8663 | raise AssertionError("Sending headers before " | ||
"start_response() called") | ||||
Eric Hopper
|
r2506 | saved_status = self.saved_status.split(None, 1) | ||
saved_status[0] = int(saved_status[0]) | ||||
self.send_response(*saved_status) | ||||
Mads Kiilerich
|
r18354 | self.length = None | ||
self._chunked = False | ||||
Eric Hopper
|
r2506 | for h in self.saved_headers: | ||
self.send_header(*h) | ||||
Eric Hopper
|
r2508 | if h[0].lower() == 'content-length': | ||
self.length = int(h[1]) | ||||
Mads Kiilerich
|
r18380 | if (self.length is None and | ||
saved_status[0] != common.HTTP_NOT_MODIFIED): | ||||
Mads Kiilerich
|
r18354 | self._chunked = (not self.close_connection and | ||
self.request_version == "HTTP/1.1") | ||||
if self._chunked: | ||||
Augie Fackler
|
r34741 | self.send_header(r'Transfer-Encoding', r'chunked') | ||
Mads Kiilerich
|
r18354 | else: | ||
Augie Fackler
|
r34741 | self.send_header(r'Connection', r'close') | ||
Eric Hopper
|
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 | ||||
Eric Hopper
|
r2508 | bad_headers = ('connection', 'transfer-encoding') | ||
Thomas Arendsen Hein
|
r4633 | self.saved_headers = [h for h in headers | ||
if h[0].lower() not in bad_headers] | ||||
Eric Hopper
|
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() | ||||
Eric Hopper
|
r2508 | if self.length is not None: | ||
if len(data) > self.length: | ||||
Martin Geisler
|
r8663 | raise AssertionError("Content-length header sent, but more " | ||
"bytes than specified are being written.") | ||||
Eric Hopper
|
r2508 | self.length = self.length - len(data) | ||
Mads Kiilerich
|
r18354 | elif self._chunked and data: | ||
data = '%x\r\n%s\r\n' % (len(data), data) | ||||
Eric Hopper
|
r2506 | self.wfile.write(data) | ||
self.wfile.flush() | ||||
Eric Hopper
|
r2355 | |||
Mads Kiilerich
|
r18354 | def _done(self): | ||
if self._chunked: | ||||
self.wfile.write('0\r\n\r\n') | ||||
self.wfile.flush() | ||||
Gregory Szorc
|
r37027 | def version_string(self): | ||
if self.server.serverheader: | ||||
return self.server.serverheader | ||||
return httpservermod.basehttprequesthandler.version_string(self) | ||||
Mads Kiilerich
|
r12784 | class _httprequesthandlerssl(_httprequesthandler): | ||
timeless@mozdev.org
|
r26202 | """HTTPS handler based on Python's ssl module""" | ||
Mads Kiilerich
|
r12784 | |||
url_scheme = 'https' | ||||
@staticmethod | ||||
Gregory Szorc
|
r29553 | def preparehttpserver(httpserver, ui): | ||
Mads Kiilerich
|
r12784 | try: | ||
Gregory Szorc
|
r29555 | from .. import sslutil | ||
sslutil.modernssl | ||||
Mads Kiilerich
|
r12784 | except ImportError: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("SSL support is unavailable")) | ||
Gregory Szorc
|
r29553 | |||
certfile = ui.config('web', 'certificate') | ||||
Gregory Szorc
|
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) | ||||
Mads Kiilerich
|
r12784 | |||
def setup(self): | ||||
self.connection = self.request | ||||
Augie Fackler
|
r36797 | self.rfile = self.request.makefile(r"rb", self.rbufsize) | ||
self.wfile = self.request.makefile(r"wb", self.wbufsize) | ||||
Mads Kiilerich
|
r12784 | |||
Dirkjan Ochtman
|
r10639 | try: | ||
Yuya Nishihara
|
r27046 | import threading | ||
threading.activeCount() # silence pyflakes and bypass demandimport | ||||
Pulkit Goyal
|
r29433 | _mixin = socketserver.ThreadingMixIn | ||
Dirkjan Ochtman
|
r10639 | except ImportError: | ||
Augie Fackler
|
r14957 | if util.safehasattr(os, "fork"): | ||
Pulkit Goyal
|
r29433 | _mixin = socketserver.ForkingMixIn | ||
Dirkjan Ochtman
|
r10639 | else: | ||
Thomas Arendsen Hein
|
r14764 | class _mixin(object): | ||
Dirkjan Ochtman
|
r10639 | pass | ||
Dirkjan Ochtman
|
r10643 | def openlog(opt, default): | ||
if opt and opt != '-': | ||||
Augie Fackler
|
r36271 | return open(opt, 'ab') | ||
Dirkjan Ochtman
|
r10643 | return default | ||
Martijn Pieters
|
r30082 | class MercurialHTTPServer(_mixin, httpservermod.httpserver, object): | ||
Dirkjan Ochtman
|
r10643 | |||
# SO_REUSEADDR has broken semantics on windows | ||||
Jun Wu
|
r34646 | if pycompat.iswindows: | ||
Dirkjan Ochtman
|
r10643 | allow_reuse_address = 0 | ||
def __init__(self, ui, app, addr, handler, **kwargs): | ||||
Pulkit Goyal
|
r29566 | httpservermod.httpserver.__init__(self, addr, handler, **kwargs) | ||
Dirkjan Ochtman
|
r10643 | self.daemon_threads = True | ||
self.application = app | ||||
Eric Hopper
|
r2355 | |||
Gregory Szorc
|
r29553 | handler.preparehttpserver(self, ui) | ||
Dirkjan Ochtman
|
r10643 | |||
Boris Feld
|
r34240 | prefix = ui.config('web', 'prefix') | ||
Dirkjan Ochtman
|
r10643 | if prefix: | ||
prefix = '/' + prefix.strip('/') | ||||
self.prefix = prefix | ||||
Boris Feld
|
r34224 | alog = openlog(ui.config('web', 'accesslog'), ui.fout) | ||
Boris Feld
|
r34237 | elog = openlog(ui.config('web', 'errorlog'), ui.ferr) | ||
Dirkjan Ochtman
|
r10643 | self.accesslog = alog | ||
self.errorlog = elog | ||||
self.addr, self.port = self.socket.getsockname()[0:2] | ||||
self.fqaddr = socket.getfqdn(addr[0]) | ||||
Gregory Szorc
|
r37027 | self.serverheader = ui.config('web', 'server-header') | ||
Dirkjan Ochtman
|
r10643 | 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) | ||||
Dirkjan Ochtman
|
r10644 | def create_server(ui, app): | ||
Eric Hopper
|
r2355 | |||
Dirkjan Ochtman
|
r10644 | if ui.config('web', 'certificate'): | ||
Siddharth Agarwal
|
r26848 | handler = _httprequesthandlerssl | ||
Brendan Cully
|
r4860 | else: | ||
Mads Kiilerich
|
r12783 | handler = _httprequesthandler | ||
Brendan Cully
|
r4860 | |||
Dirkjan Ochtman
|
r10644 | if ui.configbool('web', 'ipv6'): | ||
Dirkjan Ochtman
|
r10641 | cls = IPv6HTTPServer | ||
else: | ||||
cls = MercurialHTTPServer | ||||
Dirkjan Ochtman
|
r8224 | # ugly hack due to python issue5853 (for threaded use) | ||
Matt Mackall
|
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. | ||||
Yuya Nishihara
|
r20529 | sys.argv # unwrap demand-loader so that reload() works | ||
Matt Mackall
|
r20357 | reload(sys) # resurrect sys.setdefaultencoding() | ||
oldenc = sys.getdefaultencoding() | ||||
sys.setdefaultencoding("latin1") # or any full 8-bit encoding | ||||
mimetypes.init() | ||||
sys.setdefaultencoding(oldenc) | ||||
Dirkjan Ochtman
|
r8224 | |||
Boris Feld
|
r34225 | address = ui.config('web', 'address') | ||
Boris Feld
|
r34239 | port = util.getport(ui.config('web', 'port')) | ||
Matt Mackall
|
r3628 | try: | ||
Dirkjan Ochtman
|
r10644 | return cls(ui, app, (address, port), handler) | ||
Gregory Szorc
|
r25660 | except socket.error as inst: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("cannot start server at '%s:%d': %s") | ||
Augie Fackler
|
r34707 | % (address, port, encoding.strtolocal(inst.args[1]))) | ||