author | Matt Mackall <mpm@selenic.com> |
Wed, 15 Nov 2006 15:51:58 -0600 | |
changeset 3652 | f4dece0f7016 |
parent 3628 | dc3504af7722 |
child 3673 | eb0b4a2d70a9 |
permissions | -rw-r--r-- |
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> |
2859 | 4 |
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
131 | 5 |
# |
6 |
# This software may be used and distributed according to the terms |
|
7 |
# of the GNU General Public License, incorporated herein by reference. |
|
8 |
||
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
9 |
from mercurial.demandload import demandload |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
10 |
import os, sys, errno |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
11 |
demandload(globals(), "urllib BaseHTTPServer socket SocketServer") |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
12 |
demandload(globals(), "mercurial:ui,hg,util,templater") |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
13 |
demandload(globals(), "hgweb_mod:hgweb hgwebdir_mod:hgwebdir request:wsgiapplication") |
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
14 |
from mercurial.i18n import gettext as _ |
138 | 15 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
16 |
def _splitURI(uri): |
2123
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
17 |
""" Return path and query splited from uri |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
18 |
|
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
19 |
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
|
20 |
not. |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
21 |
""" |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
22 |
if '?' in uri: |
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.split('?', 1) |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
24 |
else: |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
25 |
path, query = uri, '' |
c0729a7f6f8a
Fixed path handling of the standalone server, fixed typo.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2122
diff
changeset
|
26 |
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
|
27 |
|
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
28 |
class _error_logger(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
29 |
def __init__(self, handler): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
30 |
self.handler = handler |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
31 |
def flush(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
32 |
pass |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
33 |
def write(self, str): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
34 |
self.writelines(str.split('\n')) |
3130
2e043c9a38a6
hgweb: fix errors spotted by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3079
diff
changeset
|
35 |
def writelines(self, seq): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
36 |
for msg in seq: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
37 |
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
|
38 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
39 |
class _hgwebhandler(object, BaseHTTPServer.BaseHTTPRequestHandler): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
40 |
def __init__(self, *args, **kargs): |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2404
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
44 |
def log_error(self, format, *args): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
45 |
errorlog = self.server.errorlog |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
46 |
errorlog.write("%s - - [%s] %s\n" % (self.address_string(), |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
47 |
self.log_date_time_string(), |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
48 |
format % args)) |
131 | 49 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
50 |
def log_message(self, format, *args): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
51 |
accesslog = self.server.accesslog |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
52 |
accesslog.write("%s - - [%s] %s\n" % (self.address_string(), |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
53 |
self.log_date_time_string(), |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
54 |
format % args)) |
538 | 55 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
56 |
def do_POST(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
57 |
try: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
58 |
self.do_hgweb() |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
59 |
except socket.error, inst: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
60 |
if inst[0] != errno.EPIPE: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
61 |
raise |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
62 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
63 |
def do_GET(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
64 |
self.do_POST() |
131 | 65 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
66 |
def do_hgweb(self): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
67 |
path_info, query = _splitURI(self.path) |
138 | 68 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
69 |
env = {} |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
70 |
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
|
71 |
env['REQUEST_METHOD'] = self.command |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
env['REQUEST_URI'] = self.path |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
75 |
env['PATH_INFO'] = path_info |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
76 |
if query: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
77 |
env['QUERY_STRING'] = query |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
78 |
host = self.address_string() |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
79 |
if host != self.client_address[0]: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
80 |
env['REMOTE_HOST'] = host |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
81 |
env['REMOTE_ADDR'] = self.client_address[0] |
1579
85803ec2daab
Remove tabs, and trailing whitespace from hgweb.py
Josef "Jeff" Sipek <jeffpc@optonline.net>
parents:
1575
diff
changeset
|
82 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
else: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
if length: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
89 |
env['CONTENT_LENGTH'] = length |
2505
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
90 |
for header in [h for h in self.headers.keys() \ |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
if hval: |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
96 |
env[hkey] = hval |
01b856927970
Fix server to set up a more WSGI compliant environment.
Eric Hopper <hopper@omnifarious.org>
parents:
2434
diff
changeset
|
97 |
env['SERVER_PROTOCOL'] = self.request_version |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
98 |
env['wsgi.version'] = (1, 0) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
99 |
env['wsgi.url_scheme'] = 'http' |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
100 |
env['wsgi.input'] = self.rfile |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
101 |
env['wsgi.errors'] = _error_logger(self) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
102 |
env['wsgi.multithread'] = isinstance(self.server, |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
103 |
SocketServer.ThreadingMixIn) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
104 |
env['wsgi.multiprocess'] = isinstance(self.server, |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
105 |
SocketServer.ForkingMixIn) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
106 |
env['wsgi.run_once'] = 0 |
601 | 107 |
|
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
108 |
self.close_connection = True |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
109 |
self.saved_status = None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
110 |
self.saved_headers = [] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
111 |
self.sent_headers = False |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
112 |
self.length = None |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
113 |
req = self.server.reqmaker(env, self._start_response) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
114 |
for data in req: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
115 |
if data: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
116 |
self._write(data) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
117 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
118 |
def send_headers(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
119 |
if not self.saved_status: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
120 |
raise AssertionError("Sending headers before start_response() called") |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
121 |
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
|
122 |
saved_status[0] = int(saved_status[0]) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
123 |
self.send_response(*saved_status) |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
124 |
should_close = True |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
125 |
for h in self.saved_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
126 |
self.send_header(*h) |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
127 |
if h[0].lower() == 'content-length': |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
128 |
should_close = False |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
129 |
self.length = int(h[1]) |
2582
276de216d2c5
Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2508
diff
changeset
|
130 |
# The value of the Connection header is a list of case-insensitive |
276de216d2c5
Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2508
diff
changeset
|
131 |
# tokens separated by commas and optional whitespace. |
2600
c4325f0a9b91
clean up trailing white space.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2582
diff
changeset
|
132 |
if 'close' in [token.strip().lower() for token in |
2582
276de216d2c5
Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2508
diff
changeset
|
133 |
self.headers.get('connection', '').split(',')]: |
276de216d2c5
Respect "Connection: close" headers sent by HTTP clients.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2508
diff
changeset
|
134 |
should_close = True |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
135 |
if should_close: |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
136 |
self.send_header('Connection', 'close') |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
137 |
self.close_connection = should_close |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
138 |
self.end_headers() |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
139 |
self.sent_headers = True |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
140 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
141 |
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
|
142 |
code, msg = http_status.split(None, 1) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
143 |
code = int(code) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
144 |
self.saved_status = http_status |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
145 |
bad_headers = ('connection', 'transfer-encoding') |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
146 |
self.saved_headers = [ h for h in headers \ |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
147 |
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
|
148 |
return self._write |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
149 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
150 |
def _write(self, data): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
151 |
if not self.saved_status: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
152 |
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
|
153 |
elif not self.sent_headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
154 |
self.send_headers() |
2508
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
155 |
if self.length is not None: |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
156 |
if len(data) > self.length: |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
157 |
raise AssertionError("Content-length header sent, but more bytes than specified are being written.") |
ab460a3f0e3a
Put support for persistent connections back in.
Eric Hopper <hopper@omnifarious.org>
parents:
2507
diff
changeset
|
158 |
self.length = self.length - len(data) |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
159 |
self.wfile.write(data) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
160 |
self.wfile.flush() |
136 | 161 |
|
2392
8238a3f039e6
Adjusting hgweb splitup to be a little cleaner.
Eric Hopper <hopper@omnifarious.org>
parents:
2391
diff
changeset
|
162 |
def create_server(ui, repo): |
2121
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
163 |
use_threads = True |
2124
27fd8b7a6c51
Cleaned trailing whitespace in hgweb.py, removed command line shortcut for webdir-conf.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2123
diff
changeset
|
164 |
|
938 | 165 |
def openlog(opt, default): |
166 |
if opt and opt != '-': |
|
167 |
return open(opt, 'w') |
|
168 |
return default |
|
169 |
||
2127
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
170 |
address = ui.config("web", "address", "") |
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
171 |
port = int(ui.config("web", "port", 8000)) |
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
172 |
use_ipv6 = ui.configbool("web", "ipv6") |
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
173 |
webdir_conf = ui.config("web", "webdir_conf") |
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
174 |
accesslog = openlog(ui.config("web", "accesslog", "-"), sys.stdout) |
8a85dbbadddf
Allow 'hg serve --webdir-conf foo' to be run outside a repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2124
diff
changeset
|
175 |
errorlog = openlog(ui.config("web", "errorlog", "-"), sys.stderr) |
2124
27fd8b7a6c51
Cleaned trailing whitespace in hgweb.py, removed command line shortcut for webdir-conf.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2123
diff
changeset
|
176 |
|
2121
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
177 |
if use_threads: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
178 |
try: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
179 |
from threading import activeCount |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
180 |
except ImportError: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
181 |
use_threads = False |
938 | 182 |
|
2121
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
183 |
if use_threads: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
184 |
_mixin = SocketServer.ThreadingMixIn |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
185 |
else: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
186 |
if hasattr(os, "fork"): |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
187 |
_mixin = SocketServer.ForkingMixIn |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
188 |
else: |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
189 |
class _mixin: pass |
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
190 |
|
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
191 |
class MercurialHTTPServer(object, _mixin, BaseHTTPServer.HTTPServer): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
192 |
def __init__(self, *args, **kargs): |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
193 |
BaseHTTPServer.HTTPServer.__init__(self, *args, **kargs) |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
194 |
self.accesslog = accesslog |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
195 |
self.errorlog = errorlog |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
196 |
self.repo = repo |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
197 |
self.webdir_conf = webdir_conf |
2392
8238a3f039e6
Adjusting hgweb splitup to be a little cleaner.
Eric Hopper <hopper@omnifarious.org>
parents:
2391
diff
changeset
|
198 |
self.webdirmaker = hgwebdir |
8238a3f039e6
Adjusting hgweb splitup to be a little cleaner.
Eric Hopper <hopper@omnifarious.org>
parents:
2391
diff
changeset
|
199 |
self.repoviewmaker = hgweb |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
200 |
self.reqmaker = wsgiapplication(self.make_handler) |
2650
56e98084e040
Make hgweb threads into daemon threads.
Brendan Cully <brendan@kublai.com>
parents:
2600
diff
changeset
|
201 |
self.daemon_threads = True |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
202 |
|
3628
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
203 |
addr, port = self.socket.getsockname() |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
204 |
if addr == '0.0.0.0': |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
205 |
addr = socket.gethostname() |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
206 |
else: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
207 |
try: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
208 |
addr = socket.gethostbyaddr(addr)[0] |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
209 |
except socket.error: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
210 |
pass |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
211 |
self.addr, self.port = addr, port |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
212 |
|
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
213 |
def make_handler(self): |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
214 |
if self.webdir_conf: |
2404
ffc3b2f1ab6a
Fixed 'hg serve --webdir-conf foo' which broke due to split of hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2392
diff
changeset
|
215 |
hgwebobj = self.webdirmaker(self.webdir_conf) |
2355
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
216 |
elif self.repo is not None: |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
217 |
hgwebobj = self.repoviewmaker(repo.__class__(repo.ui, |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
218 |
repo.origroot)) |
eb08fb4d41e1
Splitting up hgweb so it's easier to change.
Eric Hopper <hopper@omnifarious.org>
parents:
2328
diff
changeset
|
219 |
else: |
3079
4c9fcb5e3b82
If local repository is missing, make error message clearer.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2859
diff
changeset
|
220 |
raise hg.RepoError(_("There is no Mercurial repository here" |
4c9fcb5e3b82
If local repository is missing, make error message clearer.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2859
diff
changeset
|
221 |
" (.hg not found)")) |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2505
diff
changeset
|
222 |
return hgwebobj |
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
|
223 |
|
2121
150cdd6c3c90
Added threading support to hg serve.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
2119
diff
changeset
|
224 |
class IPv6HTTPServer(MercurialHTTPServer): |
881
16ce690c411d
Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents:
858
diff
changeset
|
225 |
address_family = getattr(socket, 'AF_INET6', None) |
16ce690c411d
Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents:
858
diff
changeset
|
226 |
|
16ce690c411d
Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents:
858
diff
changeset
|
227 |
def __init__(self, *args, **kwargs): |
16ce690c411d
Fix problem with "hg serve" on systems not providing IPv6.
Bryan O'Sullivan <bos@serpentine.com>
parents:
858
diff
changeset
|
228 |
if self.address_family is None: |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
229 |
raise hg.RepoError(_('IPv6 not available on this system')) |
2507
7e01da2bc7f3
Fix two small bugs that would've prevented the web interface and IPv6
Eric Hopper <hopper@omnifarious.org>
parents:
2506
diff
changeset
|
230 |
super(IPv6HTTPServer, self).__init__(*args, **kwargs) |
158
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
231 |
|
3628
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
232 |
try: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
233 |
if use_ipv6: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
234 |
return IPv6HTTPServer((address, port), _hgwebhandler) |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
235 |
else: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
236 |
return MercurialHTTPServer((address, port), _hgwebhandler) |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
237 |
except socket.error, inst: |
dc3504af7722
hgweb: internalize some socket details
Matt Mackall <mpm@selenic.com>
parents:
3263
diff
changeset
|
238 |
raise util.Abort(_('cannot start server: %s') % inst.args[1]) |