blob: b6dde85978c0bcaccca0e327bee93327f0d504d5 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// Copyright 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/http2/decoder/payload_decoders/altsvc_payload_decoder.h"
6
7#include <stddef.h>
8
QUICHE teamfd50a402018-12-07 22:54:05 -05009#include "net/third_party/quiche/src/http2/decoder/decode_buffer.h"
10#include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h"
11#include "net/third_party/quiche/src/http2/http2_constants.h"
12#include "net/third_party/quiche/src/http2/http2_structures.h"
13#include "net/third_party/quiche/src/http2/platform/api/http2_bug_tracker.h"
QUICHE team61940b42019-03-07 23:32:27 -050014#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050015#include "net/third_party/quiche/src/http2/platform/api/http2_macros.h"
16
17namespace http2 {
18
19std::ostream& operator<<(std::ostream& out,
20 AltSvcPayloadDecoder::PayloadState v) {
21 switch (v) {
22 case AltSvcPayloadDecoder::PayloadState::kStartDecodingStruct:
23 return out << "kStartDecodingStruct";
24 case AltSvcPayloadDecoder::PayloadState::kMaybeDecodedStruct:
25 return out << "kMaybeDecodedStruct";
26 case AltSvcPayloadDecoder::PayloadState::kDecodingStrings:
27 return out << "kDecodingStrings";
28 case AltSvcPayloadDecoder::PayloadState::kResumeDecodingStruct:
29 return out << "kResumeDecodingStruct";
30 }
31 // Since the value doesn't come over the wire, only a programming bug should
32 // result in reaching this point.
33 int unknown = static_cast<int>(v);
34 HTTP2_BUG << "Invalid AltSvcPayloadDecoder::PayloadState: " << unknown;
35 return out << "AltSvcPayloadDecoder::PayloadState(" << unknown << ")";
36}
37
38DecodeStatus AltSvcPayloadDecoder::StartDecodingPayload(
39 FrameDecoderState* state,
40 DecodeBuffer* db) {
QUICHE team61940b42019-03-07 23:32:27 -050041 HTTP2_DVLOG(2) << "AltSvcPayloadDecoder::StartDecodingPayload: "
42 << state->frame_header();
QUICHE teamfd50a402018-12-07 22:54:05 -050043 DCHECK_EQ(Http2FrameType::ALTSVC, state->frame_header().type);
44 DCHECK_LE(db->Remaining(), state->frame_header().payload_length);
45 DCHECK_EQ(0, state->frame_header().flags);
46
47 state->InitializeRemainders();
48 payload_state_ = PayloadState::kStartDecodingStruct;
49
50 return ResumeDecodingPayload(state, db);
51}
52
53DecodeStatus AltSvcPayloadDecoder::ResumeDecodingPayload(
54 FrameDecoderState* state,
55 DecodeBuffer* db) {
56 const Http2FrameHeader& frame_header = state->frame_header();
QUICHE team61940b42019-03-07 23:32:27 -050057 HTTP2_DVLOG(2) << "AltSvcPayloadDecoder::ResumeDecodingPayload: "
58 << frame_header;
QUICHE teamfd50a402018-12-07 22:54:05 -050059 DCHECK_EQ(Http2FrameType::ALTSVC, frame_header.type);
60 DCHECK_LE(state->remaining_payload(), frame_header.payload_length);
61 DCHECK_LE(db->Remaining(), state->remaining_payload());
62 DCHECK_NE(PayloadState::kMaybeDecodedStruct, payload_state_);
63 // |status| has to be initialized to some value to avoid compiler error in
64 // case PayloadState::kMaybeDecodedStruct below, but value does not matter,
65 // see DCHECK_NE above.
66 DecodeStatus status = DecodeStatus::kDecodeError;
67 while (true) {
QUICHE team61940b42019-03-07 23:32:27 -050068 HTTP2_DVLOG(2)
69 << "AltSvcPayloadDecoder::ResumeDecodingPayload payload_state_="
70 << payload_state_;
QUICHE teamfd50a402018-12-07 22:54:05 -050071 switch (payload_state_) {
72 case PayloadState::kStartDecodingStruct:
73 status = state->StartDecodingStructureInPayload(&altsvc_fields_, db);
74 HTTP2_FALLTHROUGH;
75
76 case PayloadState::kMaybeDecodedStruct:
77 if (status == DecodeStatus::kDecodeDone &&
78 altsvc_fields_.origin_length <= state->remaining_payload()) {
79 size_t origin_length = altsvc_fields_.origin_length;
80 size_t value_length = state->remaining_payload() - origin_length;
81 state->listener()->OnAltSvcStart(frame_header, origin_length,
82 value_length);
83 } else if (status != DecodeStatus::kDecodeDone) {
84 DCHECK(state->remaining_payload() > 0 ||
85 status == DecodeStatus::kDecodeError)
86 << "\nremaining_payload: " << state->remaining_payload()
87 << "\nstatus: " << status << "\nheader: " << frame_header;
88 // Assume in progress.
89 payload_state_ = PayloadState::kResumeDecodingStruct;
90 return status;
91 } else {
92 // The origin's length is longer than the remaining payload.
93 DCHECK_GT(altsvc_fields_.origin_length, state->remaining_payload());
94 return state->ReportFrameSizeError();
95 }
96 HTTP2_FALLTHROUGH;
97
98 case PayloadState::kDecodingStrings:
99 return DecodeStrings(state, db);
100
101 case PayloadState::kResumeDecodingStruct:
102 status = state->ResumeDecodingStructureInPayload(&altsvc_fields_, db);
103 payload_state_ = PayloadState::kMaybeDecodedStruct;
104 continue;
105 }
106 HTTP2_BUG << "PayloadState: " << payload_state_;
107 }
108}
109
110DecodeStatus AltSvcPayloadDecoder::DecodeStrings(FrameDecoderState* state,
111 DecodeBuffer* db) {
QUICHE team61940b42019-03-07 23:32:27 -0500112 HTTP2_DVLOG(2) << "AltSvcPayloadDecoder::DecodeStrings remaining_payload="
113 << state->remaining_payload()
114 << ", db->Remaining=" << db->Remaining();
QUICHE teamfd50a402018-12-07 22:54:05 -0500115 // Note that we don't explicitly keep track of exactly how far through the
116 // origin; instead we compute it from how much is left of the original
117 // payload length and the decoded total length of the origin.
118 size_t origin_length = altsvc_fields_.origin_length;
119 size_t value_length = state->frame_header().payload_length - origin_length -
120 Http2AltSvcFields::EncodedSize();
121 if (state->remaining_payload() > value_length) {
122 size_t remaining_origin_length = state->remaining_payload() - value_length;
123 size_t avail = db->MinLengthRemaining(remaining_origin_length);
124 state->listener()->OnAltSvcOriginData(db->cursor(), avail);
125 db->AdvanceCursor(avail);
126 state->ConsumePayload(avail);
127 if (remaining_origin_length > avail) {
128 payload_state_ = PayloadState::kDecodingStrings;
129 return DecodeStatus::kDecodeInProgress;
130 }
131 }
132 // All that is left is the value string.
133 DCHECK_LE(state->remaining_payload(), value_length);
134 DCHECK_LE(db->Remaining(), state->remaining_payload());
135 if (db->HasData()) {
136 size_t avail = db->Remaining();
137 state->listener()->OnAltSvcValueData(db->cursor(), avail);
138 db->AdvanceCursor(avail);
139 state->ConsumePayload(avail);
140 }
141 if (state->remaining_payload() == 0) {
142 state->listener()->OnAltSvcEnd();
143 return DecodeStatus::kDecodeDone;
144 }
145 payload_state_ = PayloadState::kDecodingStrings;
146 return DecodeStatus::kDecodeInProgress;
147}
148
149} // namespace http2