OLD | NEW |
| (Empty) |
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 "net/http2/decoder/payload_decoders/altsvc_payload_decoder.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "net/http2/decoder/decode_buffer.h" | |
12 #include "net/http2/decoder/http2_frame_decoder_listener.h" | |
13 #include "net/http2/http2_constants.h" | |
14 #include "net/http2/http2_structures.h" | |
15 #include "net/http2/tools/http2_bug_tracker.h" | |
16 | |
17 namespace net { | |
18 | |
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 return out << static_cast<int>(v); | |
32 } | |
33 | |
34 DecodeStatus AltSvcPayloadDecoder::StartDecodingPayload( | |
35 FrameDecoderState* state, | |
36 DecodeBuffer* db) { | |
37 DVLOG(2) << "AltSvcPayloadDecoder::StartDecodingPayload: " | |
38 << state->frame_header(); | |
39 DCHECK_EQ(Http2FrameType::ALTSVC, state->frame_header().type); | |
40 DCHECK_LE(db->Remaining(), state->frame_header().payload_length); | |
41 DCHECK_EQ(0, state->frame_header().flags); | |
42 | |
43 state->InitializeRemainders(); | |
44 payload_state_ = PayloadState::kStartDecodingStruct; | |
45 | |
46 return ResumeDecodingPayload(state, db); | |
47 } | |
48 | |
49 DecodeStatus AltSvcPayloadDecoder::ResumeDecodingPayload( | |
50 FrameDecoderState* state, | |
51 DecodeBuffer* db) { | |
52 const Http2FrameHeader& frame_header = state->frame_header(); | |
53 DVLOG(2) << "AltSvcPayloadDecoder::ResumeDecodingPayload: " << frame_header; | |
54 DCHECK_EQ(Http2FrameType::ALTSVC, frame_header.type); | |
55 DCHECK_LE(state->remaining_payload(), frame_header.payload_length); | |
56 DCHECK_LE(db->Remaining(), state->remaining_payload()); | |
57 DCHECK_NE(PayloadState::kMaybeDecodedStruct, payload_state_); | |
58 // |status| has to be initialized to some value to avoid compiler error in | |
59 // case PayloadState::kMaybeDecodedStruct below, but value does not matter, | |
60 // see DCHECK_NE above. | |
61 DecodeStatus status = DecodeStatus::kDecodeError; | |
62 while (true) { | |
63 DVLOG(2) << "AltSvcPayloadDecoder::ResumeDecodingPayload payload_state_=" | |
64 << payload_state_; | |
65 switch (payload_state_) { | |
66 case PayloadState::kStartDecodingStruct: | |
67 status = state->StartDecodingStructureInPayload(&altsvc_fields_, db); | |
68 // FALLTHROUGH_INTENDED | |
69 | |
70 case PayloadState::kMaybeDecodedStruct: | |
71 if (status == DecodeStatus::kDecodeDone && | |
72 altsvc_fields_.origin_length <= state->remaining_payload()) { | |
73 size_t origin_length = altsvc_fields_.origin_length; | |
74 size_t value_length = state->remaining_payload() - origin_length; | |
75 state->listener()->OnAltSvcStart(frame_header, origin_length, | |
76 value_length); | |
77 } else if (status != DecodeStatus::kDecodeDone) { | |
78 DCHECK(state->remaining_payload() > 0 || | |
79 status == DecodeStatus::kDecodeError) | |
80 << "\nremaining_payload: " << state->remaining_payload() | |
81 << "\nstatus: " << status << "\nheader: " << frame_header; | |
82 // Assume in progress. | |
83 payload_state_ = PayloadState::kResumeDecodingStruct; | |
84 return status; | |
85 } else { | |
86 // The origin's length is longer than the remaining payload. | |
87 DCHECK_GT(altsvc_fields_.origin_length, state->remaining_payload()); | |
88 return state->ReportFrameSizeError(); | |
89 } | |
90 // FALLTHROUGH_INTENDED | |
91 | |
92 case PayloadState::kDecodingStrings: | |
93 return DecodeStrings(state, db); | |
94 | |
95 case PayloadState::kResumeDecodingStruct: | |
96 status = state->ResumeDecodingStructureInPayload(&altsvc_fields_, db); | |
97 payload_state_ = PayloadState::kMaybeDecodedStruct; | |
98 continue; | |
99 } | |
100 HTTP2_BUG << "PayloadState: " << payload_state_; | |
101 } | |
102 } | |
103 | |
104 DecodeStatus AltSvcPayloadDecoder::DecodeStrings(FrameDecoderState* state, | |
105 DecodeBuffer* db) { | |
106 DVLOG(2) << "AltSvcPayloadDecoder::DecodeStrings remaining_payload=" | |
107 << state->remaining_payload() | |
108 << ", db->Remaining=" << db->Remaining(); | |
109 // Note that we don't explicitly keep track of exactly how far through the | |
110 // origin; instead we compute it from how much is left of the original | |
111 // payload length and the decoded total length of the origin. | |
112 size_t origin_length = altsvc_fields_.origin_length; | |
113 size_t value_length = state->frame_header().payload_length - origin_length - | |
114 Http2AltSvcFields::EncodedSize(); | |
115 if (state->remaining_payload() > value_length) { | |
116 size_t remaining_origin_length = state->remaining_payload() - value_length; | |
117 size_t avail = db->MinLengthRemaining(remaining_origin_length); | |
118 state->listener()->OnAltSvcOriginData(db->cursor(), avail); | |
119 db->AdvanceCursor(avail); | |
120 state->ConsumePayload(avail); | |
121 if (remaining_origin_length > avail) { | |
122 payload_state_ = PayloadState::kDecodingStrings; | |
123 return DecodeStatus::kDecodeInProgress; | |
124 } | |
125 } | |
126 // All that is left is the value string. | |
127 DCHECK_LE(state->remaining_payload(), value_length); | |
128 DCHECK_LE(db->Remaining(), state->remaining_payload()); | |
129 if (db->HasData()) { | |
130 size_t avail = db->Remaining(); | |
131 state->listener()->OnAltSvcValueData(db->cursor(), avail); | |
132 db->AdvanceCursor(avail); | |
133 state->ConsumePayload(avail); | |
134 } | |
135 if (state->remaining_payload() == 0) { | |
136 state->listener()->OnAltSvcEnd(); | |
137 return DecodeStatus::kDecodeDone; | |
138 } | |
139 payload_state_ = PayloadState::kDecodingStrings; | |
140 return DecodeStatus::kDecodeInProgress; | |
141 } | |
142 | |
143 } // namespace net | |
OLD | NEW |