Chromium Code Reviews| 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/filter/sdch_source_stream.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/numerics/safe_conversions.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "sdch/open-vcdiff/src/google/vcdecoder.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const size_t kServerIdLength = 9; | |
| 19 const char kSDCH[] = "SDCH"; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 SdchSourceStream::SdchSourceStream(std::unique_ptr<SourceStream> previous, | |
| 24 Delegate* delegate) | |
| 25 : FilterSourceStream(SourceStream::TYPE_SDCH, std::move(previous)), | |
| 26 delegate_(delegate), | |
| 27 error_occurred_(false), | |
| 28 input_state_(STATE_LOAD_DICTIONARY) { | |
| 29 CHECK(delegate); | |
| 30 } | |
| 31 | |
| 32 SdchSourceStream::~SdchSourceStream() {} | |
| 33 | |
| 34 std::string SdchSourceStream::GetTypeAsString() const { | |
| 35 return kSDCH; | |
| 36 } | |
| 37 | |
| 38 int SdchSourceStream::FilterData(IOBuffer* output_buffer, | |
| 39 int output_buffer_size, | |
| 40 IOBuffer* input_buffer, | |
| 41 int input_buffer_size, | |
| 42 int* consumed_bytes, | |
| 43 bool /*upstream_end_reached*/) { | |
| 44 DCHECK_LE(0, input_buffer_size); | |
| 45 size_t input_data_size = input_buffer_size; | |
| 46 char* input_data = input_buffer->data(); | |
| 47 int bytes_out = 0; | |
| 48 while ((input_data_size > 0 || !buffered_output_.empty()) && | |
| 49 output_buffer_size - bytes_out > 0) { | |
| 50 switch (input_state_) { | |
| 51 case STATE_LOAD_DICTIONARY: { | |
| 52 // Copy at most |kServerIdLength| from |input_buffer|. | |
| 53 size_t to_copy = std::min( | |
| 54 kServerIdLength - dictionary_server_id_.length(), input_data_size); | |
| 55 dictionary_server_id_.append(input_data, to_copy); | |
| 56 input_data_size -= to_copy; | |
| 57 input_data += to_copy; | |
| 58 | |
| 59 // Not enough bytes for a dictionary ID accumulated yet. | |
| 60 if (dictionary_server_id_.length() != kServerIdLength) { | |
| 61 *consumed_bytes = input_buffer_size - input_data_size; | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Though (i.e. not even a suggestion :-}): I believe
xunjieli
2016/10/05 13:44:57
Done.
| |
| 62 return 0; | |
| 63 } | |
| 64 // To avoid passing a std::string with a null terminator into | |
| 65 // GetDictionary(), server hash here removes the last byte blindly, | |
| 66 // and this method only calls GetDictionary() with it if | |
| 67 // CouldBeDictionaryId() returns true. | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Is this comment still valid/positioned appropriate
xunjieli
2016/10/05 13:44:57
Done. Good catch!
| |
| 68 const std::string* dictionary_text = nullptr; | |
| 69 | |
| 70 if (!CouldBeDictionaryId(dictionary_server_id_)) { | |
| 71 // If |dictionary_server_id_| is bogus, it should appear in output | |
| 72 // stream, so append it to |buffered_output_| here. | |
| 73 buffered_output_.append(dictionary_server_id_); | |
| 74 if (!HandleError( | |
| 75 delegate_->OnDictionaryIdError(this, &buffered_output_))) { | |
| 76 return ERR_CONTENT_DECODING_FAILED; | |
| 77 } | |
| 78 break; | |
| 79 } | |
| 80 if (!delegate_->OnGetDictionary( | |
| 81 dictionary_server_id_.substr(0, kServerIdLength - 1), | |
| 82 &dictionary_text)) { | |
| 83 // If GetDictionaryId fails and delegate chooses to pass through, | |
| 84 // preserve the dictionary id in the output. | |
| 85 buffered_output_.append(dictionary_server_id_); | |
| 86 if (!HandleError( | |
| 87 delegate_->OnGetDictionaryError(this, &buffered_output_))) { | |
| 88 return ERR_CONTENT_DECODING_FAILED; | |
| 89 } | |
| 90 break; | |
| 91 } | |
| 92 decoder_.reset(new open_vcdiff::VCDiffStreamingDecoder); | |
| 93 decoder_->SetAllowVcdTarget(false); | |
| 94 decoder_->StartDecoding(dictionary_text->data(), | |
| 95 dictionary_text->length()); | |
| 96 input_state_ = STATE_DECODE; | |
| 97 break; | |
| 98 } | |
| 99 case STATE_DECODE: { | |
| 100 DCHECK(buffered_output_.empty()); | |
| 101 bool ok = decoder_->DecodeChunk(input_data, input_data_size, | |
| 102 &buffered_output_); | |
| 103 // Calls to DecodeChunk always consume all their input, so this always | |
| 104 // drains the entire buffer. | |
| 105 input_data += input_data_size; | |
| 106 input_data_size = 0; | |
| 107 if (!ok) { | |
| 108 decoder_.reset(); | |
| 109 bool handled = | |
| 110 HandleError(delegate_->OnDecodingError(this, &buffered_output_)); | |
| 111 if (!handled) | |
| 112 return ERR_CONTENT_DECODING_FAILED; | |
| 113 break; | |
| 114 } | |
| 115 input_state_ = STATE_FLUSH_INTERNAL_BUFFER; | |
| 116 break; | |
| 117 } | |
| 118 case STATE_FLUSH_INTERNAL_BUFFER: { | |
| 119 bytes_out += FlushBufferedOutput(output_buffer->data() + bytes_out, | |
| 120 output_buffer_size - bytes_out); | |
| 121 if (!buffered_output_.empty()) | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Suggestion: I'm uncomfortable with the layering se
xunjieli
2016/10/05 13:44:57
Done.
| |
| 122 break; | |
| 123 if (!error_occurred_) | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
I'm confused about how this works in the |error_oc
xunjieli
2016/10/05 13:44:57
Done.
| |
| 124 input_state_ = STATE_DECODE; | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Ok, I think I'm clearer on this condition now; bas
xunjieli
2016/10/05 13:44:57
Done.
| |
| 125 break; | |
| 126 } | |
| 127 case STATE_PASS_THROUGH: { | |
| 128 if (!buffered_output_.empty()) { | |
| 129 bytes_out += FlushBufferedOutput(output_buffer->data() + bytes_out, | |
| 130 output_buffer_size - bytes_out); | |
| 131 } | |
| 132 if (!buffered_output_.empty()) | |
| 133 break; | |
| 134 size_t to_copy = | |
| 135 std::min(base::checked_cast<size_t>(output_buffer_size - bytes_out), | |
| 136 input_data_size); | |
| 137 memcpy(output_buffer->data() + bytes_out, input_data, to_copy); | |
| 138 input_data += to_copy; | |
| 139 input_data_size -= to_copy; | |
| 140 break; | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 *consumed_bytes = input_buffer_size - input_data_size; | |
| 145 return bytes_out; | |
| 146 } | |
| 147 | |
| 148 int SdchSourceStream::FlushBufferedOutput(char* output_buffer, | |
| 149 int output_buffer_size) { | |
| 150 // DCHECK_LT(0u, buffered_output_.length()); | |
| 151 size_t to_flush = std::min(base::checked_cast<size_t>(output_buffer_size), | |
| 152 buffered_output_.length()); | |
| 153 memcpy(output_buffer, buffered_output_.data(), to_flush); | |
| 154 buffered_output_.erase(0, to_flush); | |
| 155 return to_flush; | |
| 156 } | |
| 157 bool SdchSourceStream::CouldBeDictionaryId(const std::string& id) const { | |
| 158 for (size_t i = 0; i < kServerIdLength - 1; i++) { | |
| 159 char base64_char = id[i]; | |
| 160 if (!isalnum(base64_char) && '-' != base64_char && '_' != base64_char) | |
| 161 return false; | |
| 162 } | |
| 163 if (id[kServerIdLength - 1] != '\0') | |
| 164 return false; | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 bool SdchSourceStream::HandleError(Delegate::ErrorRecover error_recover) { | |
| 169 error_occurred_ = true; | |
| 170 switch (error_recover) { | |
| 171 case Delegate::NONE: | |
| 172 return false; | |
| 173 case Delegate::PASS_THROUGH: | |
| 174 input_state_ = STATE_PASS_THROUGH; | |
| 175 break; | |
| 176 case Delegate::REPLACE_OUTPUT: | |
| 177 input_state_ = STATE_FLUSH_INTERNAL_BUFFER; | |
| 178 break; | |
| 179 } | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 } // namespace net | |
| OLD | NEW |