Repository navigation
http/2: correctly handle 100-continue #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4297085
8d8b0df
b75429a
3dc432a
e83f44e
ad0afc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| #include "codec_impl.h" | ||
|
|
||
| #include "envoy/event/dispatcher.h" | ||
| #include "envoy/http/codes.h" | ||
| #include "envoy/http/header_map.h" | ||
| #include "envoy/network/connection.h" | ||
|
|
||
| #include "common/common/assert.h" | ||
| #include "common/common/enum_to_int.h" | ||
| #include "common/common/utility.h" | ||
| #include "common/http/codes.h" | ||
| #include "common/http/exception.h" | ||
| #include "common/http/headers.h" | ||
| #include "common/http/utility.h" | ||
|
|
||
| namespace Http { | ||
| namespace Http2 { | ||
|
|
@@ -28,6 +32,9 @@ bool Utility::reconstituteCrumbledCookies(const HeaderString& key, const HeaderS | |
|
|
||
| ConnectionImpl::Http2Callbacks ConnectionImpl::http2_callbacks_; | ||
| ConnectionImpl::Http2Options ConnectionImpl::http2_options_; | ||
| const std::unique_ptr<const Http::HeaderMap> ConnectionImpl::CONTINUE_HEADER{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above (regarding static non-POD). |
||
| new Http::HeaderMapImpl{ | ||
| {Http::Headers::get().Status, std::to_string(enumToInt(Code::Continue))}}}; | ||
|
|
||
| /** | ||
| * Helper to remove const during a cast. nghttp2 takes non-const pointers for headers even though | ||
|
|
@@ -39,7 +46,8 @@ template <typename T> static T* remove_const(const void* object) { | |
|
|
||
| ConnectionImpl::StreamImpl::StreamImpl(ConnectionImpl& parent) | ||
| : parent_(parent), headers_(new HeaderMapImpl()), local_end_stream_(false), | ||
| local_end_stream_sent_(false), remote_end_stream_(false), data_deferred_(false) {} | ||
| local_end_stream_sent_(false), remote_end_stream_(false), data_deferred_(false), | ||
| waiting_for_non_informational_headers_(false) {} | ||
|
|
||
| ConnectionImpl::StreamImpl::~StreamImpl() {} | ||
|
|
||
|
|
@@ -283,17 +291,63 @@ int ConnectionImpl::onFrameReceived(const nghttp2_frame* frame) { | |
| stream->headers_->addViaMove(std::move(key), std::move(stream->cookies_)); | ||
| } | ||
|
|
||
| if (frame->headers.cat == NGHTTP2_HCAT_REQUEST || frame->headers.cat == NGHTTP2_HCAT_RESPONSE) { | ||
| if (frame->headers.cat == NGHTTP2_HCAT_REQUEST && stream->headers_->Expect() && | ||
| 0 == StringUtil::caseInsensitiveCompare(stream->headers_->Expect()->value().c_str(), | ||
| Headers::get().ExpectValues._100Continue.c_str())) { | ||
| // Deal with expect: 100-continue here since higher layers are never going to do anything | ||
| // other than say to continue so that we can respond before request complete if necessary. | ||
| std::vector<nghttp2_nv> final_headers; | ||
| StreamImpl::buildHeaders(final_headers, *CONTINUE_HEADER); | ||
| int rc = nghttp2_submit_headers(session_, 0, stream->stream_id_, nullptr, &final_headers[0], | ||
| final_headers.size(), nullptr); | ||
| ASSERT(rc == 0); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this definitely a failure that can be ignored if not-debug?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on logic flow, this function should never fail, so I made it a debug only assert. We do this in a few other places in this file.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about NGHTTP2_ERR_NOMEM?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the Envoy code is safe for memory allocation failure, so we don't handle similar errors anywhere and assume we will shortly crash if OOM. (This is a design philosophy, we should discuss this outside of the context of this change if there are concerns).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can discuss this elsewhere (I do worry about libraries using ENOMEM in non-OOM scenarios, as opposed to tcmalloc). In any case, could you also include this design philosophy in https://github.com/lyft/envoy/blob/master/CONTRIBUTING.md when updating? Thanks.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that is a valid concern and might necessitate some paranoia checking in certain cases which would crash us if we receive ENOMEM, etc. I will open a GH issue to track as well as updating contributing guide. |
||
| UNREFERENCED_PARAMETER(rc); | ||
|
|
||
| stream->headers_->removeExpect(); | ||
| } | ||
|
|
||
| switch (frame->headers.cat) { | ||
| case NGHTTP2_HCAT_RESPONSE: { | ||
| if (CodeUtility::is1xx(Http::Utility::getResponseStatus(*stream->headers_))) { | ||
| stream->waiting_for_non_informational_headers_ = true; | ||
| } | ||
|
|
||
| // Fall through. | ||
| } | ||
|
|
||
| case NGHTTP2_HCAT_REQUEST: { | ||
| stream->decoder_->decodeHeaders(std::move(stream->headers_), stream->remote_end_stream_); | ||
| } else { | ||
| ASSERT(frame->headers.cat == NGHTTP2_HCAT_HEADERS); | ||
| ASSERT(stream->remote_end_stream_); | ||
| break; | ||
| } | ||
|
|
||
| // It's possible that we are waiting to send a deferred reset, so only raise trailers if local | ||
| // is not complete. | ||
| case NGHTTP2_HCAT_HEADERS: { | ||
| // It's possible that we are waiting to send a deferred reset, so only raise headers/trailers | ||
| // if local is not complete. | ||
| if (!stream->deferred_reset_.valid()) { | ||
| stream->decoder_->decodeTrailers(std::move(stream->headers_)); | ||
| if (!stream->waiting_for_non_informational_headers_) { | ||
| ASSERT(stream->remote_end_stream_); | ||
| stream->decoder_->decodeTrailers(std::move(stream->headers_)); | ||
| } else { | ||
| ASSERT(!nghttp2_session_check_server_session(session_)); | ||
| stream->waiting_for_non_informational_headers_ = false; | ||
|
|
||
| // This can only happen in the client case in a response, when we received a 1xx to | ||
| // start out with. In this case, raise as headers. nghttp2 message checking guarantees | ||
| // proper flow here. | ||
| // TODO(mattklein123): Higher layers don't currently deal with a double decodeHeaders() | ||
| // call and will probably crash. We do this in the client path for testing when the server | ||
| // responds with 1xx. In the future, if needed, we can properly handle 1xx in higher layer | ||
| // code, or just eat it. | ||
| stream->decoder_->decodeHeaders(std::move(stream->headers_), stream->remote_end_stream_); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| default: | ||
| // We do not currently support push. | ||
| NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| stream->headers_.reset(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Google C++ style guide doesn't like globals like this, since unjoined threads might observe destruction when main() is done. https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables.
Depending on whether this is something we want in the Envoy coding style (maybe we can assume we always have sane threading behavior and everything joins before the end?), this could be addressed by making this a function that returns the results of a static new* allocation of the object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do correctly join all threads before exiting main. I'm not a huge fan of the raw pointer approach because AFAIK this will cause the heap leak detector to fail on standard settings. I don't feel super strongly about this, but we do this in other places also. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also the concern about non-deterministic ordering of initialization. Not really a concern for simple local objects like this.
I think that when done sanely (e.g. don't initialize static data that depends on other static data), and given we have proper thread joining on exit, it's probably fine.
It would make sense to document this in https://github.com/lyft/envoy/blob/master/CONTRIBUTING.md though.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for init, we need to more careful, and we do handle more complex cases in init by having functions that return static variables. I will do a doc update.