mercurial/hgweb/server.py
author Stefan Rusek <stefan@rusek.org>
Mon, 02 Mar 2009 23:07:56 +0100
changeset 7809 dcdda2f59513
parent 7637 1d54e2f6c0b7
child 7921 f62482848d1b
permissions -rw-r--r--
include i18n files in the win32 installer The .mo files for translations should be included in the Windows installer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2391
d351a3be3371 Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents: 2355
diff changeset
     1
# hgweb/server.py - The standalone hg web server.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     2
#
238
3b92f8fe47ae hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents: 222
diff changeset
     3
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4633
diff changeset
     4
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     5
#
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     6
# This software may be used and distributed according to the terms
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     7
# of the GNU General Public License, incorporated herein by reference.
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
4016
a195f11ed1a2 sync with -stable
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3877 4015
diff changeset
     9
import os, sys, errno, urllib, BaseHTTPServer, socket, SocketServer, traceback
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
    10
from mercurial import hg, util, error
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3836
diff changeset
    11
from hgweb_mod import hgweb
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3836
diff changeset
    12
from hgwebdir_mod import hgwebdir
7225
59b4ae211584 i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents: 6953
diff changeset
    13
from mercurial.i18n import _
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    14
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    15
def _splitURI(uri):
2123
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    16
    """ Return path and query splited from uri
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    17
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    18
    Just like CGI environment, the path is unquoted, the query is
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    19
    not.
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    20
    """
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    21
    if '?' in uri:
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    22
        path, query = uri.split('?', 1)
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    23
    else:
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    24
        path, query = uri, ''
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    25
    return urllib.unquote(path), query
c0729a7f6f8a Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2122
diff changeset
    26
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    27
class _error_logger(object):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    28
    def __init__(self, handler):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    29
        self.handler = handler
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    30
    def flush(self):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    31
        pass
3130
2e043c9a38a6 hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3079
diff changeset
    32
    def write(self, str):
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    33
        self.writelines(str.split('\n'))
3130
2e043c9a38a6 hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3079
diff changeset
    34
    def writelines(self, seq):
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    35
        for msg in seq:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    36
            self.handler.log_error("HG error:  %s", msg)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
    37
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    38
class _hgwebhandler(object, BaseHTTPServer.BaseHTTPRequestHandler):
4870
8f430b1b3025 Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents: 4868
diff changeset
    39
8f430b1b3025 Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents: 4868
diff changeset
    40
    url_scheme = 'http'
4957
cdd33a048289 removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4871
diff changeset
    41
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    42
    def __init__(self, *args, **kargs):
2434
a2df85adface http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2404
diff changeset
    43
        self.protocol_version = 'HTTP/1.1'
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    44
        BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kargs)
1498
78590fb4a82b hgweb: Added archive download buttons to manifest page.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1473
diff changeset
    45
5549
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    46
    def _log_any(self, fp, format, *args):
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    47
        fp.write("%s - - [%s] %s\n" % (self.client_address[0],
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    48
                                       self.log_date_time_string(),
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    49
                                       format % args))
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    50
        fp.flush()
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    51
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    52
    def log_error(self, format, *args):
5549
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    53
        self._log_any(self.server.errorlog, format, *args)
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
    54
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    55
    def log_message(self, format, *args):
5549
f2f42262adbd hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents: 5150
diff changeset
    56
        self._log_any(self.server.accesslog, format, *args)
538
7140bc781655 Add multiple keyword search to hgweb
mpm@selenic.com
parents: 536
diff changeset
    57
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    58
    def do_write(self):
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    59
        try:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    60
            self.do_hgweb()
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    61
        except socket.error, inst:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    62
            if inst[0] != errno.EPIPE:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    63
                raise
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    64
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    65
    def do_POST(self):
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    66
        try:
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
    67
            self.do_write()
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7225
diff changeset
    68
        except StandardError:
4015
769be3c57564 Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3836
diff changeset
    69
            self._start_response("500 Internal Server Error", [])
769be3c57564 Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3836
diff changeset
    70
            self._write("Internal Server Error")
769be3c57564 Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3836
diff changeset
    71
            tb = "".join(traceback.format_exception(*sys.exc_info()))
769be3c57564 Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3836
diff changeset
    72
            self.log_error("Exception happened during processing request '%s':\n%s",
769be3c57564 Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3836
diff changeset
    73
                           self.path, tb)
133
fb84d3e71042 added template support for some hgweb output, also, template files for
jake@edge2.net
parents: 132
diff changeset
    74
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    75
    def do_GET(self):
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    76
        self.do_POST()
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
    77
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    78
    def do_hgweb(self):
5835
bd34f0ac3cb0 adding "prefix" option to "hg serve" (command line and [web] section)
Michele Cella <michele.cella@gmail.com>
parents: 5690
diff changeset
    79
        path, query = _splitURI(self.path)
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    80
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    81
        env = {}
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    82
        env['GATEWAY_INTERFACE'] = 'CGI/1.1'
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    83
        env['REQUEST_METHOD'] = self.command
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    84
        env['SERVER_NAME'] = self.server.server_name
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    85
        env['SERVER_PORT'] = str(self.server.server_port)
3263
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3130
diff changeset
    86
        env['REQUEST_URI'] = self.path
5835
bd34f0ac3cb0 adding "prefix" option to "hg serve" (command line and [web] section)
Michele Cella <michele.cella@gmail.com>
parents: 5690
diff changeset
    87
        env['SCRIPT_NAME'] = self.server.prefix
bd34f0ac3cb0 adding "prefix" option to "hg serve" (command line and [web] section)
Michele Cella <michele.cella@gmail.com>
parents: 5690
diff changeset
    88
        env['PATH_INFO'] = path[len(self.server.prefix):]
4359
80d3f6f0d8e5 hg serve: don't do DNS lookups
Matt Mackall <mpm@selenic.com>
parents: 4245
diff changeset
    89
        env['REMOTE_HOST'] = self.client_address[0]
80d3f6f0d8e5 hg serve: don't do DNS lookups
Matt Mackall <mpm@selenic.com>
parents: 4245
diff changeset
    90
        env['REMOTE_ADDR'] = self.client_address[0]
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    91
        if query:
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    92
            env['QUERY_STRING'] = query
1579
85803ec2daab Remove tabs, and trailing whitespace from hgweb.py
Josef "Jeff" Sipek <jeffpc@optonline.net>
parents: 1575
diff changeset
    93
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    94
        if self.headers.typeheader is None:
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    95
            env['CONTENT_TYPE'] = self.headers.type
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    96
        else:
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    97
            env['CONTENT_TYPE'] = self.headers.typeheader
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    98
        length = self.headers.getheader('content-length')
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
    99
        if length:
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   100
            env['CONTENT_LENGTH'] = length
4633
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4534
diff changeset
   101
        for header in [h for h in self.headers.keys()
2505
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   102
                       if h not in ('content-type', 'content-length')]:
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   103
            hkey = 'HTTP_' + header.replace('-', '_').upper()
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   104
            hval = self.headers.getheader(header)
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   105
            hval = hval.replace('\n', '').strip()
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   106
            if hval:
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   107
                env[hkey] = hval
01b856927970 Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents: 2434
diff changeset
   108
        env['SERVER_PROTOCOL'] = self.request_version
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   109
        env['wsgi.version'] = (1, 0)
4871
d787d9ad67cc cosmetics
Brendan Cully <brendan@kublai.com>
parents: 4870
diff changeset
   110
        env['wsgi.url_scheme'] = self.url_scheme
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   111
        env['wsgi.input'] = self.rfile
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   112
        env['wsgi.errors'] = _error_logger(self)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   113
        env['wsgi.multithread'] = isinstance(self.server,
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   114
                                             SocketServer.ThreadingMixIn)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   115
        env['wsgi.multiprocess'] = isinstance(self.server,
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   116
                                              SocketServer.ForkingMixIn)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   117
        env['wsgi.run_once'] = 0
601
8865eb8ade99 Add globals to templater/fixup RSS
mpm@selenic.com
parents: 600
diff changeset
   118
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   119
        self.close_connection = True
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   120
        self.saved_status = None
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   121
        self.saved_headers = []
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   122
        self.sent_headers = False
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   123
        self.length = None
6784
18c429ea3a0e hgweb: all protocol functions have become generators
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6403
diff changeset
   124
        for chunk in self.server.application(env, self._start_response):
18c429ea3a0e hgweb: all protocol functions have become generators
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6403
diff changeset
   125
            self._write(chunk)
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   126
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   127
    def send_headers(self):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   128
        if not self.saved_status:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   129
            raise AssertionError("Sending headers before start_response() called")
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   130
        saved_status = self.saved_status.split(None, 1)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   131
        saved_status[0] = int(saved_status[0])
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   132
        self.send_response(*saved_status)
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   133
        should_close = True
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   134
        for h in self.saved_headers:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   135
            self.send_header(*h)
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   136
            if h[0].lower() == 'content-length':
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   137
                should_close = False
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   138
                self.length = int(h[1])
2582
276de216d2c5 Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2508
diff changeset
   139
        # The value of the Connection header is a list of case-insensitive
276de216d2c5 Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2508
diff changeset
   140
        # tokens separated by commas and optional whitespace.
2600
c4325f0a9b91 clean up trailing white space.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2582
diff changeset
   141
        if 'close' in [token.strip().lower() for token in
2582
276de216d2c5 Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2508
diff changeset
   142
                       self.headers.get('connection', '').split(',')]:
276de216d2c5 Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2508
diff changeset
   143
            should_close = True
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   144
        if should_close:
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   145
            self.send_header('Connection', 'close')
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   146
        self.close_connection = should_close
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   147
        self.end_headers()
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   148
        self.sent_headers = True
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   149
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   150
    def _start_response(self, http_status, headers, exc_info=None):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   151
        code, msg = http_status.split(None, 1)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   152
        code = int(code)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   153
        self.saved_status = http_status
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   154
        bad_headers = ('connection', 'transfer-encoding')
4633
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4534
diff changeset
   155
        self.saved_headers = [h for h in headers
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4534
diff changeset
   156
                              if h[0].lower() not in bad_headers]
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   157
        return self._write
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   158
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   159
    def _write(self, data):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   160
        if not self.saved_status:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   161
            raise AssertionError("data written before start_response() called")
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   162
        elif not self.sent_headers:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   163
            self.send_headers()
2508
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   164
        if self.length is not None:
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   165
            if len(data) > self.length:
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   166
                raise AssertionError("Content-length header sent, but more bytes than specified are being written.")
ab460a3f0e3a Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents: 2507
diff changeset
   167
            self.length = self.length - len(data)
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   168
        self.wfile.write(data)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents: 2505
diff changeset
   169
        self.wfile.flush()
136
0e8d60d2bb2b added annotate
jake@edge2.net
parents: 135
diff changeset
   170
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   171
class _shgwebhandler(_hgwebhandler):
4870
8f430b1b3025 Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents: 4868
diff changeset
   172
8f430b1b3025 Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents: 4868
diff changeset
   173
    url_scheme = 'https'
4957
cdd33a048289 removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4871
diff changeset
   174
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   175
    def setup(self):
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   176
        self.connection = self.request
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   177
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   178
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   179
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   180
    def do_write(self):
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   181
        from OpenSSL.SSL import SysCallError
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   182
        try:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   183
            super(_shgwebhandler, self).do_write()
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   184
        except SysCallError, inst:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   185
            if inst.args[0] != errno.EPIPE:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   186
                raise
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   187
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   188
    def handle_one_request(self):
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   189
        from OpenSSL.SSL import SysCallError, ZeroReturnError
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   190
        try:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   191
            super(_shgwebhandler, self).handle_one_request()
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   192
        except (SysCallError, ZeroReturnError):
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   193
            self.close_connection = True
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   194
            pass
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   195
2392
8238a3f039e6 Adjusting hgweb splitup to be a little cleaner.
Eric Hopper <hopper@omnifarious.org>
parents: 2391
diff changeset
   196
def create_server(ui, repo):
2121
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   197
    use_threads = True
2124
27fd8b7a6c51 Cleaned trailing whitespace in hgweb.py, removed command line shortcut for webdir-conf.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2123
diff changeset
   198
938
54b2a42e501e hgweb: add [web] section to hgrc
mpm@selenic.com
parents: 937
diff changeset
   199
    def openlog(opt, default):
54b2a42e501e hgweb: add [web] section to hgrc
mpm@selenic.com
parents: 937
diff changeset
   200
        if opt and opt != '-':
5690
1b365c5723bc server: append to logfiles
Mirko Friedenhagen <mirko-lists@friedenhagen.de>
parents: 5579
diff changeset
   201
            return open(opt, 'a')
938
54b2a42e501e hgweb: add [web] section to hgrc
mpm@selenic.com
parents: 937
diff changeset
   202
        return default
54b2a42e501e hgweb: add [web] section to hgrc
mpm@selenic.com
parents: 937
diff changeset
   203
5083
f94dbc6c7eaf Fix hgwebdir after 9858477ed74cce9dc8f4069f9453a1bda0e13ba1 broke it.
Eric Hopper <hopper@omnifarious.org>
parents: 4835
diff changeset
   204
    if repo is None:
5086
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   205
        myui = ui
5083
f94dbc6c7eaf Fix hgwebdir after 9858477ed74cce9dc8f4069f9453a1bda0e13ba1 broke it.
Eric Hopper <hopper@omnifarious.org>
parents: 4835
diff changeset
   206
    else:
5086
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   207
        myui = repo.ui
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   208
    address = myui.config("web", "address", "")
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   209
    port = int(myui.config("web", "port", 8000))
5970
f25070ecf334 hgweb: fixes to make hg serve prefix handling more robust
Michele Cella <michele.cella@gmail.com>
parents: 5835
diff changeset
   210
    prefix = myui.config("web", "prefix", "")
f25070ecf334 hgweb: fixes to make hg serve prefix handling more robust
Michele Cella <michele.cella@gmail.com>
parents: 5835
diff changeset
   211
    if prefix:
f25070ecf334 hgweb: fixes to make hg serve prefix handling more robust
Michele Cella <michele.cella@gmail.com>
parents: 5835
diff changeset
   212
        prefix = "/" + prefix.strip("/")
5086
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   213
    use_ipv6 = myui.configbool("web", "ipv6")
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   214
    webdir_conf = myui.config("web", "webdir_conf")
5150
ff461baa9c4e merge with -stable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5148 5086
diff changeset
   215
    ssl_cert = myui.config("web", "certificate")
5086
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   216
    accesslog = openlog(myui.config("web", "accesslog", "-"), sys.stdout)
86cd6fd61042 unobfuscate part of f94dbc6c7eaf
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5083
diff changeset
   217
    errorlog = openlog(myui.config("web", "errorlog", "-"), sys.stderr)
2124
27fd8b7a6c51 Cleaned trailing whitespace in hgweb.py, removed command line shortcut for webdir-conf.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2123
diff changeset
   218
2121
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   219
    if use_threads:
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   220
        try:
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   221
            from threading import activeCount
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   222
        except ImportError:
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   223
            use_threads = False
938
54b2a42e501e hgweb: add [web] section to hgrc
mpm@selenic.com
parents: 937
diff changeset
   224
2121
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   225
    if use_threads:
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   226
        _mixin = SocketServer.ThreadingMixIn
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   227
    else:
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   228
        if hasattr(os, "fork"):
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   229
            _mixin = SocketServer.ForkingMixIn
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   230
        else:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3628
diff changeset
   231
            class _mixin:
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3628
diff changeset
   232
                pass
2121
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   233
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   234
    class MercurialHTTPServer(object, _mixin, BaseHTTPServer.HTTPServer):
4516
96d8a56d4ef9 Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4379
diff changeset
   235
4130
178007785be8 web/server: disable address reuse option for BaseHTTPServer on windows
Patrick Mezard <pmezard@gmail.com>
parents: 4091
diff changeset
   236
        # SO_REUSEADDR has broken semantics on windows
178007785be8 web/server: disable address reuse option for BaseHTTPServer on windows
Patrick Mezard <pmezard@gmail.com>
parents: 4091
diff changeset
   237
        if os.name == 'nt':
178007785be8 web/server: disable address reuse option for BaseHTTPServer on windows
Patrick Mezard <pmezard@gmail.com>
parents: 4091
diff changeset
   238
            allow_reuse_address = 0
4516
96d8a56d4ef9 Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4379
diff changeset
   239
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   240
        def __init__(self, *args, **kargs):
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   241
            BaseHTTPServer.HTTPServer.__init__(self, *args, **kargs)
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   242
            self.accesslog = accesslog
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   243
            self.errorlog = errorlog
2650
56e98084e040 Make hgweb threads into daemon threads.
Brendan Cully <brendan@kublai.com>
parents: 2600
diff changeset
   244
            self.daemon_threads = True
4245
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   245
            def make_handler():
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   246
                if webdir_conf:
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   247
                    hgwebobj = hgwebdir(webdir_conf, ui)
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   248
                elif repo is not None:
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   249
                    hgwebobj = hgweb(hg.repository(repo.ui, repo.root))
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   250
                else:
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   251
                    raise error.RepoError(_("There is no Mercurial repository"
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   252
                                            " here (.hg not found)"))
4245
bd46b83b9692 avoid wsgiapplication <-> MercurialHTTPServer circular reference
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4130
diff changeset
   253
                return hgwebobj
5566
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5549
diff changeset
   254
            self.application = make_handler()
2355
eb08fb4d41e1 Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents: 2328
diff changeset
   255
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   256
            if ssl_cert:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   257
                try:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   258
                    from OpenSSL import SSL
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   259
                    ctx = SSL.Context(SSL.SSLv23_METHOD)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   260
                except ImportError:
6953
63b5f4c73c98 i18n: mark strings for translation in Mercurial
Martin Geisler <mg@daimi.au.dk>
parents: 6784
diff changeset
   261
                    raise util.Abort(_("SSL support is unavailable"))
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   262
                ctx.use_privatekey_file(ssl_cert)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   263
                ctx.use_certificate_file(ssl_cert)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   264
                sock = socket.socket(self.address_family, self.socket_type)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   265
                self.socket = SSL.Connection(ctx, sock)
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   266
                self.server_bind()
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   267
                self.server_activate()
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   268
6262
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   269
            self.addr, self.port = self.socket.getsockname()[0:2]
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   270
            self.prefix = prefix
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   271
            self.fqaddr = socket.getfqdn(address)
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   272
2121
150cdd6c3c90 Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 2119
diff changeset
   273
    class IPv6HTTPServer(MercurialHTTPServer):
881
16ce690c411d Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents: 858
diff changeset
   274
        address_family = getattr(socket, 'AF_INET6', None)
16ce690c411d Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents: 858
diff changeset
   275
16ce690c411d Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents: 858
diff changeset
   276
        def __init__(self, *args, **kwargs):
16ce690c411d Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents: 858
diff changeset
   277
            if self.address_family is None:
6217
fe8dbbe9520d Avoid importing mercurial.node/mercurial.repo stuff from mercurial.hg
Joel Rosdahl <joel@rosdahl.net>
parents: 6212
diff changeset
   278
                raise RepoError(_('IPv6 not available on this system'))
2507
7e01da2bc7f3 Fix two small bugs that would've prevented the web interface and IPv6
Eric Hopper <hopper@omnifarious.org>
parents: 2506
diff changeset
   279
            super(IPv6HTTPServer, self).__init__(*args, **kwargs)
158
be7103467d2e Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents: 157
diff changeset
   280
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   281
    if ssl_cert:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   282
        handler = _shgwebhandler
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   283
    else:
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   284
        handler = _hgwebhandler
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   285
3628
dc3504af7722 hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents: 3263
diff changeset
   286
    try:
dc3504af7722 hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents: 3263
diff changeset
   287
        if use_ipv6:
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   288
            return IPv6HTTPServer((address, port), handler)
3628
dc3504af7722 hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents: 3263
diff changeset
   289
        else:
4860
f3802f9f1840 Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents: 4635
diff changeset
   290
            return MercurialHTTPServer((address, port), handler)
3628
dc3504af7722 hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents: 3263
diff changeset
   291
    except socket.error, inst:
6262
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   292
        raise util.Abort(_("cannot start server at '%s:%d': %s")
de7256c82fad hgweb: clarify which address and port can/cannot be bound at startup (bug 769)
Stephen Deasey <sdeasey@gmail.com>
parents: 6217
diff changeset
   293
                         % (address, port, inst.args[1]))