server.py
416 lines
| 12.7 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> | ||||
Raphaël Gomès
|
r47575 | # Copyright 2005-2007 Olivia Mackall <olivia@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 | |||
Matt Harbison
|
r52756 | from __future__ import annotations | ||
Yuya Nishihara
|
r27046 | |||
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, | ||
) | ||||
r47669 | from ..utils import ( | |||
urlutil, | ||||
) | ||||
Yuya Nishihara
|
r27046 | |||
Pulkit Goyal
|
r29566 | httpservermod = util.httpserver | ||
Pulkit Goyal
|
r29433 | socketserver = util.socketserver | ||
timeless
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
Augie Fackler
|
r43346 | 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
|
r43906 | if '?' in uri: | ||
path, query = uri.split('?', 1) | ||||
Eric Hopper
|
r2355 | else: | ||
Augie Fackler
|
r34701 | path, query = uri, r'' | ||
timeless
|
r28883 | return urlreq.unquote(path), query | ||
Eric Hopper
|
r2355 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class _error_logger: | ||
Eric Hopper
|
r2506 | def __init__(self, handler): | ||
self.handler = handler | ||||
Augie Fackler
|
r43346 | |||
Eric Hopper
|
r2506 | def flush(self): | ||
pass | ||||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r3130 | def write(self, str): | ||
Augie Fackler
|
r43347 | self.writelines(str.split(b'\n')) | ||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r3130 | def writelines(self, seq): | ||
Eric Hopper
|
r2506 | for msg in seq: | ||
Augie Fackler
|
r43809 | self.handler.log_error("HG error: %s", encoding.strfromlocal(msg)) | ||
Eric Hopper
|
r2506 | |||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r29566 | class _httprequesthandler(httpservermod.basehttprequesthandler): | ||
Augie Fackler
|
r43347 | url_scheme = b'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
|
r43346 | fp.write( | ||
pycompat.sysbytes( | ||||
r"%s - - [%s] %s" | ||||
% ( | ||||
self.client_address[0], | ||||
self.log_date_time_string(), | ||||
format % args, | ||||
) | ||||
) | ||||
Augie Fackler
|
r43347 | + b'\n' | ||
Augie Fackler
|
r43346 | ) | ||
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
|
r43906 | def log_request(self, code='-', size='-'): | ||
David Soria Parra
|
r19877 | xheaders = [] | ||
r51821 | if hasattr(self, 'headers'): | |||
Augie Fackler
|
r43346 | xheaders = [ | ||
Augie Fackler
|
r43906 | h for h in self.headers.items() if h[0].startswith('x-') | ||
Augie Fackler
|
r43346 | ] | ||
self.log_message( | ||||
Augie Fackler
|
r43906 | '"%s" %s %s%s', | ||
Augie Fackler
|
r43346 | self.requestline, | ||
str(code), | ||||
str(size), | ||||
Augie Fackler
|
r43906 | ''.join([' %s:%s' % h for h in sorted(xheaders)]), | ||
Augie Fackler
|
r43346 | ) | ||
Steven Brown
|
r14093 | |||
Brendan Cully
|
r4860 | def do_write(self): | ||
try: | ||||
self.do_hgweb() | ||||
Manuel Jacob
|
r50199 | except BrokenPipeError: | ||
pass | ||||
Brendan Cully
|
r4860 | |||
Eric Hopper
|
r2355 | def do_POST(self): | ||
try: | ||||
Brendan Cully
|
r4860 | self.do_write() | ||
Augie Fackler
|
r41620 | except Exception as e: | ||
Gregory Szorc
|
r41603 | # I/O below could raise another exception. So log the original | ||
# exception first to ensure it is recorded. | ||||
Augie Fackler
|
r43346 | if not ( | ||
Matt Harbison
|
r53050 | isinstance(e, (ConnectionResetError, ConnectionAbortedError)) | ||
Augie Fackler
|
r43346 | ): | ||
Augie Fackler
|
r43809 | tb = "".join(traceback.format_exception(*sys.exc_info())) | ||
Augie Fackler
|
r41620 | # 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) | ||||
Augie Fackler
|
r43346 | self.log_error( | ||
r"Exception happened during processing " | ||||
Augie Fackler
|
r43809 | "request '%s':%s%s", | ||
Augie Fackler
|
r43346 | self.path, | ||
newline, | ||||
tb, | ||||
) | ||||
Eric Hopper
|
r2355 | |||
Augie Fackler
|
r43809 | self._start_response("500 Internal Server Error", []) | ||
Gregory Szorc
|
r41603 | self._write(b"Internal Server Error") | ||
self._done() | ||||
Matt Harbison
|
r37165 | def do_PUT(self): | ||
self.do_POST() | ||||
Eric Hopper
|
r2355 | def do_GET(self): | ||
self.do_POST() | ||||
r50985 | def do_HEAD(self): | |||
self.do_POST() | ||||
Eric Hopper
|
r2355 | 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 | ||
Augie Fackler
|
r43346 | if path != self.server.prefix and not path.startswith( | ||
self.server.prefix + b'/' | ||||
): | ||||
self._start_response(pycompat.strurl(common.statusmessage(404)), []) | ||||
Augie Fackler
|
r43906 | if self.command == 'POST': | ||
Augie Fackler
|
r41161 | # Paranoia: tell the client we're going to close the | ||
# socket so they don't try and reuse a socket that | ||||
# might have a POST body waiting to confuse us. We do | ||||
# this by directly munging self.saved_headers because | ||||
# self._start_response ignores Connection headers. | ||||
Augie Fackler
|
r43906 | self.saved_headers = [('Connection', 'Close')] | ||
Augie Fackler
|
r38316 | self._write(b"Not Found") | ||
Matt Harbison
|
r37288 | self._done() | ||
return | ||||
Eric Hopper
|
r2355 | env = {} | ||
Augie Fackler
|
r43906 | env['GATEWAY_INTERFACE'] = 'CGI/1.1' | ||
env['REQUEST_METHOD'] = self.command | ||||
env['SERVER_NAME'] = self.server.server_name | ||||
env['SERVER_PORT'] = str(self.server.server_port) | ||||
env['REQUEST_URI'] = self.path | ||||
env['SCRIPT_NAME'] = pycompat.sysstr(self.server.prefix) | ||||
env['PATH_INFO'] = pycompat.sysstr(path[len(self.server.prefix) :]) | ||||
env['REMOTE_HOST'] = self.client_address[0] | ||||
env['REMOTE_ADDR'] = self.client_address[0] | ||||
env['QUERY_STRING'] = query or '' | ||||
Eric Hopper
|
r2355 | |||
Gregory Szorc
|
r49761 | if self.headers.get_content_type() is None: | ||
env['CONTENT_TYPE'] = self.headers.get_default_type() | ||||
Augie Fackler
|
r34720 | else: | ||
Gregory Szorc
|
r49761 | env['CONTENT_TYPE'] = self.headers.get_content_type() | ||
length = self.headers.get('content-length') | ||||
Eric Hopper
|
r2355 | if length: | ||
Augie Fackler
|
r43906 | env['CONTENT_LENGTH'] = length | ||
Augie Fackler
|
r43346 | for header in [ | ||
h | ||||
for h in self.headers.keys() | ||||
Augie Fackler
|
r43906 | if h.lower() not in ('content-type', 'content-length') | ||
Augie Fackler
|
r43346 | ]: | ||
Augie Fackler
|
r43906 | hkey = 'HTTP_' + header.replace('-', '_').upper() | ||
Augie Fackler
|
r34513 | hval = self.headers.get(header) | ||
Augie Fackler
|
r43906 | hval = hval.replace('\n', '').strip() | ||
Eric Hopper
|
r2505 | if hval: | ||
env[hkey] = hval | ||||
Augie Fackler
|
r43906 | env['SERVER_PROTOCOL'] = self.request_version | ||
env['wsgi.version'] = (1, 0) | ||||
env['wsgi.url_scheme'] = pycompat.sysstr(self.url_scheme) | ||||
Matt Harbison
|
r50868 | if env.get('HTTP_EXPECT', '').lower() == '100-continue': | ||
Augie Fackler
|
r13570 | self.rfile = common.continuereader(self.rfile, self.wfile.write) | ||
Augie Fackler
|
r43906 | env['wsgi.input'] = self.rfile | ||
env['wsgi.errors'] = _error_logger(self) | ||||
env['wsgi.multithread'] = isinstance( | ||||
Augie Fackler
|
r43346 | self.server, socketserver.ThreadingMixIn | ||
) | ||||
r51821 | if hasattr(socketserver, 'ForkingMixIn'): | |||
Augie Fackler
|
r43906 | env['wsgi.multiprocess'] = isinstance( | ||
Augie Fackler
|
r43346 | self.server, socketserver.ForkingMixIn | ||
) | ||||
Matt Harbison
|
r39866 | else: | ||
Augie Fackler
|
r43906 | env['wsgi.multiprocess'] = False | ||
Matt Harbison
|
r39866 | |||
Augie Fackler
|
r43906 | env['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: | ||||
Augie Fackler
|
r43346 | raise AssertionError( | ||
Martin von Zweigbergk
|
r43387 | b"Sending headers before start_response() called" | ||
Augie Fackler
|
r43346 | ) | ||
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) | ||||
Augie Fackler
|
r43906 | if h[0].lower() == 'content-length': | ||
Eric Hopper
|
r2508 | self.length = int(h[1]) | ||
r50985 | if ( | |||
self.length is None | ||||
and saved_status[0] != common.HTTP_NOT_MODIFIED | ||||
and self.command != 'HEAD' | ||||
): | ||||
Augie Fackler
|
r43346 | self._chunked = ( | ||
Augie Fackler
|
r43906 | not self.close_connection and self.request_version == 'HTTP/1.1' | ||
Augie Fackler
|
r43346 | ) | ||
Mads Kiilerich
|
r18354 | if self._chunked: | ||
Augie Fackler
|
r43906 | self.send_header('Transfer-Encoding', 'chunked') | ||
Mads Kiilerich
|
r18354 | else: | ||
Augie Fackler
|
r43906 | self.send_header('Connection', 'close') | ||
Eric Hopper
|
r2506 | self.end_headers() | ||
self.sent_headers = True | ||||
def _start_response(self, http_status, headers, exc_info=None): | ||||
Augie Fackler
|
r38317 | assert isinstance(http_status, str) | ||
Eric Hopper
|
r2506 | code, msg = http_status.split(None, 1) | ||
code = int(code) | ||||
self.saved_status = http_status | ||||
Augie Fackler
|
r43906 | bad_headers = ('connection', 'transfer-encoding') | ||
Augie Fackler
|
r43346 | 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: | ||||
Augie Fackler
|
r43347 | raise AssertionError(b"data written before start_response() called") | ||
Eric Hopper
|
r2506 | elif not self.sent_headers: | ||
self.send_headers() | ||||
Eric Hopper
|
r2508 | if self.length is not None: | ||
if len(data) > self.length: | ||||
Augie Fackler
|
r43346 | raise AssertionError( | ||
Augie Fackler
|
r43347 | b"Content-length header sent, but more " | ||
b"bytes than specified are being written." | ||||
Augie Fackler
|
r43346 | ) | ||
Eric Hopper
|
r2508 | self.length = self.length - len(data) | ||
Mads Kiilerich
|
r18354 | elif self._chunked and data: | ||
Augie Fackler
|
r43347 | data = b'%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: | ||||
Augie Fackler
|
r43347 | self.wfile.write(b'0\r\n\r\n') | ||
Mads Kiilerich
|
r18354 | self.wfile.flush() | ||
Gregory Szorc
|
r37027 | def version_string(self): | ||
if self.server.serverheader: | ||||
Yuya Nishihara
|
r38613 | return encoding.strfromlocal(self.server.serverheader) | ||
Gregory Szorc
|
r37027 | return httpservermod.basehttprequesthandler.version_string(self) | ||
Augie Fackler
|
r43346 | |||
Mads Kiilerich
|
r12784 | class _httprequesthandlerssl(_httprequesthandler): | ||
timeless@mozdev.org
|
r26202 | """HTTPS handler based on Python's ssl module""" | ||
Mads Kiilerich
|
r12784 | |||
Augie Fackler
|
r43347 | url_scheme = b'https' | ||
Mads Kiilerich
|
r12784 | |||
@staticmethod | ||||
Gregory Szorc
|
r29553 | def preparehttpserver(httpserver, ui): | ||
Mads Kiilerich
|
r12784 | try: | ||
Gregory Szorc
|
r29555 | from .. import sslutil | ||
Augie Fackler
|
r43346 | |||
Manuel Jacob
|
r45413 | sslutil.wrapserversocket | ||
Mads Kiilerich
|
r12784 | except ImportError: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b"SSL support is unavailable")) | ||
Gregory Szorc
|
r29553 | |||
Augie Fackler
|
r43347 | certfile = ui.config(b'web', b'certificate') | ||
Gregory Szorc
|
r29555 | |||
# These config options are currently only meant for testing. Use | ||||
# at your own risk. | ||||
Augie Fackler
|
r43347 | cafile = ui.config(b'devel', b'servercafile') | ||
reqcert = ui.configbool(b'devel', b'serverrequirecert') | ||||
Gregory Szorc
|
r29555 | |||
Augie Fackler
|
r43346 | 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
|
r43809 | self.rfile = self.request.makefile("rb", self.rbufsize) | ||
self.wfile = self.request.makefile("wb", self.wbufsize) | ||||
Mads Kiilerich
|
r12784 | |||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r10639 | try: | ||
Yuya Nishihara
|
r27046 | import threading | ||
Augie Fackler
|
r43346 | |||
Karthikeyan Singaravelan
|
r47965 | threading.active_count() # silence pyflakes and bypass demandimport | ||
Pulkit Goyal
|
r29433 | _mixin = socketserver.ThreadingMixIn | ||
Dirkjan Ochtman
|
r10639 | except ImportError: | ||
r51821 | if hasattr(os, "fork"): | |||
Pulkit Goyal
|
r29433 | _mixin = socketserver.ForkingMixIn | ||
Dirkjan Ochtman
|
r10639 | else: | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class _mixin: | ||
Dirkjan Ochtman
|
r10639 | pass | ||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r10643 | def openlog(opt, default): | ||
Augie Fackler
|
r43347 | if opt and opt != b'-': | ||
Matt Harbison
|
r53263 | return open(opt, 'ab') | ||
Dirkjan Ochtman
|
r10643 | return default | ||
Augie Fackler
|
r43346 | |||
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 | |||
Augie Fackler
|
r43347 | prefix = ui.config(b'web', b'prefix') | ||
Dirkjan Ochtman
|
r10643 | if prefix: | ||
Augie Fackler
|
r43347 | prefix = b'/' + prefix.strip(b'/') | ||
Dirkjan Ochtman
|
r10643 | self.prefix = prefix | ||
Augie Fackler
|
r43347 | alog = openlog(ui.config(b'web', b'accesslog'), ui.fout) | ||
elog = openlog(ui.config(b'web', b'errorlog'), ui.ferr) | ||||
Dirkjan Ochtman
|
r10643 | self.accesslog = alog | ||
self.errorlog = elog | ||||
self.addr, self.port = self.socket.getsockname()[0:2] | ||||
Manuel Jacob
|
r45578 | self.fqaddr = self.server_name | ||
Dirkjan Ochtman
|
r10643 | |||
Augie Fackler
|
r43347 | self.serverheader = ui.config(b'web', b'server-header') | ||
Gregory Szorc
|
r37027 | |||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r10643 | class IPv6HTTPServer(MercurialHTTPServer): | ||
address_family = getattr(socket, 'AF_INET6', None) | ||||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r10643 | def __init__(self, *args, **kwargs): | ||
if self.address_family is None: | ||||
Augie Fackler
|
r43347 | raise error.RepoError(_(b'IPv6 is not available on this system')) | ||
Dirkjan Ochtman
|
r10643 | super(IPv6HTTPServer, self).__init__(*args, **kwargs) | ||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r10644 | def create_server(ui, app): | ||
Augie Fackler
|
r43347 | if ui.config(b'web', b'certificate'): | ||
Siddharth Agarwal
|
r26848 | handler = _httprequesthandlerssl | ||
Brendan Cully
|
r4860 | else: | ||
Mads Kiilerich
|
r12783 | handler = _httprequesthandler | ||
Brendan Cully
|
r4860 | |||
Augie Fackler
|
r43347 | if ui.configbool(b'web', b'ipv6'): | ||
Dirkjan Ochtman
|
r10641 | cls = IPv6HTTPServer | ||
else: | ||||
cls = MercurialHTTPServer | ||||
Dirkjan Ochtman
|
r8224 | # ugly hack due to python issue5853 (for threaded use) | ||
Manuel Jacob
|
r50178 | import mimetypes | ||
Matt Mackall
|
r20357 | |||
Manuel Jacob
|
r50178 | mimetypes.init() | ||
Dirkjan Ochtman
|
r8224 | |||
Augie Fackler
|
r43347 | address = ui.config(b'web', b'address') | ||
r47669 | port = urlutil.getport(ui.config(b'web', b'port')) | |||
Matt Mackall
|
r3628 | try: | ||
Dirkjan Ochtman
|
r10644 | return cls(ui, app, (address, port), handler) | ||
Gregory Szorc
|
r25660 | except socket.error as inst: | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b"cannot start server at '%s:%d': %s") | ||
Augie Fackler
|
r43346 | % (address, port, encoding.strtolocal(inst.args[1])) | ||
) | ||||