Skip to content

Commit

Permalink
fix: server admin returns error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 12, 2023
1 parent 650b741 commit 9fe1b28
Showing 1 changed file with 87 additions and 70 deletions.
157 changes: 87 additions & 70 deletions subfile-exchange/src/subfile_server/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,8 @@ pub async fn handle_admin_request(
});
}

let body_bytes = match to_bytes(req.into_body()).await {
Ok(b) => b,
Err(e) => {
return Ok(create_error_response(
&e.to_string(),
StatusCode::BAD_REQUEST,
))
}
};

let json: Value = match serde_json::from_slice(&body_bytes) {
Ok(j) => j,
let (method, params) = match parse_admin_request(req).await {
Ok(r) => r,
Err(e) => {
return Ok(create_error_response(
&e.to_string(),
Expand All @@ -58,65 +48,17 @@ pub async fn handle_admin_request(
}
};

let method = match json.get("method").and_then(Value::as_str) {
Some(s) => s,
None => {
return Ok(create_error_response(
"Method not found in request",
StatusCode::BAD_REQUEST,
))
}
};

let params = json.get("params");

tracing::debug!(
method = tracing::field::debug(&method),
params = tracing::field::debug(&params),
"Received valid/authorized subfiles management request"
);

match method {
match method.as_str() {
"get_subfiles" => get_subfiles(context).await,
"add_subfile" => {
add_subfile(
params
.ok_or_else(|| {
Error::ServerError(crate::errors::ServerError::ParamsParseError(
"Params not found in request".to_string(),
))
})?
.clone(),
context,
)
.await
}
"remove_subfile" => {
remove_subfile(
params
.ok_or_else(|| {
Error::ServerError(crate::errors::ServerError::ParamsParseError(
"Params not found in request".to_string(),
))
})?
.clone(),
context,
)
.await
}
"update_price_per_byte" => {
update_price_per_byte(
params
.ok_or_else(|| {
Error::ServerError(crate::errors::ServerError::ParamsParseError(
"Params not found in request".to_string(),
))
})?
.clone(),
context,
)
.await
}
"add_subfile" => add_subfile(params, context).await,
"remove_subfile" => remove_subfile(params, context).await,
"update_price_per_byte" => update_price_per_byte(params, context).await,
_ => Ok(hyper::Response::builder()
.status(hyper::StatusCode::METHOD_NOT_ALLOWED)
.body("Method not supported".into())
Expand All @@ -128,6 +70,22 @@ pub async fn handle_admin_request(
}
}

async fn parse_admin_request(req: Request<hyper::Body>) -> Result<(String, Option<Value>), Error> {
let body_bytes = to_bytes(req.into_body()).await.map_err(|e| {
Error::ServerError(crate::errors::ServerError::RequestBodyError(e.to_string()))
})?;

let json: Value = serde_json::from_slice(&body_bytes).map_err(Error::JsonError)?;

let method = json.get("method").and_then(Value::as_str).ok_or_else(|| {
Error::ServerError(crate::errors::ServerError::MethodParseError(
"Method not found in request".to_string(),
))
})?;
let params = json.get("params");

Ok((method.to_string(), params.cloned()))
}
//TODO: rich the details
/// Function to retrieve all subfiles and their details
async fn get_subfiles(context: &ServerContext) -> Result<Response<Body>, Error> {
Expand All @@ -140,7 +98,15 @@ async fn get_subfiles(context: &ServerContext) -> Result<Response<Body>, Error>
.collect::<Vec<_>>();
drop(server_state);

let body = serde_json::to_string(&subfiles_info).map_err(Error::JsonError)?;
let body = match serde_json::to_string(&subfiles_info).map_err(Error::JsonError) {
Ok(b) => b,
Err(e) => {
return Ok(create_error_response(
&e.to_string(),
StatusCode::BAD_REQUEST,
))
}
};
tracing::trace!("Built get_subfile response");

Ok(Response::builder()
Expand All @@ -150,7 +116,20 @@ async fn get_subfiles(context: &ServerContext) -> Result<Response<Body>, Error>
}

/// Add a subfile to the server state
async fn add_subfile(params: Value, context: &ServerContext) -> Result<Response<Body>, Error> {
async fn add_subfile(
params: Option<Value>,
context: &ServerContext,
) -> Result<Response<Body>, Error> {
let params = match params {
Some(p) => p,
None => {
return Ok(create_error_response(
"Missing params",
StatusCode::BAD_REQUEST,
))
}
};

let entries: Vec<String> = serde_json::from_value(params).map_err(Error::JsonError)?;

// Validate before adding to the server state
Expand All @@ -165,7 +144,15 @@ async fn add_subfile(params: Value, context: &ServerContext) -> Result<Response<
};
let mut server_state = context.lock().await;
for (ipfs_hash, local_path) in subfile_entries {
let subfile = read_subfile(&server_state.client, &ipfs_hash, local_path).await?;
let subfile = match read_subfile(&server_state.client, &ipfs_hash, local_path).await {
Ok(s) => s,
Err(e) => {
return Ok(create_error_response(
&e.to_string(),
StatusCode::BAD_REQUEST,
))
}
};
if let Err(e) = subfile.validate_local_subfile() {
return Ok(create_error_response(
&e.to_string(),
Expand All @@ -185,8 +172,28 @@ async fn add_subfile(params: Value, context: &ServerContext) -> Result<Response<
}

/// Remove a subfile from the server state
async fn remove_subfile(params: Value, context: &ServerContext) -> Result<Response<Body>, Error> {
let ipfs_hashes: Vec<String> = serde_json::from_value(params).map_err(Error::JsonError)?;
async fn remove_subfile(
params: Option<Value>,
context: &ServerContext,
) -> Result<Response<Body>, Error> {
let params = match params {
Some(p) => p,
None => {
return Ok(create_error_response(
"Missing params",
StatusCode::BAD_REQUEST,
))
}
};
let ipfs_hashes: Vec<String> = match serde_json::from_value(params).map_err(Error::JsonError) {
Ok(h) => h,
Err(e) => {
return Ok(create_error_response(
&e.to_string(),
StatusCode::BAD_REQUEST,
))
}
};

for ipfs_hash in &ipfs_hashes {
match !is_valid_ipfs_hash(ipfs_hash) {
Expand Down Expand Up @@ -216,9 +223,19 @@ async fn remove_subfile(params: Value, context: &ServerContext) -> Result<Respon

/// Update price per byte
async fn update_price_per_byte(
params: Value,
params: Option<Value>,
context: &ServerContext,
) -> Result<Response<Body>, Error> {
let params = match params {
Some(p) => p,
None => {
return Ok(create_error_response(
"Missing params",
StatusCode::BAD_REQUEST,
))
}
};

let new_price: f32 = match serde_json::from_value(params) {
Ok(p) => p,
Err(e) => {
Expand Down

0 comments on commit 9fe1b28

Please sign in to comment.