Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle_status_3xx (response):
global _allow_redirects , _que
if not _allow_redirects:
return response
if response.status_code not in (301, 302, 307, 308):
return response
newloc = response.get_header ('location')
oldloc = response.request.uri
request = response.request
if newloc == oldloc:
response.response = http_response.FailedResponse (711, "Redirect Error", request)
return response
try:
request.relocate (response.response, newloc)
except RuntimeError:
response.response = http_response.FailedResponse (711, "Redirect Error", request)
return response
#_logger ("%s redirected to %s from %s" % (response.status_code, newloc, oldloc), "info")
# DO NOT use relocated response.request, it is None
_reque_first (request)
def handle_events (self, events):
for event in events:
if isinstance(event, ResponseReceived):
self.handle_response (event.stream_id, event.headers)
elif isinstance(event, TrailersReceived):
self.handle_trailers (event.stream_id, event.headers)
elif isinstance(event, StreamReset):
if event.remote_reset:
h = self.get_handler (event.stream_id)
if h:
h.response = http_response.FailedResponse (721, respcodes.get (721), h.request)
h.handle_callback ()
self.remove_handler (event.stream_id)
elif isinstance(event, ConnectionTerminated):
self.asyncon.handle_close (721, "HTTP2 Connection Terminated")
elif isinstance (event, DataReceived):
h = self.get_handler (event.stream_id)
if h:
h.collect_incoming_data (event.data)
self.adjust_flow_control_window (event.stream_id)
elif isinstance(event, StreamEnded):
h = self.get_handler (event.stream_id)
if h:
self.remove_handler (event.stream_id)
def connection_closed (self, why, msg):
if self.response is None:
self.response = response.FailedResponse (why, msg)
self.close_case ()
def connection_closed (self, why, msg):
with self._llock:
requests = list (self.requests.items ())
for stream_id, h in requests:
h.response = http_response.FailedResponse (720, respcodes.get (720), h.request)
h.handle_callback ()
with self._llock:
self.requests = {}
def finish_handshake (self):
if self.response.code == 200:
self.asyncon.established = True
self.response = None
self.convert_to_ssl ()
for buf in self.get_request_buffer ():
self.asyncon.push (buf)
else:
self.response = response.FailedResponse (self.response.code, self.response.msg)
self.asyncon.handle_close (720, "Returned %d %s" % (self.response.code, self.response.msg))
def create_response (self):
buffer, self.buffer = self.buffer, b""
try:
response = http_response.Response (self.request, buffer.decode ("utf8"))
if self.handle_response_code (response):
return
except ProtocolSwitchError:
return self.asyncon.handle_error (716)
except:
self.log ("response header error: `%s`" % repr (buffer [:80]), "error")
return self.asyncon.handle_error (715)
ct = response.check_accept ()
if ct:
self.log ("response content-type error: `%s`" % ct, "error")
return self.asyncon.handle_close (718)
cl = response.check_max_content_length ()
if cl:
self.log ("response content-length error: `%d`" % cl, "error")
from ..http import response
class Response (response.Response):
def __init__ (self, request, code, msg, opcode = None, data = None):
self.request = request
self.code = code
self.msg = msg
self.__data = []
if opcode:
self.__data.append ((opcode, data))
self.version = "1.1"
def add_message (self, opcode, data = None):
self.__data.append ((opcode, data))
@property
def content (self):
if not self.__data:
return None
def collect_incoming_data (self, data):
if not data:
self.end_of_data = True
return
if not self.response or self.asyncon.get_terminator () == b"\r\n":
self.buffer += data
else:
try:
self.response.collect_incoming_data (data)
except http_response.ContentLimitReached:
self.asyncon.handle_error (719)
return response
if response.status_code not in (301, 302, 307, 308):
return response
newloc = response.get_header ('location')
oldloc = response.request.uri
request = response.request
if newloc == oldloc:
response.response = http_response.FailedResponse (711, "Redirect Error", request)
return response
try:
request.relocate (response.response, newloc)
except RuntimeError:
response.response = http_response.FailedResponse (711, "Redirect Error", request)
return response
#_logger ("%s redirected to %s from %s" % (response.status_code, newloc, oldloc), "info")
# DO NOT use relocated response.request, it is None
_reque_first (request)