8 buf.update(urllib.parse.parse_qsl(req.query))
9 if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded":
11 rbody = req.input.read(2 ** 20)
12 except IOError as exc:
14 if len(rbody) >= 2 ** 20:
15 return ValueError("x-www-form-urlencoded data is absurdly long")
16 buf.update(urllib.parse.parse_qsl(rbody.decode("latin1")))
19 class badmultipart(Exception):
22 class formpart(object):
23 def __init__(self, form):
32 def fillbuf(self, sz):
34 mboundary = b"\r\n--" + self.form.boundary + b"\r\n"
35 lboundary = b"\r\n--" + self.form.boundary + b"--\r\n"
37 p = self.form.buf.find(mboundary)
39 self.buf += self.form.buf[:p]
40 self.form.buf = self.form.buf[p + len(mboundary):]
43 p = self.form.buf.find(lboundary)
45 self.buf += self.form.buf[:p]
46 self.form.buf = self.form.buf[p + len(lboundary):]
50 self.buf += self.form.buf[:-len(lboundary)]
51 self.form.buf = self.form.buf[-len(lboundary):]
52 if sz >= 0 and len(self.buf) >= sz:
54 while len(self.form.buf) <= len(lboundary):
55 ret = req.input.read(8192)
57 raise badmultipart("Missing last multipart boundary")
60 def read(self, limit=-1):
63 ret = self.buf[:limit]
64 self.buf = self.buf[limit:]
70 def readline(self, limit=-1):
73 p = self.buf.find(b'\n', last)
80 self.fillbuf(last + 128)
82 ret = self.buf[:p + 1]
83 self.buf = self.buf[p + 1:]
88 if self.read(8192) == b"":
94 def __exit__(self, *excinfo):
98 def parsehead(self, charset):
100 ln = self.readline(256)
101 if ln[-1] != ord(b'\n'):
102 raise badmultipart("Too long header line in part")
104 return ln.decode(charset).rstrip()
106 raise badmultipart("Form part header is not in assumed charset")
115 if not ln[1:].isspace():
120 raise badmultipart("Malformed multipart header line")
121 self.head[buf[:p].strip().lower()] = buf[p + 1:].lstrip()
123 val, par = proto.pmimehead(self.head.get("content-disposition", ""))
124 if val != "form-data":
125 raise badmultipart("Unexpected Content-Disposition in form part: %r" % val)
126 if not "name" in par:
127 raise badmultipart("Missing name in form part")
128 self.name = par["name"]
129 self.filename = par.get("filename")
130 val, par = proto.pmimehead(self.head.get("content-type", ""))
132 self.charset = par.get("charset")
133 encoding = self.head.get("content-transfer-encoding", "binary")
134 if encoding != "binary":
135 raise badmultipart("Form part uses unexpected transfer encoding: %r" % encoding)
137 class multipart(object):
138 def __init__(self, req, charset):
139 val, par = proto.pmimehead(req.ihead.get("Content-Type", ""))
140 if req.method != "POST" or val != "multipart/form-data":
141 raise badmultipart("Request is not a multipart form")
142 if "boundary" not in par:
143 raise badmultipart("Multipart form lacks boundary")
145 self.boundary = par["boundary"].encode("us-ascii")
147 raise badmultipart("Multipart boundary must be ASCII string")
151 self.headcs = charset
152 self.lastpart = formpart(self)
153 self.lastpart.close()
159 if not self.lastpart.eof:
160 raise RuntimeError("All form parts must be read entirely")
162 raise StopIteration()
163 self.lastpart = formpart(self)
164 self.lastpart.parsehead(self.headcs)
167 def formdata(req, onerror=Exception):
168 data = req.item(formparse)
169 if isinstance(data, Exception):
170 if onerror is Exception: