wreq 1.2.5 → 1.2.6
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.
- checksums.yaml +4 -4
- data/Cargo.lock +20 -13
- data/Cargo.toml +10 -2
- data/crates/wreq-util/src/emulate/macros.rs +12 -10
- data/crates/wreq-util/src/emulate/profile/chrome/header.rs +18 -3
- data/crates/wreq-util/src/emulate/profile/firefox/header.rs +16 -0
- data/crates/wreq-util/src/emulate/profile/opera/header.rs +6 -7
- data/crates/wreq-util/tests/client.rs +103 -1
- data/lib/wreq.rb +70 -45
- data/lib/wreq_ruby/body.rb +29 -11
- data/lib/wreq_ruby/client.rb +88 -55
- data/lib/wreq_ruby/cookie.rb +79 -19
- data/lib/wreq_ruby/emulate.rb +74 -6
- data/lib/wreq_ruby/error.rb +5 -7
- data/lib/wreq_ruby/http.rb +74 -0
- data/lib/wreq_ruby/response.rb +3 -0
- data/script/build_windows_gnu.ps1 +6 -0
- data/src/arch.rs +22 -0
- data/src/client/body/form.rs +2 -0
- data/src/client/body/json.rs +47 -14
- data/src/client/body/stream.rs +147 -43
- data/src/client/body.rs +11 -15
- data/src/client/param.rs +7 -7
- data/src/client/req.rs +87 -47
- data/src/client/resp.rs +23 -24
- data/src/client.rs +310 -230
- data/src/cookie.rs +280 -87
- data/src/emulate.rs +85 -50
- data/src/error.rs +198 -41
- data/src/extractor.rs +16 -76
- data/src/header.rs +146 -23
- data/src/http.rs +62 -33
- data/src/lib.rs +26 -20
- data/src/macros.rs +71 -46
- data/src/options.rs +284 -0
- data/src/rt.rs +22 -11
- data/src/serde/de/array_deserializer.rs +48 -0
- data/src/serde/de/array_enumerator.rs +59 -0
- data/src/serde/de/deserializer.rs +307 -0
- data/src/serde/de/enum_deserializer.rs +47 -0
- data/src/serde/de/hash_deserializer.rs +89 -0
- data/src/serde/de/number_deserializer.rs +51 -0
- data/src/serde/de/variant_deserializer.rs +101 -0
- data/src/serde/de.rs +33 -0
- data/src/serde/error.rs +146 -0
- data/src/serde/ser/enums.rs +14 -0
- data/src/serde/ser/map_serializer.rs +56 -0
- data/src/serde/ser/seq_serializer.rs +71 -0
- data/src/serde/ser/serializer.rs +194 -0
- data/src/serde/ser/struct_serializer.rs +106 -0
- data/src/serde/ser/struct_variant_serializer.rs +44 -0
- data/src/serde/ser/tuple_variant_serializer.rs +41 -0
- data/src/serde/ser.rs +18 -0
- data/src/serde/tests.rs +237 -0
- data/src/serde.rs +202 -0
- data/test/body_sender_test.rb +246 -0
- data/test/cookie_test.rb +211 -10
- data/test/json_precision_test.rb +209 -0
- data/test/option_validation_test.rb +311 -0
- data/test/value_semantics_test.rb +238 -0
- metadata +25 -2
- data/src/header/helper.rs +0 -112
data/src/header/helper.rs
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
//! Ruby value conversion helpers for `Wreq::Headers`.
|
|
2
|
-
|
|
3
|
-
use bytes::Bytes;
|
|
4
|
-
use http::{HeaderName, HeaderValue};
|
|
5
|
-
use magnus::{Error, RArray, RString, Symbol, TryConvert, Value, prelude::*, typed_data::Obj};
|
|
6
|
-
|
|
7
|
-
use crate::error::{
|
|
8
|
-
header_name_error_to_magnus, header_value_error_to_magnus, type_value_error_to_magnus,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
use super::Headers;
|
|
12
|
-
|
|
13
|
-
/// Maximum number of field-value occurrences supported by `HeaderMap`.
|
|
14
|
-
const MAX_HEADER_ENTRIES: usize = 1 << 15;
|
|
15
|
-
|
|
16
|
-
/// Build a header collection from a Ruby source object.
|
|
17
|
-
///
|
|
18
|
-
/// Accepts another `Wreq::Headers`, a Hash, or any object whose `to_a` result
|
|
19
|
-
/// contains two-element name-value pairs. Array values are delegated to
|
|
20
|
-
/// [`Headers::append`] so each value remains a separate occurrence.
|
|
21
|
-
pub(super) fn from_source(source: Value) -> Result<Headers, Error> {
|
|
22
|
-
if let Ok(headers) = Obj::<Headers>::try_convert(source) {
|
|
23
|
-
return Ok((*headers).clone());
|
|
24
|
-
}
|
|
25
|
-
if !source.respond_to("to_a", false)? {
|
|
26
|
-
return Err(type_value_error_to_magnus(
|
|
27
|
-
"Expected Headers, a Hash, or an enumerable of pairs",
|
|
28
|
-
));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let pairs: RArray = source.funcall_public("to_a", ())?;
|
|
32
|
-
let headers = Headers::default();
|
|
33
|
-
for pair in pairs {
|
|
34
|
-
let pair = RArray::try_convert(pair)
|
|
35
|
-
.map_err(|_| type_value_error_to_magnus("Expected each header entry to be a pair"))?;
|
|
36
|
-
if pair.len() != 2 {
|
|
37
|
-
return Err(type_value_error_to_magnus(
|
|
38
|
-
"Expected each header entry to contain a name and value",
|
|
39
|
-
));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
headers.append(pair.entry(0)?, pair.entry(1)?)?;
|
|
43
|
-
}
|
|
44
|
-
Ok(headers)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/// Convert a Ruby String or Symbol into a normalized HTTP header name.
|
|
48
|
-
///
|
|
49
|
-
/// Symbol underscores are changed to hyphens before [`HeaderName`] validates
|
|
50
|
-
/// and normalizes the name. Other Ruby types produce `Wreq::BuilderError`.
|
|
51
|
-
pub(super) fn parse_header_name(value: Value) -> Result<HeaderName, Error> {
|
|
52
|
-
let name = match (RString::from_value(value), Symbol::from_value(value)) {
|
|
53
|
-
(Some(name), _) => name.to_bytes(),
|
|
54
|
-
(None, Some(name)) => Bytes::from(name.name()?.replace('_', "-")),
|
|
55
|
-
(None, None) => {
|
|
56
|
-
return Err(type_value_error_to_magnus(
|
|
57
|
-
"Expected a String or Symbol header name",
|
|
58
|
-
));
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
HeaderName::from_bytes(name.as_ref()).map_err(header_name_error_to_magnus)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/// Convert a Ruby String or Array of Strings into validated header values.
|
|
65
|
-
///
|
|
66
|
-
/// Each Array element becomes one [`HeaderValue`]. An empty Array therefore
|
|
67
|
-
/// produces no values, allowing `set` to remove a header and `append` to do
|
|
68
|
-
/// nothing.
|
|
69
|
-
pub(super) fn parse_header_values(value: Value) -> Result<Vec<HeaderValue>, Error> {
|
|
70
|
-
if let Some(values) = RArray::from_value(value) {
|
|
71
|
-
values.into_iter().map(parse_header_value).collect()
|
|
72
|
-
} else {
|
|
73
|
-
Ok(vec![parse_header_value(value)?])
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/// Convert one Ruby String into a validated HTTP header value.
|
|
78
|
-
///
|
|
79
|
-
/// Invalid Ruby types and bytes rejected by [`HeaderValue`] are mapped to
|
|
80
|
-
/// `Wreq::BuilderError`.
|
|
81
|
-
fn parse_header_value(value: Value) -> Result<HeaderValue, Error> {
|
|
82
|
-
let value = RString::try_convert(value)
|
|
83
|
-
.map_err(|_| type_value_error_to_magnus("Expected a String header value"))?;
|
|
84
|
-
HeaderValue::from_maybe_shared(value.to_bytes()).map_err(header_value_error_to_magnus)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/// Validate the resulting number of header occurrences before a mutation.
|
|
88
|
-
///
|
|
89
|
-
/// `current` is the collection length, `replaced` is the number of existing
|
|
90
|
-
/// occurrences removed by `set`, and `added` is the incoming value count.
|
|
91
|
-
/// Checked arithmetic prevents overflow; an invalid calculation or a result
|
|
92
|
-
/// above the native [`HeaderMap`](http::HeaderMap) limit returns
|
|
93
|
-
/// `Wreq::BuilderError` without mutating the collection.
|
|
94
|
-
pub(super) fn ensure_header_count(
|
|
95
|
-
current: usize,
|
|
96
|
-
replaced: usize,
|
|
97
|
-
added: usize,
|
|
98
|
-
) -> Result<(), Error> {
|
|
99
|
-
let count = current
|
|
100
|
-
.checked_sub(replaced)
|
|
101
|
-
.and_then(|count| count.checked_add(added));
|
|
102
|
-
if count.is_some_and(|count| count <= MAX_HEADER_ENTRIES) {
|
|
103
|
-
Ok(())
|
|
104
|
-
} else {
|
|
105
|
-
Err(header_count_error())
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/// Build the error returned when the native header map reaches its entry limit.
|
|
110
|
-
pub(super) fn header_count_error() -> Error {
|
|
111
|
-
type_value_error_to_magnus("Header collection exceeds 32,768 entries")
|
|
112
|
-
}
|