From 7d4c8dd74d6ea5c27fc9317abf6196e29dbe8668 Mon Sep 17 00:00:00 2001 From: Eduardo Pinho Date: Tue, 29 Sep 2020 23:43:03 +0100 Subject: [PATCH] Add User-Agent to all requests from the server - Required by crates.io, which will otherwise forbid the request - Do the same thing for all other HTTP requests, for consistency and transparency --- src/interactors/crates.rs | 10 +++++++--- src/interactors/mod.rs | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/interactors/crates.rs b/src/interactors/crates.rs index 659c667..7b53e24 100644 --- a/src/interactors/crates.rs +++ b/src/interactors/crates.rs @@ -2,7 +2,7 @@ use std::str; use anyhow::{anyhow, ensure, Error}; use futures::{future, Future, IntoFuture, Stream}; -use hyper::{Body, Error as HyperError, Method, Request, Response, Uri}; +use hyper::{Body, Error as HyperError, Method, Request, Response, Uri, header::USER_AGENT}; use semver::{Version, VersionReq}; use serde::Deserialize; use tokio_service::Service; @@ -97,7 +97,9 @@ where let uri = try_future_box!(format!("{}/master/{}", CRATES_INDEX_BASE_URI, path).parse::()); - let request = Request::get(uri.clone()).body(Body::empty()).unwrap(); + let request = Request::get(uri.clone()) + .header(USER_AGENT, "deps.rs") + .body(Body::empty()).unwrap(); Box::new(self.0.call(request).from_err().and_then(move |response| { let status = response.status(); @@ -170,7 +172,9 @@ where let uri = format!("{}/summary", CRATES_API_BASE_URI) .parse::() .unwrap(); - let request = Request::get(uri.clone()).body(Body::empty()).unwrap(); + let request = Request::get(uri.clone()) + .header(USER_AGENT, "deps.rs") + .body(Body::empty()).unwrap(); Box::new(service.call(request).from_err().and_then(move |response| { let status = response.status(); diff --git a/src/interactors/mod.rs b/src/interactors/mod.rs index fba1459..70213e5 100644 --- a/src/interactors/mod.rs +++ b/src/interactors/mod.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, ensure, Error}; use futures::{Future, Stream}; -use hyper::{Body, Error as HyperError, Method, Request, Response}; +use hyper::{Body, Error as HyperError, Method, Request, Response, header::USER_AGENT}; use relative_path::RelativePathBuf; use tokio_service::Service; @@ -35,7 +35,9 @@ where &RepoSite::Bitbucket => try_future_box!(bitbucket::get_manifest_uri(&repo_path, &path)), }; - let request = Request::get(uri.clone()).body(Body::empty()).unwrap(); + let request = Request::get(uri.clone()) + .header(USER_AGENT, "deps.rs") + .body(Body::empty()).unwrap(); Box::new(self.0.call(request).from_err().and_then(move |response| { let status = response.status();