feat: #1 impl an http body parser
This commit is contained in:
parent
3afe437143
commit
e0d72b08cf
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -274,6 +274,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "json"
|
||||||
|
version = "0.12.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kv-log-macro"
|
name = "kv-log-macro"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
@ -366,6 +372,7 @@ name = "simple-auth"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-std",
|
"async-std",
|
||||||
|
"json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -6,6 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
json = "0.12.4"
|
||||||
|
|
||||||
[dependencies.async-std]
|
[dependencies.async-std]
|
||||||
version = "1.6"
|
version = "1.6"
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
//! request handles properly the incoming request
|
//! request handles properly the incoming request
|
||||||
//! it will parse the request according to the HTTP message specifications
|
//! it will parse the request according to the HTTP message specifications
|
||||||
//! see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
|
//! see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
|
||||||
|
//! NOTE: only few parts of the specification has been implemented
|
||||||
|
|
||||||
|
use json;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum HTTPVersion {
|
pub enum HTTPVersion {
|
||||||
Http1,
|
Http1,
|
||||||
//http2,
|
//Http2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<String> for HTTPVersion {
|
impl Into<String> for HTTPVersion {
|
||||||
@ -72,6 +75,31 @@ impl Into<String> for HTTPStartLine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// HTTPBody represents http request body
|
||||||
|
/// for simplicity, only json body are accepted
|
||||||
|
pub struct HTTPBody {
|
||||||
|
data: json::JsonValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HTTPBody {
|
||||||
|
fn new(data: json::JsonValue) -> HTTPBody {
|
||||||
|
HTTPBody { data }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for HTTPBody {
|
||||||
|
type Error = String;
|
||||||
|
fn try_from(body: &str) -> Result<HTTPBody, Self::Error> {
|
||||||
|
match json::parse(body) {
|
||||||
|
Ok(v) => Ok(HTTPBody::new(v)),
|
||||||
|
Err(e) => Err(format!(
|
||||||
|
"error occurred during request body parsing err={}",
|
||||||
|
e
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Request defined the HTTP request
|
/// Request defined the HTTP request
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct HTTPRequest {
|
pub struct HTTPRequest {
|
||||||
@ -156,3 +184,22 @@ fn test_handle_request() {
|
|||||||
assert_eq!(http_request.is_valid(), is_valid);
|
assert_eq!(http_request.is_valid(), is_valid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_http_body() {
|
||||||
|
let test_cases: [(&str, bool); 3] = [
|
||||||
|
("hello, how are you ?", false),
|
||||||
|
("qsdfqsdffqsdffsq", false),
|
||||||
|
(
|
||||||
|
r#"{"access_token":"AAAAAAAAAAAA.BBBBBBBBBB.CCCCCCCCCC","refresh_token": "DDDDDDDDDDD.EEEEEEEEEEE.FFFFF"}"#,
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (body, is_valid) in test_cases {
|
||||||
|
match HTTPBody::try_from(body) {
|
||||||
|
Ok(_) => assert!(is_valid),
|
||||||
|
Err(_) => assert!(!is_valid),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user