client: fixed GET

This commit is contained in:
2021-03-20 21:45:28 +01:00
parent 797cdb0c0e
commit fa8d08d63d
5 changed files with 341 additions and 142 deletions

View File

@@ -1,21 +1,35 @@
import logging
import re
import socket
from typing import Dict
from io import BufferedReader
from typing import TextIO, IO
BUFSIZE = 4096
TIMEOUT = 3
FORMAT = "UTF-8"
MAXLINE = 4096
class HTTPClient(socket.socket):
host: str
file: BufferedReader
def __init__(self, host: str):
super().__init__(socket.AF_INET, socket.SOCK_STREAM)
self.settimeout(TIMEOUT)
self.host = host
self.setblocking(True)
self.settimeout(3.0)
self.file = self.makefile("rb")
def close(self):
self.file.close()
super().close()
def reset_request(self):
self.file.close()
self.file = self.makefile("rb")
def _do_receive(self):
if self.fileno() == -1:
@@ -41,6 +55,26 @@ class HTTPClient(socket.socket):
logging.debug("Timed out after waiting %s seconds for response", TIMEOUT * count)
raise TimeoutError("Request timed out")
def read(self, size=BUFSIZE, blocking=True) -> bytes:
if blocking:
return self.file.read(size)
return self.file.read1(size)
def read_line(self):
return str(self.read_bytes_line(), FORMAT)
def read_bytes_line(self):
"""
:rtype: bytes
"""
line = self.file.readline(MAXLINE + 1)
if len(line) > MAXLINE:
raise InvalidResponse("Line too long")
return line
def validate_status_line(self, status_line: str):
split = list(filter(None, status_line.split(" ")))
if len(split) < 3:
@@ -129,3 +163,7 @@ class UnsupportedEncoding(HTTPException):
def __init(self, enc_type, encoding):
self.enc_type = enc_type
self.encoding = encoding
class IncompleteResponse(HTTPException):
def __init(self, cause):
self.cause = cause