@@ -1320,6 +1320,9 @@ int Http2Session::OnFrameNotSent(nghttp2_session* handle, |
1320 | 1320 | Debug(session, "frame type %d was not sent, code: %d", |
1321 | 1321 | frame->hd.type, error_code); |
1322 | 1322 |
|
| 1323 | + if (frame->hd.type == NGHTTP2_HEADERS) |
| 1324 | + session->HandleInfoHeadersFrame(frame, UV_ECANCELED); |
| 1325 | + |
1323 | 1326 | if (error_code == NGHTTP2_ERR_SESSION_CLOSING || |
1324 | 1327 | error_code == NGHTTP2_ERR_STREAM_CLOSED || |
1325 | 1328 | error_code == NGHTTP2_ERR_STREAM_CLOSING) { |
@@ -1361,6 +1364,9 @@ int Http2Session::OnFrameSent(nghttp2_session* handle, |
1361 | 1364 | Http2Session* session = static_cast<Http2Session*>(user_data); |
1362 | 1365 | session->statistics_.frame_sent += 1; |
1363 | 1366 |
|
| 1367 | + if (frame->hd.type == NGHTTP2_HEADERS) |
| 1368 | + session->HandleInfoHeadersFrame(frame, 0); |
| 1369 | + |
1364 | 1370 | // If nghttp2 has internally terminated the session (e.g. due to a protocol |
1365 | 1371 | // error like oversized frames, padding errors, or HPACK compression |
1366 | 1372 | // failures), it calls nghttp2_session_terminate_session() directly which |
@@ -2005,6 +2011,12 @@ void Http2Session::ClearOutgoing(int status) { |
2005 | 2011 |
|
2006 | 2012 | set_sending(false); |
2007 | 2013 |
|
| 2014 | + // Completing a request can start the next write. Take the requests that |
| 2015 | + // belong to the write that has just finished before completing any of them, |
| 2016 | + // so that the ones of the next write are left alone. |
| 2017 | + std::vector<OutgoingInfoReq> info_reqs; |
| 2018 | + info_reqs.swap(outgoing_info_reqs_); |
| 2019 | + |
2008 | 2020 | if (!outgoing_buffers_.empty()) { |
2009 | 2021 | outgoing_storage_.clear(); |
2010 | 2022 | outgoing_length_ = 0; |
@@ -2022,6 +2034,11 @@ void Http2Session::ClearOutgoing(int status) { |
2022 | 2034 | } |
2023 | 2035 | } |
2024 | 2036 |
|
| 2037 | + for (const OutgoingInfoReq& req : info_reqs) { |
| 2038 | + WriteWrap::FromObject(req.req_wrap) |
| 2039 | + ->Done(req.status != 0 ? req.status : status); |
| 2040 | + } |
| 2041 | + |
2025 | 2042 | // Now that we've finished sending queued data, if there are any pending |
2026 | 2043 | // RstStreams we should try sending again and then flush them one by one. |
2027 | 2044 | if (status == 0 && !pending_rst_streams_.empty()) { |
@@ -2039,6 +2056,24 @@ void Http2Session::ClearOutgoing(int status) { |
2039 | 2056 | } |
2040 | 2057 | } |
2041 | 2058 |
|
| 2059 | +// Called when nghttp2 has serialized a HEADERS frame, or has given up on |
| 2060 | +// sending it. Informational HEADERS frames are always sent before any other |
| 2061 | +// HEADERS frame of the same stream, and in the order they were submitted in, |
| 2062 | +// so if the stream still has informational headers pending, this frame is the |
| 2063 | +// oldest of them. Its request is completed in ClearOutgoing(), once the |
| 2064 | +// current write to the underlying stream has finished. |
| 2065 | +void Http2Session::HandleInfoHeadersFrame(const nghttp2_frame* frame, |
| 2066 | + int status) { |
| 2067 | + BaseObjectPtr<Http2Stream> stream = FindStream(frame->hd.stream_id); |
| 2068 | + if (!stream || stream->info_reqs_.empty()) return; |
| 2069 | + BaseObjectPtr<AsyncWrap> req_wrap = std::move(stream->info_reqs_.front()); |
| 2070 | + stream->info_reqs_.pop(); |
| 2071 | + if (req_wrap) { |
| 2072 | + outgoing_info_reqs_.push_back( |
| 2073 | + {std::move(req_wrap), std::move(stream), status}); |
| 2074 | + } |
| 2075 | +} |
| 2076 | + |
2042 | 2077 | void Http2Session::PushOutgoingBuffer(NgHttp2StreamWrite&& write) { |
2043 | 2078 | outgoing_length_ += write.buf.len; |
2044 | 2079 | outgoing_buffers_.emplace_back(std::move(write)); |
@@ -2314,6 +2349,9 @@ bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) { |
2314 | 2349 | for (const NgHttp2StreamWrite& wr : outgoing_buffers_) { |
2315 | 2350 | if (wr.stream.get() == stream) return true; |
2316 | 2351 | } |
| 2352 | + for (const OutgoingInfoReq& req : outgoing_info_reqs_) { |
| 2353 | + if (req.stream.get() == stream) return true; |
| 2354 | + } |
2317 | 2355 | return false; |
2318 | 2356 | } |
2319 | 2357 |
|
@@ -2418,6 +2456,7 @@ Http2Stream::~Http2Stream() { |
2418 | 2456 | void Http2Stream::MemoryInfo(MemoryTracker* tracker) const { |
2419 | 2457 | tracker->TrackField("current_headers", current_headers_); |
2420 | 2458 | tracker->TrackField("queue", queue_); |
| 2459 | + tracker->TrackField("info_reqs", info_reqs_); |
2421 | 2460 | } |
2422 | 2461 |
|
2423 | 2462 | std::string Http2Stream::diagnostic_name() const { |
@@ -2542,6 +2581,7 @@ void Http2Stream::CompleteDestroyCleanup() { |
2542 | 2581 | WriteWrap::FromObject(head.req_wrap)->Done(UV_ECANCELED); |
2543 | 2582 | queue_.pop(); |
2544 | 2583 | } |
| 2584 | + CancelPendingInfoRequests(); |
2545 | 2585 |
|
2546 | 2586 | // We can destroy the stream now if there are no writes for it |
2547 | 2587 | // already on the socket. Otherwise, we'll wait for the garbage collector |
@@ -2588,7 +2628,15 @@ int Http2Stream::SubmitResponse(const Http2Headers& headers, int options) { |
2588 | 2628 |
|
2589 | 2629 |
|
2590 | 2630 | // Submit informational headers for a stream. |
2591 | | -int Http2Stream::SubmitInfo(const Http2Headers& headers) { |
| 2631 | +void Http2Stream::CancelPendingInfoRequests() { |
| 2632 | + while (!info_reqs_.empty()) { |
| 2633 | + BaseObjectPtr<AsyncWrap> req_wrap = std::move(info_reqs_.front()); |
| 2634 | + info_reqs_.pop(); |
| 2635 | + if (req_wrap) WriteWrap::FromObject(req_wrap)->Done(UV_ECANCELED); |
| 2636 | + } |
| 2637 | +} |
| 2638 | + |
| 2639 | +int Http2Stream::SubmitInfo(const Http2Headers& headers, WriteWrap* req_wrap) { |
2592 | 2640 | CHECK(!this->is_destroyed()); |
2593 | 2641 | Http2Scope h2scope(this); |
2594 | 2642 | Debug(this, "sending %d informational headers", headers.length()); |
@@ -2601,6 +2649,12 @@ int Http2Stream::SubmitInfo(const Http2Headers& headers) { |
2601 | 2649 | headers.length(), |
2602 | 2650 | nullptr); |
2603 | 2651 | CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
| 2652 | + if (ret == 0) { |
| 2653 | + info_reqs_.emplace(req_wrap != nullptr ? req_wrap->GetAsyncWrap() |
| 2654 | + : nullptr); |
| 2655 | + } else if (req_wrap != nullptr) { |
| 2656 | + req_wrap->Dispose(); |
| 2657 | + } |
2604 | 2658 | return ret; |
2605 | 2659 | } |
2606 | 2660 |
|
@@ -3309,7 +3363,16 @@ void Http2Stream::Info(const FunctionCallbackInfo<Value>& args) { |
3309 | 3363 |
|
3310 | 3364 | Local<Array> headers = args[0].As<Array>(); |
3311 | 3365 |
|
3312 | | - args.GetReturnValue().Set(stream->SubmitInfo(Http2Headers(env, headers))); |
| 3366 | + // The optional request object is completed once the HEADERS frame has been |
| 3367 | + // written to the socket. |
| 3368 | + WriteWrap* req_wrap = nullptr; |
| 3369 | + if (args[1]->IsObject()) { |
| 3370 | + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(stream); |
| 3371 | + req_wrap = stream->CreateWriteWrap(args[1].As<Object>()); |
| 3372 | + } |
| 3373 | + |
| 3374 | + args.GetReturnValue().Set( |
| 3375 | + stream->SubmitInfo(Http2Headers(env, headers), req_wrap)); |
3313 | 3376 | } |
3314 | 3377 |
|
3315 | 3378 | // Submits trailing headers on the Http2Stream |
|