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
This commit is contained in:
Eduardo Pinho 2020-09-29 23:43:03 +01:00
parent c6b3fb46e8
commit 7d4c8dd74d
2 changed files with 11 additions and 5 deletions

View file

@ -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::<Uri>());
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::<Uri>()
.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();

View file

@ -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();