1 // 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 "quiche/http2/decoder/payload_decoders/altsvc_payload_decoder.h"
6 
7 #include <stddef.h>
8 
9 #include "absl/base/macros.h"
10 #include "quiche/http2/decoder/decode_buffer.h"
11 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
12 #include "quiche/http2/http2_constants.h"
13 #include "quiche/http2/http2_structures.h"
14 #include "quiche/common/platform/api/quiche_bug_tracker.h"
15 #include "quiche/common/platform/api/quiche_logging.h"
16 
17 namespace http2 {
18 
operator <<(std::ostream & out,AltSvcPayloadDecoder::PayloadState v)19 std::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   QUICHE_BUG(http2_bug_163_1)
35       << "Invalid AltSvcPayloadDecoder::PayloadState: " << unknown;
36   return out << "AltSvcPayloadDecoder::PayloadState(" << unknown << ")";
37 }
38 
StartDecodingPayload(FrameDecoderState * state,DecodeBuffer * db)39 DecodeStatus AltSvcPayloadDecoder::StartDecodingPayload(
40     FrameDecoderState* state, DecodeBuffer* db) {
41   QUICHE_DVLOG(2) << "AltSvcPayloadDecoder::StartDecodingPayload: "
42                   << state->frame_header();
43   QUICHE_DCHECK_EQ(Http2FrameType::ALTSVC, state->frame_header().type);
44   QUICHE_DCHECK_LE(db->Remaining(), state->frame_header().payload_length);
45   QUICHE_DCHECK_EQ(0, state->frame_header().flags);
46 
47   state->InitializeRemainders();
48   payload_state_ = PayloadState::kStartDecodingStruct;
49 
50   return ResumeDecodingPayload(state, db);
51 }
52 
ResumeDecodingPayload(FrameDecoderState * state,DecodeBuffer * db)53 DecodeStatus AltSvcPayloadDecoder::ResumeDecodingPayload(
54     FrameDecoderState* state, DecodeBuffer* db) {
55   const Http2FrameHeader& frame_header = state->frame_header();
56   QUICHE_DVLOG(2) << "AltSvcPayloadDecoder::ResumeDecodingPayload: "
57                   << frame_header;
58   QUICHE_DCHECK_EQ(Http2FrameType::ALTSVC, frame_header.type);
59   QUICHE_DCHECK_LE(state->remaining_payload(), frame_header.payload_length);
60   QUICHE_DCHECK_LE(db->Remaining(), state->remaining_payload());
61   QUICHE_DCHECK_NE(PayloadState::kMaybeDecodedStruct, payload_state_);
62   // |status| has to be initialized to some value to avoid compiler error in
63   // case PayloadState::kMaybeDecodedStruct below, but value does not matter,
64   // see QUICHE_DCHECK_NE above.
65   DecodeStatus status = DecodeStatus::kDecodeError;
66   while (true) {
67     QUICHE_DVLOG(2)
68         << "AltSvcPayloadDecoder::ResumeDecodingPayload payload_state_="
69         << payload_state_;
70     switch (payload_state_) {
71       case PayloadState::kStartDecodingStruct:
72         status = state->StartDecodingStructureInPayload(&altsvc_fields_, db);
73         ABSL_FALLTHROUGH_INTENDED;
74 
75       case PayloadState::kMaybeDecodedStruct:
76         if (status == DecodeStatus::kDecodeDone &&
77             altsvc_fields_.origin_length <= state->remaining_payload()) {
78           size_t origin_length = altsvc_fields_.origin_length;
79           size_t value_length = state->remaining_payload() - origin_length;
80           state->listener()->OnAltSvcStart(frame_header, origin_length,
81                                            value_length);
82         } else if (status != DecodeStatus::kDecodeDone) {
83           QUICHE_DCHECK(state->remaining_payload() > 0 ||
84                         status == DecodeStatus::kDecodeError)
85               << "\nremaining_payload: " << state->remaining_payload()
86               << "\nstatus: " << status << "\nheader: " << frame_header;
87           // Assume in progress.
88           payload_state_ = PayloadState::kResumeDecodingStruct;
89           return status;
90         } else {
91           // The origin's length is longer than the remaining payload.
92           QUICHE_DCHECK_GT(altsvc_fields_.origin_length,
93                            state->remaining_payload());
94           return state->ReportFrameSizeError();
95         }
96         ABSL_FALLTHROUGH_INTENDED;
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     QUICHE_BUG(http2_bug_163_2) << "PayloadState: " << payload_state_;
107   }
108 }
109 
DecodeStrings(FrameDecoderState * state,DecodeBuffer * db)110 DecodeStatus AltSvcPayloadDecoder::DecodeStrings(FrameDecoderState* state,
111                                                  DecodeBuffer* db) {
112   QUICHE_DVLOG(2) << "AltSvcPayloadDecoder::DecodeStrings remaining_payload="
113                   << state->remaining_payload()
114                   << ", db->Remaining=" << db->Remaining();
115   // 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   QUICHE_DCHECK_LE(state->remaining_payload(), value_length);
134   QUICHE_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
150