forked from cms-sw/cmsdist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cherrypy-trailers.patch
199 lines (178 loc) · 7.51 KB
/
cherrypy-trailers.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Add support for trailer headers in streaming requests.
From: Lassi Tuura <[email protected]>
---
cherrypy/_cprequest.py | 4 ++++
cherrypy/_cpwsgi.py | 9 +++++++--
cherrypy/test/test_conn.py | 35 ++++++++++++++++++++++++++++-------
cherrypy/wsgiserver/__init__.py | 15 +++++++++++++--
4 files changed, 52 insertions(+), 11 deletions(-)
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py
index 23bdad2..9619dbc 100644
--- a/cherrypy/_cprequest.py
+++ b/cherrypy/_cprequest.py
@@ -861,6 +861,7 @@ class Response(object):
headers = self.headers
if self.stream:
+ self.headers = http.HeaderMap()
if dict.get(headers, 'Content-Length') is None:
dict.pop(headers, 'Content-Length', None)
elif code < 200 or code in (204, 205, 304):
@@ -888,6 +889,9 @@ class Response(object):
name, value = line.split(": ", 1)
h.append((name, value))
+ def trailer_headers(self):
+ return (self.stream and self.headers.output(cherrypy.request.protocol)) or []
+
def check_timeout(self):
"""If now > self.time + self.timeout, set self.timed_out.
diff --git a/cherrypy/_cpwsgi.py b/cherrypy/_cpwsgi.py
index b23822e..e0a5b2e 100644
--- a/cherrypy/_cpwsgi.py
+++ b/cherrypy/_cpwsgi.py
@@ -76,9 +76,10 @@ class AppResponse(object):
def setapp(self):
try:
self.request = self.get_request()
- s, h, b = self.get_response()
+ s, h, b, t = self.get_response()
self.iter_response = iter(b)
self.start_response(s, h)
+ self.trailer = t
except self.throws:
self.close()
raise
@@ -175,6 +176,9 @@ class AppResponse(object):
self.close()
self.iredirect(ir.path, ir.query_string)
except StopIteration:
+ pass_trailer = self.environ.get('cherrypy.trailer_headers', None)
+ if pass_trailer:
+ pass_trailer(self.trailer())
raise
except:
if getattr(self.request, "throw_errors", False):
@@ -217,7 +221,8 @@ class AppResponse(object):
headers = self.translate_headers(self.environ)
rfile = self.environ['wsgi.input']
response = self.request.run(meth, path, qs, rproto, headers, rfile)
- return response.status, response.header_list, response.body
+ return response.status, response.header_list, response.body, \
+ response.trailer_headers
def get_request(self):
"""Create a Request object using environ."""
diff --git a/cherrypy/test/test_conn.py b/cherrypy/test/test_conn.py
index c1f902c..8d3e3dc 100644
--- a/cherrypy/test/test_conn.py
+++ b/cherrypy/test/test_conn.py
@@ -40,12 +40,17 @@ def setup_server():
return str(cherrypy.server.httpserver.timeout)
timeout.exposed = True
- def stream(self, set_cl=False):
+ def stream(self, set_cl=False, with_trailer=False):
if set_cl:
cherrypy.response.headers['Content-Length'] = 10
+ if with_trailer:
+ cherrypy.response.headers['Trailer'] = "X-Trailer"
+
def content():
for x in xrange(10):
+ if with_trailer and x == 9:
+ cherrypy.response.headers['X-Trailer'] = 123
yield str(x)
return content()
@@ -121,12 +126,14 @@ class ConnectionTests(helper.CPWebCase):
self.assertRaises(httplib.NotConnected, self.getPage, "/")
def test_Streaming_no_len(self):
- self._streaming(set_cl=False)
+ self._streaming(set_cl=False, with_trailer=True)
+ self._streaming(set_cl=False, with_trailer=False)
def test_Streaming_with_len(self):
- self._streaming(set_cl=True)
+ self._streaming(set_cl=True, with_trailer=True)
+ self._streaming(set_cl=True, with_trailer=False)
- def _streaming(self, set_cl):
+ def _streaming(self, set_cl, with_trailer):
if cherrypy.server.protocol_version == "HTTP/1.1":
self.PROTOCOL = "HTTP/1.1"
@@ -142,22 +149,35 @@ class ConnectionTests(helper.CPWebCase):
if set_cl:
# When a Content-Length is provided, the content should stream
# without closing the connection.
- self.getPage("/stream?set_cl=Yes")
+ self.getPage("/stream?set_cl=Yes" + ((with_trailer and "&with_trailer=Yes") or ""))
self.assertHeader("Content-Length")
self.assertNoHeader("Connection", "close")
self.assertNoHeader("Transfer-Encoding")
self.assertStatus('200 OK')
self.assertBody('0123456789')
+
+ ## NB: HTTPResponse eats trailer headers, so can't check for them.
+ # if with_trailer:
+ # self.assertHeader("X-Trailer", "123")
+ # else:
+ # self.assertNoHeader("X-Trailer")
+
else:
# When no Content-Length response header is provided,
# streamed output will either close the connection, or use
# chunked encoding, to determine transfer-length.
- self.getPage("/stream")
+ self.getPage("/stream" + ((with_trailer and "?with_trailer=Yes") or ""))
self.assertNoHeader("Content-Length")
self.assertStatus('200 OK')
self.assertBody('0123456789')
-
+
+ ## NB: HTTPResponse eats trailer headers, so can't check for them.
+ # if with_trailer:
+ # self.assertHeader("X-Trailer", "123")
+ # else:
+ # self.assertNoHeader("X-Trailer")
+
chunked_response = False
for k, v in self.headers:
if k.lower() == "transfer-encoding":
@@ -177,6 +197,7 @@ class ConnectionTests(helper.CPWebCase):
self.assertStatus('200 OK')
self.assertBody('')
self.assertNoHeader("Transfer-Encoding")
+ self.assertNoHeader("X-Trailer")
else:
self.PROTOCOL = "HTTP/1.0"
diff --git a/cherrypy/wsgiserver/__init__.py b/cherrypy/wsgiserver/__init__.py
index c380e18..5ed32e0 100644
--- a/cherrypy/wsgiserver/__init__.py
+++ b/cherrypy/wsgiserver/__init__.py
@@ -553,6 +553,7 @@ class HTTPRequest(object):
self.close_connection = True
return
+ self.environ["cherrypy.trailer_headers"] = self.outheaders.extend
response = self.wsgi_app(self.environ, self.start_response)
try:
for chunk in response:
@@ -572,8 +573,17 @@ class HTTPRequest(object):
self.sent_headers = True
self.send_headers()
if self.chunked_write:
- self.wfile.sendall("0\r\n\r\n")
-
+ try:
+ trailer = ["0\r\n"] + [k + ": " + v + "\r\n" for k, v in self.outheaders] + ["\r\n"]
+ except TypeError:
+ if not isinstance(k, str):
+ raise TypeError("WSGI trailer header key %r is not a string.")
+ if not isinstance(v, str):
+ raise TypeError("WSGI trailer header value %r is not a string.")
+ else:
+ raise
+ self.wfile.sendall("".join(trailer))
+
def simple_response(self, status, msg=""):
"""Write a simple response back to the client."""
status = str(status)
@@ -706,6 +716,7 @@ class HTTPRequest(object):
raise
buf.append("\r\n")
self.wfile.sendall("".join(buf))
+ self.outheaders[:] = []
class NoSSLError(Exception):