Skip to content

Commit

Permalink
Added support for gzipped request body decoding
Browse files Browse the repository at this point in the history
- Added dependencies on `flate2:1.0.26` in Cargo.toml, (1.0.27 breaks docker build).
- Refactored request body reading in `src/serve.rs` to support both gzip-encoded and plain request bodies.

To test the new function, use the following shell command:

```
echo '{"mydummy": "json"}' | gzip | curl -v -i --data-binary @- -H "Content-Encoding: gzip" -H 'Content-Type: application/json' http://localhost:8000/test_endpoint
```
  • Loading branch information
oylbin committed Sep 26, 2023
1 parent 9589f33 commit 36b624e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
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

0 comments on commit 36b624e

Please sign in to comment.