I’m stuck on gzip compression stage 3.
I’ve also tried sending the gzip compressed body as hex, base64. I’ve provided my current version of the code snippet below.
Here are my logs:
inner@afterlife:/mnt/d/projects/codecrafters-http-server-csharp$ codecrafters test
Initiating test run...
⚡ This is a turbo test run. https://codecrafters.io/turbo
Running tests. Logs should appear shortly...
remote: [compile] MSBuild version 17.3.2+561848881 for .NET
remote: [compile] Determining projects to restore...
remote: [compile] Restored /app/codecrafters-http-server.csproj (in 90 ms).
remote: [compile] codecrafters-http-server -> /app/bin/Release/net6.0/codecrafters-http-server.dll
remote: [compile]
remote: [compile] Build succeeded.
remote: [compile] 0 Warning(s)
remote: [compile] 0 Error(s)
remote: [compile]
remote: [compile] Time Elapsed 00:00:03.17
remote: [compile]Compilation successful.
remote:
remote: Debug = true
remote:
remote: [http-compression-3] Running tests for HTTP Compression > Stage #3: Gzip compression
remote: [http-compression-3] Running program
remote: [http-compression-3] $ ./your_server.sh
remote: [your_program] Starting custom HTTP Server.
remote: [your_program] Custom HTTP Server started.
remote: [http-compression-3] Connected to localhost port 4221
remote: [http-compression-3] $ curl -v -X GET http://localhost:4221/echo/blueberry -H "Accept-Encoding: gzip"
remote: [http-compression-3] > GET /echo/blueberry HTTP/1.1
remote: [http-compression-3] > Host: localhost:4221
remote: [http-compression-3] > Accept-Encoding: gzip
remote: [http-compression-3] >
remote: [http-compression-3] Sent bytes: "GET /echo/blueberry HTTP/1.1\r\nHost: localhost:4221\r\nAccept-Encoding: gzip\r\n\r\n"
remote: [your_program] TCP Connection 127.0.0.1:38108 established!
remote: [your_program] Hex string: 1F8B08000000000000034BCA294D4D4A2D2AAA0400D3241E7909000000
remote: [http-compression-3] Received bytes: "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/plain\r\nContent-Length: 29\r\n\r\n\x1f?\b\x00\x00\x00\x00\x00\x00\x03K?)MMJ-*?\x04\x00?$\x1ey\t\x00\x00\x00"
remote: [http-compression-3] < HTTP/1.1 200 OK
remote: [http-compression-3] < Content-Encoding: gzip
remote: [http-compression-3] < Content-Type: text/plain
remote: [http-compression-3] < Content-Length: 29
remote: [http-compression-3] <
remote: [http-compression-3] < <Binary Content>
remote: [http-compression-3] <
remote: [http-compression-3] Received response with 200 status code
remote: [http-compression-3] ✓ Content-Encoding header is present
remote: [http-compression-3] ✓ Content-Length header is present
remote: [http-compression-3] Failed to decode gzip: Failed to decompress data: gzip: invalid header
remote: [http-compression-3] Test failed
remote: [http-compression-3] Terminating program
remote: [http-compression-3] Program terminated successfully
remote:
remote: View our article on debugging test failures: https://codecrafters.io/debug
And here’s a snippet of my code:
var echoMessage = serverRequest.Path.Replace(EchoEndpointPath, string.Empty);
var acceptEncoding = serverRequest.Headers.Where(x =>
string.Equals(x.Key, "Accept-Encoding", StringComparison.InvariantCultureIgnoreCase))
.Select(x => x.Value)
.FirstOrDefault()?
.Split(',')
.Select(x => x.Trim());
var containsGzipEncoding = acceptEncoding?
.Contains("gzip", StringComparer.InvariantCultureIgnoreCase);
var includeGzip = containsGzipEncoding.HasValue && containsGzipEncoding.Value;
sb.Append("HTTP/1.1 200 OK\r\n");
if (includeGzip)
{
byte[] compressedData;
using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
var stringBytes = Encoding.UTF8.GetBytes(echoMessage);
gzipStream.Write(stringBytes, 0, stringBytes.Length);
gzipStream.Flush();
}
compressedData = memoryStream.ToArray();
Console.WriteLine($"Hex string: {BitConverter.ToString(compressedData).Replace("-", string.Empty)}");
}
sb.Append("Content-Type: text/plain\r\n");
sb.Append("Content-Encoding: gzip\r\n");
sb.Append($"Content-Length: {compressedData.Length}\r\n\r\n");
sb.Append(Encoding.UTF8.GetString(compressedData));
}
else
{
sb.Append("Content-Type: text/plain\r\n");
sb.Append($"Content-Length: {echoMessage.Length}\r\n\r\n");
sb.Append(echoMessage);
}