diff --git a/Cargo.lock b/Cargo.lock index 1dc04056..80d732e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1350,6 +1350,7 @@ dependencies = [ "futures", "h2", "impit", + "indexmap", "psl", "pyo3", "pyo3-async-runtimes", @@ -1973,6 +1974,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd274650b21d4bfc26a0a47587962c1edb425f69287324355cd040c3ea66071c" dependencies = [ "either", + "indexmap", "libc", "once_cell", "portable-atomic", diff --git a/impit-python/Cargo.toml b/impit-python/Cargo.toml index b207488a..3f517ef8 100644 --- a/impit-python/Cargo.toml +++ b/impit-python/Cargo.toml @@ -14,7 +14,7 @@ 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" @@ -22,3 +22,4 @@ cookie = "0.18.1" psl = "2.1.255" futures = "0.3.31" either = "1.15.0" +indexmap = "2.13.0" diff --git a/impit-python/src/async_client.rs b/impit-python/src/async_client.rs index 96fdfa60..f47c046f 100644 --- a/impit-python/src/async_client.rs +++ b/impit-python/src/async_client.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, sync::Arc, time::Duration}; +use std::{sync::Arc, time::Duration}; use either::{Either, Right}; use impit::{ @@ -6,6 +6,7 @@ use impit::{ impit::{Impit, ImpitBuilder}, request::RequestOptions, }; +use indexmap::IndexMap; use pyo3::{ffi::c_str, prelude::*}; use crate::{ @@ -58,7 +59,7 @@ impl AsyncClient { max_redirects: Option, cookie_jar: Option>, cookies: Option>, - headers: Option>, + headers: Option>, local_address: Option, ) -> PyResult { let builder = ImpitBuilder::default(); @@ -140,7 +141,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -168,7 +169,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -196,7 +197,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -224,7 +225,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -252,7 +253,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -280,7 +281,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -308,7 +309,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -336,7 +337,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -365,7 +366,7 @@ impl AsyncClient { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -415,7 +416,7 @@ impl AsyncClient { url: String, content: Option, mut data: Option, - mut headers: Option>, + mut headers: Option>, timeout: Option>, force_http3: Option, stream: Option, diff --git a/impit-python/src/client.rs b/impit-python/src/client.rs index 364564c9..100ed4bc 100644 --- a/impit-python/src/client.rs +++ b/impit-python/src/client.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, time::Duration}; +use std::time::Duration; use either::{Either, Right}; use impit::{ @@ -6,6 +6,7 @@ use impit::{ impit::{Impit, ImpitBuilder}, request::RequestOptions, }; +use indexmap::IndexMap; use pyo3::{ffi::c_str, prelude::*}; use crate::{ @@ -53,7 +54,7 @@ impl Client { max_redirects: Option, cookie_jar: Option>, cookies: Option>, - headers: Option>, + headers: Option>, local_address: Option, ) -> PyResult { let builder = ImpitBuilder::default(); @@ -137,7 +138,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -165,7 +166,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -193,7 +194,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -221,7 +222,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -249,7 +250,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -277,7 +278,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -305,7 +306,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -333,7 +334,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -362,7 +363,7 @@ impl Client { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, follow_redirects: Option, @@ -412,7 +413,7 @@ impl Client { url: String, content: Option, mut data: Option, - mut headers: Option>, + mut headers: Option>, timeout: Option>, force_http3: Option, stream: Option, diff --git a/impit-python/src/lib.rs b/impit-python/src/lib.rs index 4a2ecf3e..67a73e98 100644 --- a/impit-python/src/lib.rs +++ b/impit-python/src/lib.rs @@ -1,4 +1,5 @@ use either::{Either, Right}; +use indexmap::IndexMap; use pyo3::prelude::*; mod async_client; @@ -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<()> { @@ -98,7 +98,7 @@ fn impit(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, cookie_jar: Option>, @@ -127,7 +127,7 @@ fn impit(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { url: String, content: Option, data: Option, - headers: Option>, + headers: Option>, timeout: Option>, force_http3: Option, cookie_jar: Option>, diff --git a/impit-python/src/request.rs b/impit-python/src/request.rs index dfbea315..cadbe690 100644 --- a/impit-python/src/request.rs +++ b/impit-python/src/request.rs @@ -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}, @@ -138,7 +139,7 @@ fn to_stream(iterator: PyIterator) -> impl Stream> { /// Converts the Python request body into an [`ImpitBody`], streaming it if it is an iterator. pub(crate) fn to_body( data: Option, - headers: &mut Option>, + headers: &mut Option>, ) -> PyResult { Ok(match data { None => ImpitBody::Empty, diff --git a/impit-python/test/basic_client_test.py b/impit-python/test/basic_client_test.py index 3f9489b4..614db9f5 100644 --- a/impit-python/test/basic_client_test.py +++ b/impit-python/test/basic_client_test.py @@ -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)