Fix on_headers callbacks being called early
#264
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
PR #221 introduced the ability to abort a request from the
on_headersuser defined callbacks.Before this PR ,
on_headerscallbacks were called exactly one time and only once all headers are in, including from multiple redirects. This is the expected value forresponse_headersinside aon_headerscallback for a request that followed a redirect:Since we wanted all headers to be available to the callback,
on_headerswas actually called when the body write function started receiving data - and from the complete callback to handle requests without a body. This approach was endorsed by libcurl's author in response to a question about detecting the end of headers [0].Problem
PR #221 moved the execution of
on_headersto the actual header write function. I think this was done to allow aborting early, before starting to download the body and before following redirects. The problem is that libcurl calls the header write function once per header line, so when the user code inon_headersruns,response_headerscontains only the status line (ex:HTTP/1.1 200 OK).Since Typhoeus trims out the status line, when the callback ran, the headers were empty (#229, typhoeus/typhoeus#705, typhoeus/typhoeus#710).
Additionally, when following redirects, the change made so the callback was called immediately on the first line of headers received from the initial request, which is not the expected behavior (#231).
Solution
This PR reverts the change in #221 that made
on_headersbe called before receiving all headers, and keeps support for aborting from the callback by checking it's return value in the body write function.Testing
We had a test that should have caught the unintended change in #221, but the test itself had a bug (fixed in a7af5c2). I've also modified the test for returning
:abortfromon_headersso that it checks that the request gets cancelled from the body write function.Future improvements
We could offer a way for users to cancel an in-flight request right when they receive the header they need, which was the intention behind #221. This would require creating an new "raw" headers callback, to preserve the current expected
on_headersbehavior.[0] https://curl.se/mail/lib-2009-02/0243.html