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

Added support for gzipped request body decoding #4

Merged
merged 1 commit into from
Sep 27, 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
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ rusqlite = {version = "0.24", features = ["bundled"]}
rust-embed = "5"
log = "^0.4"
env_logger = "^0.9"
flate2 = { version = "=1.0.26", features = [] }
34 changes: 28 additions & 6 deletions src/serve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tiny_http::{Response, Header, Request};
use tera::{Context};
use std::io::{Cursor};
use std::io::{Read, Cursor};
use chrono::{Utc};
use std::collections::HashMap;
use url::{Url};
Expand All @@ -12,7 +12,7 @@ use rusqlite::{params, Connection};
use rust_embed::RustEmbed;

use log::{info};

use flate2::read::GzDecoder;

#[derive(RustEmbed)]
#[folder = "static"]
Expand Down Expand Up @@ -147,15 +147,37 @@ fn prune_requests(app_ctx: &mut AppContext, pct: f32) {
}
}

fn get_request_body(request: &mut Request) -> String {
let mut body = String::new();

// Check if the content is gzip-encoded
let content_encoding = request.headers()
.iter()
.find(|h| h.field.equiv("Content-Encoding"))
.and_then(|h| Option::from(h.value.as_str()));

if content_encoding == Some("gzip") {
// Use a GzDecoder to decompress the gzipped content
let mut d = GzDecoder::new(request.as_reader());
if let Err(e) = d.read_to_string(&mut body) {
body = format!("Could not parse gzipped request body: {:?}", e);
}
} else {
// Handle non-gzipped content
if let Err(e) = request.as_reader().read_to_string(&mut body) {
body = format!("Could not parse request body - is this a binary format? {:?}", e);
}
}

body
}

pub fn handle_req(request: &mut Request, app_ctx: &mut AppContext) -> Response<Cursor<Vec<u8>>> {

let base_url: Url = Url::parse("http://reqsink.local/").unwrap();
let url = base_url.join(request.url()).unwrap();

let mut body = String::new();
if let Err(e) = request.as_reader().read_to_string(&mut body) {
body = format!("Could not parse request body - is this a binary format? {:?}", e);
}
let body = get_request_body(request);

let sr = StoredRequest {
time: Utc::now().to_rfc2822(),
Expand Down
Loading