wreq-rb 0.6.1 → 0.7.1
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 +47 -21
- data/README.md +36 -0
- data/ext/wreq_rb/Cargo.toml +7 -4
- data/ext/wreq_rb/extconf.rb +10 -3
- data/ext/wreq_rb/src/client.rs +145 -14
- data/ext/wreq_rb/src/response.rs +28 -3
- data/lib/wreq-rb/version.rb +1 -1
- data/patches/0002-add-preconnect.patch +266 -0
- data/vendor/wreq/Cargo.toml +11 -7
- data/vendor/wreq/bench/support/server.rs +2 -0
- data/vendor/wreq/src/client/layer/redirect/future.rs +1 -1
- data/vendor/wreq/src/client/layer/timeout/body.rs +16 -18
- data/vendor/wreq/src/client/layer/timeout/future.rs +2 -2
- data/vendor/wreq/src/client/request.rs +27 -15
- data/vendor/wreq/src/client/ws.rs +4 -3
- data/vendor/wreq/src/client.rs +4 -24
- data/vendor/wreq/src/conn/net/tcp.rs +16 -66
- data/vendor/wreq/src/conn/proxy/socks.rs +11 -1
- data/vendor/wreq/src/conn/proxy/tunnel.rs +14 -11
- data/vendor/wreq/src/conn/timeout.rs +1 -1
- data/vendor/wreq/src/cookie/jar.rs +170 -36
- data/vendor/wreq/src/cookie/store.rs +66 -4
- data/vendor/wreq/src/cookie.rs +48 -17
- data/vendor/wreq/src/dns.rs +2 -4
- data/vendor/wreq/src/error.rs +100 -0
- data/vendor/wreq/src/ext.rs +0 -49
- data/vendor/wreq/src/lib.rs +2 -0
- data/vendor/wreq/src/rt.rs +1 -14
- data/vendor/wreq/src/tls/trust/store.rs +35 -2
- data/vendor/wreq/tests/badssl.rs +3 -239
- data/vendor/wreq/tests/cert_store.rs +168 -0
- data/vendor/wreq/tests/client.rs +89 -0
- data/vendor/wreq/tests/proxy.rs +149 -3
- data/vendor/wreq/tests/redirect.rs +35 -0
- data/vendor/wreq/tests/support/tls_cert.rs +56 -0
- data/vendor/wreq/tests/timeouts.rs +25 -0
- data/vendor/wreq/tests/unix_socket.rs +109 -81
- data/vendor/wreq/tests/websocket.rs +40 -0
- metadata +9 -6
- data/vendor/wreq/examples/rt.rs +0 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cde4314d5b59d330769e0e2b8032696b5eafc374954e0c1b63de09e69d26ca4a
|
|
4
|
+
data.tar.gz: 6914fd3852c01df30409b0235c8f57768b0643b9649f1f7ed5e60a82c5ad94ef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27eb9e5818a595babf019c2fb932f32fe6396fcb9c526927e289e51496523b6249a4a558956e745cab11f09230e530a15618f3cb145cd84df19d9204d027d31a
|
|
7
|
+
data.tar.gz: 19fe676356523bcd206da208a6c06ea6599f69a220b77a545dc5266b9e11160f396e4ed4d5dc29ee9a2a1f158f946a68a81726bbfd6701dd70ba360d5d2696bd
|
data/Cargo.lock
CHANGED
|
@@ -41,12 +41,6 @@ dependencies = [
|
|
|
41
41
|
"cc",
|
|
42
42
|
]
|
|
43
43
|
|
|
44
|
-
[[package]]
|
|
45
|
-
name = "allocator-api2"
|
|
46
|
-
version = "0.2.21"
|
|
47
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
48
|
-
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
|
49
|
-
|
|
50
44
|
[[package]]
|
|
51
45
|
name = "anes"
|
|
52
46
|
version = "0.1.6"
|
|
@@ -647,8 +641,8 @@ dependencies = [
|
|
|
647
641
|
"compression-core",
|
|
648
642
|
"flate2",
|
|
649
643
|
"memchr",
|
|
650
|
-
"zstd",
|
|
651
|
-
"zstd-safe",
|
|
644
|
+
"zstd 0.13.3",
|
|
645
|
+
"zstd-safe 7.2.4",
|
|
652
646
|
]
|
|
653
647
|
|
|
654
648
|
[[package]]
|
|
@@ -1302,8 +1296,6 @@ version = "0.17.1"
|
|
|
1302
1296
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1303
1297
|
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
|
1304
1298
|
dependencies = [
|
|
1305
|
-
"allocator-api2",
|
|
1306
|
-
"equivalent",
|
|
1307
1299
|
"foldhash 0.2.0",
|
|
1308
1300
|
]
|
|
1309
1301
|
|
|
@@ -1859,9 +1851,9 @@ dependencies = [
|
|
|
1859
1851
|
|
|
1860
1852
|
[[package]]
|
|
1861
1853
|
name = "lru"
|
|
1862
|
-
version = "0.
|
|
1854
|
+
version = "0.19.2"
|
|
1863
1855
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1864
|
-
checksum = "
|
|
1856
|
+
checksum = "d4e2a1b977b42ee8b53a2a4adbd1183431c493f1d1960b7ccd893e2f0b9407ba"
|
|
1865
1857
|
dependencies = [
|
|
1866
1858
|
"hashbrown 0.17.1",
|
|
1867
1859
|
]
|
|
@@ -2261,6 +2253,21 @@ dependencies = [
|
|
|
2261
2253
|
"unicode-ident",
|
|
2262
2254
|
]
|
|
2263
2255
|
|
|
2256
|
+
[[package]]
|
|
2257
|
+
name = "psl"
|
|
2258
|
+
version = "2.1.264"
|
|
2259
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2260
|
+
checksum = "8782e35cade255f17d5ad527b899ea5b4e6bef29dcdc3502965a7d8d2f3fc3f5"
|
|
2261
|
+
dependencies = [
|
|
2262
|
+
"psl-types",
|
|
2263
|
+
]
|
|
2264
|
+
|
|
2265
|
+
[[package]]
|
|
2266
|
+
name = "psl-types"
|
|
2267
|
+
version = "2.0.11"
|
|
2268
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2269
|
+
checksum = "33cb294fe86a74cbcf50d4445b37da762029549ebeea341421c7c70370f86cac"
|
|
2270
|
+
|
|
2264
2271
|
[[package]]
|
|
2265
2272
|
name = "quinn"
|
|
2266
2273
|
version = "0.11.9"
|
|
@@ -4103,7 +4110,7 @@ dependencies = [
|
|
|
4103
4110
|
|
|
4104
4111
|
[[package]]
|
|
4105
4112
|
name = "wreq"
|
|
4106
|
-
version = "0.16.
|
|
4113
|
+
version = "0.16.5"
|
|
4107
4114
|
dependencies = [
|
|
4108
4115
|
"brotli",
|
|
4109
4116
|
"btls",
|
|
@@ -4134,6 +4141,7 @@ dependencies = [
|
|
|
4134
4141
|
"percent-encoding",
|
|
4135
4142
|
"pin-project-lite",
|
|
4136
4143
|
"pretty_env_logger",
|
|
4144
|
+
"psl",
|
|
4137
4145
|
"reqwest",
|
|
4138
4146
|
"serde",
|
|
4139
4147
|
"serde_html_form",
|
|
@@ -4157,7 +4165,7 @@ dependencies = [
|
|
|
4157
4165
|
"windows-registry",
|
|
4158
4166
|
"wreq-proto",
|
|
4159
4167
|
"wreq-rt",
|
|
4160
|
-
"zstd",
|
|
4168
|
+
"zstd 0.13.3",
|
|
4161
4169
|
]
|
|
4162
4170
|
|
|
4163
4171
|
[[package]]
|
|
@@ -4197,21 +4205,21 @@ dependencies = [
|
|
|
4197
4205
|
|
|
4198
4206
|
[[package]]
|
|
4199
4207
|
name = "wreq-util"
|
|
4200
|
-
version = "0.2.
|
|
4208
|
+
version = "0.2.1"
|
|
4201
4209
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
4202
|
-
checksum = "
|
|
4210
|
+
checksum = "636ad931b75aa030cc320899507ce8d5e44f1d9b3b44a81d2e1a225b0d4aad3b"
|
|
4203
4211
|
dependencies = [
|
|
4204
4212
|
"brotli",
|
|
4205
4213
|
"flate2",
|
|
4206
4214
|
"serde",
|
|
4207
4215
|
"typed-builder",
|
|
4208
4216
|
"wreq",
|
|
4209
|
-
"zstd",
|
|
4217
|
+
"zstd 0.14.5",
|
|
4210
4218
|
]
|
|
4211
4219
|
|
|
4212
4220
|
[[package]]
|
|
4213
4221
|
name = "wreq_rb"
|
|
4214
|
-
version = "0.
|
|
4222
|
+
version = "0.7.1"
|
|
4215
4223
|
dependencies = [
|
|
4216
4224
|
"bytes",
|
|
4217
4225
|
"http",
|
|
@@ -4345,7 +4353,16 @@ version = "0.13.3"
|
|
|
4345
4353
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
4346
4354
|
checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
|
|
4347
4355
|
dependencies = [
|
|
4348
|
-
"zstd-safe",
|
|
4356
|
+
"zstd-safe 7.2.4",
|
|
4357
|
+
]
|
|
4358
|
+
|
|
4359
|
+
[[package]]
|
|
4360
|
+
name = "zstd"
|
|
4361
|
+
version = "0.14.5"
|
|
4362
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
4363
|
+
checksum = "465cb081cac3a28a84ef547702dba1fe0f4a9f22e4e57a0b675ceee028c2af80"
|
|
4364
|
+
dependencies = [
|
|
4365
|
+
"zstd-safe 8.4.0",
|
|
4349
4366
|
]
|
|
4350
4367
|
|
|
4351
4368
|
[[package]]
|
|
@@ -4357,11 +4374,20 @@ dependencies = [
|
|
|
4357
4374
|
"zstd-sys",
|
|
4358
4375
|
]
|
|
4359
4376
|
|
|
4377
|
+
[[package]]
|
|
4378
|
+
name = "zstd-safe"
|
|
4379
|
+
version = "8.4.0"
|
|
4380
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
4381
|
+
checksum = "9b09bddd92c53b91833f4df03e0ea07e02d8367cfcd15a3b9acda94fc273f793"
|
|
4382
|
+
dependencies = [
|
|
4383
|
+
"zstd-sys",
|
|
4384
|
+
]
|
|
4385
|
+
|
|
4360
4386
|
[[package]]
|
|
4361
4387
|
name = "zstd-sys"
|
|
4362
|
-
version = "2.0
|
|
4388
|
+
version = "2.2.0+zstd.1.6.0"
|
|
4363
4389
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
4364
|
-
checksum = "
|
|
4390
|
+
checksum = "c3f4089cda6dda4dc43c43dd9f8964140064fd2f752f3da46c3a483c3c175160"
|
|
4365
4391
|
dependencies = [
|
|
4366
4392
|
"cc",
|
|
4367
4393
|
"pkg-config",
|
data/README.md
CHANGED
|
@@ -142,6 +142,33 @@ client.request_batch([
|
|
|
142
142
|
- `cancel` and Ruby thread interrupts abort the whole batch, raising
|
|
143
143
|
`Wreq::Error` with `"request interrupted"`.
|
|
144
144
|
|
|
145
|
+
### Preconnecting
|
|
146
|
+
|
|
147
|
+
`Wreq::Client#preconnect` opens a connection to a URL's origin ahead of time —
|
|
148
|
+
DNS, TCP, proxy tunnel and TLS handshake — without sending a request. The
|
|
149
|
+
connection is kept idle in the client's pool, so the next request to the same
|
|
150
|
+
origin skips connection setup.
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
client = Wreq::Client.new
|
|
154
|
+
|
|
155
|
+
client.preconnect("https://api.example.com") # returns nil, raises Wreq::Error on failure
|
|
156
|
+
# ... later ...
|
|
157
|
+
client.get("https://api.example.com/data") # reuses the warm connection
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
- Connection-level options must match the later request for the connection to
|
|
161
|
+
be reused. Pass `proxy:` and `emulation:` (plus `emulation_os:` etc.) the same
|
|
162
|
+
way you would to the request. `timeout:` bounds the preconnect. Other request
|
|
163
|
+
options are ignored.
|
|
164
|
+
- If an idle connection to the origin already exists, it's reused and no new
|
|
165
|
+
connection is opened. Preconnect several origins concurrently from separate
|
|
166
|
+
threads, because the GVL is released.
|
|
167
|
+
- The connection is subject to the pool's idle timeout (90 seconds) and may
|
|
168
|
+
also be closed by the server. If it's closed, the request falls back to a
|
|
169
|
+
fresh connection. Preconnecting has no effect with `pool_max_idle_per_host: 0`.
|
|
170
|
+
- `cancel` and Ruby thread interrupts abort a pending preconnect.
|
|
171
|
+
|
|
145
172
|
### Cancelling Requests
|
|
146
173
|
|
|
147
174
|
Call `cancel` on a client to interrupt all in-flight requests immediately:
|
|
@@ -173,6 +200,7 @@ Pass an options hash as the second argument to any HTTP method:
|
|
|
173
200
|
| `proxy` | String | Per-request proxy URL |
|
|
174
201
|
| `emulation` | String/Boolean | Per-request emulation override |
|
|
175
202
|
| `emulation_os` | String | OS emulation: `windows`, `macos`, `linux`, `android`, `ios` |
|
|
203
|
+
| `default_headers` | Boolean | `false` skips the client's default headers for this request (default: `true`) |
|
|
176
204
|
|
|
177
205
|
## Browser Emulation
|
|
178
206
|
|
|
@@ -198,6 +226,14 @@ client = Wreq::Client.new(emulation: "chrome_148", user_agent: "MyBot/1.0")
|
|
|
198
226
|
|
|
199
227
|
# Per-request emulation override
|
|
200
228
|
resp = client.get("https://example.com", emulation: "safari_26.2")
|
|
229
|
+
|
|
230
|
+
# Send only the request's headers: skips the client's emulation headers,
|
|
231
|
+
# user_agent and headers. A per-request emulation still adds its own.
|
|
232
|
+
# The emulation's TLS and HTTP/2 settings stay. The client's header_order
|
|
233
|
+
# setting still takes precedence over the request hash's order.
|
|
234
|
+
resp = client.get("https://example.com/script.js",
|
|
235
|
+
default_headers: false,
|
|
236
|
+
headers: { "User-Agent" => "...", "Accept" => "*/*", "Sec-Fetch-Dest" => "script" })
|
|
201
237
|
```
|
|
202
238
|
|
|
203
239
|
### Supported Browsers
|
data/ext/wreq_rb/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "wreq_rb"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.7.1"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
publish = false
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ crate-type = ["cdylib"]
|
|
|
10
10
|
[dependencies]
|
|
11
11
|
magnus = { version = "0.8", features = ["rb-sys"] }
|
|
12
12
|
rb-sys = "0.9"
|
|
13
|
-
wreq = { path = "../../vendor/wreq", version = "=0.16.
|
|
13
|
+
wreq = { path = "../../vendor/wreq", version = "=0.16.5", features = [
|
|
14
14
|
"cookies",
|
|
15
15
|
"json",
|
|
16
16
|
"gzip",
|
|
@@ -24,14 +24,17 @@ wreq = { path = "../../vendor/wreq", version = "=0.16.1", features = [
|
|
|
24
24
|
"query",
|
|
25
25
|
"form",
|
|
26
26
|
] }
|
|
27
|
-
wreq-util = { version = "=0.2.
|
|
27
|
+
wreq-util = { version = "=0.2.1", features = ["emulation", "emulation-serde", "emulation-compression"] }
|
|
28
28
|
tokio = { version = "1", features = ["full"] }
|
|
29
29
|
tokio-util = "0.7"
|
|
30
30
|
serde_json = "1.0"
|
|
31
31
|
bytes = "1"
|
|
32
32
|
http = "1"
|
|
33
33
|
|
|
34
|
+
[dev-dependencies]
|
|
35
|
+
magnus = { version = "0.8", features = ["embed"] }
|
|
36
|
+
|
|
34
37
|
[target.'cfg(target_os = "linux")'.dependencies]
|
|
35
|
-
wreq = { path = "../../vendor/wreq", version = "=0.16.
|
|
38
|
+
wreq = { path = "../../vendor/wreq", version = "=0.16.5", features = [
|
|
36
39
|
"prefix-symbols",
|
|
37
40
|
] }
|
data/ext/wreq_rb/extconf.rb
CHANGED
|
@@ -8,9 +8,16 @@ patch_dir = File.expand_path("../../patches", __dir__)
|
|
|
8
8
|
wreq_dir = File.expand_path("../../vendor/wreq", __dir__)
|
|
9
9
|
|
|
10
10
|
if File.directory?(patch_dir)
|
|
11
|
-
Dir.glob(File.join(patch_dir, "*.patch")).sort
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
patches = Dir.glob(File.join(patch_dir, "*.patch")).sort
|
|
12
|
+
# Patches stack in order and may touch the same files, so a patch only reverse-applies
|
|
13
|
+
# cleanly while no later patch is applied. The newest patch that reverse-applies marks
|
|
14
|
+
# itself and every earlier patch as already applied.
|
|
15
|
+
applied_count = (patches.rindex do |patch|
|
|
16
|
+
system("git", "apply", "--check", "--reverse", patch, chdir: wreq_dir, out: File::NULL, err: File::NULL)
|
|
17
|
+
end || -1) + 1
|
|
18
|
+
|
|
19
|
+
patches.each_with_index do |patch, index|
|
|
20
|
+
if index < applied_count
|
|
14
21
|
puts "Patch already applied: #{File.basename(patch)}"
|
|
15
22
|
else
|
|
16
23
|
puts "Applying patch: #{File.basename(patch)}"
|
data/ext/wreq_rb/src/client.rs
CHANGED
|
@@ -47,7 +47,7 @@ fn runtime() -> &'static Runtime {
|
|
|
47
47
|
/// # Safety
|
|
48
48
|
/// The closure must NOT access any Ruby objects or call any Ruby C API.
|
|
49
49
|
/// Extract all data from Ruby before calling this, convert results after.
|
|
50
|
-
unsafe fn without_gvl<F, R>(f: F) -> R
|
|
50
|
+
unsafe fn without_gvl<F, R>(f: F) -> Result<R, magnus::Error>
|
|
51
51
|
where
|
|
52
52
|
F: FnOnce(CancellationToken) -> R,
|
|
53
53
|
{
|
|
@@ -89,19 +89,22 @@ where
|
|
|
89
89
|
let data_ptr = &mut data as *mut CallData<F, R> as *mut c_void;
|
|
90
90
|
|
|
91
91
|
unsafe {
|
|
92
|
-
rb_sys::
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
magnus::rb_sys::protect(|| {
|
|
93
|
+
rb_sys::rb_thread_call_without_gvl(
|
|
94
|
+
Some(call::<F, R>),
|
|
95
|
+
data_ptr,
|
|
96
|
+
Some(ubf::<F, R>),
|
|
97
|
+
data_ptr,
|
|
98
|
+
);
|
|
99
|
+
0
|
|
100
|
+
})?;
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
if let Some(payload) = data.panic_payload {
|
|
101
104
|
panic::resume_unwind(payload);
|
|
102
105
|
}
|
|
103
106
|
|
|
104
|
-
data.result.unwrap()
|
|
107
|
+
Ok(data.result.unwrap())
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
/// Collected response data as pure Rust types (no Ruby objects).
|
|
@@ -288,12 +291,13 @@ impl Client {
|
|
|
288
291
|
builder = builder.default_headers(hmap);
|
|
289
292
|
}
|
|
290
293
|
|
|
291
|
-
|
|
292
|
-
|
|
294
|
+
let timeout = hash_get_float(&opts, "timeout")?;
|
|
295
|
+
if let Some(timeout) = timeout {
|
|
296
|
+
builder = builder.timeout(Duration::from_secs_f64(timeout));
|
|
293
297
|
}
|
|
294
298
|
|
|
295
|
-
if let Some(
|
|
296
|
-
builder = builder.connect_timeout(Duration::from_secs_f64(
|
|
299
|
+
if let Some(connect_timeout) = hash_get_float(&opts, "connect_timeout")?.or(timeout) {
|
|
300
|
+
builder = builder.connect_timeout(Duration::from_secs_f64(connect_timeout));
|
|
297
301
|
}
|
|
298
302
|
|
|
299
303
|
if let Some(t) = hash_get_float(&opts, "read_timeout")? {
|
|
@@ -437,6 +441,42 @@ impl Client {
|
|
|
437
441
|
self.execute_method("OPTIONS", args)
|
|
438
442
|
}
|
|
439
443
|
|
|
444
|
+
/// client.preconnect(url) or client.preconnect(url, opts)
|
|
445
|
+
///
|
|
446
|
+
/// Opens a connection (TCP + proxy tunnel + TLS handshake) to the URL's origin
|
|
447
|
+
/// and keeps it idle in the pool without sending a request.
|
|
448
|
+
fn preconnect(&self, args: &[Value]) -> Result<(), magnus::Error> {
|
|
449
|
+
let url: String = match args.first() {
|
|
450
|
+
Some(v) => TryConvert::try_convert(*v)?,
|
|
451
|
+
None => return Err(generic_error("url is required")),
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
let mut req = self.inner.get(&url);
|
|
455
|
+
if let Some(opts) = args.get(1) {
|
|
456
|
+
req = apply_request_options(req, &RHash::try_convert(*opts)?)?;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
let client_token = self.cancel_token.lock().unwrap_or_else(|e| e.into_inner()).clone();
|
|
460
|
+
|
|
461
|
+
let outcome: Option<Result<(), wreq::Error>> = unsafe {
|
|
462
|
+
without_gvl(|thread_token| {
|
|
463
|
+
runtime().block_on(async {
|
|
464
|
+
tokio::select! {
|
|
465
|
+
biased;
|
|
466
|
+
_ = thread_token.cancelled() => None,
|
|
467
|
+
_ = client_token.cancelled() => None,
|
|
468
|
+
res = req.preconnect() => Some(res),
|
|
469
|
+
}
|
|
470
|
+
})
|
|
471
|
+
})
|
|
472
|
+
}?;
|
|
473
|
+
|
|
474
|
+
match outcome {
|
|
475
|
+
Some(res) => res.map_err(to_magnus_error),
|
|
476
|
+
None => Err(generic_error("request interrupted")),
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
440
480
|
fn cancel(&self) {
|
|
441
481
|
// Replace the cancel token first so new requests use a fresh token,
|
|
442
482
|
// then cancel the old one to unblock all current in-flight select!s.
|
|
@@ -489,7 +529,7 @@ impl Client {
|
|
|
489
529
|
}
|
|
490
530
|
})
|
|
491
531
|
})
|
|
492
|
-
}
|
|
532
|
+
}?;
|
|
493
533
|
|
|
494
534
|
let data = match outcome {
|
|
495
535
|
RequestOutcome::Ok(d) => d,
|
|
@@ -545,7 +585,7 @@ impl Client {
|
|
|
545
585
|
}
|
|
546
586
|
})
|
|
547
587
|
})
|
|
548
|
-
}
|
|
588
|
+
}?;
|
|
549
589
|
|
|
550
590
|
let items = match outcome {
|
|
551
591
|
BatchOutcome::Done(items) => items,
|
|
@@ -638,6 +678,10 @@ fn apply_request_options(
|
|
|
638
678
|
req = req.headers(hmap);
|
|
639
679
|
}
|
|
640
680
|
|
|
681
|
+
if let Some(enabled) = hash_get_bool(opts, "default_headers")? {
|
|
682
|
+
req = req.default_headers(enabled);
|
|
683
|
+
}
|
|
684
|
+
|
|
641
685
|
if let Some(body_str) = hash_get_string(opts, "body")? {
|
|
642
686
|
req = req.body(body_str);
|
|
643
687
|
}
|
|
@@ -866,6 +910,7 @@ pub fn init(_ruby: &magnus::Ruby, module: &magnus::RModule) -> Result<(), magnus
|
|
|
866
910
|
client_class.define_method("head", method!(Client::head, -1))?;
|
|
867
911
|
client_class.define_method("options", method!(Client::options, -1))?;
|
|
868
912
|
client_class.define_method("request_batch", method!(Client::request_batch, -1))?;
|
|
913
|
+
client_class.define_method("preconnect", method!(Client::preconnect, -1))?;
|
|
869
914
|
client_class.define_method("cancel", method!(Client::cancel, 0))?;
|
|
870
915
|
|
|
871
916
|
module.define_module_function("get", function!(wreq_get, -1))?;
|
|
@@ -877,3 +922,89 @@ pub fn init(_ruby: &magnus::Ruby, module: &magnus::RModule) -> Result<(), magnus
|
|
|
877
922
|
|
|
878
923
|
Ok(())
|
|
879
924
|
}
|
|
925
|
+
|
|
926
|
+
#[cfg(test)]
|
|
927
|
+
mod tests {
|
|
928
|
+
use super::*;
|
|
929
|
+
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
930
|
+
|
|
931
|
+
static READY: AtomicBool = AtomicBool::new(false);
|
|
932
|
+
static LIVE: AtomicUsize = AtomicUsize::new(0);
|
|
933
|
+
|
|
934
|
+
struct DropProbe;
|
|
935
|
+
|
|
936
|
+
impl DropProbe {
|
|
937
|
+
fn new() -> Self {
|
|
938
|
+
LIVE.fetch_add(1, Ordering::SeqCst);
|
|
939
|
+
Self
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
impl Drop for DropProbe {
|
|
944
|
+
fn drop(&mut self) {
|
|
945
|
+
LIVE.fetch_sub(1, Ordering::SeqCst);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
fn probe_ready() -> bool {
|
|
950
|
+
READY.load(Ordering::SeqCst)
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
fn drop_probe() -> Result<(), magnus::Error> {
|
|
954
|
+
let _caller_resource = DropProbe::new();
|
|
955
|
+
let result = unsafe {
|
|
956
|
+
without_gvl(|token| {
|
|
957
|
+
let _callback_resource = DropProbe::new();
|
|
958
|
+
READY.store(true, Ordering::SeqCst);
|
|
959
|
+
runtime().block_on(token.cancelled());
|
|
960
|
+
DropProbe::new()
|
|
961
|
+
})
|
|
962
|
+
}?;
|
|
963
|
+
drop(result);
|
|
964
|
+
Ok(())
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
#[test]
|
|
968
|
+
fn ruby_interrupts_drop_native_resources() {
|
|
969
|
+
let ruby = unsafe { magnus::embed::init() };
|
|
970
|
+
let result = unsafe { without_gvl(|_| DropProbe::new()) }.unwrap();
|
|
971
|
+
assert_eq!(LIVE.load(Ordering::SeqCst), 1);
|
|
972
|
+
drop(result);
|
|
973
|
+
assert_eq!(LIVE.load(Ordering::SeqCst), 0);
|
|
974
|
+
|
|
975
|
+
let panicked = panic::catch_unwind(|| unsafe {
|
|
976
|
+
without_gvl::<_, ()>(|_| {
|
|
977
|
+
let _resource = DropProbe::new();
|
|
978
|
+
panic!("probe panic");
|
|
979
|
+
})
|
|
980
|
+
});
|
|
981
|
+
assert!(panicked.is_err());
|
|
982
|
+
assert_eq!(LIVE.load(Ordering::SeqCst), 0);
|
|
983
|
+
|
|
984
|
+
ruby.define_global_function("wreq_drop_probe", function!(drop_probe, 0));
|
|
985
|
+
ruby.define_global_function("wreq_probe_ready", function!(probe_ready, 0));
|
|
986
|
+
|
|
987
|
+
for (interrupt, expected) in [
|
|
988
|
+
("worker.kill", None),
|
|
989
|
+
("worker.raise(RuntimeError, 'stop request')", Some("stop request")),
|
|
990
|
+
] {
|
|
991
|
+
READY.store(false, Ordering::SeqCst);
|
|
992
|
+
let result: Option<String> = ruby.eval(&format!(
|
|
993
|
+
"worker = Thread.new do
|
|
994
|
+
begin
|
|
995
|
+
wreq_drop_probe
|
|
996
|
+
'returned normally'
|
|
997
|
+
rescue RuntimeError => error
|
|
998
|
+
error.message
|
|
999
|
+
end
|
|
1000
|
+
end
|
|
1001
|
+
Thread.pass until wreq_probe_ready
|
|
1002
|
+
{interrupt}
|
|
1003
|
+
worker.value"
|
|
1004
|
+
)).unwrap();
|
|
1005
|
+
|
|
1006
|
+
assert_eq!(result.as_deref(), expected);
|
|
1007
|
+
assert_eq!(LIVE.load(Ordering::SeqCst), 0, "resources leaked after {interrupt}");
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
data/ext/wreq_rb/src/response.rs
CHANGED
|
@@ -5,7 +5,8 @@ use magnus::{
|
|
|
5
5
|
use crate::error::generic_error;
|
|
6
6
|
|
|
7
7
|
/// Wraps a wreq::Response in a Ruby-accessible type.
|
|
8
|
-
#[magnus::
|
|
8
|
+
#[derive(magnus::TypedData)]
|
|
9
|
+
#[magnus(class = "Wreq::Response", free_immediately, size)]
|
|
9
10
|
pub struct Response {
|
|
10
11
|
status: u16,
|
|
11
12
|
headers: Vec<(String, String)>,
|
|
@@ -26,7 +27,7 @@ impl Response {
|
|
|
26
27
|
content_length: Option<u64>,
|
|
27
28
|
transfer_size: Option<u64>,
|
|
28
29
|
) -> Self {
|
|
29
|
-
Self {
|
|
30
|
+
let response = Self {
|
|
30
31
|
status,
|
|
31
32
|
headers,
|
|
32
33
|
body,
|
|
@@ -34,7 +35,18 @@ impl Response {
|
|
|
34
35
|
version,
|
|
35
36
|
content_length,
|
|
36
37
|
transfer_size,
|
|
37
|
-
}
|
|
38
|
+
};
|
|
39
|
+
unsafe { Ruby::get_unchecked() }
|
|
40
|
+
.gc_adjust_memory_usage(response.heap_size() as isize);
|
|
41
|
+
response
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn heap_size(&self) -> usize {
|
|
45
|
+
self.body.capacity()
|
|
46
|
+
+ self.url.capacity()
|
|
47
|
+
+ self.version.capacity()
|
|
48
|
+
+ self.headers.capacity() * std::mem::size_of::<(String, String)>()
|
|
49
|
+
+ self.headers.iter().map(|(name, value)| name.capacity() + value.capacity()).sum::<usize>()
|
|
38
50
|
}
|
|
39
51
|
|
|
40
52
|
fn status(&self) -> u16 {
|
|
@@ -119,6 +131,19 @@ impl Response {
|
|
|
119
131
|
}
|
|
120
132
|
}
|
|
121
133
|
|
|
134
|
+
impl magnus::DataTypeFunctions for Response {
|
|
135
|
+
fn size(&self) -> usize {
|
|
136
|
+
std::mem::size_of::<Self>() + self.heap_size()
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
impl Drop for Response {
|
|
141
|
+
fn drop(&mut self) {
|
|
142
|
+
unsafe { Ruby::get_unchecked() }
|
|
143
|
+
.gc_adjust_memory_usage(-(self.heap_size() as isize));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
122
147
|
pub fn init(ruby: &magnus::Ruby, module: &magnus::RModule) -> Result<(), magnus::Error> {
|
|
123
148
|
let class = module.define_class("Response", ruby.class_object())?;
|
|
124
149
|
class.define_method("status", method!(Response::status, 0))?;
|
data/lib/wreq-rb/version.rb
CHANGED