Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(873)

Side by Side Diff: net/filter/gzip_source_stream.cc

Issue 2604233002: Fix bug in deflate code. (Closed)
Patch Set: Fix leak Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/filter/gzip_source_stream.h ('k') | net/filter/gzip_source_stream_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/filter/gzip_source_stream.h" 5 #include "net/filter/gzip_source_stream.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bit_cast.h" 11 #include "base/bit_cast.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
13 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
14 #include "third_party/zlib/zlib.h" 15 #include "third_party/zlib/zlib.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 namespace { 19 namespace {
19 20
20 const char kDeflate[] = "DEFLATE"; 21 const char kDeflate[] = "DEFLATE";
21 const char kGzip[] = "GZIP"; 22 const char kGzip[] = "GZIP";
22 const char kGzipFallback[] = "GZIP_FALLBACK"; 23 const char kGzipFallback[] = "GZIP_FALLBACK";
23 24
25 // For deflate streams, if more than this many bytes have been received without
26 // an error and without adding a Zlib header, assume the original stream had a
27 // Zlib header. In practice, don't need nearly this much data, but since the
28 // detection logic is a heuristic, best to be safe. Data is freed once it's been
29 // determined whether the stream has a zlib header or not, so larger values
30 // shouldn't affect memory usage, in practice.
31 const int kMaxZlibHeaderSniffBytes = 1000;
32
24 } // namespace 33 } // namespace
25 34
26 GzipSourceStream::~GzipSourceStream() { 35 GzipSourceStream::~GzipSourceStream() {
27 if (zlib_stream_) 36 if (zlib_stream_)
28 inflateEnd(zlib_stream_.get()); 37 inflateEnd(zlib_stream_.get());
29 } 38 }
30 39
31 std::unique_ptr<GzipSourceStream> GzipSourceStream::Create( 40 std::unique_ptr<GzipSourceStream> GzipSourceStream::Create(
32 std::unique_ptr<SourceStream> upstream, 41 std::unique_ptr<SourceStream> upstream,
33 SourceStream::SourceType type) { 42 SourceStream::SourceType type) {
34 std::unique_ptr<GzipSourceStream> source( 43 std::unique_ptr<GzipSourceStream> source(
35 new GzipSourceStream(std::move(upstream), type)); 44 new GzipSourceStream(std::move(upstream), type));
36 45
37 if (!source->Init()) 46 if (!source->Init())
38 return nullptr; 47 return nullptr;
39 return source; 48 return source;
40 } 49 }
41 50
42 GzipSourceStream::GzipSourceStream(std::unique_ptr<SourceStream> upstream, 51 GzipSourceStream::GzipSourceStream(std::unique_ptr<SourceStream> upstream,
43 SourceStream::SourceType type) 52 SourceStream::SourceType type)
44 : FilterSourceStream(type, std::move(upstream)), 53 : FilterSourceStream(type, std::move(upstream)),
45 zlib_header_added_(false),
46 gzip_footer_bytes_left_(0), 54 gzip_footer_bytes_left_(0),
47 input_state_(STATE_START) {} 55 input_state_(STATE_START),
56 replay_state_(STATE_COMPRESSED_BODY) {}
48 57
49 bool GzipSourceStream::Init() { 58 bool GzipSourceStream::Init() {
50 zlib_stream_.reset(new z_stream); 59 zlib_stream_.reset(new z_stream);
51 if (!zlib_stream_) 60 if (!zlib_stream_)
52 return false; 61 return false;
53 memset(zlib_stream_.get(), 0, sizeof(z_stream)); 62 memset(zlib_stream_.get(), 0, sizeof(z_stream));
54 63
55 int ret; 64 int ret;
56 if (type() == TYPE_GZIP || type() == TYPE_GZIP_FALLBACK) { 65 if (type() == TYPE_GZIP || type() == TYPE_GZIP_FALLBACK) {
57 ret = inflateInit2(zlib_stream_.get(), -MAX_WBITS); 66 ret = inflateInit2(zlib_stream_.get(), -MAX_WBITS);
(...skipping 16 matching lines...) Expand all
74 NOTREACHED(); 83 NOTREACHED();
75 return ""; 84 return "";
76 } 85 }
77 } 86 }
78 87
79 int GzipSourceStream::FilterData(IOBuffer* output_buffer, 88 int GzipSourceStream::FilterData(IOBuffer* output_buffer,
80 int output_buffer_size, 89 int output_buffer_size,
81 IOBuffer* input_buffer, 90 IOBuffer* input_buffer,
82 int input_buffer_size, 91 int input_buffer_size,
83 int* consumed_bytes, 92 int* consumed_bytes,
84 bool /*upstream_end_reached*/) { 93 bool upstream_end_reached) {
85 *consumed_bytes = 0; 94 *consumed_bytes = 0;
86 char* input_data = input_buffer->data(); 95 char* input_data = input_buffer->data();
87 int input_data_size = input_buffer_size; 96 int input_data_size = input_buffer_size;
88 int bytes_out = 0; 97 int bytes_out = 0;
89 bool state_compressed_entered = false; 98 bool state_compressed_entered = false;
90 while (input_data_size > 0 && bytes_out < output_buffer_size) { 99 while (input_data_size > 0 && bytes_out < output_buffer_size) {
91 InputState state = input_state_; 100 InputState state = input_state_;
92 switch (state) { 101 switch (state) {
93 case STATE_START: { 102 case STATE_START: {
94 if (type() == TYPE_DEFLATE) { 103 if (type() == TYPE_DEFLATE) {
95 input_state_ = STATE_COMPRESSED_BODY; 104 input_state_ = STATE_SNIFFING_DEFLATE_HEADER;
96 break; 105 break;
97 } 106 }
98 // If this stream is not really gzipped as detected by 107 // If this stream is not really gzipped as detected by
99 // ShouldFallbackToPlain, pretend that the zlib stream has ended. 108 // ShouldFallbackToPlain, pretend that the zlib stream has ended.
100 DCHECK_LT(0, input_data_size); 109 DCHECK_LT(0, input_data_size);
101 if (ShouldFallbackToPlain(input_data[0])) { 110 if (ShouldFallbackToPlain(input_data[0])) {
102 input_state_ = STATE_UNCOMPRESSED_BODY; 111 input_state_ = STATE_UNCOMPRESSED_BODY;
103 } else { 112 } else {
104 input_state_ = STATE_GZIP_HEADER; 113 input_state_ = STATE_GZIP_HEADER;
105 } 114 }
(...skipping 12 matching lines...) Expand all
118 gzip_footer_bytes_left_ = kGzipFooterBytes; 127 gzip_footer_bytes_left_ = kGzipFooterBytes;
119 int bytes_consumed = end - input_data; 128 int bytes_consumed = end - input_data;
120 input_data += bytes_consumed; 129 input_data += bytes_consumed;
121 input_data_size -= bytes_consumed; 130 input_data_size -= bytes_consumed;
122 input_state_ = STATE_COMPRESSED_BODY; 131 input_state_ = STATE_COMPRESSED_BODY;
123 } else if (status == GZipHeader::INVALID_HEADER) { 132 } else if (status == GZipHeader::INVALID_HEADER) {
124 return ERR_CONTENT_DECODING_FAILED; 133 return ERR_CONTENT_DECODING_FAILED;
125 } 134 }
126 break; 135 break;
127 } 136 }
128 case STATE_COMPRESSED_BODY: { 137 case STATE_SNIFFING_DEFLATE_HEADER: {
129 DCHECK(!state_compressed_entered);
130 DCHECK_LE(0, input_data_size);
131
132 state_compressed_entered = true;
133 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data); 138 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data);
134 zlib_stream_.get()->avail_in = input_data_size; 139 zlib_stream_.get()->avail_in = input_data_size;
135 zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data()); 140 zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data());
136 zlib_stream_.get()->avail_out = output_buffer_size; 141 zlib_stream_.get()->avail_out = output_buffer_size;
137 142
138 int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH); 143 int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH);
139 144
140 // Sometimes misconfigured servers omit the zlib header, relying on 145 // On error, try adding a zlib header and replaying the response. Note
141 // clients to splice it back in. 146 // that data just received doesn't have to be replayed, since it hasn't
142 if (ret < 0 && !zlib_header_added_) { 147 // been removed from input_data yet, only data from previous FilterData
143 zlib_header_added_ = true; 148 // calls needs to be replayed.
149 if (ret != Z_STREAM_END && ret != Z_OK) {
144 if (!InsertZlibHeader()) 150 if (!InsertZlibHeader())
145 return ERR_CONTENT_DECODING_FAILED; 151 return ERR_CONTENT_DECODING_FAILED;
152 input_state_ = STATE_REPLAY_DATA;
153 continue;
154 }
146 155
147 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data); 156 int bytes_used = input_data_size - zlib_stream_.get()->avail_in;
148 zlib_stream_.get()->avail_in = input_data_size; 157 bytes_out = output_buffer_size - zlib_stream_.get()->avail_out;
149 zlib_stream_.get()->next_out = 158 // If any bytes are output, enough total bytes have been received, or at
150 bit_cast<Bytef*>(output_buffer->data()); 159 // the end of the stream, assume the response had a valid Zlib header.
151 zlib_stream_.get()->avail_out = output_buffer_size; 160 if (bytes_out > 0 ||
161 bytes_used + replay_data_.size() >= kMaxZlibHeaderSniffBytes ||
162 ret == Z_STREAM_END) {
163 std::move(replay_data_);
164 if (ret == Z_STREAM_END) {
165 input_state_ = STATE_GZIP_FOOTER;
166 } else {
167 input_state_ = STATE_COMPRESSED_BODY;
168 }
169 } else {
170 replay_data_.append(input_data, bytes_used);
171 }
152 172
153 ret = inflate(zlib_stream_.get(), Z_NO_FLUSH); 173 input_data_size -= bytes_used;
154 // TODO(xunjieli): add a histogram to see how often this happens. The 174 input_data += bytes_used;
155 // original bug for this behavior was ancient and maybe it doesn't 175 break;
156 // happen in the wild any more? crbug.com/649339 176 }
177 case STATE_REPLAY_DATA: {
178 if (!replay_data_.size()) {
179 std::move(replay_data_);
180 input_state_ = replay_state_;
181 continue;
157 } 182 }
183
184 // Call FilterData recursively, after updating |input_state_|, with
185 // |replay_data_|. This recursive call makes handling data from
186 // |replay_data_| and |input_buffer| much simpler than the alternative
187 // operations, though it's not pretty.
188 input_state_ = replay_state_;
189 int bytes_used;
190 scoped_refptr<IOBuffer> reaplay_buffer_(
xunjieli 2017/01/03 16:21:18 nit: Do you mean replay_buffer instead of reaplay_
mmenke 2017/01/03 18:25:42 Done (The underscore at the end was also wrong).
191 new WrappedIOBuffer(replay_data_.data()));
192 int result =
193 FilterData(output_buffer, output_buffer_size, reaplay_buffer_.get(),
194 replay_data_.size(), &bytes_used, upstream_end_reached);
195 replay_data_.erase(0, bytes_used);
196 // Back up resulting state, and return state to STATE_REPLAY_DATA.
197 replay_state_ = input_state_;
198 input_state_ = STATE_REPLAY_DATA;
199
200 // On error, or if bytes were read, just return result immediately.
201 // Could continue consuming data in the success case, but simplest not
202 // to.
203 if (result != 0)
204 return result;
205 continue;
206 }
207 case STATE_COMPRESSED_BODY: {
208 DCHECK(!state_compressed_entered);
209 DCHECK_LE(0, input_data_size);
210
211 state_compressed_entered = true;
212 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data);
213 zlib_stream_.get()->avail_in = input_data_size;
214 zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data());
215 zlib_stream_.get()->avail_out = output_buffer_size;
216
217 int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH);
158 if (ret != Z_STREAM_END && ret != Z_OK) 218 if (ret != Z_STREAM_END && ret != Z_OK)
159 return ERR_CONTENT_DECODING_FAILED; 219 return ERR_CONTENT_DECODING_FAILED;
160 220
161 int bytes_used = input_data_size - zlib_stream_.get()->avail_in; 221 int bytes_used = input_data_size - zlib_stream_.get()->avail_in;
162 bytes_out = output_buffer_size - zlib_stream_.get()->avail_out; 222 bytes_out = output_buffer_size - zlib_stream_.get()->avail_out;
163 input_data_size -= bytes_used; 223 input_data_size -= bytes_used;
164 input_data += bytes_used; 224 input_data += bytes_used;
165 if (ret == Z_STREAM_END) 225 if (ret == Z_STREAM_END)
166 input_state_ = STATE_GZIP_FOOTER; 226 input_state_ = STATE_GZIP_FOOTER;
167 // zlib has written as much data to |output_buffer| as it could. 227 // zlib has written as much data to |output_buffer| as it could.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // 1952 2.3.1, so if the first byte isn't the first byte of the gzip magic, and 271 // 1952 2.3.1, so if the first byte isn't the first byte of the gzip magic, and
212 // this filter is checking whether it should fallback, then fallback. 272 // this filter is checking whether it should fallback, then fallback.
213 bool GzipSourceStream::ShouldFallbackToPlain(char first_byte) { 273 bool GzipSourceStream::ShouldFallbackToPlain(char first_byte) {
214 if (type() != TYPE_GZIP_FALLBACK) 274 if (type() != TYPE_GZIP_FALLBACK)
215 return false; 275 return false;
216 static const char kGzipFirstByte = 0x1f; 276 static const char kGzipFirstByte = 0x1f;
217 return first_byte != kGzipFirstByte; 277 return first_byte != kGzipFirstByte;
218 } 278 }
219 279
220 } // namespace net 280 } // namespace net
OLDNEW
« no previous file with comments | « net/filter/gzip_source_stream.h ('k') | net/filter/gzip_source_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698