Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lexiforest committed Dec 17, 2024
1 parent 5cffdbe commit 45a9ead
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ labels: bug

---

Please check the following items before reporting a bug, otherwise it may be closed immediately.
Please check the following items and answer all the questions when reporting a bug,
otherwise it will be closed immediately.

- [ ] **This is NOT a site-related "bugs"**, e.g. some site blocks me when using curl_cffi,
UNLESS it has been verified that the reason is missing pieces in the impersonation.
Expand Down
13 changes: 1 addition & 12 deletions curl_cffi/requests/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@
from collections import Counter
from io import BytesIO
from json import dumps
from typing import (
TYPE_CHECKING,
Any,
Callable,
cast,
Dict,
List,
Literal,
Optional,
Tuple,
Union,
)
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tuple, Union, cast
from urllib.parse import ParseResult, parse_qsl, quote, unquote, urlencode, urljoin, urlparse

from .. import CurlHttpVersion, CurlOpt, CurlSslVersion
Expand Down
28 changes: 5 additions & 23 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
Union,
cast,
)
from urllib.parse import ParseResult, parse_qsl, quote, urlencode, urlparse
from urllib.parse import urlparse

from .. import AsyncCurl, Curl, CurlError, CurlHttpVersion, CurlInfo, CurlOpt, CurlSslVersion
from ..curl import CURL_WRITEFUNC_ERROR, CurlMime
from ..aio import AsyncCurl
from ..const import CurlHttpVersion, CurlInfo, CurlOpt
from ..curl import Curl, CurlError, CurlMime
from .cookies import Cookies, CookieTypes, CurlMorsel
from .exceptions import RequestException, SessionClosed, code2error
from .headers import Headers, HeaderTypes
from .impersonate import (
BrowserTypeLiteral,
ExtraFingerprints,
ExtraFpDict,
)
from .impersonate import BrowserTypeLiteral, ExtraFingerprints, ExtraFpDict
from .models import Response
from .options import set_curl_options, update_url_params
from .websockets import AsyncWebSocket
Expand Down Expand Up @@ -99,21 +96,6 @@ def _is_absolute_url(url: str) -> bool:
SAFE_CHARS = set("!#$%&'()*+,/:;=?@[]~")


def _quote_path_and_params(url: str, quote_str: str = ""):
safe = "".join(SAFE_CHARS - set(quote_str))
parsed_url = urlparse(url)
parsed_get_args = parse_qsl(parsed_url.query, keep_blank_values=True)
encoded_get_args = urlencode(parsed_get_args, doseq=True, safe=safe)
return ParseResult(
parsed_url.scheme,
parsed_url.netloc,
quote(parsed_url.path, safe=safe),
parsed_url.params,
encoded_get_args,
parsed_url.fragment,
).geturl()


def _peek_queue(q: queue.Queue, default=None):
try:
return q.queue[0]
Expand Down
50 changes: 26 additions & 24 deletions curl_cffi/requests/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(
on_error: Optional[ON_ERROR_T] = None,
):
"""
Parameters:
Args:
autoclose: whether to close the WebSocket after receiving a close frame.
skip_utf8_validation: whether to skip UTF-8 validation for text frames in run_forever().
debug: print extra curl debug info.
Expand Down Expand Up @@ -242,7 +242,7 @@ def connect(
libcurl automatically handles pings and pongs.
ref: https://curl.se/libcurl/c/libcurl-ws.html
Parameters:
Args:
url: url for the requests.
params: query string for the requests.
headers: headers to send.
Expand Down Expand Up @@ -339,7 +339,7 @@ def recv(self) -> Tuple[bytes, int]:
"""
Receive a frame as bytes.
libcurl split frames into fragments, so we have to collect all the chunks for
libcurl splits frames into fragments, so we have to collect all the chunks for
a frame.
"""
chunks = []
Expand Down Expand Up @@ -375,7 +375,7 @@ def recv_str(self) -> str:
def recv_json(self, *, loads: Callable[[str], T] = loads) -> T:
"""Receive a JSON frame.
Parameters:
args:
loads: JSON decoder, default is json.loads.
"""
data = self.recv_str()
Expand All @@ -384,7 +384,7 @@ def recv_json(self, *, loads: Callable[[str], T] = loads) -> T:
def send(self, payload: Union[str, bytes], flags: CurlWsFlag = CurlWsFlag.BINARY):
"""Send a data frame.
Parameters:
Args:
payload: data to send.
flags: flags for the frame.
"""
Expand All @@ -399,31 +399,31 @@ def send(self, payload: Union[str, bytes], flags: CurlWsFlag = CurlWsFlag.BINARY
def send_binary(self, payload: bytes):
"""Send a binary frame.
Parameters:
Args:
payload: binary data to send.
"""
return self.send(payload, CurlWsFlag.BINARY)

def send_bytes(self, payload: bytes):
"""Send a binary frame. Same as :meth:`send_binary`.
Parameters:
Args:
payload: binary data to send.
"""
return self.send(payload, CurlWsFlag.BINARY)

def send_str(self, payload: str):
"""Send a text frame.
Parameters:
Args:
payload: text data to send.
"""
return self.send(payload, CurlWsFlag.TEXT)

def send_json(self, payload: Any, *, dumps: Callable[[Any], str] = dumps):
"""Send a JSON frame.
Parameters:
Args:
payload: data to send.
dumps: JSON encoder, default is json.dumps.
"""
Expand All @@ -432,7 +432,7 @@ def send_json(self, payload: Any, *, dumps: Callable[[Any], str] = dumps):
def ping(self, payload: Union[str, bytes]):
"""Send a ping frame.
Parameters:
Args:
payload: data to send.
"""
return self.send(payload, CurlWsFlag.PING)
Expand Down Expand Up @@ -497,7 +497,7 @@ def run_forever(self, url: str, **kwargs):
def close(self, code: int = WsCloseCode.OK, message: bytes = b""):
"""Close the connection.
Parameters:
Args:
code: close code.
message: close reason.
"""
Expand Down Expand Up @@ -542,7 +542,7 @@ async def __anext__(self) -> bytes:
async def recv_fragment(self, *, timeout: Optional[float] = None) -> Tuple[bytes, CurlWsFrame]:
"""Receive a single frame as bytes.
Parameters:
Args:
timeout: how many seconds to wait before giving up.
"""
if self.closed:
Expand All @@ -559,7 +559,9 @@ async def recv_fragment(self, *, timeout: Optional[float] = None) -> Tuple[bytes
raise WebSocketTimeout("WebSocket recv_fragment() timed out")
if frame.flags & CurlWsFlag.CLOSE:
try:
code, message = self._close_code, self._close_reason = self._unpack_close_frame(chunk)
code, message = self._close_code, self._close_reason = self._unpack_close_frame(
chunk
)
except WebSocketError as e:
# Follow the spec to close the connection
# Errors do not respect autoclose
Expand All @@ -575,10 +577,10 @@ async def recv(self, *, timeout: Optional[float] = None) -> Tuple[bytes, int]:
"""
Receive a frame as bytes.
libcurl split frames into fragments, so we have to collect all the chunks for
libcurl splits frames into fragments, so we have to collect all the chunks for
a frame.
Parameters:
Args:
timeout: how many seconds to wait before giving up.
"""
loop = self.loop
Expand Down Expand Up @@ -608,7 +610,7 @@ async def recv(self, *, timeout: Optional[float] = None) -> Tuple[bytes, int]:
async def recv_str(self, *, timeout: Optional[float] = None) -> str:
"""Receive a text frame.
Parameters:
Args:
timeout: how many seconds to wait before giving up.
"""
data, flags = await self.recv(timeout=timeout)
Expand All @@ -621,7 +623,7 @@ async def recv_json(
) -> T:
"""Receive a JSON frame.
Parameters:
Args:
loads: JSON decoder, default is json.loads.
timeout: how many seconds to wait before giving up.
"""
Expand All @@ -631,7 +633,7 @@ async def recv_json(
async def send(self, payload: Union[str, bytes], flags: CurlWsFlag = CurlWsFlag.BINARY):
"""Send a data frame.
Parameters:
Args:
payload: data to send.
flags: flags for the frame.
"""
Expand All @@ -649,31 +651,31 @@ async def send(self, payload: Union[str, bytes], flags: CurlWsFlag = CurlWsFlag.
async def send_binary(self, payload: bytes):
"""Send a binary frame.
Parameters:
Args:
payload: binary data to send.
"""
return await self.send(payload, CurlWsFlag.BINARY)

async def send_bytes(self, payload: bytes):
"""Send a binary frame. Same as :meth:`send_binary`.
Parameters:
Args:
payload: binary data to send.
"""
return await self.send(payload, CurlWsFlag.BINARY)

async def send_str(self, payload: str):
"""Send a text frame.
Parameters:
Args:
payload: text data to send.
"""
return await self.send(payload, CurlWsFlag.TEXT)

async def send_json(self, payload: Any, *, dumps: Callable[[Any], str] = dumps):
"""Send a JSON frame.
Parameters:
Args:
payload: data to send.
dumps: JSON encoder, default is json.dumps.
"""
Expand All @@ -682,15 +684,15 @@ async def send_json(self, payload: Any, *, dumps: Callable[[Any], str] = dumps):
async def ping(self, payload: Union[str, bytes]):
"""Send a ping frame.
Parameters:
Args:
payload: data to send.
"""
return await self.send(payload, CurlWsFlag.PING)

async def close(self, code: int = WsCloseCode.OK, message: bytes = b""):
"""Close the connection.
Parameters:
Args:
code: close code.
message: close reason.
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "curl_cffi"
version = "0.8.0b7"
version = "0.8.1b1"
authors = [{ name = "Lyonnet", email = "[email protected]" }]
description = "libcurl ffi bindings for Python, with impersonation support."
license = { file = "LICENSE" }
Expand Down

0 comments on commit 45a9ead

Please sign in to comment.