Stage #FS3 works locally, but fails tests

I’m stuck on Stage #FS3.

When run locally through the terminal, it returns the correct Content-Length value in the HTTP response, but when the tester runs the program, the Content-Length value is different. I’m running the same command the tester uses.

Here are my logs:

Debugger:

[compile] Compilation successful.
[tester::#FS3] Running tests for Stage #FS3 (Read header)
[tester::#FS3] $ ./your_server.sh
[your_program] Logs from your program will appear here!
[your_program] Waiting for a client to connect...
[tester::#FS3] $ curl -v http://localhost:4221/user-agent -H "User-Agent: grape/mango"
[your_program] Client connected
[tester::#FS3] Received response with 200 status code
[tester::#FS3] Expected "Content-Length" header value to be "11", got "0"
[tester::#FS3] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

Terminal:

* Host localhost:4221 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:4221...
* connect to ::1 port 4221 from ::1 port 63457 failed: Connection refused
*   Trying 127.0.0.1:4221...
* Connected to localhost (127.0.0.1) port 4221
> GET /user-agent HTTP/1.1
> Host: localhost:4221
> Accept: */*
> User-Agent: grape/mango
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 11
< 
* Connection #0 to host localhost left intact
grape/mango%

And here’s a snippet of my code:

for (int i = 0; i < 4; i++) {
    requestTarget = strtok(NULL, "\r\n"); // requestTarget now contains "User-Agent: *user-agent here*"
    }
requestTarget += strlen("User-Agent: "); // skips past User-Agent: 
char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %i\r\n\r\n%s";
char userAgentResponse[1024];
sprintf(userAgentResponse, response, strlen(requestTarget), requestTarget); // replaces placeholders in response and saves to userAgentResponse
send(client_fd, userAgentResponse, strlen(userAgentResponse), 0);
close(server_fd);
return 0;

@Airetex could you try enabling debug logs and share the output including debug logs? That’ll help track this down.

More on this here: How do I debug test failures? - CodeCrafters.

Debug logs should include the exact bytes that were received/sent.

It turns out that the HTTP headers that I received locally were in a different order than what the tester was receiving. I believe it specifically was the Accept and User-Agent headers that were swapped on my machine. I’m not sure why it does that, I was testing in my terminal with curl.