Mercurial > hg-stable
annotate mercurial/hgweb/server.py @ 37150:a2566597acb5
lfs: add basic routing for the server side wire protocol processing
The recent hgweb refactoring yielded a clean point to wrap a function that could
handle this, so I moved the routing for this out of the core. While not an hg
wire protocol, this seems logically close enough. For now, these handlers do
nothing other than check permissions.
The protocol requires support for PUT requests, so that has been added to the
core, and funnels into the same handler as GET and POST. The permission
checking code was assuming that anything not checking 'pull' or None ops should
be using POST. But that breaks the upload check if it checks 'push'. So I
invented a new 'upload' permission, and used it to avoid the mandate to POST. A
function wrap point could be added, but security code should probably stay
grouped together. Given that anything not 'pull' or None was requiring POST,
the comment on hgweb.common.permhooks is probably wrong- there is no 'read'.
The rationale for the URIs is that the spec for the Batch API[1] defines the URL
as the LFS server url + '/objects/batch'. The default git URLs are:
Git remote: https://git-server.com/foo/bar
LFS server: https://git-server.com/foo/bar.git/info/lfs
Batch API: https://git-server.com/foo/bar.git/info/lfs/objects/batch
'.git/' seems like it's not something a user would normally track. If we adhere
to how git defines the URLs, then the hg-git extension should be able to talk to
a git based server without any additional work.
The URI for the transfer requests starts with '.hg/' to ensure that there are no
conflicts with tracked files. Since these are handed out by the Batch API, we
can change this at any point in the future. (Specifically, it might be a good
idea to use something under the proposed /api/ namespace.) In any case, no
files are stored at these locations in the repository directory.
I started a new module for this because it seems like a good idea to keep all of
the security sensitive server side code together. There's also an issue with
`hg verify` in that it will want to download *all* blobs in order to run.
Sadly, there's no way in the protocol to ask the server to verify the content of
a blob it may have. (The verify action is for storing files on a 3rd party
server, and then informing the LFS server when that completes.) So we may end
up implementing a custom transfer adapter that simply indicates if the blobs are
valid, and fall back to basic transfers for non-hg servers. In other words,
this code is likely to get bigger before this is made non-experimental.
[1] https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 17 Mar 2018 01:23:01 -0400 |
parents | 5890e5872f36 |
children | b5ca5d34fe8d |
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 |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
9 from __future__ import absolute_import |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
10 |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
11 import errno |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
12 import os |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
13 import socket |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
14 import sys |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
15 import traceback |
36811
8e1556ac01bb
hgweb: validate WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
16 import wsgiref.validate |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
17 |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
18 from ..i18n import _ |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
19 |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
20 from .. import ( |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
21 encoding, |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
22 error, |
30644
d524c88511a7
py3: replace os.name with pycompat.osname (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30278
diff
changeset
|
23 pycompat, |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
24 util, |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
25 ) |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
26 |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29555
diff
changeset
|
27 httpservermod = util.httpserver |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28883
diff
changeset
|
28 socketserver = util.socketserver |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
27046
diff
changeset
|
29 urlerr = util.urlerr |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
27046
diff
changeset
|
30 urlreq = util.urlreq |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
27046
diff
changeset
|
31 |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
32 from . import ( |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
33 common, |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
34 ) |
138 | 35 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
36 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
|
37 """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
|
38 |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
39 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
|
40 not. |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
41 """ |
34700
8e5132ece156
hgweb: more native string treatment in query string parsing
Augie Fackler <augie@google.com>
parents:
34645
diff
changeset
|
42 if r'?' in uri: |
8e5132ece156
hgweb: more native string treatment in query string parsing
Augie Fackler <augie@google.com>
parents:
34645
diff
changeset
|
43 path, query = uri.split(r'?', 1) |
2123
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
44 else: |
34700
8e5132ece156
hgweb: more native string treatment in query string parsing
Augie Fackler <augie@google.com>
parents:
34645
diff
changeset
|
45 path, query = uri, r'' |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
27046
diff
changeset
|
46 return urlreq.unquote(path), query |
2123
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
47 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
48 class _error_logger(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
49 def __init__(self, handler): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
50 self.handler = handler |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
51 def flush(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
52 pass |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
53 def write(self, str): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
54 self.writelines(str.split('\n')) |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
55 def writelines(self, seq): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
56 for msg in seq: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
57 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
|
58 |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29555
diff
changeset
|
59 class _httprequesthandler(httpservermod.basehttprequesthandler): |
4870
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
60 |
8f430b1b3025
Make hg serve set the wsgi.url_scheme property correctly.
Wesley J. Landaker <wjl@icecavern.net>
parents:
4868
diff
changeset
|
61 url_scheme = 'http' |
4957
cdd33a048289
removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4871
diff
changeset
|
62 |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
63 @staticmethod |
29553
cd3e58862cab
hgweb: pass ui into preparehttpserver
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29433
diff
changeset
|
64 def preparehttpserver(httpserver, ui): |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
65 """Prepare .socket of new HTTPServer instance""" |
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
66 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
67 def __init__(self, *args, **kargs): |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
68 self.protocol_version = r'HTTP/1.1' |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29555
diff
changeset
|
69 httpservermod.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
|
70 |
5549
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
71 def _log_any(self, fp, format, *args): |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
72 fp.write(pycompat.sysbytes( |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
73 r"%s - - [%s] %s" % (self.client_address[0], |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
74 self.log_date_time_string(), |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
75 format % args)) + '\n') |
5549
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
76 fp.flush() |
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
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 log_error(self, format, *args): |
5549
f2f42262adbd
hgweb.server: flush log files after every access
Patrick Mezard <pmezard@gmail.com>
parents:
5150
diff
changeset
|
79 self._log_any(self.server.errorlog, format, *args) |
131 | 80 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
81 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
|
82 self._log_any(self.server.accesslog, format, *args) |
538 | 83 |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
84 def log_request(self, code=r'-', size=r'-'): |
19877
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
85 xheaders = [] |
52ed85d9ac26
hgweb: log headers only if headers were successfully parsed
David Soria Parra <dsp@experimentalworks.net>
parents:
18380
diff
changeset
|
86 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
|
87 xheaders = [h for h in self.headers.items() |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
88 if h[0].startswith(r'x-')] |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
89 self.log_message(r'"%s" %s %s%s', |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
90 self.requestline, str(code), str(size), |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
91 r''.join([r' %s:%s' % h for h in sorted(xheaders)])) |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13570
diff
changeset
|
92 |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
93 def do_write(self): |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
94 try: |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
95 self.do_hgweb() |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23409
diff
changeset
|
96 except socket.error as inst: |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
97 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
|
98 raise |
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
99 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
100 def do_POST(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
101 try: |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
102 self.do_write() |
13443
8fa83d7159eb
serve: catch and log all Exceptions, not only StandardException
Mads Kiilerich <mads@kiilerich.com>
parents:
12797
diff
changeset
|
103 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
|
104 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
|
105 self._write("Internal Server Error") |
23409
dc4d2cd3aa3e
hgweb: send proper HTTP response after uncaught exception
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23070
diff
changeset
|
106 self._done() |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
107 tb = r"".join(traceback.format_exception(*sys.exc_info())) |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
108 # We need a native-string newline to poke in the log |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
109 # message, because we won't get a newline when using an |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
110 # r-string. This is the easy way out. |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
111 newline = chr(10) |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
112 self.log_error(r"Exception happened during processing " |
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
113 r"request '%s':%s%s", self.path, newline, tb) |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
114 |
37150
a2566597acb5
lfs: add basic routing for the server side wire protocol processing
Matt Harbison <matt_harbison@yahoo.com>
parents:
37012
diff
changeset
|
115 def do_PUT(self): |
a2566597acb5
lfs: add basic routing for the server side wire protocol processing
Matt Harbison <matt_harbison@yahoo.com>
parents:
37012
diff
changeset
|
116 self.do_POST() |
a2566597acb5
lfs: add basic routing for the server side wire protocol processing
Matt Harbison <matt_harbison@yahoo.com>
parents:
37012
diff
changeset
|
117 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
118 def do_GET(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
119 self.do_POST() |
131 | 120 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
121 def do_hgweb(self): |
34720
60e7da55e5e6
hgweb: set sent_headers attr as early as practical
Augie Fackler <augie@google.com>
parents:
34719
diff
changeset
|
122 self.sent_headers = False |
5835
bd34f0ac3cb0
adding "prefix" option to "hg serve" (command line and [web] section)
Michele Cella <michele.cella@gmail.com>
parents:
5690
diff
changeset
|
123 path, query = _splitURI(self.path) |
138 | 124 |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
125 env = {} |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
126 env[r'GATEWAY_INTERFACE'] = r'CGI/1.1' |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
127 env[r'REQUEST_METHOD'] = self.command |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
128 env[r'SERVER_NAME'] = self.server.server_name |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
129 env[r'SERVER_PORT'] = str(self.server.server_port) |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
130 env[r'REQUEST_URI'] = self.path |
36810
7fc80c982656
hgweb: ensure all wsgi environment values are str
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36787
diff
changeset
|
131 env[r'SCRIPT_NAME'] = pycompat.sysstr(self.server.prefix) |
7fc80c982656
hgweb: ensure all wsgi environment values are str
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36787
diff
changeset
|
132 env[r'PATH_INFO'] = pycompat.sysstr(path[len(self.server.prefix):]) |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
133 env[r'REMOTE_HOST'] = self.client_address[0] |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
134 env[r'REMOTE_ADDR'] = self.client_address[0] |
36811
8e1556ac01bb
hgweb: validate WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
135 env[r'QUERY_STRING'] = query or r'' |
1579
85803ec2daab
Remove tabs, and trailing whitespace from hgweb.py
Josef "Jeff" Sipek <jeffpc@optonline.net>
parents:
1575
diff
changeset
|
136 |
34719
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
137 if pycompat.ispy3: |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
138 if self.headers.get_content_type() is None: |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
139 env[r'CONTENT_TYPE'] = self.headers.get_default_type() |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
140 else: |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
141 env[r'CONTENT_TYPE'] = self.headers.get_content_type() |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
142 length = self.headers.get('content-length') |
6e1b05692b2c
hgweb: detect Python 3-era libraries and use modern attribute names
Augie Fackler <augie@google.com>
parents:
34718
diff
changeset
|
143 else: |
34718
01206460897a
server: indent block that's about to get conditionalized
Augie Fackler <augie@google.com>
parents:
34706
diff
changeset
|
144 if self.headers.typeheader is None: |
01206460897a
server: indent block that's about to get conditionalized
Augie Fackler <augie@google.com>
parents:
34706
diff
changeset
|
145 env[r'CONTENT_TYPE'] = self.headers.type |
01206460897a
server: indent block that's about to get conditionalized
Augie Fackler <augie@google.com>
parents:
34706
diff
changeset
|
146 else: |
01206460897a
server: indent block that's about to get conditionalized
Augie Fackler <augie@google.com>
parents:
34706
diff
changeset
|
147 env[r'CONTENT_TYPE'] = self.headers.typeheader |
01206460897a
server: indent block that's about to get conditionalized
Augie Fackler <augie@google.com>
parents:
34706
diff
changeset
|
148 length = self.headers.getheader('content-length') |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
149 if length: |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
150 env[r'CONTENT_LENGTH'] = length |
4633
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4534
diff
changeset
|
151 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
|
152 if h not in ('content-type', 'content-length')]: |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
153 hkey = r'HTTP_' + header.replace(r'-', r'_').upper() |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
154 hval = self.headers.get(header) |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
155 hval = hval.replace(r'\n', r'').strip() |
2505
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
156 if hval: |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
157 env[hkey] = hval |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
158 env[r'SERVER_PROTOCOL'] = self.request_version |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
159 env[r'wsgi.version'] = (1, 0) |
36810
7fc80c982656
hgweb: ensure all wsgi environment values are str
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36787
diff
changeset
|
160 env[r'wsgi.url_scheme'] = pycompat.sysstr(self.url_scheme) |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
161 if env.get(r'HTTP_EXPECT', '').lower() == '100-continue': |
13570
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
13443
diff
changeset
|
162 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
|
163 |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
164 env[r'wsgi.input'] = self.rfile |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
165 env[r'wsgi.errors'] = _error_logger(self) |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
166 env[r'wsgi.multithread'] = isinstance(self.server, |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28883
diff
changeset
|
167 socketserver.ThreadingMixIn) |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
168 env[r'wsgi.multiprocess'] = isinstance(self.server, |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28883
diff
changeset
|
169 socketserver.ForkingMixIn) |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34377
diff
changeset
|
170 env[r'wsgi.run_once'] = 0 |
601 | 171 |
36811
8e1556ac01bb
hgweb: validate WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
172 wsgiref.validate.check_environ(env) |
8e1556ac01bb
hgweb: validate WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
173 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
174 self.saved_status = None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
175 self.saved_headers = [] |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
176 self.length = None |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
177 self._chunked = None |
6784
18c429ea3a0e
hgweb: all protocol functions have become generators
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6403
diff
changeset
|
178 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
|
179 self._write(chunk) |
18349
c007e5c54b16
serve: send response headers even if response has no body
Mads Kiilerich <mads@kiilerich.com>
parents:
17427
diff
changeset
|
180 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
|
181 self.send_headers() |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
182 self._done() |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
183 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
184 def send_headers(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
185 if not self.saved_status: |
8663
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
186 raise AssertionError("Sending headers before " |
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
187 "start_response() called") |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
188 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
|
189 saved_status[0] = int(saved_status[0]) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
190 self.send_response(*saved_status) |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
191 self.length = None |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
192 self._chunked = False |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
193 for h in self.saved_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
194 self.send_header(*h) |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
195 if h[0].lower() == 'content-length': |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 self._chunked = (not self.close_connection and |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
200 self.request_version == "HTTP/1.1") |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
201 if self._chunked: |
34740
b2601c5977a4
hgweb: more "http headers are native strs" cleanup
Augie Fackler <augie@google.com>
parents:
34720
diff
changeset
|
202 self.send_header(r'Transfer-Encoding', r'chunked') |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
203 else: |
34740
b2601c5977a4
hgweb: more "http headers are native strs" cleanup
Augie Fackler <augie@google.com>
parents:
34720
diff
changeset
|
204 self.send_header(r'Connection', r'close') |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
205 self.end_headers() |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
206 self.sent_headers = True |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
207 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
208 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
|
209 code, msg = http_status.split(None, 1) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
210 code = int(code) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
211 self.saved_status = http_status |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
212 bad_headers = ('connection', 'transfer-encoding') |
4633
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4534
diff
changeset
|
213 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
|
214 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
|
215 return self._write |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
216 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
217 def _write(self, data): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
218 if not self.saved_status: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
219 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
|
220 elif not self.sent_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
221 self.send_headers() |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
222 if self.length is not None: |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
223 if len(data) > self.length: |
8663
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
224 raise AssertionError("Content-length header sent, but more " |
45f626a39def
wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
225 "bytes than specified are being written.") |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
226 self.length = self.length - len(data) |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
227 elif self._chunked and data: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
228 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
|
229 self.wfile.write(data) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
230 self.wfile.flush() |
136 | 231 |
18354
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
232 def _done(self): |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
233 if self._chunked: |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
234 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
|
235 self.wfile.flush() |
cf5c76017e11
serve: use chunked encoding in hgweb responses
Mads Kiilerich <mads@kiilerich.com>
parents:
18353
diff
changeset
|
236 |
37012
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
237 def version_string(self): |
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
238 if self.server.serverheader: |
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
239 return self.server.serverheader |
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
240 return httpservermod.basehttprequesthandler.version_string(self) |
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
241 |
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
|
242 class _httprequesthandlerssl(_httprequesthandler): |
26202
04af43009c8b
hgweb.server: fix _httprequesthandlerssl help text
timeless@mozdev.org
parents:
25660
diff
changeset
|
243 """HTTPS handler based on Python's ssl module""" |
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
|
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 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
|
246 |
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 @staticmethod |
29553
cd3e58862cab
hgweb: pass ui into preparehttpserver
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29433
diff
changeset
|
248 def preparehttpserver(httpserver, ui): |
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
|
249 try: |
29555
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
250 from .. import sslutil |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
251 sslutil.modernssl |
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
|
252 except ImportError: |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26202
diff
changeset
|
253 raise error.Abort(_("SSL support is unavailable")) |
29553
cd3e58862cab
hgweb: pass ui into preparehttpserver
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29433
diff
changeset
|
254 |
cd3e58862cab
hgweb: pass ui into preparehttpserver
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29433
diff
changeset
|
255 certfile = ui.config('web', 'certificate') |
29555
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
256 |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
257 # These config options are currently only meant for testing. Use |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
258 # at your own risk. |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
259 cafile = ui.config('devel', 'servercafile') |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
260 reqcert = ui.configbool('devel', 'serverrequirecert') |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
261 |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
262 httpserver.socket = sslutil.wrapserversocket(httpserver.socket, |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
263 ui, |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
264 certfile=certfile, |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
265 cafile=cafile, |
121d11814c62
hgweb: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29553
diff
changeset
|
266 requireclientcert=reqcert) |
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
|
267 |
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
|
268 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
|
269 self.connection = self.request |
36787
bf9a04d78084
hgweb: adapt to socket._fileobject changes in Python 3
Augie Fackler <augie@google.com>
parents:
36290
diff
changeset
|
270 self.rfile = self.request.makefile(r"rb", self.rbufsize) |
bf9a04d78084
hgweb: adapt to socket._fileobject changes in Python 3
Augie Fackler <augie@google.com>
parents:
36290
diff
changeset
|
271 self.wfile = self.request.makefile(r"wb", self.wbufsize) |
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
|
272 |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
273 try: |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
274 import threading |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26848
diff
changeset
|
275 threading.activeCount() # silence pyflakes and bypass demandimport |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28883
diff
changeset
|
276 _mixin = socketserver.ThreadingMixIn |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
277 except ImportError: |
14957
16e5271b216f
hgweb: move remaining hasattr calls to safehasattr
Augie Fackler <durin42@gmail.com>
parents:
14764
diff
changeset
|
278 if util.safehasattr(os, "fork"): |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28883
diff
changeset
|
279 _mixin = socketserver.ForkingMixIn |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
280 else: |
14764
a7d5816087a9
classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents:
14093
diff
changeset
|
281 class _mixin(object): |
10639
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
282 pass |
a6808629f450
server: externalize and streamline mixin setup
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10638
diff
changeset
|
283 |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
284 def openlog(opt, default): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
285 if opt and opt != '-': |
36290
46c97973ee46
hgweb: open server logs in binary mode
Augie Fackler <augie@google.com>
parents:
34740
diff
changeset
|
286 return open(opt, 'ab') |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
287 return default |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
288 |
30082
ebc03e64548a
hgweb: fix the MRO in Python 3
Martijn Pieters <mjpieters@fb.com>
parents:
29566
diff
changeset
|
289 class MercurialHTTPServer(_mixin, httpservermod.httpserver, object): |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
290 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
291 # SO_REUSEADDR has broken semantics on windows |
34645 | 292 if pycompat.iswindows: |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
293 allow_reuse_address = 0 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
294 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
295 def __init__(self, ui, app, addr, handler, **kwargs): |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29555
diff
changeset
|
296 httpservermod.httpserver.__init__(self, addr, handler, **kwargs) |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
297 self.daemon_threads = True |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
298 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
|
299 |
29553
cd3e58862cab
hgweb: pass ui into preparehttpserver
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29433
diff
changeset
|
300 handler.preparehttpserver(self, ui) |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
301 |
34250
d24816dfdcff
configitems: register the 'web.prefix' config
Boris Feld <boris.feld@octobus.net>
parents:
34249
diff
changeset
|
302 prefix = ui.config('web', 'prefix') |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
303 if prefix: |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
304 prefix = '/' + prefix.strip('/') |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
305 self.prefix = prefix |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
306 |
34234
ac96ff471c9a
configitems: register the 'web.accesslog' config
Boris Feld <boris.feld@octobus.net>
parents:
30644
diff
changeset
|
307 alog = openlog(ui.config('web', 'accesslog'), ui.fout) |
34247
c97a750c28a5
configitems: register the 'web.errorlog' config
Boris Feld <boris.feld@octobus.net>
parents:
34235
diff
changeset
|
308 elog = openlog(ui.config('web', 'errorlog'), ui.ferr) |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
309 self.accesslog = alog |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
310 self.errorlog = elog |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
311 |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
312 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
|
313 self.fqaddr = socket.getfqdn(addr[0]) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
314 |
37012
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
315 self.serverheader = ui.config('web', 'server-header') |
5890e5872f36
hgweb: allow defining Server response header for HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
316 |
10643
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
317 class IPv6HTTPServer(MercurialHTTPServer): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
318 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
|
319 def __init__(self, *args, **kwargs): |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
320 if self.address_family is None: |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
321 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
|
322 super(IPv6HTTPServer, self).__init__(*args, **kwargs) |
1874697a8863
server: unnest server classes into module namespace
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10642
diff
changeset
|
323 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
324 def create_server(ui, app): |
938 | 325 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
326 if ui.config('web', 'certificate'): |
26848
d962e955da08
hgweb.server: drop support for Python 2.4
Siddharth Agarwal <sid0@fb.com>
parents:
26587
diff
changeset
|
327 handler = _httprequesthandlerssl |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
328 else: |
12783
191d0fd5c2fd
hgweb: refactor all pyOpenSSL references into one class
Mads Kiilerich <mads@kiilerich.com>
parents:
12740
diff
changeset
|
329 handler = _httprequesthandler |
4860
f3802f9f1840
Add SSL support to hg serve, activated via --certificate option
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
330 |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
331 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
|
332 cls = IPv6HTTPServer |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
333 else: |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
334 cls = MercurialHTTPServer |
dedf88fe945a
server: abstract setup of ipv6 vs. normal server
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10640
diff
changeset
|
335 |
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
|
336 # ugly hack due to python issue5853 (for threaded use) |
20357
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
337 try: |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
338 import mimetypes |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
339 mimetypes.init() |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
340 except UnicodeDecodeError: |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
341 # Python 2.x's mimetypes module attempts to decode strings |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
342 # from Windows' ANSI APIs as ascii (fail), then re-encode them |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
343 # as ascii (clown fail), because the default Python Unicode |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
344 # codec is hardcoded as ascii. |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
345 |
20529
ca970d6acedb
hgweb: make sure sys module is loaded prior to reload hack
Yuya Nishihara <yuya@tcha.org>
parents:
20357
diff
changeset
|
346 sys.argv # unwrap demand-loader so that reload() works |
20357
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
347 reload(sys) # resurrect sys.setdefaultencoding() |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
348 oldenc = sys.getdefaultencoding() |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
349 sys.setdefaultencoding("latin1") # or any full 8-bit encoding |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
350 mimetypes.init() |
6863d42eb59a
hgweb: hack around mimetypes encoding thinko (issue4160)
Matt Mackall <mpm@selenic.com>
parents:
19877
diff
changeset
|
351 sys.setdefaultencoding(oldenc) |
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
|
352 |
34235
af4f0c74f8b5
configitems: register the 'web.address' config
Boris Feld <boris.feld@octobus.net>
parents:
34234
diff
changeset
|
353 address = ui.config('web', 'address') |
34249
e2d633f8ee65
configitems: register the 'web.port' config
Boris Feld <boris.feld@octobus.net>
parents:
34247
diff
changeset
|
354 port = util.getport(ui.config('web', 'port')) |
3628
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
355 try: |
10644
63948e7d37f7
server: initialize wsgi app in command, then wrap server around it
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents:
10643
diff
changeset
|
356 return cls(ui, app, (address, port), handler) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23409
diff
changeset
|
357 except socket.error as inst: |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26202
diff
changeset
|
358 raise error.Abort(_("cannot start server at '%s:%d': %s") |
34706
8782076874f5
hgweb: fix logging to use native strings as appropriate
Augie Fackler <augie@google.com>
parents:
34700
diff
changeset
|
359 % (address, port, encoding.strtolocal(inst.args[1]))) |