QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/http/quic_client_promised_info.h" |
| 6 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 7 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
danzh | 2a93046 | 2019-07-03 07:28:06 -0700 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/http/spdy_server_push_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/spdy/core/spdy_protocol.h" |
| 14 | |
| 15 | using spdy::SpdyHeaderBlock; |
| 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | QuicClientPromisedInfo::QuicClientPromisedInfo( |
| 20 | QuicSpdyClientSessionBase* session, |
| 21 | QuicStreamId id, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 22 | std::string url) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | : session_(session), |
| 24 | id_(id), |
| 25 | url_(std::move(url)), |
| 26 | client_request_delegate_(nullptr) {} |
| 27 | |
| 28 | QuicClientPromisedInfo::~QuicClientPromisedInfo() {} |
| 29 | |
| 30 | void QuicClientPromisedInfo::CleanupAlarm::OnAlarm() { |
| 31 | QUIC_DVLOG(1) << "self GC alarm for stream " << promised_->id_; |
| 32 | promised_->session()->OnPushStreamTimedOut(promised_->id_); |
| 33 | promised_->Reset(QUIC_PUSH_STREAM_TIMED_OUT); |
| 34 | } |
| 35 | |
| 36 | void QuicClientPromisedInfo::Init() { |
| 37 | cleanup_alarm_.reset(session_->connection()->alarm_factory()->CreateAlarm( |
| 38 | new QuicClientPromisedInfo::CleanupAlarm(this))); |
| 39 | cleanup_alarm_->Set( |
| 40 | session_->connection()->helper()->GetClock()->ApproximateNow() + |
| 41 | QuicTime::Delta::FromSeconds(kPushPromiseTimeoutSecs)); |
| 42 | } |
| 43 | |
| 44 | bool QuicClientPromisedInfo::OnPromiseHeaders(const SpdyHeaderBlock& headers) { |
| 45 | // RFC7540, Section 8.2, requests MUST be safe [RFC7231], Section |
| 46 | // 4.2.1. GET and HEAD are the methods that are safe and required. |
| 47 | SpdyHeaderBlock::const_iterator it = headers.find(spdy::kHttp2MethodHeader); |
| 48 | if (it == headers.end()) { |
| 49 | QUIC_DVLOG(1) << "Promise for stream " << id_ << " has no method"; |
| 50 | Reset(QUIC_INVALID_PROMISE_METHOD); |
| 51 | return false; |
| 52 | } |
| 53 | if (!(it->second == "GET" || it->second == "HEAD")) { |
| 54 | QUIC_DVLOG(1) << "Promise for stream " << id_ << " has invalid method " |
| 55 | << it->second; |
| 56 | Reset(QUIC_INVALID_PROMISE_METHOD); |
| 57 | return false; |
| 58 | } |
danzh | 2a93046 | 2019-07-03 07:28:06 -0700 | [diff] [blame] | 59 | if (!SpdyServerPushUtils::PromisedUrlIsValid(headers)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | QUIC_DVLOG(1) << "Promise for stream " << id_ << " has invalid URL " |
| 61 | << url_; |
| 62 | Reset(QUIC_INVALID_PROMISE_URL); |
| 63 | return false; |
| 64 | } |
| 65 | if (!session_->IsAuthorized( |
danzh | 2a93046 | 2019-07-03 07:28:06 -0700 | [diff] [blame] | 66 | SpdyServerPushUtils::GetPromisedHostNameFromHeaders(headers))) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | Reset(QUIC_UNAUTHORIZED_PROMISE_URL); |
| 68 | return false; |
| 69 | } |
| 70 | request_headers_ = headers.Clone(); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void QuicClientPromisedInfo::OnResponseHeaders(const SpdyHeaderBlock& headers) { |
| 75 | response_headers_ = QuicMakeUnique<SpdyHeaderBlock>(headers.Clone()); |
| 76 | if (client_request_delegate_) { |
| 77 | // We already have a client request waiting. |
| 78 | FinalValidation(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void QuicClientPromisedInfo::Reset(QuicRstStreamErrorCode error_code) { |
| 83 | QuicClientPushPromiseIndex::Delegate* delegate = client_request_delegate_; |
| 84 | session_->ResetPromised(id_, error_code); |
| 85 | session_->DeletePromised(this); |
| 86 | if (delegate) { |
| 87 | delegate->OnRendezvousResult(nullptr); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | QuicAsyncStatus QuicClientPromisedInfo::FinalValidation() { |
| 92 | if (!client_request_delegate_->CheckVary( |
| 93 | client_request_headers_, request_headers_, *response_headers_)) { |
| 94 | Reset(QUIC_PROMISE_VARY_MISMATCH); |
| 95 | return QUIC_FAILURE; |
| 96 | } |
| 97 | QuicSpdyStream* stream = session_->GetPromisedStream(id_); |
| 98 | if (!stream) { |
| 99 | // This shouldn't be possible, as |ClientRequest| guards against |
| 100 | // closed stream for the synchronous case. And in the |
| 101 | // asynchronous case, a RST can only be caught by |OnAlarm()|. |
| 102 | QUIC_BUG << "missing promised stream" << id_; |
| 103 | } |
| 104 | QuicClientPushPromiseIndex::Delegate* delegate = client_request_delegate_; |
| 105 | session_->DeletePromised(this); |
| 106 | // Stream can start draining now |
| 107 | if (delegate) { |
| 108 | delegate->OnRendezvousResult(stream); |
| 109 | } |
| 110 | return QUIC_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | QuicAsyncStatus QuicClientPromisedInfo::HandleClientRequest( |
| 114 | const SpdyHeaderBlock& request_headers, |
| 115 | QuicClientPushPromiseIndex::Delegate* delegate) { |
| 116 | if (session_->IsClosedStream(id_)) { |
| 117 | // There was a RST on the response stream. |
| 118 | session_->DeletePromised(this); |
| 119 | return QUIC_FAILURE; |
| 120 | } |
| 121 | |
| 122 | if (is_validating()) { |
| 123 | // The push promise has already been matched to another request though |
| 124 | // pending for validation. Returns QUIC_FAILURE to the caller as it couldn't |
| 125 | // match a new request any more. This will not affect the validation of the |
| 126 | // other request. |
| 127 | return QUIC_FAILURE; |
| 128 | } |
| 129 | |
| 130 | client_request_delegate_ = delegate; |
| 131 | client_request_headers_ = request_headers.Clone(); |
| 132 | if (response_headers_ == nullptr) { |
| 133 | return QUIC_PENDING; |
| 134 | } |
| 135 | return FinalValidation(); |
| 136 | } |
| 137 | |
| 138 | void QuicClientPromisedInfo::Cancel() { |
| 139 | // Don't fire OnRendezvousResult() for client initiated cancel. |
| 140 | client_request_delegate_ = nullptr; |
| 141 | Reset(QUIC_STREAM_CANCELLED); |
| 142 | } |
| 143 | |
| 144 | } // namespace quic |