def __init__( self, method: Union[str, bytes, Item, None], target: Union[str, bytes, Item, None], protocol: Union[str, bytes, Item, None], headers: Union[dict, Headers, None] = None, body: Union[str, bytes, Item, None] = None, ) -> None: self._method = Item(method) self._target = Item(target) self._protocol = Item(protocol) first_line = (self._method.raw, self._target.raw, self._protocol.raw) super().__init__(b"%b %b %b" % first_line, headers, body)
def __init__( self, protocol: Union[str, bytes, Item, None], status: Union[str, bytes, int, Item, None], status_msg: Union[str, bytes, Item, None], headers: Union[dict, Headers, None] = None, body: Union[str, bytes, Item, None] = None, ) -> None: self._protocol = Item(protocol) self._status = Item(status) self._status_msg = Item(status_msg) first_line = self._protocol.raw, self._status.raw, self._status_msg.raw super().__init__(b"%b %b %b" % first_line, headers, body)
def __init__( self, first_line: Union[str, bytes, Item, None], headers: Union[dict, Headers, None] = None, body: Union[str, bytes, Item, None] = None, ) -> None: self._first_line = Item(first_line) if isinstance(headers, Headers): self._headers = headers elif headers == None: self._headers = Headers() else: self._headers = Headers(headers) self._body = Item(body)
def parse( cls: Union[Response, Request], message: Union[str, bytes] ) -> Union[Request, Response]: """Parses a raw ``Message``. Args: message (Union[str, bytes]): The raw string or bytes representation of a HTTP request or response. Returns: Union[Request, Response]: A ``Request`` or ``Response`` object. """ if not isinstance(message, Item): message = Item(message) first_line = b"" headers = Headers({}) body = b"" top_frame = True for index, line in enumerate(message.raw.splitlines()): if line == b"": top_frame = False if top_frame: if index == 0: first_line = line elif b":" in line: key, value = line.split(b":", 1) headers += {key: value.lstrip(b" ")} elif line != b"": body += line args = (*first_line.split(b" "), headers, body) try: instance = cls(*args) except TypeError: # pragma: no cover err = "You cannot use parse with type Message. Use Request or Response." raise TypeError(err) return instance
def status_msg(self, value: Union[str, bytes, Item, None]) -> None: self._status_msg = Item(value)
def protocol(self, value: Union[str, bytes, Item, None]) -> None: self._protocol = Item(value)
def _compile_first_line(self) -> None: """Sets the ``Response`` first line to ``protocol status status_msg`` values. """ first_line = self._protocol.raw, self._status.raw, self._status_msg.raw self._first_line = Item(b"%b %b %b" % first_line)
def target(self, value: Union[str, bytes, Item, None]) -> None: self._target = Item(value)
def method(self, value: Union[str, bytes, Item, None]) -> None: self._method = Item(value)
def _compile_first_line(self) -> None: """ Sets the ``Request`` first line to ``method target protocol`` values. """ first_line = (self._method.raw, self._target.raw, self._protocol.raw) self._first_line = Item(b"%b %b %b" % first_line)
def first_line(self, value: Union[str, bytes]) -> None: self._first_line = Item(value) self._parse_first_line()
def body(self, value: Union[str, bytes, int, Item, None]) -> None: self._body = Item(value)