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

**URL:** <https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127>\
**Category:** Challenges\
**Tags:** challenge:http-server\
**Created:** [July 22, 2024, 1:23pm UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127 "2024-07-22T13:23:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erin-Codes](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/erin-codes/32/1048_2.png) [@Erin-Codes](https://forum.codecrafters.io/u/Erin-Codes)\
**Post date:** [July 22, 2024, 1:23pm UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/1 "2024-07-22T13:23:50Z")

</div>

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):

```auto
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:

```python
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()

```

---

<div class="post-metadata">

**Author:** ![Erin-Codes](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/erin-codes/32/1048_2.png) [@Erin-Codes](https://forum.codecrafters.io/u/Erin-Codes)\
**Post date:** [July 22, 2024, 1:39pm UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/2 "2024-07-22T13:39:23Z")

</div>

`oha http://localhost:4221/`

 ![Screenshot 2024-07-22 153744](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/a/ad352919c7ab58d7288aacdb870697dae0eda316.png)

---

<div class="post-metadata">

**Author:** ![jasonflorentino](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/jasonflorentino/32/1059_2.png) [@jasonflorentino](https://forum.codecrafters.io/u/jasonflorentino)\
**Post date:** [July 23, 2024, 9:40pm UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/3 "2024-07-23T21:40:44Z")

</div>

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.

> [@Erin-Codes](#):
>
> ```auto
> responder(server_socket)
> thread = Thread(target=responder(server_socket))
> 
> ```

Side note, according to the [Usage section](https://github.com/hatoo/oha?tab=readme-ov-file#usage) 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.

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [July 29, 2024, 6:56pm UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/4 "2024-07-29T18:56:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Erin-Codes](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/erin-codes/32/1048_2.png) [@Erin-Codes](https://forum.codecrafters.io/u/Erin-Codes)\
**Post date:** [July 30, 2024, 7:58am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/5 "2024-07-30T07:58:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [July 30, 2024, 10:19am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/6 "2024-07-30T10:19:40Z")

</div>

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](https://forum.codecrafters.io/t/ej5-tests-green-without-implementing-concurrency-c-lang/511).

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

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [July 30, 2024, 10:24am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/7 "2024-07-30T10:24:02Z")

</div>

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

 ![Screenshot 2024-07-30 at 11.21.27](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/4/41a6239bacb4dbdd0b36b245421f80996e0e5bb3.png)

(Which is essentially what @jasonflorentino was referring to)

Test results:

 ![Screenshot 2024-07-30 at 11.23.14](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/3/3b59909914b347218690cefeebf789cd44069bdf.png)

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?

---

<div class="post-metadata">

**Author:** ![Erin-Codes](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/erin-codes/32/1048_2.png) [@Erin-Codes](https://forum.codecrafters.io/u/Erin-Codes)\
**Post date:** [July 30, 2024, 11:08am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/8 "2024-07-30T11:08:08Z")

</div>

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🤦‍♂️)!

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [July 30, 2024, 11:10am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/9 "2024-07-30T11:10:55Z")

</div>

Aha, glad all’s sorted!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [August 4, 2024, 11:11am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received-ej5/1127/10 "2024-08-04T11:11:09Z")

</div>

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