I’m stuck on Stage #CR8.
Here are my logs:
[compile] Compilation successful.
Debug = true
[tester::#CR8] Running tests for Stage #CR8 (HTTP Compression - Gzip compression)
[tester::#CR8] Running program
[tester::#CR8] $ ./your_server.sh
[tester::#CR8] Connected to localhost port 4221
[tester::#CR8] $ curl -v http://localhost:4221/echo/banana -H "Accept-Encoding: gzip"
[tester::#CR8] > GET /echo/banana HTTP/1.1
[tester::#CR8] > Host: localhost:4221
[tester::#CR8] > Accept-Encoding: gzip
[tester::#CR8] >
[tester::#CR8] Sent bytes: "GET /echo/banana HTTP/1.1\r\nHost: localhost:4221\r\nAccept-Encoding: gzip\r\n\r\n"
[tester::#CR8] Received bytes: "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: plain/text\r\nContent-Length: 6\r\n\r\n\x1f\x8b\b\x00\x00\x00"
[tester::#CR8] < HTTP/1.1 200 OK
[tester::#CR8] < Content-Encoding: gzip
[tester::#CR8] < Content-Type: plain/text
[tester::#CR8] < Content-Length: 6
[tester::#CR8] <
[tester::#CR8] < <Binary Content>
[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: unexpected EOF
[tester::#CR8] Test failed
[tester::#CR8] Terminating program
[tester::#CR8] Program terminated successfully
And here’s a snippet of my code:
else if (url.includes('/echo/')) {
const str = url.split('/echo/')[1];
const acceptEncoding = headers['accept-encoding'];
const encodings = acceptEncoding ? acceptEncoding.split(',').map(encoding => encoding.trim()) : [];
if (encodings.includes('gzip')) {
const bodyContent = url.split('/')[2];
const compressedBody = zlib.gzipSync(bodyContent);
const res = `HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: plain/text\r\nContent-Length: ${bodyContent.length}\r\n\r\n`;
socket.write(res);
socket.write(compressedBody);
} else {
const res = `HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${str.length}\r\n\r\n${str}`;
socket.write(res);
}