Error: Expected: HTTP-version, Received: "" (#EJ5)

I’m stuck on Stage #EJ5.

I’ve tried code from other users, and the error remains even though I can confirm that the response is not “”. Any tips on how to fix will greatly appreciated. I would love to complete this project.

I’ve tried using oha with the request from the error message and everything looks like it works fine.

Here are my logs(I’ve been trying to debug for days, excuse the messy code):

remote: Running tests on your code. Logs should appear shortly...
remote:
remote: [compile] Compilation successful.
remote: 
remote: Debug = true
remote: 
remote: [tester::#EJ5] Running tests for Stage #EJ5 (Concurrent connections)
remote: [tester::#EJ5] Running program
remote: [tester::#EJ5] $ ./your_server.sh
remote: [tester::#EJ5] Creating 2 parallel connections
remote: [tester::#EJ5] Creating connection 1
remote: [tester::#EJ5] Creating connection 2
remote: [tester::#EJ5] Sending first set of requests
remote: [tester::#EJ5] client-2: $ curl -v http://localhost:4221/
remote: [tester::#EJ5] client-2: > GET / HTTP/1.1
remote: [tester::#EJ5] client-2: > Host: localhost:4221
remote: [tester::#EJ5] client-2: >
remote: [tester::#EJ5] client-2: Sent bytes: "GET / HTTP/1.1\r\nHost: localhost:4221\r\n\r\n"
remote: [tester::#EJ5] Failed to read response:
remote: [tester::#EJ5] Received: "" (no content received)
remote: [tester::#EJ5]            ^ error
remote: [tester::#EJ5] Error: Expected: HTTP-version, Received: ""
remote: [tester::#EJ5] Test failed
remote: [tester::#EJ5] Terminating program
remote: [tester::#EJ5] Program terminated successfully
remote: 
remote: Try our CLI to run tests faster without Git: https://codecrafters.io/cli
remote:
remote: View our article on debugging test failures: https://codecrafters.io/debug
remote:
To https://git.codecrafters.io/28ff28027b4a73c7
   91bbdfe..e761749  master -> master

And here’s a snippet of my code:

import socket
from threading import Thread


def responder(server_socket):
    client, adress = server_socket.accept()

    # Call the recv method with 1024 as the buffer size.
    # Then decode the return value.
    decoded_request = client.recv(1024).decode()

    # Split the request by the CRLF.
    request_list = decoded_request.split("\r\n")
    url_path = request_list[0].split()[1]
    if 'echo' in url_path.casefold():
        echo_str: str = request_list[0].split()[1].split('/echo/')[1]
        response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {len(echo_str)}\r\n\r\n{echo_str}".encode()
        client.send(response)
        # client.close()
        print(type(response), 'echo')
    elif 'foobar/1.2.3' in request_list[2].casefold():
        user_agent: str = request_list[2].split('User-Agent: ')[1]
        print(request_list[2])
        response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {len(user_agent)}\r\n\r\n{user_agent}".encode()
        client.send(response)
        # client.close()
        print(type(response), 'user-')
    else:
        # Encoding the response.
        ok = "HTTP/1.1 200 OK\r\n\r\n".encode()
        not_found = "HTTP/1.1 404 Not Found\r\n\r\n".encode()

        # Send 404 if url_path is not '/' else 200.
        client.send(not_found) if url_path != '/' else client.send(ok)
        print(not_found) if url_path != '/' else print(ok)
    # client.close()
    

def main():

    # Creating a Socket(TCP server) bound to the address localhost:4221
    # (listening on 4221)
    server_socket = socket.create_server(("localhost", 4221))
    # Wait for an incoming connection & returns the tuple below 
    # --> (client_request_obj, address info).
    while True:
        responder(server_socket)
        thread = Thread(target=responder(server_socket))
        thread.start()
    

if __name__ == "__main__":
    main()

1 Like

oha http://localhost:4221/

Hi, looks like the error is that concurrent connections aren’t being handled properly. It’s been a while since I’ve written any Python, and I’ve never had to handle concurrency with it, but it looks like your responder is being called twice? I imagine the first call would block, causing issues with concurrent requests.

Side note, according to the Usage section of their docs, the oha util has -c flag you can use to test concurrent connections. I’d assume otherwise that value is 1 which is probably why the test results shown might indicate everything working as expected.

Hope that helps.

2 Likes

@Erin-Codes I’ve marked the answer above as a solution, please let us know if you still need help!

Hi! Yes I do still need help unfortunately. I’ve tried the above proposed fix and still the error remains. As mentioned above, I have also tried using other users solutions as I feared the issue may be with the tester, these solutions also failed? I’m inclined to believe there is an issue with the tester… Is there any way you can rule this out @rohitpaulk?

Edit: I’ve taken @xDeadcain 's code from the Code solutions section and tested it against the Tester and still no luck, the error remains the same although it should pass.

Ah, okay for this stage in particular I think some older solutions might not work since we fixed the tests to actually test concurrency in #EJ5: Tests green without implementing concurrency - C lang.

Happy to take a look at your code and get to the bottom of this. Will share results shortly!

@Erin-Codes I was able to get tests passing with this diff:

(Which is essentially what @jasonflorentino was referring to)

Test results:

I ran tests 2-3 times to make sure the results are consistent. @Erin-Codes could you try this, let me know if it works?

Perfect! Thank you so much @jasonflorentino and @rohitpaulk ! You guys were correct, however, another reason why I was failing was due to my .accept() method being outside of the main loop(such a silly mistake🤦‍♂️)!

1 Like

Aha, glad all’s sorted!

1 Like

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