Mercurial > hg
annotate mercurial/hgweb/request.py @ 36917:7affcabf561e
dagop: move annotateline and _annotatepair from context.py
The annotate logic is large. Let's move it out of the context module, which
is basically an abstraction layer of repository operations.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 28 Feb 2018 15:09:05 -0500 |
parents | f0a851542a05 |
children | 44467a4d472f |
rev | line source |
---|---|
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> |
2859 | 4 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
131 | 5 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7742
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:
26846
diff
changeset
|
9 from __future__ import absolute_import |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26846
diff
changeset
|
10 |
36814
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
11 import wsgiref.headers as wsgiheaders |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
12 #import wsgiref.validate |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26846
diff
changeset
|
13 |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
14 from ..thirdparty import ( |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
15 attr, |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
16 ) |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26846
diff
changeset
|
17 from .. import ( |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
18 error, |
34514
528b21b853aa
request: coerce content-type to native str
Augie Fackler <augie@google.com>
parents:
34513
diff
changeset
|
19 pycompat, |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26846
diff
changeset
|
20 util, |
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
26846
diff
changeset
|
21 ) |
138 | 22 |
36862
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
23 class multidict(object): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
24 """A dict like object that can store multiple values for a key. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
25 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
26 Used to store parsed request parameters. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
27 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
28 This is inspired by WebOb's class of the same name. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
29 """ |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
30 def __init__(self): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
31 # Stores (key, value) 2-tuples. This isn't the most efficient. But we |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
32 # don't rely on parameters that much, so it shouldn't be a perf issue. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
33 # we can always add dict for fast lookups. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
34 self._items = [] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
35 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
36 def __getitem__(self, key): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
37 """Returns the last set value for a key.""" |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
38 for k, v in reversed(self._items): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
39 if k == key: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
40 return v |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
41 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
42 raise KeyError(key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
43 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
44 def __setitem__(self, key, value): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
45 """Replace a values for a key with a new value.""" |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
46 try: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
47 del self[key] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
48 except KeyError: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
49 pass |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
50 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
51 self._items.append((key, value)) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
52 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
53 def __delitem__(self, key): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
54 """Delete all values for a key.""" |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
55 oldlen = len(self._items) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
56 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
57 self._items[:] = [(k, v) for k, v in self._items if k != key] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
58 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
59 if oldlen == len(self._items): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
60 raise KeyError(key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
61 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
62 def __contains__(self, key): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
63 return any(k == key for k, v in self._items) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
64 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
65 def __len__(self): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
66 return len(self._items) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
67 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
68 def get(self, key, default=None): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
69 try: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
70 return self.__getitem__(key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
71 except KeyError: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
72 return default |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
73 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
74 def add(self, key, value): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
75 """Add a new value for a key. Does not replace existing values.""" |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
76 self._items.append((key, value)) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
77 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
78 def getall(self, key): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
79 """Obtains all values for a key.""" |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
80 return [v for k, v in self._items if k == key] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
81 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
82 def getone(self, key): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
83 """Obtain a single value for a key. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
84 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
85 Raises KeyError if key not defined or it has multiple values set. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
86 """ |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
87 vals = self.getall(key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
88 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
89 if not vals: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
90 raise KeyError(key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
91 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
92 if len(vals) > 1: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
93 raise KeyError('multiple values for %r' % key) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
94 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
95 return vals[0] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
96 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
97 def asdictoflists(self): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
98 d = {} |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
99 for k, v in self._items: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
100 if k in d: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
101 d[k].append(v) |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
102 else: |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
103 d[k] = [v] |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
104 |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
105 return d |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
106 |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
107 @attr.s(frozen=True) |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
108 class parsedrequest(object): |
36857
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
109 """Represents a parsed WSGI request. |
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
110 |
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
111 Contains both parsed parameters as well as a handle on the input stream. |
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
112 """ |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
113 |
36848
16292bbda39c
hgweb: store and use request method on parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36847
diff
changeset
|
114 # Request method. |
16292bbda39c
hgweb: store and use request method on parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36847
diff
changeset
|
115 method = attr.ib() |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
116 # Full URL for this request. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
117 url = attr.ib() |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
118 # URL without any path components. Just <proto>://<host><port>. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
119 baseurl = attr.ib() |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
120 # Advertised URL. Like ``url`` and ``baseurl`` but uses SERVER_NAME instead |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
121 # of HTTP: Host header for hostname. This is likely what clients used. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
122 advertisedurl = attr.ib() |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
123 advertisedbaseurl = attr.ib() |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
124 # URL scheme (part before ``://``). e.g. ``http`` or ``https``. |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
125 urlscheme = attr.ib() |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
126 # Value of REMOTE_USER, if set, or None. |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
127 remoteuser = attr.ib() |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
128 # Value of REMOTE_HOST, if set, or None. |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
129 remotehost = attr.ib() |
36899
e67a2e05fa8a
hgweb: clarify that apppath begins with a forward slash
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36898
diff
changeset
|
130 # Relative WSGI application path. If defined, will begin with a |
e67a2e05fa8a
hgweb: clarify that apppath begins with a forward slash
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36898
diff
changeset
|
131 # ``/``. |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
132 apppath = attr.ib() |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
133 # List of path parts to be used for dispatch. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
134 dispatchparts = attr.ib() |
36898
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
135 # URL path component (no query string) used for dispatch. Can be |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
136 # ``None`` to signal no path component given to the request, an |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
137 # empty string to signal a request to the application's root URL, |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
138 # or a string not beginning with ``/`` containing the requested |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
139 # path under the application. |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
140 dispatchpath = attr.ib() |
36868
8ddb5c354906
hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36867
diff
changeset
|
141 # The name of the repository being accessed. |
8ddb5c354906
hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36867
diff
changeset
|
142 reponame = attr.ib() |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
143 # Raw query string (part after "?" in URL). |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
144 querystring = attr.ib() |
36862
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
145 # multidict of query string parameters. |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
146 qsparams = attr.ib() |
36814
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
147 # wsgiref.headers.Headers instance. Operates like a dict with case |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
148 # insensitive keys. |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
149 headers = attr.ib() |
36857
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
150 # Request body input stream. |
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
151 bodyfh = attr.ib() |
36909
84110a1d0f7d
hgweb: store the raw WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36908
diff
changeset
|
152 # WSGI environment dict, unmodified. |
84110a1d0f7d
hgweb: store the raw WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36908
diff
changeset
|
153 rawenv = attr.ib() |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
154 |
36911
f0a851542a05
hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36909
diff
changeset
|
155 def parserequestfromenv(env, reponame=None, altbaseurl=None): |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
156 """Parse URL components from environment variables. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
157 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
158 WSGI defines request attributes via environment variables. This function |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
159 parses the environment variables into a data structure. |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
160 |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
161 If ``reponame`` is defined, the leading path components matching that |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
162 string are effectively shifted from ``PATH_INFO`` to ``SCRIPT_NAME``. |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
163 This simulates the world view of a WSGI application that processes |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
164 requests from the base URL of a repo. |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
165 |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
166 If ``altbaseurl`` (typically comes from ``web.baseurl`` config option) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
167 is defined, it is used - instead of the WSGI environment variables - for |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
168 constructing URL components up to and including the WSGI application path. |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
169 For example, if the current WSGI application is at ``/repo`` and a request |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
170 is made to ``/rev/@`` with this argument set to |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
171 ``http://myserver:9000/prefix``, the URL and path components will resolve as |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
172 if the request were to ``http://myserver:9000/prefix/rev/@``. In other |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
173 words, ``wsgi.url_scheme``, ``SERVER_NAME``, ``SERVER_PORT``, and |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
174 ``SCRIPT_NAME`` are all effectively replaced by components from this URL. |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
175 """ |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
176 # PEP 3333 defines the WSGI spec and is a useful reference for this code. |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
177 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
178 # We first validate that the incoming object conforms with the WSGI spec. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
179 # We only want to be dealing with spec-conforming WSGI implementations. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
180 # TODO enable this once we fix internal violations. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
181 #wsgiref.validate.check_environ(env) |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
182 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
183 # PEP-0333 states that environment keys and values are native strings |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
184 # (bytes on Python 2 and str on Python 3). The code points for the Unicode |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
185 # strings on Python 3 must be between \00000-\000FF. We deal with bytes |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
186 # in Mercurial, so mass convert string keys and values to bytes. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
187 if pycompat.ispy3: |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
188 env = {k.encode('latin-1'): v for k, v in env.iteritems()} |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
189 env = {k: v.encode('latin-1') if isinstance(v, str) else v |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
190 for k, v in env.iteritems()} |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
191 |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
192 if altbaseurl: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
193 altbaseurl = util.url(altbaseurl) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
194 |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
195 # https://www.python.org/dev/peps/pep-0333/#environ-variables defines |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
196 # the environment variables. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
197 # https://www.python.org/dev/peps/pep-0333/#url-reconstruction defines |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
198 # how URLs are reconstructed. |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
199 fullurl = env['wsgi.url_scheme'] + '://' |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
200 |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
201 if altbaseurl and altbaseurl.scheme: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
202 advertisedfullurl = altbaseurl.scheme + '://' |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
203 else: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
204 advertisedfullurl = fullurl |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
205 |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
206 def addport(s, port): |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
207 if s.startswith('https://'): |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
208 if port != '443': |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
209 s += ':' + port |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
210 else: |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
211 if port != '80': |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
212 s += ':' + port |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
213 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
214 return s |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
215 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
216 if env.get('HTTP_HOST'): |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
217 fullurl += env['HTTP_HOST'] |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
218 else: |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
219 fullurl += env['SERVER_NAME'] |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
220 fullurl = addport(fullurl, env['SERVER_PORT']) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
221 |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
222 if altbaseurl and altbaseurl.host: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
223 advertisedfullurl += altbaseurl.host |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
224 |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
225 if altbaseurl.port: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
226 port = altbaseurl.port |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
227 elif altbaseurl.scheme == 'http' and not altbaseurl.port: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
228 port = '80' |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
229 elif altbaseurl.scheme == 'https' and not altbaseurl.port: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
230 port = '443' |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
231 else: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
232 port = env['SERVER_PORT'] |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
233 |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
234 advertisedfullurl = addport(advertisedfullurl, port) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
235 else: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
236 advertisedfullurl += env['SERVER_NAME'] |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
237 advertisedfullurl = addport(advertisedfullurl, env['SERVER_PORT']) |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
238 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
239 baseurl = fullurl |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
240 advertisedbaseurl = advertisedfullurl |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
241 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
242 fullurl += util.urlreq.quote(env.get('SCRIPT_NAME', '')) |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
243 fullurl += util.urlreq.quote(env.get('PATH_INFO', '')) |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
244 |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
245 if altbaseurl: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
246 path = altbaseurl.path or '' |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
247 if path and not path.startswith('/'): |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
248 path = '/' + path |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
249 advertisedfullurl += util.urlreq.quote(path) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
250 else: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
251 advertisedfullurl += util.urlreq.quote(env.get('SCRIPT_NAME', '')) |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
252 |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
253 advertisedfullurl += util.urlreq.quote(env.get('PATH_INFO', '')) |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
254 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
255 if env.get('QUERY_STRING'): |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
256 fullurl += '?' + env['QUERY_STRING'] |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
257 advertisedfullurl += '?' + env['QUERY_STRING'] |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
258 |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
259 # If ``reponame`` is defined, that must be a prefix on PATH_INFO |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
260 # that represents the repository being dispatched to. When computing |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
261 # the dispatch info, we ignore these leading path components. |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
262 |
36900
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
263 if altbaseurl: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
264 apppath = altbaseurl.path or '' |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
265 if apppath and not apppath.startswith('/'): |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
266 apppath = '/' + apppath |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
267 else: |
219b23359f4c
hgweb: support constructing URLs from an alternate base URL
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36899
diff
changeset
|
268 apppath = env.get('SCRIPT_NAME', '') |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
269 |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
270 if reponame: |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
271 repoprefix = '/' + reponame.strip('/') |
36808
0031e972ded2
hgweb: use the parsed application path directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36806
diff
changeset
|
272 |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
273 if not env.get('PATH_INFO'): |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
274 raise error.ProgrammingError('reponame requires PATH_INFO') |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
275 |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
276 if not env['PATH_INFO'].startswith(repoprefix): |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
277 raise error.ProgrammingError('PATH_INFO does not begin with repo ' |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
278 'name: %s (%s)' % (env['PATH_INFO'], |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
279 reponame)) |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
280 |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
281 dispatchpath = env['PATH_INFO'][len(repoprefix):] |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
282 |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
283 if dispatchpath and not dispatchpath.startswith('/'): |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
284 raise error.ProgrammingError('reponame prefix of PATH_INFO does ' |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
285 'not end at path delimiter: %s (%s)' % |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
286 (env['PATH_INFO'], reponame)) |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
287 |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
288 apppath = apppath.rstrip('/') + repoprefix |
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
289 dispatchparts = dispatchpath.strip('/').split('/') |
36898
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
290 dispatchpath = '/'.join(dispatchparts) |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
291 |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
292 elif 'PATH_INFO' in env: |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
293 if env['PATH_INFO'].strip('/'): |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
294 dispatchparts = env['PATH_INFO'].strip('/').split('/') |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
295 dispatchpath = '/'.join(dispatchparts) |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
296 else: |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
297 dispatchparts = [] |
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
298 dispatchpath = '' |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
299 else: |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
300 dispatchparts = [] |
36898
d0b0fedbfb53
hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36897
diff
changeset
|
301 dispatchpath = None |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
302 |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
303 querystring = env.get('QUERY_STRING', '') |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
304 |
36809
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36808
diff
changeset
|
305 # We store as a list so we have ordering information. We also store as |
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36808
diff
changeset
|
306 # a dict to facilitate fast lookup. |
36862
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
307 qsparams = multidict() |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
308 for k, v in util.urlreq.parseqsl(querystring, keep_blank_values=True): |
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
309 qsparams.add(k, v) |
36809
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36808
diff
changeset
|
310 |
36814
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
311 # HTTP_* keys contain HTTP request headers. The Headers structure should |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
312 # perform case normalization for us. We just rewrite underscore to dash |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
313 # so keys match what likely went over the wire. |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
314 headers = [] |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
315 for k, v in env.iteritems(): |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
316 if k.startswith('HTTP_'): |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
317 headers.append((k[len('HTTP_'):].replace('_', '-'), v)) |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
318 |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
319 headers = wsgiheaders.Headers(headers) |
f9078c6caeb6
hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36811
diff
changeset
|
320 |
36847
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
321 # This is kind of a lie because the HTTP header wasn't explicitly |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
322 # sent. But for all intents and purposes it should be OK to lie about |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
323 # this, since a consumer will either either value to determine how many |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
324 # bytes are available to read. |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
325 if 'CONTENT_LENGTH' in env and 'HTTP_CONTENT_LENGTH' not in env: |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
326 headers['Content-Length'] = env['CONTENT_LENGTH'] |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36814
diff
changeset
|
327 |
36911
f0a851542a05
hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36909
diff
changeset
|
328 bodyfh = env['wsgi.input'] |
f0a851542a05
hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36909
diff
changeset
|
329 if 'Content-Length' in headers: |
f0a851542a05
hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36909
diff
changeset
|
330 bodyfh = util.cappedreader(bodyfh, int(headers['Content-Length'])) |
36857
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
331 |
36848
16292bbda39c
hgweb: store and use request method on parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36847
diff
changeset
|
332 return parsedrequest(method=env['REQUEST_METHOD'], |
16292bbda39c
hgweb: store and use request method on parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36847
diff
changeset
|
333 url=fullurl, baseurl=baseurl, |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
334 advertisedurl=advertisedfullurl, |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
335 advertisedbaseurl=advertisedbaseurl, |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
336 urlscheme=env['wsgi.url_scheme'], |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
337 remoteuser=env.get('REMOTE_USER'), |
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36866
diff
changeset
|
338 remotehost=env.get('REMOTE_HOST'), |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
339 apppath=apppath, |
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
340 dispatchparts=dispatchparts, dispatchpath=dispatchpath, |
36897
d7fd203e36cc
hgweb: refactor repository name URL parsing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36896
diff
changeset
|
341 reponame=reponame, |
36809
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36808
diff
changeset
|
342 querystring=querystring, |
36862
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
343 qsparams=qsparams, |
36857
da4e2f87167d
hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36856
diff
changeset
|
344 headers=headers, |
36909
84110a1d0f7d
hgweb: store the raw WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36908
diff
changeset
|
345 bodyfh=bodyfh, |
84110a1d0f7d
hgweb: store the raw WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36908
diff
changeset
|
346 rawenv=env) |
36806
69b2d0900cd7
hgweb: parse WSGI request into a data structure
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36713
diff
changeset
|
347 |
36875
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
348 class offsettrackingwriter(object): |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
349 """A file object like object that is append only and tracks write count. |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
350 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
351 Instances are bound to a callable. This callable is called with data |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
352 whenever a ``write()`` is attempted. |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
353 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
354 Instances track the amount of written data so they can answer ``tell()`` |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
355 requests. |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
356 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
357 The intent of this class is to wrap the ``write()`` function returned by |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
358 a WSGI ``start_response()`` function. Since ``write()`` is a callable and |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
359 not a file object, it doesn't implement other file object methods. |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
360 """ |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
361 def __init__(self, writefn): |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
362 self._write = writefn |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
363 self._offset = 0 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
364 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
365 def write(self, s): |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
366 res = self._write(s) |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
367 # Some Python objects don't report the number of bytes written. |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
368 if res is None: |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
369 self._offset += len(s) |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
370 else: |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
371 self._offset += res |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
372 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
373 def flush(self): |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
374 pass |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
375 |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
376 def tell(self): |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
377 return self._offset |
16499427f6de
hgweb: refactor fake file object proxy for archiving
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36868
diff
changeset
|
378 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
379 class wsgiresponse(object): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
380 """Represents a response to a WSGI request. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
381 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
382 A response consists of a status line, headers, and a body. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
383 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
384 Consumers must populate the ``status`` and ``headers`` fields and |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
385 make a call to a ``setbody*()`` method before the response can be |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
386 issued. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
387 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
388 When it is time to start sending the response over the wire, |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
389 ``sendresponse()`` is called. It handles emitting the header portion |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
390 of the response message. It then yields chunks of body data to be |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
391 written to the peer. Typically, the WSGI application itself calls |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
392 and returns the value from ``sendresponse()``. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
393 """ |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
394 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
395 def __init__(self, req, startresponse): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
396 """Create an empty response tied to a specific request. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
397 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
398 ``req`` is a ``parsedrequest``. ``startresponse`` is the |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
399 ``start_response`` function passed to the WSGI application. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
400 """ |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
401 self._req = req |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
402 self._startresponse = startresponse |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
403 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
404 self.status = None |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
405 self.headers = wsgiheaders.Headers([]) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
406 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
407 self._bodybytes = None |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
408 self._bodygen = None |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
409 self._bodywillwrite = False |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
410 self._started = False |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
411 self._bodywritefn = None |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
412 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
413 def _verifybody(self): |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
414 if (self._bodybytes is not None or self._bodygen is not None |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
415 or self._bodywillwrite): |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
416 raise error.ProgrammingError('cannot define body multiple times') |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
417 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
418 def setbodybytes(self, b): |
36878
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
419 """Define the response body as static bytes. |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
420 |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
421 The empty string signals that there is no response body. |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
422 """ |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
423 self._verifybody() |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
424 self._bodybytes = b |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
425 self.headers['Content-Length'] = '%d' % len(b) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
426 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
427 def setbodygen(self, gen): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
428 """Define the response body as a generator of bytes.""" |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
429 self._verifybody() |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
430 self._bodygen = gen |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
431 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
432 def setbodywillwrite(self): |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
433 """Signal an intent to use write() to emit the response body. |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
434 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
435 **This is the least preferred way to send a body.** |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
436 |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
437 It is preferred for WSGI applications to emit a generator of chunks |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
438 constituting the response body. However, some consumers can't emit |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
439 data this way. So, WSGI provides a way to obtain a ``write(data)`` |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
440 function that can be used to synchronously perform an unbuffered |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
441 write. |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
442 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
443 Calling this function signals an intent to produce the body in this |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
444 manner. |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
445 """ |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
446 self._verifybody() |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
447 self._bodywillwrite = True |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
448 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
449 def sendresponse(self): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
450 """Send the generated response to the client. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
451 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
452 Before this is called, ``status`` must be set and one of |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
453 ``setbodybytes()`` or ``setbodygen()`` must be called. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
454 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
455 Calling this method multiple times is not allowed. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
456 """ |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
457 if self._started: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
458 raise error.ProgrammingError('sendresponse() called multiple times') |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
459 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
460 self._started = True |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
461 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
462 if not self.status: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
463 raise error.ProgrammingError('status line not defined') |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
464 |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
465 if (self._bodybytes is None and self._bodygen is None |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
466 and not self._bodywillwrite): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
467 raise error.ProgrammingError('response body not defined') |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
468 |
36878
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
469 # RFC 7232 Section 4.1 states that a 304 MUST generate one of |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
470 # {Cache-Control, Content-Location, Date, ETag, Expires, Vary} |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
471 # and SHOULD NOT generate other headers unless they could be used |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
472 # to guide cache updates. Furthermore, RFC 7230 Section 3.3.2 |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
473 # states that no response body can be issued. Content-Length can |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
474 # be sent. But if it is present, it should be the size of the response |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
475 # that wasn't transferred. |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
476 if self.status.startswith('304 '): |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
477 # setbodybytes('') will set C-L to 0. This doesn't conform with the |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
478 # spec. So remove it. |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
479 if self.headers.get('Content-Length') == '0': |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
480 del self.headers['Content-Length'] |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
481 |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
482 # Strictly speaking, this is too strict. But until it causes |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
483 # problems, let's be strict. |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
484 badheaders = {k for k in self.headers.keys() |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
485 if k.lower() not in ('date', 'etag', 'expires', |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
486 'cache-control', |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
487 'content-location', |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
488 'vary')} |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
489 if badheaders: |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
490 raise error.ProgrammingError( |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
491 'illegal header on 304 response: %s' % |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
492 ', '.join(sorted(badheaders))) |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
493 |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
494 if self._bodygen is not None or self._bodywillwrite: |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
495 raise error.ProgrammingError("must use setbodybytes('') with " |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
496 "304 responses") |
ccb70a77f746
hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36876
diff
changeset
|
497 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
498 # Various HTTP clients (notably httplib) won't read the HTTP response |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
499 # until the HTTP request has been sent in full. If servers (us) send a |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
500 # response before the HTTP request has been fully sent, the connection |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
501 # may deadlock because neither end is reading. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
502 # |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
503 # We work around this by "draining" the request data before |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
504 # sending any response in some conditions. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
505 drain = False |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
506 close = False |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
507 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
508 # If the client sent Expect: 100-continue, we assume it is smart enough |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
509 # to deal with the server sending a response before reading the request. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
510 # (httplib doesn't do this.) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
511 if self._req.headers.get('Expect', '').lower() == '100-continue': |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
512 pass |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
513 # Only tend to request methods that have bodies. Strictly speaking, |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
514 # we should sniff for a body. But this is fine for our existing |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
515 # WSGI applications. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
516 elif self._req.method not in ('POST', 'PUT'): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
517 pass |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
518 else: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
519 # If we don't know how much data to read, there's no guarantee |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
520 # that we can drain the request responsibly. The WSGI |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
521 # specification only says that servers *should* ensure the |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
522 # input stream doesn't overrun the actual request. So there's |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
523 # no guarantee that reading until EOF won't corrupt the stream |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
524 # state. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
525 if not isinstance(self._req.bodyfh, util.cappedreader): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
526 close = True |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
527 else: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
528 # We /could/ only drain certain HTTP response codes. But 200 and |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
529 # non-200 wire protocol responses both require draining. Since |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
530 # we have a capped reader in place for all situations where we |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
531 # drain, it is safe to read from that stream. We'll either do |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
532 # a drain or no-op if we're already at EOF. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
533 drain = True |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
534 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
535 if close: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
536 self.headers['Connection'] = 'Close' |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
537 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
538 if drain: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
539 assert isinstance(self._req.bodyfh, util.cappedreader) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
540 while True: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
541 chunk = self._req.bodyfh.read(32768) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
542 if not chunk: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
543 break |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
544 |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
545 write = self._startresponse(pycompat.sysstr(self.status), |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
546 self.headers.items()) |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
547 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
548 if self._bodybytes: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
549 yield self._bodybytes |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
550 elif self._bodygen: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
551 for chunk in self._bodygen: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
552 yield chunk |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
553 elif self._bodywillwrite: |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
554 self._bodywritefn = write |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
555 else: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
556 error.ProgrammingError('do not know how to send body') |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36859
diff
changeset
|
557 |
36876
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
558 def getbodyfile(self): |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
559 """Obtain a file object like object representing the response body. |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
560 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
561 For this to work, you must call ``setbodywillwrite()`` and then |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
562 ``sendresponse()`` first. ``sendresponse()`` is a generator and the |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
563 function won't run to completion unless the generator is advanced. The |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
564 generator yields not items. The easiest way to consume it is with |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
565 ``list(res.sendresponse())``, which should resolve to an empty list - |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
566 ``[]``. |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
567 """ |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
568 if not self._bodywillwrite: |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
569 raise error.ProgrammingError('must call setbodywillwrite() first') |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
570 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
571 if not self._started: |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
572 raise error.ProgrammingError('must call sendresponse() first; did ' |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
573 'you remember to consume it since it ' |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
574 'is a generator?') |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
575 |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
576 assert self._bodywritefn |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
577 return offsettrackingwriter(self._bodywritefn) |
97f44b0720e2
hgweb: port archive command to modern response API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36875
diff
changeset
|
578 |
5566
d74fc8dec2b4
Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5563
diff
changeset
|
579 def wsgiapplication(app_maker): |
5887
41a3fce17625
hgweb: return iterable, add deprecation note
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5886
diff
changeset
|
580 '''For compatibility with old CGI scripts. A plain hgweb() or hgwebdir() |
41a3fce17625
hgweb: return iterable, add deprecation note
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5886
diff
changeset
|
581 can and should now be used as a WSGI application.''' |
5760
0145f9afb0e7
Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5566
diff
changeset
|
582 application = app_maker() |
0145f9afb0e7
Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5566
diff
changeset
|
583 def run_wsgi(env, respond): |
5887
41a3fce17625
hgweb: return iterable, add deprecation note
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5886
diff
changeset
|
584 return application(env, respond) |
5760
0145f9afb0e7
Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5566
diff
changeset
|
585 return run_wsgi |