I’m stuck on Stage #(change to your stage, ex. #CR8).
I’ve tried sending compressed body, and hex code, I’ve read all the problems from forums and read the code examples. Still couldn’t get it to work.
Here are my logs:
[tester::#CR8] Running tests for Stage #CR8 (HTTP Compression - Gzip compression)
[tester::#CR8] Running program
[tester::#CR8] $ ./your_server.sh
[your_program] Logs from your program will appear here!
[tester::#CR8] Connected to localhost port 4221
[tester::#CR8] $ curl -v http://localhost:4221/echo/mango -H “Accept-Encoding: gzip”
[tester::#CR8] > GET /echo/mango HTTP/1.1
[tester::#CR8] > Host: localhost:4221
[tester::#CR8] > Accept-Encoding: gzip
[tester::#CR8] >
[tester::#CR8] Sent bytes: “GET /echo/mango HTTP/1.1\r\nHost: localhost:4221\r\nAccept-Encoding: gzip\r\n\r\n”
[your_program] Accepted new connection
[tester::#CR8] Received bytes: “HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/plain\r\nContent-Length: 25\r\n\r\n1f8b08000000000000ffcb4dc”
[tester::#CR8] < HTTP/1.1 200 OK
[tester::#CR8] < Content-Encoding: gzip
[tester::#CR8] < Content-Type: text/plain
[tester::#CR8] < Content-Length: 25
[tester::#CR8] <
[tester::#CR8] < 1f8b08000000000000ffcb4dc
[tester::#CR8] <
[tester::#CR8] Received response with 200 status code
[tester::#CR8] ✓ Content-Encoding header is present
[tester::#CR8] ✓ Content-Length header is present
[tester::#CR8] Failed to decode gzip: Failed to decompress data: gzip: invalid header
[tester::#CR8] Test failed
[tester::#CR8] Terminating program
[tester::#CR8] Program terminated successfully
include relevant logs here (please make sure to keep the backticks around this!)
And here’s a snippet of my code:
// here is my echo function for echo request
pub fn echo(request: &Cow<str>, path: &str) -> String {
let mut encoding = "";
for header in request.lines() {
if header.starts_with("Accept-Encoding:") {
encoding = header.trim_start_matches("Accept-Encoding:").trim();
break;
}
}
let response_body = path.strip_prefix("/echo/").unwrap().as_bytes();
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(response_body).unwrap();
let body = encoder.finish().unwrap();
let response = match encoding {
"gzip" => format!(
"{}Content-Encoding: gzip\r\nContent-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}",
OK.part(), // this results to "HTTP/1.1 200 OK\r\n"
body.len(),
hex::encode(body)
),
_ => format!(
"{}Content-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}",
OK.part(),
response_body.len(),
String::from_utf8_lossy(&response_body)
),
};
response
}
// here is the main function code
match stream.read(&mut buffer) {
Ok(size) => {
let request = String::from_utf8_lossy(&buffer[..size]);
let response = match extract_request_method_and_path(request.lines().next()) {
(GET, path) => match path {
"/" => OK.whole(),
p if p.starts_with("/echo/") => echo(&request, path),
u if u.starts_with("/user-agent") => user_agent(&request),
f if f.starts_with("/files/") => file(path),
_ => NotFound.whole(),
},
(POST, path) => match path {
p if p.starts_with("/files/") => create_file(&request, path),
_ => NotFound.whole(),
},
(NONE, "") => String::new(),
_ => String::new(),
};
stream.write(response.as_bytes()).unwrap();
}
Thanks a lot