Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class Curl(): compile reason_phrase and status_line regex patterns #277

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions curl_cffi/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from .const import CurlHttpVersion, CurlInfo, CurlOpt, CurlWsFlag

DEFAULT_CACERT = certifi.where()
REASON_PHRASE_RE = re.compile(rb"HTTP/\d\.\d [0-9]{3} (.*)")
STATUS_LINE_RE = re.compile(rb"HTTP/(\d\.\d) ([0-9]{3}) (.*)")


class CurlError(Exception):
Expand Down Expand Up @@ -341,7 +343,7 @@ def parse_cookie_headers(self, headers: List[bytes]) -> SimpleCookie:
@staticmethod
def get_reason_phrase(status_line: bytes) -> bytes:
"""Extract reason phrase, like ``OK``, ``Not Found`` from response status line."""
m = re.match(rb"HTTP/\d\.\d [0-9]{3} (.*)", status_line)
m = REASON_PHRASE_RE.match(status_line)
return m.group(1) if m else b""

@staticmethod
Expand All @@ -351,7 +353,7 @@ def parse_status_line(status_line: bytes) -> Tuple[CurlHttpVersion, int, bytes]:
Returns:
http_version, status_code, and reason phrase
"""
m = re.match(rb"HTTP/(\d\.\d) ([0-9]{3}) (.*)", status_line)
m = STATUS_LINE_RE.match(status_line)
if not m:
return CurlHttpVersion.V1_0, 0, b""
if m.group(1) == "2.0":
Expand Down
Loading