CrowdHailer / Ace

HTTP web server and client, supports http1 and http2

Home Page:https://hex.pm/packages/ace

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

handle_tail doesn't get called with GET requests

nietaki opened this issue · comments

(or probably all requests with no body)

I was writing a couple of handlers using handle_head, handle_data and handle_tail (as opposed to handle_request) where I was only sending the response in handle_tail, after doing some processing beforehand. This worked well for POST requests with body, but for GET requests handle_data and handle_tail wasn't called and the response was never completed. It was still confusing to me after looking at the docs.

After some consults I now understand that if the request in handle_head has body set to false I shouldn't be expecting the other callbacks to be called and I should do all the work in handle_head and potentially handle_info.

While I see that this is a perfectly workable approach, I think handle_tail should be called in all cases.

  • It leads to a simpler, more consistent mental model (and possibly code): "when handle_tail is called, the request is done" instead of "if I know in handle_head there's no body, I have to do the cleanup here, otherwise I have to wait until handle_tail".
  • the argument that GET requests won't have trailers so the corresponding callback shouldn't get called doesn't convince me - the vast majority of requests won't have trailers and handle_tail is just a way to signal the controller that the request is finished - there isn't even a way to determine it otherwise if the request has body, as far as I know.
  • Not having it this way forces the implementers to fall back on their knowledge of HTTP or deconstructing the request, which in my mind impedes Ace as the "powerful, but easy to get started with" server.
  • lastly, but not leastly, Raxx says so ;): "handle_tail(list, state) - Called once when a request finishes"

So what I'm proposing is always calling handle_tail callback for all requests (if handled with the 3 callbacks approach, naturally) and only calling handle_data if there is body in the request. Also calling handle_data with an empty binary chunk as well probably makes less sense.

Anyone opposed?

I think the reason I don't call handle_tail was because that maps more closely to the messages as described here https://tools.ietf.org/html/rfc7540#section-8.1

The tail being the optional headers frame after the body.

Adding this for context rather than a reason not to proceed