Skip to content

Commit

Permalink
Code cleanup in cargo-registry (#33711)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 authored Oct 16, 2023
1 parent 167dac2 commit f4fb957
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 104 deletions.
22 changes: 11 additions & 11 deletions cargo-registry/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use {
std::{error, sync::Arc, time::Duration},
};

pub struct ClientConfig<'a>(pub ProgramV4CommandConfig<'a>);
pub(crate) struct RPCCommandConfig<'a>(pub ProgramV4CommandConfig<'a>);

impl<'a> ClientConfig<'a> {
impl<'a> RPCCommandConfig<'a> {
pub fn new(client: &'a Client) -> Self {
Self(ProgramV4CommandConfig {
websocket_url: &client.websocket_url,
Expand All @@ -34,7 +34,7 @@ impl<'a> ClientConfig<'a> {
}
}

pub struct Client {
pub(crate) struct Client {
pub rpc_client: Arc<RpcClient>,
pub port: u16,
pub server_url: String,
Expand Down Expand Up @@ -161,35 +161,35 @@ impl Client {
)
}

pub fn new() -> Result<Client, Box<dyn error::Error>> {
pub(crate) fn new() -> Result<Client, Box<dyn error::Error>> {
let matches = Self::get_clap_app(
crate_name!(),
crate_description!(),
solana_version::version!(),
)
.get_matches();

let config = if let Some(config_file) = matches.value_of("config_file") {
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
Config::load(config_file).unwrap_or_default()
} else {
Config::default()
};

let (_, json_rpc_url) = ConfigInput::compute_json_rpc_url_setting(
matches.value_of("json_rpc_url").unwrap_or(""),
&config.json_rpc_url,
&cli_config.json_rpc_url,
);

let (_, websocket_url) = ConfigInput::compute_websocket_url_setting(
matches.value_of("websocket_url").unwrap_or(""),
&config.websocket_url,
&cli_config.websocket_url,
matches.value_of("json_rpc_url").unwrap_or(""),
&config.json_rpc_url,
&cli_config.json_rpc_url,
);

let (_, commitment) = ConfigInput::compute_commitment_config(
matches.value_of("commitment").unwrap_or(""),
&config.commitment,
&cli_config.commitment,
);

let rpc_timeout = value_t_or_exit!(matches, "rpc_timeout", u64);
Expand All @@ -200,8 +200,8 @@ impl Client {
let confirm_transaction_initial_timeout =
Duration::from_secs(confirm_transaction_initial_timeout);

let payer_keypair = Self::get_keypair(&matches, &config.keypair_path, "keypair")?;
let authority_keypair = Self::get_keypair(&matches, &config.keypair_path, "authority")?;
let payer_keypair = Self::get_keypair(&matches, &cli_config.keypair_path, "keypair")?;
let authority_keypair = Self::get_keypair(&matches, &cli_config.keypair_path, "authority")?;

let port = value_t_or_exit!(matches, "port", u16);

Expand Down
110 changes: 22 additions & 88 deletions cargo-registry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,47 +79,29 @@ 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 response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request.",
);
return response_builder::error_in_parsing();
};

if path.len() != PATH_PREFIX.len() {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

fn handle_unyank_request(
path: &str,
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name, _version)) = Self::get_crate_name_and_version(path) else {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request.",
);
return response_builder::error_in_parsing();
};

if path.len() != PATH_PREFIX.len() {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

fn get_crate_name(path: &str) -> Option<(&str, &str)> {
Expand All @@ -131,71 +113,44 @@ impl CargoRegistryService {
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request.",
);
return response_builder::error_in_parsing();
};

if path.len() != PATH_PREFIX.len() {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

fn handle_add_owners_request(
path: &str,
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request.",
);
return response_builder::error_in_parsing();
};

if path.len() != PATH_PREFIX.len() {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

fn handle_delete_owners_request(
path: &str,
_request: &hyper::Request<hyper::Body>,
) -> hyper::Response<hyper::Body> {
let Some((path, _crate_name)) = Self::get_crate_name(path) else {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request.",
);
return response_builder::error_in_parsing();
};

if path.len() != PATH_PREFIX.len() {
return response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

fn handle_get_crates_request(
Expand All @@ -208,16 +163,10 @@ 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 response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
);
return response_builder::error_incorrect_length();
}

response_builder::error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
response_builder::error_not_implemented()
}

async fn handler(
Expand Down Expand Up @@ -255,41 +204,26 @@ impl CargoRegistryService {
Method::PUT => match endpoint {
"new" => {
if path.len() != PATH_PREFIX.len() {
response_builder::error_response(
hyper::StatusCode::BAD_REQUEST,
"Invalid length of the request.",
)
response_builder::error_incorrect_length()
} else {
Self::handle_publish_request(request, client.clone(), index.clone()).await
}
}
"unyank" => Self::handle_unyank_request(path, &request),
"owners" => Self::handle_add_owners_request(path, &request),
_ => response_builder::error_response(
hyper::StatusCode::METHOD_NOT_ALLOWED,
"Unknown request",
),
_ => response_builder::error_not_allowed(),
},
Method::GET => match endpoint {
"crates" => Self::handle_get_crates_request(path, &request),
"owners" => Self::handle_get_owners_request(path, &request),
_ => response_builder::error_response(
hyper::StatusCode::METHOD_NOT_ALLOWED,
"Unknown request",
),
_ => response_builder::error_not_allowed(),
},
Method::DELETE => match endpoint {
"yank" => Self::handle_yank_request(path, &request),
"owners" => Self::handle_delete_owners_request(path, &request),
_ => response_builder::error_response(
hyper::StatusCode::METHOD_NOT_ALLOWED,
"Unknown request",
),
_ => response_builder::error_not_allowed(),
},
_ => response_builder::error_response(
hyper::StatusCode::METHOD_NOT_ALLOWED,
"Unknown request",
),
_ => response_builder::error_not_allowed(),
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions cargo-registry/src/publisher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::{Client, ClientConfig},
client::{Client, RPCCommandConfig},
sparse_index::{IndexEntry, RegistryIndex},
},
flate2::read::GzDecoder,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Publisher {
let tempdir = tempdir()?;
archive.unpack(tempdir.path())?;

let config = ClientConfig::new(client.as_ref());
let command_config = RPCCommandConfig::new(client.as_ref());

let lib_name = Self::program_library_name(&tempdir, &meta_data)?;

Expand All @@ -152,7 +152,7 @@ impl Publisher {

process_deploy_program(
client.rpc_client.clone(),
&config.0,
&command_config.0,
&program_data,
program_data.len() as u32,
&program_keypair.pubkey(),
Expand Down
29 changes: 27 additions & 2 deletions cargo-registry/src/response_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {crate::response_builder, log::error};
use log::error;

pub(crate) fn error_response(status: hyper::StatusCode, msg: &str) -> hyper::Response<hyper::Body> {
error!("{}", msg);
Expand All @@ -23,5 +23,30 @@ pub(crate) fn success_response_str(value: &str) -> hyper::Response<hyper::Body>
}

pub(crate) fn success_response() -> hyper::Response<hyper::Body> {
response_builder::success_response_str("")
success_response_str("")
}

pub(crate) fn error_not_allowed() -> hyper::Response<hyper::Body> {
error_response(hyper::StatusCode::METHOD_NOT_ALLOWED, "Unknown request")
}

pub(crate) fn error_not_implemented() -> hyper::Response<hyper::Body> {
error_response(
hyper::StatusCode::NOT_IMPLEMENTED,
"This command is not implemented yet",
)
}

pub(crate) fn error_in_parsing() -> hyper::Response<hyper::Body> {
error_response(
hyper::StatusCode::BAD_REQUEST,
"Failed to parse the request",
)
}

pub(crate) fn error_incorrect_length() -> hyper::Response<hyper::Body> {
error_response(
hyper::StatusCode::BAD_REQUEST,
"Request length is incorrect",
)
}

0 comments on commit f4fb957

Please sign in to comment.