Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wrapped_start_response(status, response_headers, exc_info=None):
response_headers.append(('Server',
http_runtime_constants.SERVER_SOFTWARE))
if should_log_request:
headers = wsgiref.headers.Headers(response_headers)
status_code = int(status.split(' ', 1)[0])
content_length = int(headers.get('Content-Length', 0))
# TODO: Remove after the Files API is really gone.
if (self._filesapi_warning_message is not None
and self._request_data.was_filesapi_used(request_id)):
logging.warning(self._filesapi_warning_message)
self._insert_log_message(self._filesapi_warning_message,
2, request_id)
logservice.end_request(request_id, status_code, content_length)
if any(resource.startswith(prefix) for prefix in _QUIETER_RESOURCES):
level = logging.DEBUG
else:
level = logging.INFO
logging.log(level, '%(module_name)s: '
'"%(method)s %(resource)s %(http_version)s" '
'%(status)d %(content_length)s',
def get_headers_from_environ(environ):
"""Get a wsgiref.headers.Headers object with headers from the environment.
Headers in environ are prefixed with 'HTTP_', are all uppercase, and have
had dashes replaced with underscores. This strips the HTTP_ prefix and
changes underscores back to dashes before adding them to the returned set
of headers.
Args:
environ: An environ dict for the request as defined in PEP-333.
Returns:
A wsgiref.headers.Headers object that's been filled in with any HTTP
headers found in environ.
"""
headers = wsgiref.headers.Headers([])
for header, value in environ.iteritems():
if header.startswith('HTTP_'):
headers[header[5:].replace('_', '-')] = value
# Content-Type is special; it does not start with 'HTTP_'.
if 'CONTENT_TYPE' in environ:
headers['CONTENT-TYPE'] = environ['CONTENT_TYPE']
return headers
def __init__(self):
"""Constructs a response with the default settings."""
self.out = StringIO.StringIO()
self.__wsgi_headers = []
self.headers = wsgiref.headers.Headers(self.__wsgi_headers)
self.headers['Content-Type'] = 'text/html; charset=utf-8'
self.headers['Cache-Control'] = 'no-cache'
self.set_status(200)
def __init__(self, environ):
super(Context, self).__init__()
self.environ = environ
self.response_headers = wsgiref.headers.Headers()
self.response_cookies = []
def __init__(self, environ, status, headers, body):
"""Create a new RewriterState.
Args:
environ: An environ dict for the current request as defined in PEP-333.
status: A status code and message as a string. (e.g., '200 OK'.)
headers: A list of tuples containing the response headers.
body: An iterable of strings containing the response body.
"""
self.environ = environ
self.status = status
self.headers = wsgiref.headers.Headers(headers)
self.body = body
self.allow_large_response = False
except httplib.HTTPException as e:
# The runtime process has written a bad HTTP response. For example,
# a Go runtime process may have crashed in app-specific code.
yield self._handle_error(
'the runtime process gave a bad HTTP response: %s' % e,
start_response)
return
# Ensures that we avoid merging repeat headers into a single header,
# allowing use of multiple Set-Cookie headers.
headers = []
for name in response.msg:
for value in response.msg.getheaders(name):
headers.append((name, value))
response_headers = wsgiref.headers.Headers(headers)
error_file = self._get_error_file()
if (error_file and
http_runtime_constants.ERROR_CODE_HEADER in response_headers):
try:
with open(error_file) as f:
content = f.read()
except IOError:
content = 'Failed to load error handler'
logging.exception('failed to load error file: %s', error_file)
start_response('500 Internal Server Error',
[('Content-Type', 'text/html'),
('Content-Length', str(len(content)))])
yield content
return
del response_headers[http_runtime_constants.ERROR_CODE_HEADER]
def update_headers(self, headers_in):
"""Add CORS headers to the response, if needed."""
if not self.allow_cors_request:
return
# Add CORS headers.
headers = wsgiref.headers.Headers(headers_in)
headers[_CORS_HEADER_ALLOW_ORIGIN] = self.origin
headers[_CORS_HEADER_ALLOW_METHODS] = ','.join(tuple(
_CORS_ALLOWED_METHODS))
if self.cors_request_headers is not None:
headers[_CORS_HEADER_ALLOW_HEADERS] = self.cors_request_headers
except httplib.HTTPException as e:
# The runtime process has written a bad HTTP response. For example,
# a Go runtime process may have crashed in app-specific code.
yield self._respond_with_error(
'the runtime process gave a bad HTTP response: %s' % e,
start_response)
return
# Ensures that we avoid merging repeat headers into a single header,
# allowing use of multiple Set-Cookie headers.
headers = []
for name in response.msg:
for value in response.msg.getheaders(name):
headers.append((name, value))
response_headers = wsgiref.headers.Headers(headers)
if self._error_handler_file and (
http_runtime_constants.ERROR_CODE_HEADER in response_headers):
try:
with open(self._error_handler_file) as f:
content = f.read()
except IOError:
content = 'Failed to load error handler'
logging.exception('failed to load error file: %s',
self._error_handler_file)
start_response('500 Internal Server Error',
[('Content-Type', 'text/html'),
('Content-Length', str(len(content)))])
yield content
return
del response_headers[http_runtime_constants.ERROR_CODE_HEADER]
def __init__(self, environ, status, headers, body):
"""Create a new RewriterState.
Args:
environ: An environ dict for the current request as defined in PEP-333.
status: A status code and message as a string. (e.g., '200 OK'.)
headers: A list of tuples containing the response headers.
body: An iterable of strings containing the response body.
"""
self.environ = environ
self.status = status
self.headers = wsgiref.headers.Headers(headers)
self.body = body
self.allow_large_response = False
def __init__(self, connection):
self._connection = connection
self.headers = headers.Headers([('Transfer-Encoding', 'chunked')])
self.out = OutFile(self._connection)