-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpform.py
305 lines (263 loc) · 10.4 KB
/
httpform.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Copyright 2012 Gregory Holt and 2011 OpenStack, LLC.
# Original source taken from OpenStack Swift FormPost middleware and
# modified to be more generic.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Module for working with HTTP Form POSTs iteratively; in other words,
parsing an HTTP Form POST without reading the whole thing into memory
first. Many thanks to Michael Barton for the original prototype which
I mangled into OpenStack Swift's formpost middleware and then into
this module.
More and better docs later as well as tests, this should be
considered just an early prototype for now and subject to drastic
change.
The basic usage is to iterate over iter_form results, which
rfc822.Message instances::
from brim.httpform import iter_form, parse_attrs
def wsgi_app(env, start_response):
for message in iter_form(env):
body = message.fp.read()
value, attrs = \\
parse_attrs(message.getheader('content-disposition'))
if value != 'form-data':
continue
if 'filename' in attrs:
filevarname = attrs['name']
filename = attrs['filename']
filecontent = body
else:
varname = attrs['name']
varvalue = body
See also the simple test at the end of this file.
"""
import re
import rfc822
_ATTRIBUTES_RE = re.compile(r'(\w+)=(".*?"|[^";]+)(; ?|$)')
class FormInvalid(Exception):
"""
Raised by iter_form if it has parsing difficulties of an incoming
form.
"""
pass
def parse_attrs(header):
"""
Given the value of a header like::
Content-Disposition: form-data; name="somefile"; filename="test.html"
Return data like::
("form-data", {"name": "somefile", "filename": "test.html"})
Example usage with an rfc822.Message::
value, attrs = parse_attrs(message.getheader('content-disposition'))
:param header: Value of a header (the part after the ': ').
:returns: (value name, dict) of the attribute data parsed (see above).
"""
attributes = {}
attrs = ''
if '; ' in header:
header, attrs = header.split('; ', 1)
m = True
while m:
m = _ATTRIBUTES_RE.match(attrs)
if m:
attrs = attrs[len(m.group(0)):]
attributes[m.group(1)] = m.group(2).strip('"')
return header, attributes
class _FormPartFileLikeObject(object):
def __init__(self, wsgi_input, boundary, input_buffer, read_chunk_size):
self.no_more_data_for_this_message = False
self.no_more_messages = False
self.wsgi_input = wsgi_input
self.boundary = boundary
self.input_buffer = input_buffer
self.read_chunk_size = read_chunk_size
def read(self, length=None):
if not length:
length = self.read_chunk_size
if self.no_more_data_for_this_message:
return ''
# read enough data to know whether we're going to run
# into a boundary in next [length] bytes
if len(self.input_buffer) < length + len(self.boundary) + 2:
to_read = length + len(self.boundary) + 2
while to_read > 0:
chunk = self.wsgi_input.read(to_read)
to_read -= len(chunk)
self.input_buffer += chunk
if not chunk:
self.no_more_messages = True
break
boundary_pos = self.input_buffer.find(self.boundary)
# boundary does not exist in the next (length) bytes
if boundary_pos == -1 or boundary_pos > length:
ret = self.input_buffer[:length]
self.input_buffer = self.input_buffer[length:]
# if it does, just return data up to the boundary
else:
ret, self.input_buffer = self.input_buffer.split(self.boundary, 1)
self.no_more_messages = self.input_buffer.startswith('--')
self.no_more_data_for_this_message = True
self.input_buffer = self.input_buffer[2:]
return ret
def readline(self):
if self.no_more_data_for_this_message:
return ''
boundary_pos = newline_pos = -1
while newline_pos < 0 and boundary_pos < 0:
chunk = self.wsgi_input.read(self.read_chunk_size)
self.input_buffer += chunk
newline_pos = self.input_buffer.find('\r\n')
boundary_pos = self.input_buffer.find(self.boundary)
if not chunk:
self.no_more_messages = True
break
# found a newline
if newline_pos >= 0 and \
(boundary_pos < 0 or newline_pos < boundary_pos):
# Use self.read to ensure any logic there happens...
ret = ''
to_read = newline_pos + 2
while to_read > 0:
chunk = self.read(to_read)
# Should never happen since we're reading from input_buffer,
# but just for completeness...
if not chunk:
break
to_read -= len(chunk)
ret += chunk
return ret
else: # no newlines, just return up to next boundary
return self.read(len(self.input_buffer))
class CappedFileLikeObject(object):
"""
A file-like object wrapping another file-like object that raises
an EOFError if the amount of data read exceeds a given
max_file_size.
This is useful to cap the form data size accepted::
for message in iter_form(env):
try:
content = CappedFileLikeObject(message.fp, 4096).read()
except EOFError:
raise HTTPRequestEntityTooLarge(
'Max form part size is 4096.\\n')
:param fp: The file-like object to wrap.
:param max_file_size: The maximum bytes to read before raising an
EOFError.
"""
def __init__(self, fp, max_file_size):
self.fp = fp
self.max_file_size = max_file_size
self.amount_read = 0
def read(self, size=None):
ret = self.fp.read(size)
self.amount_read += len(ret)
if self.amount_read > self.max_file_size:
raise EOFError('max_file_size exceeded')
return ret
def readline(self):
ret = self.fp.readline()
self.amount_read += len(ret)
if self.amount_read > self.max_file_size:
raise EOFError('max_file_size exceeded')
return ret
def iter_form(env, read_chunk_size=4096):
"""
Parses an HTTP Form POST and yields rfc822.Message instances for
each form part. See the overview module documentation for usage.
:param env: The WSGI environment for the incoming request.
:param read_chunk_size: The maximum amount to read at once from
the incoming request.
:returns: A generator yielding rfc822.Messages; be sure to fully
read from the message.fp file-like object before
continuing to the next message of the generator.
"""
content_type, attrs = parse_attrs(env.get('CONTENT_TYPE') or '')
if content_type != 'multipart/form-data':
raise FormInvalid('Content-Type not "multipart/form-data".')
boundary = attrs.get('boundary')
if not boundary:
raise FormInvalid('Content-Type does not define a form boundary.')
boundary = '--' + boundary
wsgi_input = env['wsgi.input']
if wsgi_input.readline().strip() != boundary:
raise FormInvalid('Invalid starting boundary.')
boundary = '\r\n' + boundary
input_buffer = ''
done = False
while not done:
fp = _FormPartFileLikeObject(wsgi_input, boundary, input_buffer,
read_chunk_size)
yield rfc822.Message(fp, 0)
done = fp.no_more_messages
input_buffer = fp.input_buffer
if __name__ == '__main__':
# TODO: Real tests
# This is just a quick test.
from StringIO import StringIO
wsgi_input = StringIO('\r\n'.join([
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="redirect"',
'',
'redirect value',
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="max_file_size"',
'',
str(15),
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="max_file_count"',
'',
str(25),
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="expires"',
'',
str(1234),
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="signature"',
'',
'sig value',
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="file1"; '
'filename="testfile1.txt"',
'Content-Type: text/plain',
'',
'Test File\nOne\n',
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="file2"; '
'filename="testfile2.txt"',
'Content-Type: text/plain',
'',
'Test\nFile\nTwo\n',
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'Content-Disposition: form-data; name="file3"; filename=""',
'Content-Type: application/octet-stream',
'',
'',
'------WebKitFormBoundaryNcxTqxSlX7t4TDkR--',
'',
]))
env = {
'CONTENT_TYPE': 'multipart/form-data; '
'boundary=----WebKitFormBoundaryNcxTqxSlX7t4TDkR',
'wsgi.input': wsgi_input
}
for message in iter_form(env):
print '---'
body = message.fp.read()
value, attrs = parse_attrs(message.getheader('content-disposition'))
if value != 'form-data':
continue
if 'filename' in attrs:
print 'FILE %s named %r:' % (attrs['name'], attrs['filename'])
print body
else:
print 'VARIABLE %s = %r' % (attrs['name'], body)