def _perform_method_overloading(self): """ Perform method and content type overloading. Provides support for browser PUT, PATCH, DELETE & other requests, by specifing a '_method' form field. Also provides support for browser non-form requests (eg JSON), by specifing '_content' and '_content_type' form fields. """ self._method = super(APIRequest, self).method self._stream = super(APIRequest, self).stream self._content_type = self.headers.get("Content-Type") self._content_length = get_content_length(self.environ) if self._method == "POST" and self._content_type == "application/x-www-form-urlencoded": # Read the request data, then push it back onto the stream again. body = self.get_data() data = url_decode_stream(io.BytesIO(body)) self._stream = io.BytesIO(body) if "_method" in data: # Support browser forms with PUT, PATCH, DELETE & other methods. self._method = data["_method"] if "_content" in data and "_content_type" in data: # Support browser forms with non-form data, such as JSON. body = data["_content"].encode("utf8") self._stream = io.BytesIO(body) self._content_type = data["_content_type"] self._content_length = len(body)
def _perform_method_overloading(self): """ Perform method and content type overloading. Provides support for browser PUT, PATCH, DELETE & other requests, by specifing a '_method' form field. Also provides support for browser non-form requests (eg JSON), by specifing '_content' and '_content_type' form fields. """ self._method = super(APIRequest, self).method self._stream = super(APIRequest, self).stream self._content_type = self.headers.get('Content-Type') self._content_length = get_content_length(self.environ) if (self._method == 'POST' and self._content_type == 'application/x-www-form-urlencoded'): # Read the request data, then push it back onto the stream again. body = self.get_data() data = url_decode_stream(io.BytesIO(body)) self._stream = io.BytesIO(body) if '_method' in data: # Support browser forms with PUT, PATCH, DELETE & other methods. self._method = data['_method'] if '_content' in data and '_content_type' in data: # Support browser forms with non-form data, such as JSON. body = data['_content'].encode('utf8') self._stream = io.BytesIO(body) self._content_type = data['_content_type'] self._content_length = len(body)
def __init__(self, environ, populate_request=True, shallow=False): environ = copy(environ) stream = ProgressWsgiInputWrapper(environ['wsgi.input'], get_content_length(environ)) environ['wsgi.input'] = stream super(FileShareRequest, self).__init__(environ, populate_request, shallow) progress_streams[self.path] = stream
def parse_from_environ(self, environ): """Parses the information from the environment as form data. :param environ: the WSGI environment to be used for parsing. :return: A tuple in the form ``(stream, form, files)``. """ content_type = environ.get("CONTENT_TYPE", "") content_length = get_content_length(environ) mimetype, options = parse_options_header(content_type) return self.parse(get_input_stream(environ), mimetype, content_length, options)
def upload(): temp_files = [ open('./data/stream-split-a', mode='wb'), open('./data/stream-split-b', mode='wb'), open('./data/stream-split-c', mode='wb'), ] spx = SubprocessInputStreamer( ['exiftool', '-'], popen_kwargs={'stdin': subprocess.PIPE, 'stdout': subprocess.PIPE} ) writer = SplitStreamWriter(temp_files + [SubprocessStreamProxy(spx)]) content_length = get_content_length(request.environ) print('Starting to read request: content_length=%s' % content_length) start = time.time() stream = request.stream while True: data = stream.read(500 * 1024) if not data: break writer.write(data) writer.flush() end = time.time() print('Finished reading request: time=%s' % (end - start)) print('Temp files: %s' % [f.name for f in temp_files]) for f in temp_files: f.close() if spx.error: print('SPX ERROR: error=%s, spx.stderr=%s' % (spx.error, spx.stderr)) else: print('SPX: ') print(spx.stdout.decode() if spx.stdout else '<NO STDOUT>') print(spx.stderr.decode() if spx.stderr else '<NO STDERR>') return Response(status=200)
def content_length(self): """The content length of the request payload.""" return get_content_length(self.environ)
def parse_from_environ(self, environ): content_type = environ.get('CONTENT_TYPE', '') content_length = get_content_length(environ) mimetype, options = parse_options_header(content_type) return self._parse_multipart(get_input_stream(environ), mimetype, content_length, options)
def __call__(self, environ, start_response): stream = environ['wsgi.input'] content_length = get_content_length(environ) environ['wsgi.input'] = SizeLimitStream( stream, content_length, self.max_content_length) return self.app(environ, start_response)