Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response to HTTP messages over Tcp #107

Closed
drbh opened this issue Nov 7, 2021 · 4 comments
Closed

Response to HTTP messages over Tcp #107

drbh opened this issue Nov 7, 2021 · 4 comments

Comments

@drbh
Copy link

drbh commented Nov 7, 2021

After setting up

handler
    .network()
    .listen(Transport::Tcp, "0.0.0.0:6443")
    .unwrap();

and then sending

Received: GET / HTTP/1.1
Host: localhost:6443
User-Agent: insomnia/2021.5.3
Accept: */*

I think I should be able to close an HTTP 1.1 request with the following message.

NetEvent::Accepted(_endpoint, _listener) => {
    println!("Client connected")

    // attempt to close HTTP connection
    handler
        .network()
        .send(endpoint, b"HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n");
}

However the connection just hangs and fails to complete.

@lemunozm do you have any recommendations for handling HTTP requests over TCP? message-io is an awesome library and I'd love to replace a current server with it, but I need to be able to process HTTP requests. Thanks in advance!

@drbh
Copy link
Author

drbh commented Nov 7, 2021

^ also the example above simply closes a connection - but long term I'd like to be able to process POST requests and send data in the responses. 🙏

@lemunozm
Copy link
Owner

Hi @drbh, I'm glad you find message-io useful :)

If I understand well, you are implementing the HTTP server part in message-io, right? Until I know the header Connection: close is sent by clients in order to tell the server that the connection will be closed after the server response. If you send it from the server to the client I think the behaviour is not specified (unless the client implements some extra mechanism to detect that).

The NetEvent::Accepted event is dispatched only when the client is connected and before the first client message. In order to respond to the server, the server should send the message after reading it in the NetEvent::Message. Take into account that an HTTP request could be received spllited in several TCP packets which means several NetEvent::Message. You will need some decoding mechanism to accumulate the message until the entire HTTP request is received.

In order to close the connection from the server perspective after sending the response, you could use handler.network().remove(endpoint).

@lemunozm
Copy link
Owner

Regarding the HTTP decoding part, you could use httparse, which tells if a request is completed or patial. The server will wait for more NetEvent::Message events until the whole received data is considered completed.

@lemunozm
Copy link
Owner

I think the issue can be closed, if not, please feel free to reopen it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants