1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Improve body parsing

This commit is contained in:
Pierre HUBERT 2020-05-23 09:54:13 +02:00
parent 7c18f19674
commit 4b12de6e50
2 changed files with 26 additions and 6 deletions

View File

@ -7,12 +7,12 @@ use crate::controllers::routes::Method::{GET, POST};
use crate::data::http_request_handler::{HttpRequestHandler, RequestValue};
use actix_web::dev::{PayloadStream, Payload, Decompress};
use actix_web::web::{BytesMut};
use futures::future::{ok, LocalBoxFuture, err};
use futures::{FutureExt, StreamExt, TryFutureExt};
use actix_web::error::{PayloadError, ErrorBadRequest};
use std::error::Error;
use futures::future::{LocalBoxFuture};
use futures::{FutureExt, StreamExt};
use actix_web::error::{ErrorBadRequest};
use encoding_rs::UTF_8;
use std::collections::HashMap;
use percent_encoding::{percent_decode_str};
/// Main server functions
@ -87,8 +87,8 @@ impl FromRequest for CustomRequest {
// Add the value to the body
body_args.insert(
args[0].to_string(),
RequestValue::string(args[1].to_string())
percent_decode_str(args[0]).decode_utf8_lossy().to_string(),
RequestValue::string(percent_decode_str(args[1]).decode_utf8_lossy().to_string())
);
}
}
@ -148,9 +148,18 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
// Set default error response if required
Err(e) => {
let err_msg= e.to_string();
if !request.has_response() {
request.internal_error(e).unwrap_err();
}
println!("{} - {} - {}",
request.response_status_code(),
request.request_path(),
err_msg
);
}
// Set default success response if required
@ -158,6 +167,8 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
if !request.has_response() {
request.success("Success").unwrap()
}
println!("{} - {}", request.response_status_code(), request.request_path());
}
}

View File

@ -51,6 +51,11 @@ impl HttpRequestHandler {
self.response.is_some()
}
/// Get the response status code, eg. 200 or 404
pub fn response_status_code(&self) -> u16 {
self.response.as_ref().unwrap().status().as_u16()
}
/// Take the response from this struct
pub fn response(self) -> HttpResponse {
self.response.unwrap()
@ -71,6 +76,10 @@ impl HttpRequestHandler {
Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap())
}
/// Get the path of the request
pub fn request_path(&self) -> String {
self.request.path().to_string()
}
/// Check login tokens
pub fn check_client_token(&mut self) -> ResultExecError<()> {