Exemplo n.º 1
0
    def make(
            cls,
            method: str,
            url: str,
            content: Union[bytes, str] = "",
            headers: Union[Headers, Dict[Union[str, bytes], Union[str, bytes]], Iterable[Tuple[bytes, bytes]]] = ()
    ) -> "Request":
        """
        Simplified API for creating request objects.
        """
        # Headers can be list or dict, we differentiate here.
        if isinstance(headers, Headers):
            pass
        elif isinstance(headers, dict):
            headers = Headers(
                (always_bytes(k, "utf-8", "surrogateescape"),
                 always_bytes(v, "utf-8", "surrogateescape"))
                for k, v in headers.items()
            )
        elif isinstance(headers, Iterable):
            headers = Headers(headers)
        else:
            raise TypeError("Expected headers to be an iterable or dict, but is {}.".format(
                type(headers).__name__
            ))

        req = cls(
            "",
            0,
            method.encode("utf-8", "surrogateescape"),
            b"",
            b"",
            b"",
            b"HTTP/1.1",
            headers,
            b"",
            None,
            time.time(),
            time.time(),
        )

        req.url = url
        # Assign this manually to update the content-length header.
        if isinstance(content, bytes):
            req.content = content
        elif isinstance(content, str):
            req.text = content
        else:
            raise TypeError(f"Expected content to be str or bytes, but is {type(content).__name__}.")

        return req
Exemplo n.º 2
0
def parse_upstream_auth(auth):
    pattern = re.compile(".+:")
    if pattern.search(auth) is None:
        raise exceptions.OptionsError(
            "Invalid upstream auth specification: %s" % auth
        )
    return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
Exemplo n.º 3
0
    def make(
            cls,
            status_code: int = 200,
            content: Union[bytes, str] = b"",
            headers: Union[Headers, Mapping[str, Union[str, bytes]], Iterable[Tuple[bytes, bytes]]] = ()
    ) -> "Response":
        """
        Simplified API for creating response objects.
        """
        if isinstance(headers, Headers):
            headers = headers
        elif isinstance(headers, dict):
            headers = Headers(
                (always_bytes(k, "utf-8", "surrogateescape"),
                 always_bytes(v, "utf-8", "surrogateescape"))
                for k, v in headers.items()
            )
        elif isinstance(headers, Iterable):
            headers = Headers(headers)
        else:
            raise TypeError("Expected headers to be an iterable or dict, but is {}.".format(
                type(headers).__name__
            ))

        resp = cls(
            b"HTTP/1.1",
            status_code,
            status_codes.RESPONSES.get(status_code, "").encode(),
            headers,
            None,
            None,
            time.time(),
            time.time(),
        )

        # Assign this manually to update the content-length header.
        if isinstance(content, bytes):
            resp.content = content
        elif isinstance(content, str):
            resp.text = content
        else:
            raise TypeError(f"Expected content to be str or bytes, but is {type(content).__name__}.")

        return resp
Exemplo n.º 4
0
    def apply_proxy_auth(self, f):
        """Apply proxy authorization to the request if configured."""
        auth = None

        try:
            auth = self.config.options.upstream_custom_auth
        except AttributeError:
            pass

        if not auth:
            try:
                auth = self.config.options.upstream_auth

                if auth:
                    auth = b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
            except AttributeError:
                pass

        if auth:
            f.request.headers["Proxy-Authorization"] = auth
Exemplo n.º 5
0
def create_server_nonce(client_nonce):
    return base64.b64encode(
        hashlib.sha1(strutils.always_bytes(client_nonce) + MAGIC).digest())
Exemplo n.º 6
0
 def http_version(self, http_version: Union[str, bytes]) -> None:
     self.data.http_version = strutils.always_bytes(http_version, "utf-8", "surrogateescape")
Exemplo n.º 7
0
 def path(self, val: Union[str, bytes]) -> None:
     self.data.path = always_bytes(val, "utf-8", "surrogateescape")
Exemplo n.º 8
0
 def scheme(self, val: Union[str, bytes]) -> None:
     self.data.scheme = always_bytes(val, "utf-8", "surrogateescape")
Exemplo n.º 9
0
 def method(self, val: Union[str, bytes]) -> None:
     self.data.method = always_bytes(val, "utf-8", "surrogateescape")
Exemplo n.º 10
0
def _always_bytes(x):
    return strutils.always_bytes(x, "utf-8", "surrogateescape")
Exemplo n.º 11
0
 def reason(self, reason: Union[str, bytes]) -> None:
     self.data.reason = strutils.always_bytes(reason, "ISO-8859-1")