Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion impit-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ tokio = { version="1.41.1", features = ["full"] }
h2 = "0.4.7"
reqwest = "0.13.1"
bytes = "1.9.0"
pyo3 = { version = "0.29", features = ["extension-module", "auto-initialize", "either"] }
pyo3 = { version = "0.29", features = ["extension-module", "auto-initialize", "either", "indexmap"] }
pyo3-async-runtimes = { version = "0.29", features = ["attributes", "async-std-runtime", "tokio-runtime"] }
urlencoding = "2.1.3"
encoding = "0.2.33"
cookie = "0.18.1"
psl = "2.1.255"
futures = "0.3.31"
either = "1.15.0"
indexmap = "2.13.0"
25 changes: 13 additions & 12 deletions impit-python/src/async_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{collections::HashMap, sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration};

use either::{Either, Right};
use impit::{
errors::ImpitError,
impit::{Impit, ImpitBuilder},
request::RequestOptions,
};
use indexmap::IndexMap;
use pyo3::{ffi::c_str, prelude::*};

use crate::{
Expand Down Expand Up @@ -58,7 +59,7 @@ impl AsyncClient {
max_redirects: Option<u16>,
cookie_jar: Option<crate::Bound<'_, crate::PyAny>>,
cookies: Option<crate::Bound<'_, crate::PyAny>>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
local_address: Option<String>,
) -> PyResult<Self> {
let builder = ImpitBuilder::default();
Expand Down Expand Up @@ -140,7 +141,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -168,7 +169,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -196,7 +197,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -224,7 +225,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -252,7 +253,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -280,7 +281,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -308,7 +309,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -336,7 +337,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -365,7 +366,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -415,7 +416,7 @@ impl AsyncClient {
url: String,
content: Option<RequestBody>,
mut data: Option<RequestBody>,
mut headers: Option<HashMap<String, String>>,
mut headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
stream: Option<bool>,
Expand Down
25 changes: 13 additions & 12 deletions impit-python/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{collections::HashMap, time::Duration};
use std::time::Duration;

use either::{Either, Right};
use impit::{
errors::ImpitError,
impit::{Impit, ImpitBuilder},
request::RequestOptions,
};
use indexmap::IndexMap;
use pyo3::{ffi::c_str, prelude::*};

use crate::{
Expand Down Expand Up @@ -53,7 +54,7 @@ impl Client {
max_redirects: Option<u16>,
cookie_jar: Option<crate::Bound<'_, crate::PyAny>>,
cookies: Option<crate::Bound<'_, crate::PyAny>>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
local_address: Option<String>,
) -> PyResult<Self> {
let builder = ImpitBuilder::default();
Expand Down Expand Up @@ -137,7 +138,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -165,7 +166,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -193,7 +194,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -221,7 +222,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -249,7 +250,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -277,7 +278,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -305,7 +306,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -333,7 +334,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -362,7 +363,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
Expand Down Expand Up @@ -412,7 +413,7 @@ impl Client {
url: String,
content: Option<RequestBody>,
mut data: Option<RequestBody>,
mut headers: Option<HashMap<String, String>>,
mut headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
stream: Option<bool>,
Expand Down
6 changes: 3 additions & 3 deletions impit-python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use either::{Either, Right};
use indexmap::IndexMap;
use pyo3::prelude::*;

mod async_client;
Expand All @@ -13,7 +14,6 @@ mod response;
use async_client::AsyncClient;
use client::Client;
use request::{RequestBody, USE_CLIENT_DEFAULT_SENTINEL};
use std::collections::HashMap;

#[pymodule]
fn impit(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
Expand Down Expand Up @@ -98,7 +98,7 @@ fn impit(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
cookie_jar: Option<pyo3::Bound<'_, pyo3::PyAny>>,
Expand Down Expand Up @@ -127,7 +127,7 @@ fn impit(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
url: String,
content: Option<RequestBody>,
data: Option<RequestBody>,
headers: Option<HashMap<String, String>>,
headers: Option<IndexMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
cookie_jar: Option<pyo3::Bound<'_, pyo3::PyAny>>,
Expand Down
3 changes: 2 additions & 1 deletion impit-python/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bytes::Bytes;
use either::{Either, Left, Right};
use futures::{stream, Stream};
use impit::{impit::RedirectBehavior, request::ImpitBody};
use indexmap::IndexMap;
use pyo3::{
exceptions::{PyStopAsyncIteration, PyStopIteration, PyTypeError},
types::{PyAnyMethods, PyMapping},
Expand Down Expand Up @@ -138,7 +139,7 @@ fn to_stream(iterator: PyIterator) -> impl Stream<Item = io::Result<Bytes>> {
/// Converts the Python request body into an [`ImpitBody`], streaming it if it is an iterator.
pub(crate) fn to_body(
data: Option<RequestBody>,
headers: &mut Option<HashMap<String, String>>,
headers: &mut Option<IndexMap<String, String>>,
) -> PyResult<ImpitBody> {
Ok(match data {
None => ImpitBody::Empty,
Expand Down
13 changes: 13 additions & 0 deletions impit-python/test/basic_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,19 @@ def test_removing_impersonated_headers_with_empty_string(self, browser: Browser)
assert response.status_code == 200
assert 'Sec-Fetch-User' not in response.json()['headers']

def test_headers_keep_dict_order(self, browser: Browser) -> None:
client_headers = {f'X-Client-{i}': str(i) for i in reversed(range(8))}
request_headers = {f'X-Request-{i}': str(i) for i in reversed(range(8))}
impit = Client(browser=browser, headers=client_headers)

with echoing_server() as port:
response = impit.post(f'http://localhost:{port}/', content='body', headers=request_headers, timeout=5)

sent = [line.split(':')[0].lower() for line in response.text.split('\r\n')[1:] if line]
for headers in (client_headers, request_headers):
names = [name.lower() for name in headers]
assert [name for name in sent if name in names] == names

@pytest.mark.skip(reason='Flaky under the CI environment')
def test_http3_works(self, browser: Browser) -> None:
impit = Client(browser=browser, http3=True)
Expand Down
Loading