author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Tue, 20 Jun 2006 15:14:12 -0700 | |
changeset 2463 | 6ab016edd5c4 |
parent 2434 | a2df85adface |
child 2464 | 09b1c9ef317c |
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/request.py - An http request from either CGI or the standalone 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> |
575 | 4 |
# Copyright 2005 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 |
2394
a8f1049d1d2d
hgweb: fix errors and warnings found by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2391
diff
changeset
|
10 |
demandload(globals(), "socket sys cgi os errno") |
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
|
11 |
from mercurial.i18n import gettext as _ |
138 | 12 |
|
1559
59b3639df0a9
Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents:
1554
diff
changeset
|
13 |
class hgrequest(object): |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
14 |
def __init__(self, inp=None, out=None, env=None): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
15 |
self.inp = inp or sys.stdin |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
16 |
self.out = out or sys.stdout |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
17 |
self.env = env or os.environ |
1407
db571bcaa35d
allow empty values for url so we can have /?tip
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1406
diff
changeset
|
18 |
self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
19 |
self.will_close = True |
131 | 20 |
|
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
21 |
def write(self, *things): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
22 |
for thing in things: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
23 |
if hasattr(thing, "__iter__"): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
24 |
for part in thing: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
25 |
self.write(part) |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
26 |
else: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
27 |
try: |
1160
0da98529a476
Fix TypeError
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1159
diff
changeset
|
28 |
self.out.write(str(thing)) |
1174
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
29 |
except socket.error, inst: |
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
30 |
if inst[0] != errno.ECONNRESET: |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
31 |
raise |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
32 |
|
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
33 |
def done(self): |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
34 |
if self.will_close: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
35 |
self.inp.close() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
36 |
self.out.close() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
37 |
else: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
38 |
self.out.flush() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
39 |
|
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
40 |
def header(self, headers=[('Content-type','text/html')]): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
41 |
for header in headers: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
42 |
self.out.write("%s: %s\r\n" % header) |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
43 |
self.out.write("\r\n") |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
44 |
|
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
45 |
def httphdr(self, type, filename=None, length=0): |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
46 |
|
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
47 |
headers = [('Content-type', type)] |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
48 |
if filename: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
49 |
headers.append(('Content-disposition', 'attachment; filename=%s' % |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
50 |
filename)) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
51 |
# we do not yet support http 1.1 chunked transfer, so we have |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
52 |
# to force connection to close if content-length not known |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
53 |
if length: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
54 |
headers.append(('Content-length', str(length))) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
55 |
self.will_close = False |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
56 |
else: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
57 |
headers.append(('Connection', 'close')) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
58 |
self.will_close = True |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
59 |
self.header(headers) |