lemunozm / message-io

Fast and easy-to-use event-driven network library.
Apache License 2.0
1.11k stars 74 forks source link

Response to HTTP messages over Tcp #107

Closed drbh closed 2 years ago

drbh commented 2 years ago

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 commented 2 years ago

^ 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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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