def insert_data(environ, start_response): with eliot.start_action(action_type='api:insert-data') as action: instream = make_line_iter( get_input_stream(environ, safe_fallback=False)) lines = (json.loads(line.decode('utf-8')) for line in instream) keys = db.insert(lines) action.add_success_fields(inserted_count=len(keys)) return {'message': 'Inserted OK', 'keys': keys}
def forward_request(environ, start_response): body = get_input_stream(environ, safe_fallback=False) while True: data = body.read(65536) if data == '': break ping() start_response('200 OK', TEXT_HEADER) return ['Success']
def __call__(self, environ, start_response): content_encoding = environ.get('HTTP_CONTENT_ENCODING', '').lower().strip() if content_encoding == 'gzip': compressed_body = get_input_stream(environ).read() body = gzip.GzipFile(fileobj=StringIO(compressed_body)).read() environ['wsgi.input'] = StringIO(body) environ['CONTENT_LENGTH'] = str(len(body)) del environ['HTTP_CONTENT_ENCODING'] return self.app(environ, start_response)
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 gzip(environ, start_response): compressor = zlib.compressobj(9, zlib.DEFLATED, 31) body = get_input_stream(environ, safe_fallback=False) start_response('200 OK', BINARY_HEADER) while True: data = body.read(65536) if data == '': break yield compressor.compress(data) yield compressor.flush(zlib.Z_FINISH)
def __call__(self, environ, start_response): if environ["REQUEST_METHOD"].upper() == "POST": environ["wsgi.input"] = stream = \ BytesIO(get_input_stream(environ).read()) formdata = parse_form_data(environ)[1] stream.seek(0) method = formdata.get('_method', '').upper() if method in ('GET', 'POST', 'PUT', 'DELETE'): environ['REQUEST_METHOD'] = method return self.app(environ, start_response)
def sha512(environ, start_response): digest = hashlib.sha512() body = get_input_stream(environ, safe_fallback=False) while True: data = body.read(65536) if data == '': break digest.update(data) start_response('200 OK', TEXT_HEADER) return [digest.hexdigest()]
def __call__(self, environ, start_response): if environ['REQUEST_METHOD'].upper() == 'POST': environ['wsgi.input'] = stream = \ BytesIO(get_input_stream(environ).read()) formdata = parse_form_data(environ)[1] stream.seek(0) method = formdata.get('_method', '').upper() if method in ('GET', 'POST', 'PUT', 'DELETE'): environ['REQUEST_METHOD'] = method return self.app(environ, start_response)
def to_request(self, environ): werkzeug_request = WerkzeugRequest(environ) request = Request(server_port=environ.get('SERVER_PORT', None), server_name=environ.get('SERVER_NAME', None), remote_addr=werkzeug_request.remote_addr, uri=werkzeug_request.path, script_name=werkzeug_request.script_root, query_string=werkzeug_request.query_string, scheme=werkzeug_request.scheme, method=werkzeug_request.method, headers=dict(werkzeug_request.headers), body=get_input_stream(environ).read()) return request
def __call__(self, environ, start_response): # type: (WSGIEnvironment, StartResponse) -> Iterable[bytes] content_encoding = environ.get('HTTP_CONTENT_ENCODING', '').lower().strip() if content_encoding == 'gzip': compressed_body = get_input_stream(environ).read() try: body = gzip.GzipFile(fileobj=BytesIO(compressed_body)).read() except IOError as e: if 'Not a gzipped file' in str(e): bad_request = BadRequest() return bad_request(environ, start_response) raise environ['wsgi.input'] = BytesIO(body) environ['CONTENT_LENGTH'] = str(len(body)) del environ['HTTP_CONTENT_ENCODING'] return self.app(environ, start_response)
def to_request(self, environ): uri = wsgi_decoding_dance(environ.get('PATH_INFO', ''), 'utf-8') uri = '/' + uri.lstrip('/') request = Request( server_port=environ.get('SERVER_PORT', None), server_name=environ.get('SERVER_NAME', None), remote_addr=environ.get('REMOTE_ADDR', None), uri=uri, script_name=wsgi_decoding_dance(environ.get('SCRIPT_NAME', ''), 'utf-8'), query_string=environ.get('QUERY_STRING', '').encode('latin1'), scheme=environ.get('wsgi.url_scheme', None), method=environ.get('REQUEST_METHOD', "").upper(), headers=self._get_wsgi_headers(environ), body=get_input_stream(environ).read() ) return request
def to_request(self, environ): uri = wsgi_decoding_dance(environ.get('PATH_INFO', ''), 'utf-8') uri = '/' + uri.lstrip('/') request = Request(server_port=environ.get('SERVER_PORT', None), server_name=environ.get('SERVER_NAME', None), remote_addr=environ.get('REMOTE_ADDR', None), uri=uri, script_name=wsgi_decoding_dance( environ.get('SCRIPT_NAME', ''), 'utf-8'), query_string=environ.get('QUERY_STRING', '').encode('latin1'), scheme=environ.get('wsgi.url_scheme', None), method=environ.get('REQUEST_METHOD', "").upper(), headers=self._get_wsgi_headers(environ), body=get_input_stream(environ).read()) return request
def to_request(self, environ): werkzeug_request = WerkzeugRequest(environ) request = Request( server_port=environ.get('SERVER_PORT', None), server_name=environ.get('SERVER_NAME', None), remote_addr=werkzeug_request.remote_addr, uri=werkzeug_request.path, script_name=werkzeug_request.script_root, query_string=werkzeug_request.query_string, scheme=werkzeug_request.scheme, method=werkzeug_request.method, headers=CaseInsensitiveDict(dict(werkzeug_request.headers)), body=get_input_stream(environ).read() ) return request
def __call__(self, environ, start_response): encoding = environ.pop('HTTP_CONTENT_ENCODING', 'identity') if encoding == 'gzip': environ['wsgi.input'] = gzip.GzipFile( fileobj=get_input_stream(environ), mode='rb') # Content length is no longer correct after unzipping. environ.pop('CONTENT_LENGTH', None) # HACK: Force werkzeug to read the stream without the content length. environ['wsgi.input_terminated'] = True elif encoding != 'identity': error = "Unsupported content encoding: %r" % encoding return BadRequest(error)(environ, start_response) try: return self.app(environ, start_response) except (OSError, zlib.error) as ex: # gzip raises OSError on invalid input... blah. error = "Failed to decode input: %s" % ex return BadRequest(error)(environ, start_response)
def save_chunk(self, environ, mailbox, message_id, chunk_num): instream = get_input_stream(environ, safe_fallback=False) filename = self.get_filename(mailbox, message_id, chunk_num) with open(filename, 'wb') as f: if environ.get('HTTP_CONTENT_ENCODING') == 'gzip': while True: data = instream.read(IO_BLOCK_SIZE) if data: f.write(data) else: break else: compressor = zlib.compressobj(9, zlib.DEFLATED, 31) while True: data = instream.read(IO_BLOCK_SIZE) if data: f.write(compressor.compress(data)) else: f.write(compressor.flush(zlib.Z_FINISH)) break
def get_request_data(environ: WSGIEnviron): if not bool(environ.get('CONTENT_TYPE')): mimetype = None else: mimetype, _ = parse_options_header(environ['CONTENT_TYPE']) if mimetype is None: value = None elif mimetype == 'application/json': body = get_input_stream(environ).read() value = json.loads(body.decode('utf-8')) elif mimetype in ('multipart/form-data', 'application/x-www-form-urlencoded'): parser = FormDataParser() stream, form, files = parser.parse_from_environ(environ) value = ImmutableMultiDict(list(form.items()) + list(files.items())) else: raise exceptions.UnsupportedMediaType() return value
def build(cls, environ: WSGIEnviron): if not bool(environ.get('CONTENT_TYPE')): mimetype = None else: mimetype, _ = parse_options_header(environ['CONTENT_TYPE']) if mimetype is None: value = None elif mimetype == 'application/json': body = get_input_stream(environ).read() value = json.loads(body.decode('utf-8')) elif mimetype in ('multipart/form-data', 'application/x-www-form-urlencoded'): stream, form, files = parse_form_data(environ) value = ImmutableMultiDict(list(form.items()) + list(files.items())) else: raise exceptions.UnsupportedMediaType() if cls.schema is None: return value if not isinstance(value, cls.schema): value = validate(cls.schema, value) return value
def build(cls, environ: WSGIEnviron): return get_input_stream(environ).read()
def resolve(self, environ: WSGIEnviron) -> http.Body: return http.Body(get_input_stream(environ).read())
def get_body(environ: WSGIEnviron): return get_input_stream(environ).read()
def get_stream(environ: WSGIEnviron): return get_input_stream(environ)
def resolve(self, environ: WSGIEnviron) -> RequestStream: return get_input_stream(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)