Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup in cargo-registry #33711

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
)
}