Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
update response and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Oct 7, 2023
1 parent b185579 commit 74eef67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
95 changes: 57 additions & 38 deletions cargo-registry/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! The `registry_service` module implements the Solana cargo registry service.
use {
crate::{client::Client, dummy_git_index::DummyGitIndex, publisher::Publisher},
crate::{
client::Client,
dummy_git_index::DummyGitIndex,
publisher::{Error, Publisher},
},
hyper::{
body,
service::{make_service_fn, service_fn},
Expand All @@ -20,22 +24,29 @@ mod dummy_git_index;
mod publisher;

const PATH_PREFIX: &str = "/api/v1/crates";
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

pub struct CargoRegistryService {}

impl CargoRegistryService {
fn error_response() -> hyper::Response<hyper::Body> {
fn error_response(error: &str) -> hyper::Response<hyper::Body> {
error!("{}", error);
hyper::Response::builder()
.status(hyper::StatusCode::OK)
.body(hyper::Body::from("error"))
.status(hyper::StatusCode::BAD_REQUEST)
.body(hyper::Body::from(
serde_json::json!({
"errors" : [
{"details": error}
]
})
.to_string(),
))
.unwrap()
}

fn success_response() -> hyper::Response<hyper::Body> {
hyper::Response::builder()
.status(hyper::StatusCode::OK)
.body(hyper::Body::from("success"))
.body(hyper::Body::from(""))
.unwrap()
}

Expand All @@ -45,20 +56,28 @@ impl CargoRegistryService {
) -> hyper::Response<hyper::Body> {
info!("Handling request to publish the crate");
let bytes = body::to_bytes(request.into_body()).await;
if let Ok(bytes) = bytes {
let result =
tokio::task::spawn_blocking(move || Publisher::publish_crate(bytes, client)).await;

if result.is_ok() {
info!("Published the crate successfully. {:?}", result);
Self::success_response()
} else {
error!("Failed to publish the crate. {:?}", result);
Self::error_response()

match bytes {
Ok(data) => {
let Ok(result) =
tokio::task::spawn_blocking(move || Publisher::publish_crate(data, client))
.await
else {
return Self::error_response(
"Internal error. Failed to wait for program deployment",
);
};

if result.is_ok() {
info!("Published the crate successfully. {:?}", result);
Self::success_response()
} else {
Self::error_response(
format!("Failed to publish the crate. {:?}", result).as_str(),
)
}
}
} else {
error!("Failed to receive the data from the request");
Self::error_response()
Err(_) => Self::error_response("Failed to receive the crate data from the client."),
}
}

Expand All @@ -75,11 +94,11 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name, _version)) = Self::get_crate_name_and_version(path) else {
return Self::error_response();
return Self::error_response("Failed to parse the request.");
};

if path.len() != PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -90,11 +109,11 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name, _version)) = Self::get_crate_name_and_version(path) else {
return Self::error_response();
return Self::error_response("Failed to parse the request.");
};

if path.len() != PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -109,11 +128,11 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return Self::error_response();
return Self::error_response("Failed to parse the request.");
};

if path.len() != PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -124,11 +143,11 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return Self::error_response();
return Self::error_response("Failed to parse the request.");
};

if path.len() != PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -139,11 +158,11 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return Self::error_response();
return Self::error_response("Failed to parse the request.");
};

if path.len() != PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -159,7 +178,7 @@ impl CargoRegistryService {
// full path started with PATH_PREFIX. So it's sufficient to check that provided
// path is smaller than PATH_PREFIX.
if path.len() >= PATH_PREFIX.len() {
return Self::error_response();
return Self::error_response("Request length is incorrect");
}

Self::success_response()
Expand All @@ -174,41 +193,41 @@ impl CargoRegistryService {
return Static::new("/tmp/dummy-git")
.serve(request)
.await
.or_else(|_| Ok(Self::error_response()));
.or_else(|_| Ok(Self::error_response("Failed to serve git index")));
}

if !path.starts_with(PATH_PREFIX) {
return Ok(Self::error_response());
return Ok(Self::error_response("Invalid path for the request"));
}

let Some((path, endpoint)) = path.rsplit_once('/') else {
return Ok(Self::error_response());
return Ok(Self::error_response("Invalid endpoint in the path"));
};

Ok(match *request.method() {
Method::PUT => match endpoint {
"new" => {
if path.len() != PATH_PREFIX.len() {
Self::error_response()
Self::error_response("Invalid length of the request.")
} else {
Self::handle_publish_request(request, client.clone()).await
}
}
"unyank" => Self::handle_unyank_request(path, &request),
"owners" => Self::handle_add_owners_request(path, &request),
_ => Self::error_response(),
_ => Self::error_response("Unknown request"),
},
Method::GET => match endpoint {
"crates" => Self::handle_get_crates_request(path, &request),
"owners" => Self::handle_get_owners_request(path, &request),
_ => Self::error_response(),
_ => Self::error_response("Unknown request"),
},
Method::DELETE => match endpoint {
"yank" => Self::handle_yank_request(path, &request),
"owners" => Self::handle_delete_owners_request(path, &request),
_ => Self::error_response(),
_ => Self::error_response("Unknown request"),
},
_ => Self::error_response(),
_ => Self::error_response("Unknown request"),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion cargo-registry/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use {
tempfile::{tempdir, TempDir},
};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
Expand Down

0 comments on commit 74eef67

Please sign in to comment.