view mercurial/hgweb/server.py @ 42044:bb271ec2fbfb

compression: introduce a `storage.revlog.zstd.level` configuration This option control the zstd compression level used when compressing revlog chunk. The usage of zstd for revlog compression has not graduated from experimental yet, but we intend to fix that soon. The option name for the compression level is more straight forward to pick, so this changesets comes first. Having a dedicated option for each compression engine is useful because they don't support the same range of values. I ran the same measurement as for the zlib compression level (in the parent changesets). The variation in repository size is stay mostly in the same (small) range. The "read/write" performance see smallish variation, but are overall much better than zlib. Write performance show the same tend of having better write performance for when reaching high-end compression. Again, we don't intend to change the default zstd compression level (currently: 3) in this series. However this is worth investigating in the future. The Performance comparison of zlib vs zstd is quite impressive. The repository size stay in the same range, but the performance are much better in all situations. Comparison summary ================== We are looking at: - performance range for zlib - performance range for zstd - comparison of default zstd (level-3) to default zlib (level 6) - comparison of the slowest zstd time to the fastest zlib time Read performance: ----------------- | zlib | zstd | cmp | f2s mercurial | 0.170159 - 0.189219 | 0.144127 - 0.149624 | 80% | 88% pypy | 2.679217 - 2.768691 | 1.532317 - 1.705044 | 60% | 63% netbeans | 122.477027 - 141.620281 | 72.996346 - 89.731560 | 58% | 73% mozilla | 147.867662 - 170.572118 | 91.700995 - 105.853099 | 56% | 71% Write performance: ------------------ | zlib | zstd | cmp | f2s mercurial | 53.250304 - 56.2936129 | 40.877025 - 45.677286 | 75% | 86% pypy | 460.721984 - 476.589918 | 270.545409 - 301.002219 | 63% | 65% netbeans | 520.560316 - 715.930400 | 370.356311 - 428.329652 | 55% | 82% mozilla | 739.803002 - 987.056093 | 505.152906 - 591.930683 | 57% | 80% Raw data -------- repo alg lvl .hg/store size 00manifest.d read write mercurial zlib 1 49,402,813 5,963,475 0.170159 53.250304 mercurial zlib 6 47,197,397 5,875,730 0.182820 56.264320 mercurial zlib 9 47,121,596 5,849,781 0.189219 56.293612 mercurial zstd 1 49,737,084 5,966,355 0.144127 40.877025 mercurial zstd 3 48,961,867 5,895,208 0.146376 42.268142 mercurial zstd 5 48,200,592 5,938,676 0.149624 43.162875 mercurial zstd 10 47,833,520 5,913,353 0.145185 44.012489 mercurial zstd 15 47,314,604 5,728,679 0.147686 45.677286 mercurial zstd 20 47,330,502 5,830,539 0.145789 45.025407 mercurial zstd 22 47,330,076 5,830,539 0.143996 44.690460 pypy zlib 1 370,830,572 28,462,425 2.679217 460.721984 pypy zlib 6 340,112,317 27,648,747 2.768691 467.537158 pypy zlib 9 338,360,736 27,639,003 2.763495 476.589918 pypy zstd 1 362,377,479 27,916,214 1.532317 270.545409 pypy zstd 3 354,137,693 27,905,988 1.686718 294.951509 pypy zstd 5 342,640,043 27,655,774 1.705044 301.002219 pypy zstd 10 334,224,327 27,164,493 1.567287 285.186239 pypy zstd 15 329,000,363 26,645,965 1.637729 299.561332 pypy zstd 20 324,534,039 26,199,547 1.526813 302.149827 pypy zstd 22 324,530,595 26,198,932 1.525718 307.821218 netbeans zlib 1 1,281,847,810 165,495,457 122.477027 520.560316 netbeans zlib 6 1,205,284,353 159,161,207 139.876147 715.930400 netbeans zlib 9 1,197,135,671 155,034,586 141.620281 678.297064 netbeans zstd 1 1,259,581,737 160,840,613 72.996346 370.356311 netbeans zstd 3 1,232,978,122 157,691,551 81.622317 396.733087 netbeans zstd 5 1,208,034,075 160,246,880 83.080549 364.342626 netbeans zstd 10 1,188,624,176 156,083,417 79.323935 403.594602 netbeans zstd 15 1,176,973,589 153,859,477 89.731560 428.329652 netbeans zstd 20 1,162,958,258 151,147,535 82.842667 392.335349 netbeans zstd 22 1,162,707,029 151,150,220 82.565695 402.840655 mozilla zlib 1 2,775,497,186 298,527,987 147.867662 751.263721 mozilla zlib 6 2,596,856,420 286,597,671 170.572118 987.056093 mozilla zlib 9 2,587,542,494 287,018,264 163.622338 739.803002 mozilla zstd 1 2,723,159,348 286,617,532 91.700995 570.042751 mozilla zstd 3 2,665,055,001 286,152,013 95.240155 561.412805 mozilla zstd 5 2,607,819,817 288,060,030 101.978048 505.152906 mozilla zstd 10 2,558,761,085 283,967,648 104.113481 497.771202 mozilla zstd 15 2,526,216,060 275,581,300 105.853099 591.930683 mozilla zstd 20 2,485,114,806 266,478,859 95.268795 576.515389 mozilla zstd 22 2,484,869,080 266,456,505 94.429282 572.785537
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 27 Mar 2019 18:35:59 +0100
parents 6bbb12cba5a8
children e554cfd93975
line wrap: on
line source

# hgweb/server.py - The standalone hg web server.
#
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

import errno
import os
import socket
import sys
import traceback
import wsgiref.validate

from ..i18n import _

from .. import (
    encoding,
    error,
    pycompat,
    util,
)

httpservermod = util.httpserver
socketserver = util.socketserver
urlerr = util.urlerr
urlreq = util.urlreq

from . import (
    common,
)

def _splitURI(uri):
    """Return path and query that has been split from uri

    Just like CGI environment, the path is unquoted, the query is
    not.
    """
    if r'?' in uri:
        path, query = uri.split(r'?', 1)
    else:
        path, query = uri, r''
    return urlreq.unquote(path), query

class _error_logger(object):
    def __init__(self, handler):
        self.handler = handler
    def flush(self):
        pass
    def write(self, str):
        self.writelines(str.split('\n'))
    def writelines(self, seq):
        for msg in seq:
            self.handler.log_error(r"HG error:  %s", encoding.strfromlocal(msg))

class _httprequesthandler(httpservermod.basehttprequesthandler):

    url_scheme = 'http'

    @staticmethod
    def preparehttpserver(httpserver, ui):
        """Prepare .socket of new HTTPServer instance"""

    def __init__(self, *args, **kargs):
        self.protocol_version = r'HTTP/1.1'
        httpservermod.basehttprequesthandler.__init__(self, *args, **kargs)

    def _log_any(self, fp, format, *args):
        fp.write(pycompat.sysbytes(
            r"%s - - [%s] %s" % (self.client_address[0],
                                 self.log_date_time_string(),
                                 format % args)) + '\n')
        fp.flush()

    def log_error(self, format, *args):
        self._log_any(self.server.errorlog, format, *args)

    def log_message(self, format, *args):
        self._log_any(self.server.accesslog, format, *args)

    def log_request(self, code=r'-', size=r'-'):
        xheaders = []
        if util.safehasattr(self, 'headers'):
            xheaders = [h for h in self.headers.items()
                        if h[0].startswith(r'x-')]
        self.log_message(r'"%s" %s %s%s',
                         self.requestline, str(code), str(size),
                         r''.join([r' %s:%s' % h for h in sorted(xheaders)]))

    def do_write(self):
        try:
            self.do_hgweb()
        except socket.error as inst:
            if inst.errno != errno.EPIPE:
                raise

    def do_POST(self):
        try:
            self.do_write()
        except Exception as e:
            # I/O below could raise another exception. So log the original
            # exception first to ensure it is recorded.
            if not (isinstance(e, (OSError, socket.error))
                    and e.errno == errno.ECONNRESET):
                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)

            self._start_response(r"500 Internal Server Error", [])
            self._write(b"Internal Server Error")
            self._done()

    def do_PUT(self):
        self.do_POST()

    def do_GET(self):
        self.do_POST()

    def do_hgweb(self):
        self.sent_headers = False
        path, query = _splitURI(self.path)

        # Ensure the slicing of path below is valid
        if (path != self.server.prefix
            and not path.startswith(self.server.prefix + b'/')):
            self._start_response(pycompat.strurl(common.statusmessage(404)),
                                 [])
            if self.command == 'POST':
                # 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.
                self.saved_headers = [(r'Connection', r'Close')]
            self._write(b"Not Found")
            self._done()
            return

        env = {}
        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'] = pycompat.sysstr(self.server.prefix)
        env[r'PATH_INFO'] = pycompat.sysstr(path[len(self.server.prefix):])
        env[r'REMOTE_HOST'] = self.client_address[0]
        env[r'REMOTE_ADDR'] = self.client_address[0]
        env[r'QUERY_STRING'] = query or r''

        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()
            length = self.headers.get(r'content-length')
        else:
            if self.headers.typeheader is None:
                env[r'CONTENT_TYPE'] = self.headers.type
            else:
                env[r'CONTENT_TYPE'] = self.headers.typeheader
            length = self.headers.getheader(r'content-length')
        if length:
            env[r'CONTENT_LENGTH'] = length
        for header in [h for h in self.headers.keys()
                      if h.lower() not in (r'content-type', r'content-length')]:
            hkey = r'HTTP_' + header.replace(r'-', r'_').upper()
            hval = self.headers.get(header)
            hval = hval.replace(r'\n', r'').strip()
            if hval:
                env[hkey] = hval
        env[r'SERVER_PROTOCOL'] = self.request_version
        env[r'wsgi.version'] = (1, 0)
        env[r'wsgi.url_scheme'] = pycompat.sysstr(self.url_scheme)
        if env.get(r'HTTP_EXPECT', '').lower() == '100-continue':
            self.rfile = common.continuereader(self.rfile, self.wfile.write)

        env[r'wsgi.input'] = self.rfile
        env[r'wsgi.errors'] = _error_logger(self)
        env[r'wsgi.multithread'] = isinstance(self.server,
                                             socketserver.ThreadingMixIn)
        if util.safehasattr(socketserver, 'ForkingMixIn'):
            env[r'wsgi.multiprocess'] = isinstance(self.server,
                                                   socketserver.ForkingMixIn)
        else:
            env[r'wsgi.multiprocess'] = False

        env[r'wsgi.run_once'] = 0

        wsgiref.validate.check_environ(env)

        self.saved_status = None
        self.saved_headers = []
        self.length = None
        self._chunked = None
        for chunk in self.server.application(env, self._start_response):
            self._write(chunk)
        if not self.sent_headers:
            self.send_headers()
        self._done()

    def send_headers(self):
        if not self.saved_status:
            raise AssertionError("Sending headers before "
                                 "start_response() called")
        saved_status = self.saved_status.split(None, 1)
        saved_status[0] = int(saved_status[0])
        self.send_response(*saved_status)
        self.length = None
        self._chunked = False
        for h in self.saved_headers:
            self.send_header(*h)
            if h[0].lower() == r'content-length':
                self.length = int(h[1])
        if (self.length is None and
            saved_status[0] != common.HTTP_NOT_MODIFIED):
            self._chunked = (not self.close_connection and
                             self.request_version == r'HTTP/1.1')
            if self._chunked:
                self.send_header(r'Transfer-Encoding', r'chunked')
            else:
                self.send_header(r'Connection', r'close')
        self.end_headers()
        self.sent_headers = True

    def _start_response(self, http_status, headers, exc_info=None):
        assert isinstance(http_status, str)
        code, msg = http_status.split(None, 1)
        code = int(code)
        self.saved_status = http_status
        bad_headers = (r'connection', r'transfer-encoding')
        self.saved_headers = [h for h in headers
                              if h[0].lower() not in bad_headers]
        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()
        if self.length is not None:
            if len(data) > self.length:
                raise AssertionError("Content-length header sent, but more "
                                     "bytes than specified are being written.")
            self.length = self.length - len(data)
        elif self._chunked and data:
            data = '%x\r\n%s\r\n' % (len(data), data)
        self.wfile.write(data)
        self.wfile.flush()

    def _done(self):
        if self._chunked:
            self.wfile.write('0\r\n\r\n')
            self.wfile.flush()

    def version_string(self):
        if self.server.serverheader:
            return encoding.strfromlocal(self.server.serverheader)
        return httpservermod.basehttprequesthandler.version_string(self)

class _httprequesthandlerssl(_httprequesthandler):
    """HTTPS handler based on Python's ssl module"""

    url_scheme = 'https'

    @staticmethod
    def preparehttpserver(httpserver, ui):
        try:
            from .. import sslutil
            sslutil.modernssl
        except ImportError:
            raise error.Abort(_("SSL support is unavailable"))

        certfile = ui.config('web', 'certificate')

        # 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)

    def setup(self):
        self.connection = self.request
        self.rfile = self.request.makefile(r"rb", self.rbufsize)
        self.wfile = self.request.makefile(r"wb", self.wbufsize)

try:
    import threading
    threading.activeCount() # silence pyflakes and bypass demandimport
    _mixin = socketserver.ThreadingMixIn
except ImportError:
    if util.safehasattr(os, "fork"):
        _mixin = socketserver.ForkingMixIn
    else:
        class _mixin(object):
            pass

def openlog(opt, default):
    if opt and opt != '-':
        return open(opt, 'ab')
    return default

class MercurialHTTPServer(_mixin, httpservermod.httpserver, object):

    # SO_REUSEADDR has broken semantics on windows
    if pycompat.iswindows:
        allow_reuse_address = 0

    def __init__(self, ui, app, addr, handler, **kwargs):
        httpservermod.httpserver.__init__(self, addr, handler, **kwargs)
        self.daemon_threads = True
        self.application = app

        handler.preparehttpserver(self, ui)

        prefix = ui.config('web', 'prefix')
        if prefix:
            prefix = '/' + prefix.strip('/')
        self.prefix = prefix

        alog = openlog(ui.config('web', 'accesslog'), ui.fout)
        elog = openlog(ui.config('web', 'errorlog'), ui.ferr)
        self.accesslog = alog
        self.errorlog = elog

        self.addr, self.port = self.socket.getsockname()[0:2]
        self.fqaddr = socket.getfqdn(addr[0])

        self.serverheader = ui.config('web', 'server-header')

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)

def create_server(ui, app):

    if ui.config('web', 'certificate'):
        handler = _httprequesthandlerssl
    else:
        handler = _httprequesthandler

    if ui.configbool('web', 'ipv6'):
        cls = IPv6HTTPServer
    else:
        cls = MercurialHTTPServer

    # ugly hack due to python issue5853 (for threaded use)
    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.

        sys.argv # unwrap demand-loader so that reload() works
        reload(sys) # resurrect sys.setdefaultencoding()
        oldenc = sys.getdefaultencoding()
        sys.setdefaultencoding("latin1") # or any full 8-bit encoding
        mimetypes.init()
        sys.setdefaultencoding(oldenc)

    address = ui.config('web', 'address')
    port = util.getport(ui.config('web', 'port'))
    try:
        return cls(ui, app, (address, port), handler)
    except socket.error as inst:
        raise error.Abort(_("cannot start server at '%s:%d': %s")
                          % (address, port, encoding.strtolocal(inst.args[1])))