Skip to content

Allow flushing HTTP headers #1895

@notaphplover

Description

@notaphplover

As user I would very much like to deliver http status and headers in the context of a chunked http response before sending any body chunks.

The challenge I'm facing

I am currently implementing SSE in the inversify framework. I'm super interested in allowing users to use uWebSockets.js. When implementing sse events, the first message might arrive after an undetermined period of time. It seems chunked responses are handled via HttpResponse.write, but I really don't want to wait to send headers until I got the first chunk for it could take ages. Invoking HttpResponse.write with an empty body is not an option, it does not trigger sending the crlf start of the body mark. It could be great if we could have a way to flush headers and send the mark so we can inform http clients our disposition to eventually deliver body chunks. Otherwise clients might fail after a given timeout.

The proposal

Update HttpResponse with a flush headers method:

template <bool SSL>
struct HttpResponse : public AsyncSocket<SSL> {
public:
    void flushHeaders() {
        /* Write status if not already done */
        writeStatus(HTTP_200_OK);

        HttpResponseData<SSL> *httpResponseData = getHttpResponseData();

        if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
            /* Write mark on first call to write */
            writeMark();

            writeHeader("Transfer-Encoding", "chunked");
            httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;

            /* Start of the body */
            Super::write("\r\n", 2);
        }
    }
}

This way we could propagate it to uWebSockets.js

I'd be glad to submit a PR or discuss about any implementation details. If I'm missing something and I'm not understanding the way I'm supposed to solve my current challenge, I'd be more than glad to know the right way to go ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions