@@ -4,94 +4,84 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
from io import BufferedReader
|
||||
|
||||
BUFF_SIZE = 1024
|
||||
CRLF = b'\r\n'
|
||||
|
||||
def bytes_next(self):
|
||||
if self.status == 'eof':
|
||||
|
||||
def handler_status(response):
|
||||
status = ' '.join([
|
||||
response.version,
|
||||
str(response.code),
|
||||
response.phrase
|
||||
])
|
||||
raw = status.encode()
|
||||
raw += CRLF
|
||||
return raw
|
||||
|
||||
|
||||
def handler_headers(response):
|
||||
raw = b''
|
||||
response.headers['Content-Type'] = response.content_type
|
||||
for key in response.headers:
|
||||
value = response.headers[key]
|
||||
raw += key.encode() + b': ' + value.encode() + CRLF
|
||||
raw += CRLF
|
||||
return raw
|
||||
|
||||
|
||||
def bytes_next(state):
|
||||
if state.status == 'eof':
|
||||
raise StopIteration
|
||||
response = self.response
|
||||
crlf = b'\r\n'
|
||||
if self.position == 'status':
|
||||
status = ' '.join([
|
||||
response.version,
|
||||
str(response.code),
|
||||
response.phrase
|
||||
])
|
||||
raw = status.encode()
|
||||
raw += crlf
|
||||
self.position = 'headers'
|
||||
response = state.response
|
||||
if state.position == 'status':
|
||||
raw = handler_status(response)
|
||||
state.position = 'headers'
|
||||
return raw
|
||||
if self.position == 'headers':
|
||||
self.position = 'body'
|
||||
raw = b''
|
||||
for key in response.headers:
|
||||
value = response.headers[key]
|
||||
raw += key.encode() + b' ' + value.encode() + crlf
|
||||
raw += crlf
|
||||
if state.position == 'headers':
|
||||
raw = handler_headers(response)
|
||||
state.position = 'body'
|
||||
return raw
|
||||
if self.position == 'body':
|
||||
self.status = 'eof'
|
||||
if state.position == 'body':
|
||||
state.status = 'eof'
|
||||
return response.data
|
||||
|
||||
|
||||
def str_next(instance):
|
||||
if instance.status == 'eof':
|
||||
def str_next(state):
|
||||
if state.status == 'eof':
|
||||
raise StopIteration
|
||||
response = instance.response
|
||||
crlf = b'\r\n'
|
||||
if instance.position == 'status':
|
||||
status = ' '.join([
|
||||
response.version,
|
||||
str(response.code),
|
||||
response.phrase
|
||||
])
|
||||
raw = status.encode()
|
||||
raw += crlf
|
||||
instance.position = 'headers'
|
||||
response = state.response
|
||||
CRLF = b'\r\n'
|
||||
if state.position == 'status':
|
||||
raw = handler_status(response)
|
||||
state.position = 'headers'
|
||||
return raw
|
||||
if instance.position == 'headers':
|
||||
instance.position = 'body'
|
||||
raw = b''
|
||||
response.headers['Content-Type'] = response.content_type
|
||||
for key in response.headers:
|
||||
value = response.headers[key]
|
||||
raw += key.encode() + b' ' + value.encode() + crlf
|
||||
raw += crlf
|
||||
if state.position == 'headers':
|
||||
raw = handler_headers(response)
|
||||
state.position = 'body'
|
||||
return raw
|
||||
if instance.position == 'body':
|
||||
if state.position == 'body':
|
||||
raw = response.data.encode()
|
||||
instance.status = 'eof'
|
||||
state.status = 'eof'
|
||||
return raw
|
||||
|
||||
|
||||
def stream_next(self):
|
||||
if self.status == 'eof':
|
||||
def stream_next(state):
|
||||
if state.status == 'eof':
|
||||
raise StopIteration
|
||||
response = self.response
|
||||
crlf = b'\r\n'
|
||||
if self.position == 'status':
|
||||
status = ' '.join([
|
||||
response.version,
|
||||
str(response.code),
|
||||
response.phrase
|
||||
])
|
||||
raw = status.encode()
|
||||
raw += crlf
|
||||
self.position = 'headers'
|
||||
response = state.response
|
||||
CRLF = b'\r\n'
|
||||
if state.position == 'status':
|
||||
raw = handler_status(response)
|
||||
state.position = 'headers'
|
||||
return raw
|
||||
if self.position == 'headers':
|
||||
self.position = 'body'
|
||||
raw = b''
|
||||
for key in response.headers:
|
||||
value = response.headers[key]
|
||||
raw += key.encode() + b' ' + value.encode() + crlf
|
||||
raw += crlf
|
||||
if state.position == 'headers':
|
||||
state.position = 'body'
|
||||
return raw
|
||||
if self.position == 'body':
|
||||
if state.position == 'body':
|
||||
raw = response.data.read(BUFF_SIZE)
|
||||
if raw:
|
||||
return raw
|
||||
else:
|
||||
self.status = 'eof'
|
||||
state.status = 'eof'
|
||||
raise StopIteration
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user