reorganize module
This commit is contained in:
1119
utils/Cargo.lock
generated
Normal file
1119
utils/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
utils/Cargo.toml
Normal file
12
utils/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "utils"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Integration between Rhai scripting and Tera templating"
|
||||
|
||||
[dependencies]
|
||||
rhai = { version = "1.15.1", features = ["serde"] }
|
||||
tera = "1.19.0"
|
||||
serde_json = "1.0.107"
|
||||
hyper = { version = "0.14", features = ["full"] }
|
||||
httparse = "1"
|
42
utils/src/lib.rs
Normal file
42
utils/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use hyper::{Body, Method, Request, Response};
|
||||
use std::io::{self, Read};
|
||||
|
||||
pub fn read_request_from_stdin() -> io::Result<String> {
|
||||
let mut buffer = String::new();
|
||||
io::stdin().read_to_string(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
pub fn parse_http_request(input: &str) -> Result<Request<Body>, Box<dyn std::error::Error>> {
|
||||
let mut headers = [httparse::EMPTY_HEADER; 32];
|
||||
let mut req = httparse::Request::new(&mut headers);
|
||||
|
||||
let bytes = input.as_bytes();
|
||||
let parsed_len = req.parse(bytes)?.unwrap();
|
||||
|
||||
let method = req.method.ok_or("missing method")?.parse::<Method>()?;
|
||||
let path = req.path.ok_or("missing path")?;
|
||||
let uri = path.parse::<hyper::Uri>()?;
|
||||
|
||||
let body = &bytes[parsed_len..];
|
||||
let request = Request::builder()
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.body(Body::from(body.to_vec()))?;
|
||||
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
pub async fn print_http_response(res: Response<Body>) {
|
||||
println!("HTTP/1.1 {}", res.status());
|
||||
|
||||
for (key, value) in res.headers() {
|
||||
println!("{}: {}", key, value.to_str().unwrap_or_default());
|
||||
}
|
||||
|
||||
println!(); // blank line before body
|
||||
|
||||
let body_bytes = hyper::body::to_bytes(res.into_body()).await.unwrap();
|
||||
let body = String::from_utf8_lossy(&body_bytes);
|
||||
println!("{}", body);
|
||||
}
|
Reference in New Issue
Block a user