python-http_request 0.0.7__tar.gz → 0.0.7.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-http_request
3
- Version: 0.0.7
3
+ Version: 0.0.7.1
4
4
  Summary: Python http response utils.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-http_request
6
6
  License: MIT
@@ -26,7 +26,7 @@ from texttools import text_to_dict
26
26
 
27
27
  AnyStr = TypeVar("AnyStr", bytes, str, covariant=True)
28
28
 
29
- CRE_URL_SCHEME: Final = re_compile(r"^(?i:[a-z][a-z0-9.+-]*)://")
29
+ CRE_URL_SCHEME_match: Final = re_compile(r"(?i:[a-z][a-z0-9.+-]*)://").match
30
30
 
31
31
 
32
32
  @runtime_checkable
@@ -34,27 +34,41 @@ class SupportsGeturl(Protocol[AnyStr]):
34
34
  def geturl(self) -> AnyStr: ...
35
35
 
36
36
 
37
- def url_origin(url: str, /) -> str:
38
- if url.startswith("://"):
37
+ def url_origin(url: str, /, default_port: int = 0) -> str:
38
+ if url.startswith("/"):
39
+ url = "http://localhost" + url
40
+ elif url.startswith("//"):
41
+ url = "http:" + url
42
+ elif url.startswith("://"):
39
43
  url = "http" + url
40
- elif CRE_URL_SCHEME.match(url) is None:
44
+ elif not CRE_URL_SCHEME_match(url):
41
45
  url = "http://" + url
42
46
  urlp = urlsplit(url)
43
- scheme, netloc = urlp.scheme, urlp.netloc
44
- if not netloc:
45
- netloc = "localhost"
47
+ scheme, netloc = urlp.scheme or "http", urlp.netloc or "localhost"
48
+ if default_port and not urlp.port:
49
+ netloc = netloc.removesuffix(":") + f":{default_port}"
46
50
  return f"{scheme}://{netloc}"
47
51
 
48
52
 
49
- def complete_url(url: str, /) -> str:
50
- if url.startswith("://"):
53
+ def complete_url(url: str, /, default_port: int = 0) -> str:
54
+ if url.startswith("/"):
55
+ url = "http://localhost" + url
56
+ elif url.startswith("//"):
57
+ url = "http:" + url
58
+ elif url.startswith("://"):
51
59
  url = "http" + url
52
- elif CRE_URL_SCHEME.match(url) is None:
60
+ elif not CRE_URL_SCHEME_match(url):
53
61
  url = "http://" + url
54
62
  urlp = urlsplit(url)
55
63
  repl = {"query": "", "fragment": ""}
56
- if not urlp.netloc:
57
- repl["path"] = "localhost"
64
+ if not urlp.scheme:
65
+ repl["scheme"] = "http"
66
+ netloc = urlp.netloc
67
+ if not netloc:
68
+ netloc = "localhost"
69
+ if default_port and not urlp.port:
70
+ netloc = netloc.removesuffix(":") + f":{default_port}"
71
+ repl["netloc"] = netloc
58
72
  return urlunsplit(urlp._replace(**repl)).rstrip("/")
59
73
 
60
74
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-http_request"
3
- version = "0.0.7"
3
+ version = "0.0.7.1"
4
4
  description = "Python http response utils."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"