def parse(self, *lines, **kwargs): self.write(*lines) self.data.seek(0) kwargs["environ"] = self.env kwargs["strict"] = True kwargs["charset"] = "utf8" return mp.parse_form_data(**kwargs)
def wrapper(**kwargs): try: if kwargs: # The idea of this design is to consume the kwargs here and store the data in self. raise RuntimeError('unprocessed arguments', kwargs) if bottle.request.environ.get('REQUEST_METHOD', 'GET').upper() in ['POST', 'PUT']: # As multipart is not (yet) integrated in bottle, we make the request data available # in the environment. try: self.forms, self.files = multipart.parse_form_data(bottle.request.environ, strict=cfg.multipart_strict, memfile_limit=cfg.multipart_memfile_limit*1024, mem_limit=cfg.multipart_mem_limit*1024, disk_limit=cfg.multipart_disk_limit*1024) except multipart.MultipartError as e: bottle.abort(413, e) result = func(self) except: user_agent = bottle.request.environ.get('HTTP_USER_AGENT') self.db.add(orm.Log(None, None, None, user_agent, traceback.format_exc())) raise finally: if hasattr(self, '_db'): self.db.commit() self.db.close() return result
def parseMultipart(self,request): reqInfo = self._extender._helpers.analyzeRequest(request) if reqInfo.getMethod() == "POST" and reqInfo.getContentType() == reqInfo.CONTENT_TYPE_MULTIPART: #Going thru headers to find Content-Type header headers = reqInfo.getHeaders() for header in headers: #Reading boundary res = re.search(r'Content-Type: multipart/.*; boundary=(.*)', header) if res != None : boundary = res.group(1) try: env = {'REQUEST_METHOD': 'POST','CONTENT_TYPE': 'multipart/form-data; boundary='+boundary,'wsgi.input': IO(request[reqInfo.getBodyOffset():])} rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8') for files in rfiles: for file in rfiles.getall(files): print "file:"+file.name+":"+file.content_type if file.content_type in self._listOfOOXMLContentType: parameter = self._extender._helpers.getRequestParameter(request, file.name) #Saving zip content and the span where the zip is self._zip = parameter.getValue() self._valueStart = parameter.getValueStart() self._valueEnd = parameter.getValueEnd() print "OOXML Document detected in the following parameter:" + file.name break; except Exception as e: print("Error: {0}".format(e))
def parse(self, *lines, **kwargs): self.write(*lines) self.data.seek(0) kwargs['environ'] = self.env kwargs['strict'] = True kwargs['charset'] = 'utf8' return mp.parse_form_data(**kwargs)
def form_parse(args): """ >>> import control >>> body = "LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1mNDNkYjAzNTQyNmQzYmZjDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9InBob3RvIjsgZmlsZW5hbWU9ImhlbGxvLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpoZWxsbwoNCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tZjQzZGIwMzU0MjZkM2JmYw0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJuYW1lIg0KDQpoZWxsbw0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1mNDNkYjAzNTQyNmQzYmZjDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImVtYWlsIg0KDQpoQGwubw0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1mNDNkYjAzNTQyNmQzYmZjLS0NCg==" >>> ctype = "multipart/form-data; boundary=------------------------f43db035426d3bfc" >>> args = { "__ow_body": body, "__ow_headers": { "content-type": ctype}, "__ow_method": "post" } >>> fields, files = control.form_parse(args) >>> print("%s %s" % (fields["name"], fields["email"])) hello [email protected] >>> photo = files["photo"].file.read() >>> photo_type = files["photo"].content_type >>> print("%s%s" % (photo.decode("ascii"), photo_type)) hello text/plain """ import io, base64, multipart body = args.get("__ow_body") method = args["__ow_method"] ctype = args["__ow_headers"]["content-type"] input = io.BytesIO(base64.b64decode(body)) env = { 'REQUEST_METHOD': method, 'CONTENT_TYPE': ctype, 'wsgi.input': input } return multipart.parse_form_data(env, strict=True, charset='utf-8')
def assertMPError(self, *a, **ka): self.data.seek(0) ka['environ'] = self.env ka['strict'] = True ka['charset'] = 'utf8' self.assertRaises(mp.MultipartError, mp.parse_form_data, **ka) ka['strict'] = False self.data.seek(0) self.assertTrue(mp.parse_form_data(**ka))
def assertMPError(self, *a, **ka): self.data.seek(0) ka["environ"] = self.env ka["strict"] = True ka["charset"] = "utf8" self.assertRaises(mp.MultipartError, mp.parse_form_data, **ka) ka["strict"] = False self.data.seek(0) self.assertTrue(mp.parse_form_data(**ka))
def test_werkzeug_examples(self): """Tests multipart parsing against data collected from webbrowsers""" for name in browser_test_cases: boundary = browser_test_cases[name]['boundary'] files = browser_test_cases[name]['files'] forms = browser_test_cases[name]['forms'] env = {'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary=%s'%boundary, 'wsgi.input': BytesIO(browser_test_cases[name]['data'])} rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8') for field in files: self.assertEqual(rfiles[field].name, field) self.assertEqual(rfiles[field].filename, files[field][0]) self.assertEqual(rfiles[field].content_type, files[field][1]) self.assertEqual(rfiles[field].file.read(), tob(files[field][2])) for field in forms: self.assertEqual(rforms[field], forms[field])
def test_werkzeug_examples(self): """Tests multipart parsing against data collected from webbrowsers""" for name in browser_test_cases: boundary = browser_test_cases[name]['boundary'] files = browser_test_cases[name]['files'] forms = browser_test_cases[name]['forms'] env = {'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary=%s'%boundary, 'wsgi.input': BytesIO(browser_test_cases[name]['data'])} rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8') for field in files: self.assertEqual(rfiles[field].name, field) self.assertEqual(rfiles[field].filename, files[field][0]) self.assertEqual(rfiles[field].content_type, files[field][1]) self.assertEqual(rfiles[field].file.read(), to_bytes(files[field][2])) for field in forms: self.assertEqual(rforms[field], forms[field])
def get_posted_image(environ): path = environ['PATH_INFO'] if environ['REQUEST_METHOD'] != 'POST' or path != '/': raise HTTPException('405 Method Not Allowed') content_length = int(environ.get('CONTENT_LENGTH', '-1')) if content_length == -1: raise HTTPException('411 Length Required') elif content_length > PRNET_MAX_IMAGE_SIZE: raise HTTPException('413 Payload Too Large') forms, files = parse_form_data(environ) if len(files) != 1 or 'image' not in files: raise HTTPException("400 Bad Request") img = files['image'] imgfile_path = tempdir.join(img.filename) img.save_as(imgfile_path) return imgfile_path
def test_werkzeug_examples(self): """Tests multipart parsing against data collected from webbrowsers""" for name in browser_test_cases: boundary = browser_test_cases[name]["boundary"] files = browser_test_cases[name]["files"] forms = browser_test_cases[name]["forms"] env = { "REQUEST_METHOD": "POST", "CONTENT_TYPE": "multipart/form-data; boundary=%s" % boundary, "wsgi.input": BytesIO(browser_test_cases[name]["data"]), } rforms, rfiles = mp.parse_form_data(env, strict=True, charset="utf8") for field in files: self.assertEqual(rfiles[field].name, field) self.assertEqual(rfiles[field].filename, files[field][0]) self.assertEqual(rfiles[field].content_type, files[field][1]) self.assertEqual(rfiles[field].file.read(), tob(files[field][2])) for field in forms: self.assertEqual(rforms[field], forms[field])
def createTorrent(self,env,start_response,session): if not 'CONTENT_TYPE' in env: return vanilla.http_error(411,env,start_response,'missing Content-Type header') contentType = env['CONTENT_TYPE'] if 'multipart/form-data' not in contentType: return vanilla.http_error(415,env,start_response,'must be form upload') forms,files = multipart.parse_form_data(env) if 'torrent' not in files or 'title' not in forms: return vanilla.http_error(400,env,start_response,'missing torrent or title') try: extended = json.loads(forms.get('extended','{}')) except ValueError: return vanilla.http_error(400,env,start_response,'bad extended info') if not isinstance(extended,dict): return vanilla.http_error(400,env,start_response,'extended info must be dict') data = files['torrent'].raw try: newTorrent = torrents.Torrent.fromBencodedData(data) except ValueError as e: return vanilla.http_error(400,env,start_response,str(e)) response = {} response['redownload'] = newTorrent.scrub() response['redownload'] |= self.torrents.getAnnounceUrlForUser(session.getId())!=newTorrent.getAnnounceUrl() try: url,infoUrl = self.torrents.addTorrent(newTorrent,forms['title'],session.getId(),extended) except ValueError as e: #Thrown when a torrent already exists with this info hash return vanilla.http_error(400,env,start_response,e.message) response['metainfo'] = { 'href' : url } response['info'] = { 'href' : infoUrl } return vanilla.sendJsonWsgiResponse(env,start_response,response)
def parseForm(request): return parse_form_data(request.env)
def processInputs(self): 'See IPublisherRequest' items = [] # We could simply not parse QUERY_STRING if it's absent, but this # provides slightly better doctest-compatibility with the old code # based on cgi.FieldStorage. self._environ.setdefault('QUERY_STRING', '') if self.method in _get_or_head: # PEP-3333 specifies that strings must only contain codepoints # representable in ISO-8859-1. kwargs = {} if not PYTHON2: kwargs['encoding'] = 'ISO-8859-1' kwargs['errors'] = 'replace' items.extend(parse_qsl( self._environ['QUERY_STRING'], keep_blank_values=True, **kwargs)) elif self.method not in _get_or_head: env = self._environ.copy() env['wsgi.input'] = self._body_instream # cgi.FieldStorage used to set the default Content-Type for POST # requests to a "traditional" value. Do that here for # compatibility. if env.get('REQUEST_METHOD') == 'POST': env.setdefault( 'CONTENT_TYPE', 'application/x-www-form-urlencoded') ctype = env.get('CONTENT_TYPE') # Of course this isn't email, but email.message.Message has # a handy Content-Type parser. msg = Message() msg['Content-Type'] = ctype # cgi.FieldStorage treated any multipart/* Content-Type as # multipart/form-data. This seems a bit dodgy, but for # compatibility we emulate it for now. if ctype is not None and msg.get_content_maintype() == 'multipart': msg.set_type('multipart/form-data') env['CONTENT_TYPE'] = msg['Content-Type'] # cgi.FieldStorage allowed any HTTP method, while # multipart.parse_form_data only allows POST or PUT. However, # it's helpful to support methods such as PATCH too, and # multipart doesn't actually care beyond an initial check, so # just pretend everything is POST from here on. env['REQUEST_METHOD'] = 'POST' forms, files = multipart.parse_form_data( env, charset='ISO-8859-1', memfile_limit=0) items.extend(forms.iterallitems()) for key, item in files.iterallitems(): # multipart puts fields in 'files' even if no upload was # made. We only consider fields to be file uploads if a # filename was passed in and data was uploaded. if item.file: if item.filename: # RFC 7578 section 4.2 says: # Some commonly deployed systems use # multipart/form-data with file names directly # encoded including octets outside the US-ASCII # range. The encoding used for the file names is # typically UTF-8, although HTML forms will use # the charset associated with the form. # So we must decode the filename according to our # usual rules. item.filename = self._decode(item.filename) item = FileUpload(item) else: value = item.value item.file.close() item = value else: item = item.value self.hold(item) items.append((key, item)) if items: self.__meth = None self.__tuple_items = {} self.__defaults = {} # process all entries in the field storage (form) for key, item in items: self.__processItem(key, item) if self.__defaults: self.__insertDefaults() if self.__tuple_items: self.__convertToTuples() if self.__meth: self.setPathSuffix((self.__meth,))