Failed to decode gzip: Failed to decompress data: gzip: invalid header JAVASCRIPT

I’m stuck on Stage #11.

I’ve tried converting to base64, hex, binary, buffers, etc. yet I get the same result. Online research has shown me similar errors where the problem was that it was not actually being gzip compressed, but I have ran echo -n "H4sIAAAAAAAAA8tNzEvPBwAEs+KRBQAAAA==" | base64 --decode | gunzip and I get the right string back.
Here are my logs:

remote: [tester::#CR8] Running tests for Stage #CR8 (HTTP Compression - Gzip compression)
remote: [tester::#CR8] Running program
remote: [tester::#CR8] $ ./your_server.sh
remote: [tester::#CR8] Connected to localhost port 4221
remote: [tester::#CR8] $ curl -v http://localhost:4221/echo/orange -H "Accept-Encoding: gzip"
remote: [tester::#CR8] > GET /echo/orange HTTP/1.1
remote: [tester::#CR8] > Host: localhost:4221
remote: [tester::#CR8] > Accept-Encoding: gzip
remote: [tester::#CR8] >
remote: [tester::#CR8] Sent bytes: "GET /echo/orange HTTP/1.1\r\nHost: localhost:4221\r\nAccept-Encoding: gzip\r\n\r\n"
remote: [your_program] GET /echo/orange HTTP/1.1
remote: [your_program] Host: localhost:4221
remote: [your_program] Accept-Encoding: gzip
remote: [your_program]
remote: [your_program]
remote: [your_program] [
remote: [your_program]   'echo/orange HTTP/1.1',
remote: [your_program]   'Host: localhost:4221',
remote: [your_program]   'Accept-Encoding: gzip',
remote: [your_program]   '',
remote: [your_program]   ''
remote: [your_program] ]
remote: [your_program] [ 'echo', 'orange' ]
remote: [your_program] 1f8b0800000000000003cb2f4acc4b4f050078b1c51d06000000
remote: [tester::#CR8] Received bytes: "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/plain\r\nContent-Length:52\r\n\r\n1f8b0800000000000003cb2f4acc4b4f050078b1c51d06000000"
remote: [tester::#CR8] < HTTP/1.1 200 OK
remote: [tester::#CR8] < Content-Encoding: gzip
remote: [tester::#CR8] < Content-Type: text/plain
remote: [tester::#CR8] < Content-Length: 52
remote: [tester::#CR8] <
remote: [tester::#CR8] < 1f8b0800000000000003cb2f4acc4b4f050078b1c51d06000000
remote: [tester::#CR8] <
remote: [tester::#CR8] Received response with 200 status code
remote: [tester::#CR8] ✓ Content-Encoding header is present
remote: [tester::#CR8] ✓ Content-Length header is present
remote: [tester::#CR8] Failed to decode gzip: Failed to decompress data: gzip: invalid header
remote: [tester::#CR8] Test failed
remote: [tester::#CR8] Terminating program
remote: [tester::#CR8] Program terminated successfully

And here’s a snippet of my code:

case "echo":
                let content = pathData[1];
                let contentLength = 0;
                if (compression == "gzip") {
                  content = zlib.gzipSync(content).toString("hex");
                  contentLength = Buffer.byteLength(content);
                }
                console.log(content);
                socket.write(
                  "HTTP/1.1 200 OK\r\n" +
                    (compression.length > 0
                      ? "Content-Encoding: " + compression + "\r\n"
                      : "") +
                    "Content-Type: text/plain\r\nContent-Length:" +
                    contentLength +
                    "\r\n\r\n" +
                    content
                );
                break;

The response doesn’t need to be encoded as hex, it needs to be raw bytes. I think removing toString("hex") should do the trick (haven’t tried this, would recommend checking code examples)

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.