Edit on GitHub

mitmproxy.net.server_spec

Server specs are used to describe an upstream proxy or server.

 1"""
 2Server specs are used to describe an upstream proxy or server.
 3"""
 4import functools
 5import re
 6from typing import Literal, NamedTuple
 7
 8from mitmproxy.net import check
 9
10
11class ServerSpec(NamedTuple):
12    scheme: Literal["http", "https"]
13    address: tuple[str, int]
14
15
16server_spec_re = re.compile(
17    r"""
18        ^
19        (?:(?P<scheme>\w+)://)?  # scheme is optional
20        (?P<host>[^:/]+|\[.+\])  # hostname can be DNS name, IPv4, or IPv6 address.
21        (?::(?P<port>\d+))?  #  port is optional
22        /?  #  we allow a trailing backslash, but no path
23        $
24        """,
25    re.VERBOSE,
26)
27
28
29@functools.lru_cache
30def parse(server_spec: str) -> ServerSpec:
31    """
32    Parses a server mode specification, e.g.:
33
34     - http://example.com/
35     - example.org
36     - example.com:443
37
38    *Raises:*
39     - ValueError, if the server specification is invalid.
40    """
41    m = server_spec_re.match(server_spec)
42    if not m:
43        raise ValueError(f"Invalid server specification: {server_spec}")
44
45    if m.group("scheme"):
46        scheme = m.group("scheme")
47    else:
48        scheme = "https" if m.group("port") in ("443", None) else "http"
49    if scheme not in ("http", "https"):
50        raise ValueError(f"Invalid server scheme: {scheme}")
51
52    host = m.group("host")
53    # IPv6 brackets
54    if host.startswith("[") and host.endswith("]"):
55        host = host[1:-1]
56    if not check.is_valid_host(host):
57        raise ValueError(f"Invalid hostname: {host}")
58
59    if m.group("port"):
60        port = int(m.group("port"))
61    else:
62        port = {"http": 80, "https": 443}[scheme]
63    if not check.is_valid_port(port):
64        raise ValueError(f"Invalid port: {port}")
65
66    return ServerSpec(scheme, (host, port))  # type: ignore
67
68
69def parse_with_mode(mode: str) -> tuple[str, ServerSpec]:
70    """
71    Parse a proxy mode specification, which is usually just `(reverse|upstream):server-spec`.
72
73    *Raises:*
74     - ValueError, if the specification is invalid.
75    """
76    mode, server_spec = mode.split(":", maxsplit=1)
77    return mode, parse(server_spec)
class ServerSpec(typing.NamedTuple):
12class ServerSpec(NamedTuple):
13    scheme: Literal["http", "https"]
14    address: tuple[str, int]

ServerSpec(scheme, address)

ServerSpec(scheme: Literal['http', 'https'], address: tuple[str, int])

Create new instance of ServerSpec(scheme, address)

scheme: Literal['http', 'https']

Alias for field number 0

address: tuple[str, int]

Alias for field number 1

Inherited Members
builtins.tuple
index
count
@functools.lru_cache
def parse(server_spec: str) -> mitmproxy.net.server_spec.ServerSpec:
30@functools.lru_cache
31def parse(server_spec: str) -> ServerSpec:
32    """
33    Parses a server mode specification, e.g.:
34
35     - http://example.com/
36     - example.org
37     - example.com:443
38
39    *Raises:*
40     - ValueError, if the server specification is invalid.
41    """
42    m = server_spec_re.match(server_spec)
43    if not m:
44        raise ValueError(f"Invalid server specification: {server_spec}")
45
46    if m.group("scheme"):
47        scheme = m.group("scheme")
48    else:
49        scheme = "https" if m.group("port") in ("443", None) else "http"
50    if scheme not in ("http", "https"):
51        raise ValueError(f"Invalid server scheme: {scheme}")
52
53    host = m.group("host")
54    # IPv6 brackets
55    if host.startswith("[") and host.endswith("]"):
56        host = host[1:-1]
57    if not check.is_valid_host(host):
58        raise ValueError(f"Invalid hostname: {host}")
59
60    if m.group("port"):
61        port = int(m.group("port"))
62    else:
63        port = {"http": 80, "https": 443}[scheme]
64    if not check.is_valid_port(port):
65        raise ValueError(f"Invalid port: {port}")
66
67    return ServerSpec(scheme, (host, port))  # type: ignore

Parses a server mode specification, e.g.:

Raises:

  • ValueError, if the server specification is invalid.
def parse_with_mode(mode: str) -> tuple[str, mitmproxy.net.server_spec.ServerSpec]:
70def parse_with_mode(mode: str) -> tuple[str, ServerSpec]:
71    """
72    Parse a proxy mode specification, which is usually just `(reverse|upstream):server-spec`.
73
74    *Raises:*
75     - ValueError, if the specification is invalid.
76    """
77    mode, server_spec = mode.split(":", maxsplit=1)
78    return mode, parse(server_spec)

Parse a proxy mode specification, which is usually just (reverse|upstream):server-spec.

Raises:

  • ValueError, if the specification is invalid.