Skip to content

Commit 4702fa3

Browse files
committed
Include the IP address in HTTP errors.
1 parent c7c9b8f commit 4702fa3

File tree

6 files changed

+74
-32
lines changed

6 files changed

+74
-32
lines changed

src/cargo/core/package.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -880,13 +880,12 @@ impl<'a, 'cfg> Downloads<'a, 'cfg> {
880880

881881
let code = handle.response_code()?;
882882
if code != 200 && code != 0 {
883-
let url = handle.effective_url()?.unwrap_or(url);
884-
return Err(HttpNotSuccessful {
885-
code,
886-
url: url.to_string(),
887-
body: data,
883+
return Err(HttpNotSuccessful::new_from_handle(
884+
&mut handle,
885+
&url,
886+
data,
888887
headers,
889-
}
888+
)
890889
.into());
891890
}
892891
Ok(data)

src/cargo/sources/registry/http_remote.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,13 @@ impl<'cfg> HttpRegistry<'cfg> {
288288
304 => StatusCode::NotModified,
289289
401 => StatusCode::Unauthorized,
290290
404 | 410 | 451 => StatusCode::NotFound,
291-
code => {
292-
let url = handle.effective_url()?.unwrap_or(&url);
293-
return Err(HttpNotSuccessful {
294-
code,
295-
url: url.to_owned(),
296-
body: data,
297-
headers: download.header_map.take().others,
298-
}
291+
_ => {
292+
return Err(HttpNotSuccessful::new_from_handle(
293+
&mut handle,
294+
&url,
295+
data,
296+
download.header_map.take().others,
297+
)
299298
.into());
300299
}
301300
};
@@ -548,6 +547,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
548547
code: 401,
549548
body: result.data,
550549
url: self.full_url(path),
550+
ip: None,
551551
headers: result.header_map.others,
552552
}
553553
.into());

src/cargo/util/errors.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(unknown_lints)]
22

33
use anyhow::Error;
4+
use curl::easy::Easy;
45
use std::fmt;
56
use std::path::PathBuf;
67

@@ -22,10 +23,35 @@ pub const DEBUG_HEADERS: &[&str] = &[
2223
pub struct HttpNotSuccessful {
2324
pub code: u32,
2425
pub url: String,
26+
pub ip: Option<String>,
2527
pub body: Vec<u8>,
2628
pub headers: Vec<String>,
2729
}
2830

31+
impl HttpNotSuccessful {
32+
pub fn new_from_handle(
33+
handle: &mut Easy,
34+
initial_url: &str,
35+
body: Vec<u8>,
36+
headers: Vec<String>,
37+
) -> HttpNotSuccessful {
38+
let ip = handle.primary_ip().ok().flatten().map(|s| s.to_string());
39+
let url = handle
40+
.effective_url()
41+
.ok()
42+
.flatten()
43+
.unwrap_or(initial_url)
44+
.to_string();
45+
HttpNotSuccessful {
46+
code: handle.response_code().unwrap_or(0),
47+
url,
48+
ip,
49+
body,
50+
headers,
51+
}
52+
}
53+
}
54+
2955
impl fmt::Display for HttpNotSuccessful {
3056
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3157
let body = std::str::from_utf8(&self.body)
@@ -34,9 +60,13 @@ impl fmt::Display for HttpNotSuccessful {
3460

3561
write!(
3662
f,
37-
"failed to get successful HTTP response from `{}`, got {}\n",
38-
self.url, self.code,
63+
"failed to get successful HTTP response from `{}`",
64+
self.url
3965
)?;
66+
if let Some(ip) = &self.ip {
67+
write!(f, " ({ip})")?;
68+
}
69+
write!(f, ", got {}\n", self.code,)?;
4070
if !self.headers.is_empty() {
4171
write!(f, "debug headers:\n{}\n", self.headers.join("\n"))?;
4272
}

src/cargo/util/network/retry.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ fn with_retry_repeats_the_call_then_works() {
150150
let error1 = HttpNotSuccessful {
151151
code: 501,
152152
url: "Uri".to_string(),
153+
ip: None,
153154
body: Vec::new(),
154155
headers: Vec::new(),
155156
}
156157
.into();
157158
let error2 = HttpNotSuccessful {
158159
code: 502,
159160
url: "Uri".to_string(),
161+
ip: None,
160162
body: Vec::new(),
161163
headers: Vec::new(),
162164
}
@@ -177,13 +179,15 @@ fn with_retry_finds_nested_spurious_errors() {
177179
let error1 = anyhow::Error::from(HttpNotSuccessful {
178180
code: 501,
179181
url: "Uri".to_string(),
182+
ip: None,
180183
body: Vec::new(),
181184
headers: Vec::new(),
182185
});
183186
let error1 = anyhow::Error::from(error1.context("A non-spurious wrapping err"));
184187
let error2 = anyhow::Error::from(HttpNotSuccessful {
185188
code: 502,
186189
url: "Uri".to_string(),
190+
ip: None,
187191
body: Vec::new(),
188192
headers: Vec::new(),
189193
});
@@ -203,6 +207,7 @@ fn default_retry_schedule() {
203207
Err(anyhow::Error::from(HttpNotSuccessful {
204208
code: 500,
205209
url: "Uri".to_string(),
210+
ip: None,
206211
body: Vec::new(),
207212
headers: Vec::new(),
208213
}))

tests/testsuite/registry.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,10 +2743,10 @@ fn sparse_retry_single() {
27432743
.with_stderr(
27442744
"\
27452745
[UPDATING] `dummy-registry` index
2746-
warning: spurious network error (3 tries remaining): failed to get successful HTTP response from `[..]`, got 500
2746+
warning: spurious network error (3 tries remaining): failed to get successful HTTP response from `[..]` (127.0.0.1), got 500
27472747
body:
27482748
internal server error
2749-
warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `[..]`, got 500
2749+
warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `[..]` (127.0.0.1), got 500
27502750
body:
27512751
internal server error
27522752
[DOWNLOADING] crates ...
@@ -2816,7 +2816,7 @@ fn sparse_retry_multiple() {
28162816
&mut expected,
28172817
"warning: spurious network error ({remain} tries remaining): \
28182818
failed to get successful HTTP response from \
2819-
`http://127.0.0.1:[..]/{ab}/{cd}/{name}`, got 500\n\
2819+
`http://127.0.0.1:[..]/{ab}/{cd}/{name}` (127.0.0.1), got 500\n\
28202820
body:\n\
28212821
internal server error\n"
28222822
)
@@ -2876,10 +2876,12 @@ fn dl_retry_single() {
28762876
.with_stderr("\
28772877
[UPDATING] `dummy-registry` index
28782878
[DOWNLOADING] crates ...
2879-
warning: spurious network error (3 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 500
2879+
warning: spurious network error (3 tries remaining): \
2880+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 500
28802881
body:
28812882
internal server error
2882-
warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 500
2883+
warning: spurious network error (2 tries remaining): \
2884+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 500
28832885
body:
28842886
internal server error
28852887
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
@@ -2954,7 +2956,7 @@ fn dl_retry_multiple() {
29542956
&mut expected,
29552957
"warning: spurious network error ({remain} tries remaining): \
29562958
failed to get successful HTTP response from \
2957-
`http://127.0.0.1:[..]/dl/{name}/1.0.0/download`, got 500\n\
2959+
`http://127.0.0.1:[..]/dl/{name}/1.0.0/download` (127.0.0.1), got 500\n\
29582960
body:\n\
29592961
internal server error\n"
29602962
)
@@ -3315,21 +3317,24 @@ fn debug_header_message_index() {
33153317
.build();
33163318
p.cargo("fetch").with_status(101).with_stderr("\
33173319
[UPDATING] `dummy-registry` index
3318-
warning: spurious network error (3 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar`, got 503
3320+
warning: spurious network error (3 tries remaining): \
3321+
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
33193322
debug headers:
33203323
x-amz-cf-pop: SFO53-P2
33213324
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
33223325
x-cache: Hit from cloudfront
33233326
body:
33243327
Please slow down
3325-
warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar`, got 503
3328+
warning: spurious network error (2 tries remaining): \
3329+
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
33263330
debug headers:
33273331
x-amz-cf-pop: SFO53-P2
33283332
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
33293333
x-cache: Hit from cloudfront
33303334
body:
33313335
Please slow down
3332-
warning: spurious network error (1 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar`, got 503
3336+
warning: spurious network error (1 tries remaining): \
3337+
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
33333338
debug headers:
33343339
x-amz-cf-pop: SFO53-P2
33353340
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
@@ -3345,7 +3350,7 @@ Caused by:
33453350
download of 3/b/bar failed
33463351
33473352
Caused by:
3348-
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar`, got 503
3353+
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
33493354
debug headers:
33503355
x-amz-cf-pop: SFO53-P2
33513356
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
@@ -3386,21 +3391,24 @@ fn debug_header_message_dl() {
33863391
p.cargo("fetch").with_status(101).with_stderr("\
33873392
[UPDATING] `dummy-registry` index
33883393
[DOWNLOADING] crates ...
3389-
warning: spurious network error (3 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 503
3394+
warning: spurious network error (3 tries remaining): \
3395+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
33903396
debug headers:
33913397
x-amz-cf-pop: SFO53-P2
33923398
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
33933399
x-cache: Hit from cloudfront
33943400
body:
33953401
Please slow down
3396-
warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 503
3402+
warning: spurious network error (2 tries remaining): \
3403+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
33973404
debug headers:
33983405
x-amz-cf-pop: SFO53-P2
33993406
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
34003407
x-cache: Hit from cloudfront
34013408
body:
34023409
Please slow down
3403-
warning: spurious network error (1 tries remaining): failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 503
3410+
warning: spurious network error (1 tries remaining): \
3411+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
34043412
debug headers:
34053413
x-amz-cf-pop: SFO53-P2
34063414
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
@@ -3410,7 +3418,7 @@ Please slow down
34103418
error: failed to download from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`
34113419
34123420
Caused by:
3413-
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`, got 503
3421+
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
34143422
debug headers:
34153423
x-amz-cf-pop: SFO53-P2
34163424
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==

tests/testsuite/registry_auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn requires_nightly() {
5757
error: failed to download from `[..]/dl/bar/0.0.1/download`
5858
5959
Caused by:
60-
failed to get successful HTTP response from `[..]`, got 401
60+
failed to get successful HTTP response from `[..]` (127.0.0.1), got 401
6161
body:
6262
Unauthorized message from server.
6363
"#,
@@ -415,7 +415,7 @@ fn incorrect_token_git() {
415415
[ERROR] failed to download from `http://[..]/dl/bar/0.0.1/download`
416416
417417
Caused by:
418-
failed to get successful HTTP response from `http://[..]/dl/bar/0.0.1/download`, got 401
418+
failed to get successful HTTP response from `http://[..]/dl/bar/0.0.1/download` (127.0.0.1), got 401
419419
body:
420420
Unauthorized message from server.",
421421
)

0 commit comments

Comments
 (0)