Mercurial > hg-stable
annotate mercurial/hgweb/server.py @ 20224:34d4a037dced
discovery: enforce filtering into revlogbaseddag._internalizeall
One more step toward discovery running on filtered repo.
author | Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
---|---|
date | Fri, 15 Nov 2013 23:27:39 -0500 |
parents | 52ed85d9ac26 |
children | 6863d42eb59a |
rev | line source |
---|---|
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 | 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 | 5 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8224
diff
changeset
|
6 # This software may be used and distributed according to the terms of the |
10263 | 7 # GNU General Public License version 2 or any later version. |
131 | 8 |
4016 | 9 import os, sys, errno, urllib, BaseHTTPServer, socket, SocketServer, traceback |
10905
13a1b2fb7ef2
pylint, pyflakes: remove unused or duplicate imports
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
10644
diff
changeset
|
10 from mercurial import util, error |
13570
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
13443
diff
changeset
|
11 from mercurial.hgweb import common |
7225
59b4ae211584
i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents:
6953
diff
changeset
|
12 from mercurial.i18n import _ |
138 | 13 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
14 def _splitURI(uri): |
17427
57c6c24b9bc4
improve some comments and docstrings, fixing issues found when spell checking
Mads Kiilerich <mads@kiilerich.com>
parents:
17424
diff
changeset
|
15 """Return path and query that has been split from uri |
2123
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
16 |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
17 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
|
18 not. |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
19 """ |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
20 if '?' in uri: |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
21 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
|
22 else: |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
23 path, query = uri, '' |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
24 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
|
25 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
26 class _error_logger(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
27 def __init__(self, handler): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
28 self.handler = handler |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
29 def flush(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
30 pass |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
31 def write(self, str): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
32 self.writelines(str.split('\n')) |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
33 def writelines(self, seq): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
34 for msg in seq: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
35 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
|
36 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
37 class _httprequesthandler(BaseHTTPServer.BaseHTTPRequestHandler): |
4870
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
38 |
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
39 url_scheme = 'http' |
4957
cdd33a048289
removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4871
diff
changeset
|
40 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
41 @staticmethod |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
42 def preparehttpserver(httpserver, ssl_cert): |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
43 """Prepare .socket of new HTTPServer instance""" |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
44 pass |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
45 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
46 def __init__(self, *args, **kargs): |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2404
diff
changeset
|
47 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
|
48 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
|
49 |
5549
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
50 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
|
51 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
|
52 self.log_date_time_string(), |
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
53 format % args)) |
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
54 fp.flush() |
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
55 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
56 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
|
57 self._log_any(self.server.errorlog, format, *args) |
131 | 58 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
59 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
|
60 self._log_any(self.server.accesslog, format, *args) |
538 | 61 |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
62 def log_request(self, code='-', size='-'): |
19877
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
63 xheaders = [] |
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
64 if util.safehasattr(self, 'headers'): |
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
65 xheaders = [h for h in self.headers.items() |
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
66 if h[0].startswith('x-')] |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
67 self.log_message('"%s" %s %s%s', |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
68 self.requestline, str(code), str(size), |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
69 ''.join([' %s:%s' % h for h in sorted(xheaders)])) |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
70 |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
71 def do_write(self): |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
72 try: |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
73 self.do_hgweb() |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
74 except socket.error, inst: |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
75 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
|
76 raise |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
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_POST(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
79 try: |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
80 self.do_write() |
13443
8fa83d7159eb
serve: catch and log all Exceptions, not only StandardException
Mads Kiilerich <mads@kiilerich.com>
parents:
12797
diff
changeset
|
81 except Exception: |
4015
769be3c57564
Handle exceptions in do_hgweb: Send "Internal Server Error", log traceback
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3836
diff
changeset
|
82 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
|
83 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
|
84 tb = "".join(traceback.format_exception(*sys.exc_info())) |
8663
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
85 self.log_error("Exception happened during processing " |
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
86 "request '%s':\n%s", self.path, tb) |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
87 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
88 def do_GET(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
89 self.do_POST() |
131 | 90 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
91 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
|
92 path, query = _splitURI(self.path) |
138 | 93 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
94 env = {} |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
95 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
|
96 env['REQUEST_METHOD'] = self.command |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 env['REMOTE_HOST'] = self.client_address[0] |
80d3f6f0d8e5
hg serve: don't do DNS lookups
Matt Mackall <mpm@selenic.com>
parents:
4245
diff
changeset
|
103 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
|
104 if query: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
105 env['QUERY_STRING'] = query |
1579
85803ec2daab
Remove tabs, and trailing whitespace from hgweb.py
Josef "Jeff" Sipek <jeffpc@optonline.net>
parents:
1575
diff
changeset
|
106 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
107 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
|
108 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
|
109 else: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
110 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
|
111 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
|
112 if length: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
113 env['CONTENT_LENGTH'] = length |
4633
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4534
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 if hval: |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
120 env[hkey] = hval |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
121 env['SERVER_PROTOCOL'] = self.request_version |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
122 env['wsgi.version'] = (1, 0) |
4871 | 123 env['wsgi.url_scheme'] = self.url_scheme |
13570
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
13443
diff
changeset
|
124 if env.get('HTTP_EXPECT', '').lower() == '100-continue': |
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
13443
diff
changeset
|
125 self.rfile = common.continuereader(self.rfile, self.wfile.write) |
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
13443
diff
changeset
|
126 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
127 env['wsgi.input'] = self.rfile |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
128 env['wsgi.errors'] = _error_logger(self) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
129 env['wsgi.multithread'] = isinstance(self.server, |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
130 SocketServer.ThreadingMixIn) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
131 env['wsgi.multiprocess'] = isinstance(self.server, |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
132 SocketServer.ForkingMixIn) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
133 env['wsgi.run_once'] = 0 |
601 | 134 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
135 self.saved_status = None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
136 self.saved_headers = [] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
137 self.sent_headers = False |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
138 self.length = None |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
139 self._chunked = None |
6784
18c429ea3a0e
hgweb: all protocol functions have become generators
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6403
diff
changeset
|
140 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
|
141 self._write(chunk) |
18349
c007e5c54b16
serve: send response headers even if response has no body
Mads Kiilerich <mads@kiilerich.com>
parents:
17427
diff
changeset
|
142 if not self.sent_headers: |
c007e5c54b16
serve: send response headers even if response has no body
Mads Kiilerich <mads@kiilerich.com>
parents:
17427
diff
changeset
|
143 self.send_headers() |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
144 self._done() |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
145 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
146 def send_headers(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
147 if not self.saved_status: |
8663
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
148 raise AssertionError("Sending headers before " |
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
149 "start_response() called") |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
150 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
|
151 saved_status[0] = int(saved_status[0]) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
152 self.send_response(*saved_status) |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
153 self.length = None |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
154 self._chunked = False |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
155 for h in self.saved_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
156 self.send_header(*h) |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
157 if h[0].lower() == 'content-length': |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
158 self.length = int(h[1]) |
18380
a4d7fd7ad1f7
serve: don't send any content headers with 304 responses
Mads Kiilerich <madski@unity3d.com>
parents:
18354
diff
changeset
|
159 if (self.length is None and |
a4d7fd7ad1f7
serve: don't send any content headers with 304 responses
Mads Kiilerich <madski@unity3d.com>
parents:
18354
diff
changeset
|
160 saved_status[0] != common.HTTP_NOT_MODIFIED): |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
161 self._chunked = (not self.close_connection and |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
162 self.request_version == "HTTP/1.1") |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
163 if self._chunked: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
164 self.send_header('Transfer-Encoding', 'chunked') |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
165 else: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
166 self.send_header('Connection', 'close') |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
167 self.end_headers() |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
168 self.sent_headers = True |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
169 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
170 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
|
171 code, msg = http_status.split(None, 1) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
172 code = int(code) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
173 self.saved_status = http_status |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
174 bad_headers = ('connection', 'transfer-encoding') |
4633
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4534
diff
changeset
|
175 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
|
176 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
|
177 return self._write |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
178 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
179 def _write(self, data): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
180 if not self.saved_status: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
181 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
|
182 elif not self.sent_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
183 self.send_headers() |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
184 if self.length is not None: |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
185 if len(data) > self.length: |
8663
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
186 raise AssertionError("Content-length header sent, but more " |
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
187 "bytes than specified are being written.") |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
188 self.length = self.length - len(data) |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
189 elif self._chunked and data: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
190 data = '%x\r\n%s\r\n' % (len(data), data) |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
191 self.wfile.write(data) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
192 self.wfile.flush() |
136 | 193 |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
194 def _done(self): |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
195 if self._chunked: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
196 self.wfile.write('0\r\n\r\n') |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
197 self.wfile.flush() |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
198 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
199 class _httprequesthandleropenssl(_httprequesthandler): |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
200 """HTTPS handler based on pyOpenSSL""" |
4870
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
201 |
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
202 url_scheme = 'https' |
4957
cdd33a048289
removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4871
diff
changeset
|
203 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
204 @staticmethod |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
205 def preparehttpserver(httpserver, ssl_cert): |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
206 try: |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
207 import OpenSSL |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
208 OpenSSL.SSL.Context |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
209 except ImportError: |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
210 raise util.Abort(_("SSL support is unavailable")) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
211 ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
212 ctx.use_privatekey_file(ssl_cert) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
213 ctx.use_certificate_file(ssl_cert) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
214 sock = socket.socket(httpserver.address_family, httpserver.socket_type) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
215 httpserver.socket = OpenSSL.SSL.Connection(ctx, sock) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
216 httpserver.server_bind() |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
217 httpserver.server_activate() |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
218 |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
219 def setup(self): |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
220 self.connection = self.request |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
221 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
|
222 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
|
223 |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
224 def do_write(self): |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
225 import OpenSSL |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
226 try: |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
227 _httprequesthandler.do_write(self) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
228 except OpenSSL.SSL.SysCallError, inst: |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
229 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
|
230 raise |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
231 |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
232 def handle_one_request(self): |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
233 import OpenSSL |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
234 try: |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
235 _httprequesthandler.handle_one_request(self) |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
236 except (OpenSSL.SSL.SysCallError, OpenSSL.SSL.ZeroReturnError): |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
237 self.close_connection = True |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
238 pass |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
239 |
12784
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
240 class _httprequesthandlerssl(_httprequesthandler): |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
241 """HTTPS handler based on Pythons ssl module (introduced in 2.6)""" |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
242 |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
243 url_scheme = 'https' |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
244 |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
245 @staticmethod |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
246 def preparehttpserver(httpserver, ssl_cert): |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
247 try: |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
248 import ssl |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
249 ssl.wrap_socket |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
250 except ImportError: |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
251 raise util.Abort(_("SSL support is unavailable")) |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
252 httpserver.socket = ssl.wrap_socket(httpserver.socket, server_side=True, |
12797
076bbbf0ba86
hgweb: let HTTPS serve use more compatible and less secure encryption
Mads Kiilerich <mads@kiilerich.com>
parents:
12784
diff
changeset
|
253 certfile=ssl_cert, ssl_version=ssl.PROTOCOL_SSLv23) |
12784
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
254 |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
255 def setup(self): |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
256 self.connection = self.request |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
257 self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
258 self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
259 |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
260 try: |
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
261 from threading import activeCount |
15223
fc035e5370ca
pyflakes: clean up some import noise
Matt Mackall <mpm@selenic.com>
parents:
14957
diff
changeset
|
262 activeCount() # silence pyflakes |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
263 _mixin = SocketServer.ThreadingMixIn |
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
264 except ImportError: |
14957
16e5271b216f
hgweb: move remaining hasattr calls to safehasattr
Augie Fackler <durin42@gmail.com>
parents:
14764
diff
changeset
|
265 if util.safehasattr(os, "fork"): |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
266 _mixin = SocketServer.ForkingMixIn |
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
267 else: |
14764
a7d5816087a9
classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents:
14093
diff
changeset
|
268 class _mixin(object): |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
269 pass |
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
270 |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
271 def openlog(opt, default): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
272 if opt and opt != '-': |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
273 return open(opt, 'a') |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
274 return default |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
275 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
276 class MercurialHTTPServer(object, _mixin, BaseHTTPServer.HTTPServer): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
277 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
278 # SO_REUSEADDR has broken semantics on windows |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
279 if os.name == 'nt': |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
280 allow_reuse_address = 0 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
281 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
282 def __init__(self, ui, app, addr, handler, **kwargs): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
283 BaseHTTPServer.HTTPServer.__init__(self, addr, handler, **kwargs) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
284 self.daemon_threads = True |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
285 self.application = app |
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
|
286 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
287 handler.preparehttpserver(self, ui.config('web', 'certificate')) |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
288 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
289 prefix = ui.config('web', 'prefix', '') |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
290 if prefix: |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
291 prefix = '/' + prefix.strip('/') |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
292 self.prefix = prefix |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
293 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
294 alog = openlog(ui.config('web', 'accesslog', '-'), sys.stdout) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
295 elog = openlog(ui.config('web', 'errorlog', '-'), sys.stderr) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
296 self.accesslog = alog |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
297 self.errorlog = elog |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
298 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
299 self.addr, self.port = self.socket.getsockname()[0:2] |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
300 self.fqaddr = socket.getfqdn(addr[0]) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
301 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
302 class IPv6HTTPServer(MercurialHTTPServer): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
303 address_family = getattr(socket, 'AF_INET6', None) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
304 def __init__(self, *args, **kwargs): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
305 if self.address_family is None: |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
306 raise error.RepoError(_('IPv6 is not available on this system')) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
307 super(IPv6HTTPServer, self).__init__(*args, **kwargs) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
308 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
309 def create_server(ui, app): |
938 | 310 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
311 if ui.config('web', 'certificate'): |
12784
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
312 if sys.version_info >= (2, 6): |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
313 handler = _httprequesthandlerssl |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
314 else: |
763be3cd084a
hgweb: use Pythons ssl module for HTTPS serve when using Python 2.6 or later
Mads Kiilerich <mads@kiilerich.com>
parents:
12783
diff
changeset
|
315 handler = _httprequesthandleropenssl |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
316 else: |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
317 handler = _httprequesthandler |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
318 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
319 if ui.configbool('web', 'ipv6'): |
10641
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
320 cls = IPv6HTTPServer |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
321 else: |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
322 cls = MercurialHTTPServer |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
323 |
8224
1075f5c1b3fa
hgweb: pre-init mimetypes module (fixes ugly bug in python-2.6.2 mimetypes)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7928
diff
changeset
|
324 # ugly hack due to python issue5853 (for threaded use) |
1075f5c1b3fa
hgweb: pre-init mimetypes module (fixes ugly bug in python-2.6.2 mimetypes)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7928
diff
changeset
|
325 import mimetypes; mimetypes.init() |
1075f5c1b3fa
hgweb: pre-init mimetypes module (fixes ugly bug in python-2.6.2 mimetypes)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7928
diff
changeset
|
326 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
327 address = ui.config('web', 'address', '') |
12076
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
10905
diff
changeset
|
328 port = util.getport(ui.config('web', 'port', 8000)) |
3628
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
329 try: |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
330 return cls(ui, app, (address, port), handler) |
3628
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
331 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
|
332 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
|
333 % (address, port, inst.args[1])) |