示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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
示例#5
0
 def status_msg(self, value: Union[str, bytes, Item, None]) -> None:
     self._status_msg = Item(value)
示例#6
0
 def protocol(self, value: Union[str, bytes, Item, None]) -> None:
     self._protocol = Item(value)
示例#7
0
 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)
示例#8
0
 def target(self, value: Union[str, bytes, Item, None]) -> None:
     self._target = Item(value)
示例#9
0
 def method(self, value: Union[str, bytes, Item, None]) -> None:
     self._method = Item(value)
示例#10
0
 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)
示例#11
0
 def first_line(self, value: Union[str, bytes]) -> None:
     self._first_line = Item(value)
     self._parse_first_line()
示例#12
0
 def body(self, value: Union[str, bytes, int, Item, None]) -> None:
     self._body = Item(value)