cargo/crates/crates-io/lib.rs

501 lines
15 KiB
Rust
Raw Normal View History

2021-04-13 16:02:07 +00:00
#![allow(clippy::all)]
2018-12-06 19:21:24 +00:00
use std::collections::BTreeMap;
use std::fmt;
use std::fs::File;
use std::io::prelude::*;
use std::io::{Cursor, SeekFrom};
2019-05-13 20:18:42 +00:00
use std::time::Instant;
use anyhow::{bail, format_err, Context, Result};
2020-01-08 03:58:45 +00:00
use curl::easy::{Easy, List};
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
2018-12-12 21:20:44 +00:00
use serde::{Deserialize, Serialize};
use url::Url;
pub struct Registry {
2018-12-20 01:40:01 +00:00
/// The base URL for issuing API requests.
host: String,
2018-12-20 01:40:01 +00:00
/// Optional authorization token.
/// If None, commands requiring authorization will fail.
2014-11-22 14:36:14 +00:00
token: Option<String>,
2018-12-20 01:40:01 +00:00
/// Curl handle for issuing requests.
2016-05-18 17:01:40 +00:00
handle: Easy,
}
2015-04-05 07:00:01 +00:00
#[derive(PartialEq, Clone, Copy)]
2014-11-22 14:36:14 +00:00
pub enum Auth {
Authorized,
Unauthorized,
2016-05-18 17:01:40 +00:00
}
#[derive(Deserialize)]
2014-11-22 14:36:14 +00:00
pub struct Crate {
pub name: String,
pub description: Option<String>,
pub max_version: String,
2014-11-22 14:36:14 +00:00
}
#[derive(Serialize, Deserialize)]
pub struct NewCrate {
pub name: String,
pub vers: String,
pub deps: Vec<NewCrateDependency>,
pub features: BTreeMap<String, Vec<String>>,
pub authors: Vec<String>,
pub description: Option<String>,
pub documentation: Option<String>,
pub homepage: Option<String>,
pub readme: Option<String>,
pub readme_file: Option<String>,
pub keywords: Vec<String>,
pub categories: Vec<String>,
pub license: Option<String>,
pub license_file: Option<String>,
pub repository: Option<String>,
pub badges: BTreeMap<String, BTreeMap<String, String>>,
2018-12-20 01:40:01 +00:00
pub links: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct NewCrateDependency {
pub optional: bool,
pub default_features: bool,
pub name: String,
pub features: Vec<String>,
pub version_req: String,
pub target: Option<String>,
pub kind: String,
Fix publishing renamed dependencies to crates.io This commit fixes publishing crates which contain locally renamed dependencies to crates.io. Previously this lack of information meant that although we could resolve the crate graph correctly it wouldn't work well with respect to optional features and optional dependencies. The fix here is to persist this information into the registry about the crate being renamed in `Cargo.toml`, allowing Cargo to correctly deduce feature names as it does when it has `Cargo.toml` locally. A dual side of this commit is to publish this information to crates.io. We'll want to merge the associated PR (link to come soon) on crates.io first and make sure that's deployed as well before we stabilize the crate renaming feature. The index format is updated as well as part of this change. The `name` key for dependencies is now unconditionally what was written in `Cargo.toml` as the left-hand-side of the dependency specification. In other words this is the raw crate name, but only for the local crate. A new key, `package`, is added to dependencies (and it can be `None`). This key indicates the crates.io package is being linked against, an represents the `package` key in `Cargo.toml`. It's important to consider the interaction with older Cargo implementations which don't support the `package` key in the index. In these situations older Cargo binaries will likely fail to resolve entirely as the renamed name is unlikely to exist on crates.io. For example the `futures` crate now has an optional dependency with the name `futures01` which depends on an older version of `futures` on crates.io. The string `futures01` will be listed in the index under the `"name"` key, but no `futures01` crate exists on crates.io so older Cargo will generate an error. If the crate does exist on crates.io, then even weirder error messages will likely result. Closes #5962
2018-09-07 16:37:06 +00:00
#[serde(skip_serializing_if = "Option::is_none")]
pub registry: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub explicit_name_in_toml: Option<String>,
}
#[derive(Deserialize)]
pub struct User {
2015-01-13 16:41:04 +00:00
pub id: u32,
pub login: String,
2015-08-05 18:33:56 +00:00
pub avatar: Option<String>,
pub email: Option<String>,
pub name: Option<String>,
}
pub struct Warnings {
pub invalid_categories: Vec<String>,
pub invalid_badges: Vec<String>,
pub other: Vec<String>,
}
2018-03-14 15:17:44 +00:00
#[derive(Deserialize)]
struct R {
ok: bool,
}
#[derive(Deserialize)]
struct OwnerResponse {
ok: bool,
msg: String,
}
#[derive(Deserialize)]
struct ApiErrorList {
errors: Vec<ApiError>,
}
#[derive(Deserialize)]
struct ApiError {
detail: String,
}
#[derive(Serialize)]
struct OwnersReq<'a> {
users: &'a [&'a str],
}
#[derive(Deserialize)]
struct Users {
users: Vec<User>,
}
#[derive(Deserialize)]
struct TotalCrates {
total: u32,
}
#[derive(Deserialize)]
struct Crates {
crates: Vec<Crate>,
meta: TotalCrates,
}
#[derive(Debug)]
pub enum ResponseError {
Curl(curl::Error),
Api {
code: u32,
errors: Vec<String>,
},
Code {
code: u32,
headers: Vec<String>,
body: String,
},
Other(anyhow::Error),
}
impl std::error::Error for ResponseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ResponseError::Curl(..) => None,
ResponseError::Api { .. } => None,
ResponseError::Code { .. } => None,
ResponseError::Other(e) => Some(e.as_ref()),
}
}
}
impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ResponseError::Curl(e) => write!(f, "{}", e),
ResponseError::Api { code, errors } => {
f.write_str("the remote server responded with an error")?;
if *code != 200 {
write!(f, " (status {} {})", code, reason(*code))?;
};
write!(f, ": {}", errors.join(", "))
}
ResponseError::Code {
code,
headers,
body,
} => write!(
f,
"failed to get a 200 OK response, got {}\n\
headers:\n\
\t{}\n\
body:\n\
{}",
code,
headers.join("\n\t"),
body
),
ResponseError::Other(..) => write!(f, "invalid response from server"),
}
}
}
impl From<curl::Error> for ResponseError {
fn from(error: curl::Error) -> Self {
ResponseError::Curl(error)
}
}
impl Registry {
2021-01-22 19:06:51 +00:00
/// Creates a new `Registry`.
///
/// ## Example
///
/// ```rust
/// use curl::easy::Easy;
/// use crates_io::Registry;
///
/// let mut handle = Easy::new();
/// // If connecting to crates.io, a user-agent is required.
/// handle.useragent("my_crawler (example.com/info)");
/// let mut reg = Registry::new_handle(String::from("https://crates.io"), None, handle);
/// ```
2018-03-14 15:17:44 +00:00
pub fn new_handle(host: String, token: Option<String>, handle: Easy) -> Registry {
Registry {
host,
token,
handle,
}
}
2018-12-20 01:40:01 +00:00
pub fn host(&self) -> &str {
&self.host
}
pub fn host_is_crates_io(&self) -> bool {
is_url_crates_io(&self.host)
}
pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> {
let body = serde_json::to_string(&OwnersReq { users: owners })?;
2018-07-20 19:39:05 +00:00
let body = self.put(&format!("/crates/{}/owners", krate), body.as_bytes())?;
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
Ok(serde_json::from_str::<OwnerResponse>(&body)?.msg)
}
pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<()> {
let body = serde_json::to_string(&OwnersReq { users: owners })?;
2018-07-20 19:39:05 +00:00
let body = self.delete(&format!("/crates/{}/owners", krate), Some(body.as_bytes()))?;
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
Ok(())
}
pub fn list_owners(&mut self, krate: &str) -> Result<Vec<User>> {
2018-07-20 19:39:05 +00:00
let body = self.get(&format!("/crates/{}/owners", krate))?;
Ok(serde_json::from_str::<Users>(&body)?.users)
}
pub fn publish(&mut self, krate: &NewCrate, mut tarball: &File) -> Result<Warnings> {
let json = serde_json::to_string(krate)?;
// Prepare the body. The format of the upload request is:
//
// <le u32 of json>
// <json request> (metadata for the package)
// <le u32 of tarball>
// <source tarball>
// NOTE: This can be replaced with `stream_len` if it is ever stabilized.
//
// This checks the length using seeking instead of metadata, because
// on some filesystems, getting the metadata will fail because
// the file was renamed in ops::package.
let tarball_len = tarball
.seek(SeekFrom::End(0))
.with_context(|| "failed to seek tarball")?;
tarball
.seek(SeekFrom::Start(0))
.with_context(|| "failed to seek tarball")?;
let header = {
let mut w = Vec::new();
w.extend(&(json.len() as u32).to_le_bytes());
w.extend(json.as_bytes().iter().cloned());
w.extend(&(tarball_len as u32).to_le_bytes());
w
};
let size = tarball_len as usize + header.len();
let mut body = Cursor::new(header).chain(tarball);
let url = format!("{}/api/v1/crates/new", self.host);
2014-11-22 14:36:14 +00:00
2015-03-09 18:30:37 +00:00
let token = match self.token.as_ref() {
Some(s) => s,
None => bail!("no upload token found, please run `cargo login`"),
2015-03-09 18:30:37 +00:00
};
self.handle.put(true)?;
self.handle.url(&url)?;
self.handle.in_filesize(size as u64)?;
2016-05-18 17:01:40 +00:00
let mut headers = List::new();
headers.append("Accept: application/json")?;
headers.append(&format!("Authorization: {}", token))?;
self.handle.http_headers(headers)?;
2016-05-18 17:01:40 +00:00
let started = Instant::now();
let body = self
.handle(&mut |buf| body.read(buf).unwrap_or(0))
.map_err(|e| match e {
ResponseError::Code { code, .. }
if code == 503
&& started.elapsed().as_secs() >= 29
&& self.host_is_crates_io() =>
{
format_err!(
"Request timed out after 30 seconds. If you're trying to \
upload a crate it may be too large. If the crate is under \
10MB in size, you can email help@crates.io for assistance.\n\
Total size was {}.",
tarball_len
)
}
_ => e.into(),
})?;
let response = if body.is_empty() {
"{}".parse()?
} else {
body.parse::<serde_json::Value>()?
};
let invalid_categories: Vec<String> = response
.get("warnings")
.and_then(|j| j.get("invalid_categories"))
.and_then(|j| j.as_array())
.map(|x| x.iter().flat_map(|j| j.as_str()).map(Into::into).collect())
.unwrap_or_else(Vec::new);
let invalid_badges: Vec<String> = response
.get("warnings")
.and_then(|j| j.get("invalid_badges"))
.and_then(|j| j.as_array())
.map(|x| x.iter().flat_map(|j| j.as_str()).map(Into::into).collect())
.unwrap_or_else(Vec::new);
let other: Vec<String> = response
.get("warnings")
.and_then(|j| j.get("other"))
.and_then(|j| j.as_array())
.map(|x| x.iter().flat_map(|j| j.as_str()).map(Into::into).collect())
.unwrap_or_else(Vec::new);
Ok(Warnings {
invalid_categories,
invalid_badges,
other,
})
}
2018-03-10 14:42:08 +00:00
pub fn search(&mut self, query: &str, limit: u32) -> Result<(Vec<Crate>, u32)> {
let formatted_query = percent_encode(query.as_bytes(), NON_ALPHANUMERIC);
let body = self.req(
2018-07-20 19:39:05 +00:00
&format!("/crates?q={}&per_page={}", formatted_query, limit),
2018-03-14 15:17:44 +00:00
None,
Auth::Unauthorized,
)?;
2014-11-22 14:36:14 +00:00
let crates = serde_json::from_str::<Crates>(&body)?;
Ok((crates.crates, crates.meta.total))
2014-11-22 14:36:14 +00:00
}
pub fn yank(&mut self, krate: &str, version: &str) -> Result<()> {
2018-07-20 19:39:05 +00:00
let body = self.delete(&format!("/crates/{}/{}/yank", krate, version), None)?;
assert!(serde_json::from_str::<R>(&body)?.ok);
Ok(())
}
pub fn unyank(&mut self, krate: &str, version: &str) -> Result<()> {
2018-07-20 19:39:05 +00:00
let body = self.put(&format!("/crates/{}/{}/unyank", krate, version), &[])?;
assert!(serde_json::from_str::<R>(&body)?.ok);
Ok(())
}
2018-07-20 19:39:05 +00:00
fn put(&mut self, path: &str, b: &[u8]) -> Result<String> {
self.handle.put(true)?;
2016-05-18 17:01:40 +00:00
self.req(path, Some(b), Auth::Authorized)
}
2018-07-20 19:39:05 +00:00
fn get(&mut self, path: &str) -> Result<String> {
self.handle.get(true)?;
2016-05-18 17:01:40 +00:00
self.req(path, None, Auth::Authorized)
}
2018-07-20 19:39:05 +00:00
fn delete(&mut self, path: &str, b: Option<&[u8]>) -> Result<String> {
self.handle.custom_request("DELETE")?;
2016-05-18 17:01:40 +00:00
self.req(path, b, Auth::Authorized)
}
2018-07-20 19:39:05 +00:00
fn req(&mut self, path: &str, body: Option<&[u8]>, authorized: Auth) -> Result<String> {
self.handle.url(&format!("{}/api/v1{}", self.host, path))?;
2016-05-18 17:01:40 +00:00
let mut headers = List::new();
headers.append("Accept: application/json")?;
headers.append("Content-Type: application/json")?;
2014-11-22 14:36:14 +00:00
if authorized == Auth::Authorized {
2015-03-09 18:30:37 +00:00
let token = match self.token.as_ref() {
Some(s) => s,
None => bail!("no upload token found, please run `cargo login`"),
2015-03-09 18:30:37 +00:00
};
headers.append(&format!("Authorization: {}", token))?;
2014-11-22 14:36:14 +00:00
}
self.handle.http_headers(headers)?;
match body {
2016-05-18 17:01:40 +00:00
Some(mut body) => {
self.handle.upload(true)?;
self.handle.in_filesize(body.len() as u64)?;
self.handle(&mut |buf| body.read(buf).unwrap_or(0))
.map_err(|e| e.into())
2016-05-18 17:01:40 +00:00
}
None => self.handle(&mut |_| 0).map_err(|e| e.into()),
}
}
2016-05-18 17:01:40 +00:00
fn handle(
&mut self,
read: &mut dyn FnMut(&mut [u8]) -> usize,
) -> std::result::Result<String, ResponseError> {
let mut headers = Vec::new();
let mut body = Vec::new();
{
let mut handle = self.handle.transfer();
handle.read_function(|buf| Ok(read(buf)))?;
handle.write_function(|data| {
body.extend_from_slice(data);
Ok(data.len())
})?;
handle.header_function(|data| {
// Headers contain trailing \r\n, trim them to make it easier
// to work with.
let s = String::from_utf8_lossy(data).trim().to_string();
headers.push(s);
true
})?;
handle.perform()?;
2019-05-13 20:18:42 +00:00
}
let body = match String::from_utf8(body) {
Ok(body) => body,
Err(..) => {
return Err(ResponseError::Other(format_err!(
"response body was not valid utf-8"
)))
}
};
let errors = serde_json::from_str::<ApiErrorList>(&body)
.ok()
.map(|s| s.errors.into_iter().map(|s| s.detail).collect::<Vec<_>>());
match (self.handle.response_code()?, errors) {
(0, None) | (200, None) => Ok(body),
(code, Some(errors)) => Err(ResponseError::Api { code, errors }),
(code, None) => Err(ResponseError::Code {
code,
headers,
body,
}),
}
}
}
fn reason(code: u32) -> &'static str {
// Taken from https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
match code {
100 => "Continue",
101 => "Switching Protocol",
103 => "Early Hints",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choice",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Payload Too Large",
414 => "URI Too Long",
415 => "Unsupported Media Type",
416 => "Request Range Not Satisfiable",
417 => "Expectation Failed",
429 => "Too Many Requests",
431 => "Request Header Fields Too Large",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
_ => "<unknown>",
}
}
/// Returns `true` if the host of the given URL is "crates.io".
pub fn is_url_crates_io(url: &str) -> bool {
Url::parse(url)
.map(|u| u.host_str() == Some("crates.io"))
.unwrap_or(false)
}