I’m stuck on stage (#EJ5) of build your own HTTP server; help needed! (C#)

I’m stuck on stage 6 of build your own HTTP server. I’m using C# for this challenge.

All I did was wrap a call to a method inside Task.Run() which would presumably allow it to run in a separate thread. The tests are failing, though. And they’re failing early, at stage #2.

Here are my logs:

remote: [stage-2] $ curl -v -X GET http://localhost:4221/
remote: [stage-2] Sending request (status line): GET / HTTP/1.1
remote: [your_program] Client accepted!
remote: [stage-2] Hint: EOF is short for 'end of file'. This usually means that your program either:
remote: [stage-2]  (a) didn't send a complete response, or
remote: [stage-2]  (b) closed the connection early
remote: [stage-2] Failed to connect to server, err: 'Get "http://localhost:4221/": EOF'
remote: [stage-2] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

This is the line of code (GitHub link) that has essentially changed. If I remove the Task.Run() all tests pass, except for stage #6 tests of course.

Anyone has any idea what’s going on here?

This is the Main method just for reference:

    private static void Main(string[] args)
    {
        try
        {
            using TcpListener server = new(IPAddress.Any, 4221);
            server.Start();

            while (true)
            {
                using var client = server.AcceptTcpClient();
                Console.WriteLine("Client accepted!");
                _ = Task.Run(() => HandleClient(client));
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine($"SocketException: {e}");
        }
    }

I managed to fix the problem and pass stage #6. The main issue was with the way I was parsing headers. Somehow, the regex also matched when there were no headers present.

1 Like

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

Note: I’ve updated the title of this post to include the stage ID (#EJ5). You can learn about the stages rename here: Upcoming change: Stages overhaul