Send response headers as soon as possible

Hi,

I’m trying to make a small app with a file upload capability. I’m using PUT method and it works pretty well, thanks to Slim :slight_smile:

This upload is done with an XMLHttpRequest object so I can monitor the progress and all.

My backend (the Slim part) also detects if a file already exists and, in this case, returns an HTTP/1.1 409 Conflict response. The user can then decide if he wants to rename his file or overwrite the existing one.

My issue is that the response headers are sent only when the PUT request is finished. I’d like them to be sent ASAP.

Why ? Well:

The XMLHttpRequest readyState property can have 5 values:

  • UNSENT (0):
    The XMLHttpRequest client has been created, but the open() method hasn’t been called yet.

  • OPENED (1):
    open() method has been invoked. During this state, the request headers can be set using the setRequestHeader() method and the send() method can be called which will initiate the fetch.

  • HEADERS_RECEIVED (2):
    send() has been called, and headers and status are available.

  • LOADING (3):
    Response’s body is being received. If responseType is “text” or empty string, responseText will have the partial text response as it loads.

  • DONE (4):
    The fetch operation is complete. This could mean that either the data transfer has been completed successfully or failed.

Since you can detect the change of this value by listening to the XMLHttpRequest.readystatechange event, I was planning to use the value HEADERS_RECEIVED and test the HTTP status code as soon as possible.

This would allow me to abort (stop) the file upload as soon as a 409 Conflict is detected, maybe before the whole file is uploaded, which could save time and bandwidth.

In my testings, it seems like the events do fire in this order, but both HEADERS_RECEIVED and LOADING fire at virtually the same time as the DONE event.

Is there a way to send the response headers immediately using Slim ?

Thanks !