| 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/gzip_stream_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bit_cast.h" |
| 9 #include "base/logging.h" |
| 10 #include "third_party/zlib/zlib.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kDeflate[] = "DEFLATE"; |
| 17 const char kGzip[] = "GZIP"; |
| 18 const char kGzipFallback[] = "GZIP_FALLBACK"; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 GzipStreamSource::GzipStreamSource(std::unique_ptr<StreamSource> previous, |
| 23 GzipStreamSourceMode mode) |
| 24 : FilterStreamSource(StreamSource::TYPE_GZIP, std::move(previous)), |
| 25 mode_(mode), |
| 26 zlib_eof_(false), |
| 27 zlib_header_added_(false), |
| 28 should_check_gzip_header_(true), |
| 29 gzip_footer_bytes_left_(0) {} |
| 30 |
| 31 GzipStreamSource::~GzipStreamSource() { |
| 32 if (zlib_stream_) |
| 33 inflateEnd(zlib_stream_.get()); |
| 34 } |
| 35 |
| 36 bool GzipStreamSource::Init() { |
| 37 zlib_stream_.reset(new z_stream); |
| 38 if (!zlib_stream_) |
| 39 return false; |
| 40 memset(zlib_stream_.get(), 0, sizeof(z_stream)); |
| 41 |
| 42 if (mode_ == GZIP_STREAM_SOURCE_GZIP || |
| 43 mode_ == GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK) { |
| 44 if (inflateInit2(zlib_stream_.get(), -MAX_WBITS) != Z_OK) |
| 45 return false; |
| 46 } else { |
| 47 should_check_gzip_header_ = false; |
| 48 if (inflateInit(zlib_stream_.get()) != Z_OK) |
| 49 return false; |
| 50 } |
| 51 return true; |
| 52 } |
| 53 |
| 54 std::string GzipStreamSource::GetTypeAsString() const { |
| 55 switch (type()) { |
| 56 case TYPE_GZIP: |
| 57 return kGzip; |
| 58 case TYPE_GZIP_FALLBACK: |
| 59 return kGzipFallback; |
| 60 case TYPE_DEFLATE: |
| 61 return kDeflate; |
| 62 default: |
| 63 NOTREACHED(); |
| 64 return ""; |
| 65 } |
| 66 } |
| 67 |
| 68 int GzipStreamSource::FilterData(IOBuffer* output_buffer, |
| 69 size_t output_buffer_size, |
| 70 DrainableIOBuffer* input_buffer) { |
| 71 // If this stream is not really gzipped as detected by |
| 72 // ShouldFallbackToPlain, pretend the zlib stream already ended. |
| 73 if (ShouldFallbackToPlain(input_buffer)) { |
| 74 zlib_eof_ = true; |
| 75 should_check_gzip_header_ = false; |
| 76 } |
| 77 |
| 78 // Require a valid gzip header when decompressing a gzip stream. |
| 79 if (should_check_gzip_header_ && IsGzipHeaderInvalid(input_buffer)) |
| 80 return ERR_CONTENT_DECODING_FAILED; |
| 81 |
| 82 size_t bytes_read = |
| 83 Decompress(output_buffer, output_buffer_size, input_buffer); |
| 84 |
| 85 // If there was already some data buffered internally in |buffer_|, |
| 86 // or some output buffered internally in zlib, |Decompress| can succeed |
| 87 // synchronously. If this happens, return right here. |
| 88 if (bytes_read > 0) |
| 89 return bytes_read; |
| 90 |
| 91 // Since Decompress needs more input, it has consumed all existing input. |
| 92 DCHECK_EQ(0, input_buffer->BytesRemaining()); |
| 93 |
| 94 return bytes_read; |
| 95 } |
| 96 |
| 97 int GzipStreamSource::Decompress(IOBuffer* output_buffer, |
| 98 size_t output_buffer_size, |
| 99 DrainableIOBuffer* input_buffer) { |
| 100 DCHECK(output_buffer); |
| 101 DCHECK_NE(0u, output_buffer_size); |
| 102 |
| 103 if (input_buffer->BytesRemaining() == 0) |
| 104 return 0; |
| 105 |
| 106 // If the zlib stream has already ended, pass any further data through. |
| 107 if (zlib_eof_) |
| 108 return Passthrough(output_buffer->data(), output_buffer_size, input_buffer); |
| 109 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_buffer->data()); |
| 110 zlib_stream_.get()->avail_in = input_buffer->BytesRemaining(); |
| 111 zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data()); |
| 112 zlib_stream_.get()->avail_out = output_buffer_size; |
| 113 |
| 114 int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH); |
| 115 |
| 116 // Sometime misconfigured servers omit the zlib header, relying on clients |
| 117 // to splice it back in. |
| 118 if (ret < 0 && !zlib_header_added_) { |
| 119 zlib_header_added_ = true; |
| 120 if (!InsertZlibHeader()) |
| 121 return ERR_CONTENT_DECODING_FAILED; |
| 122 |
| 123 zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_buffer->data()); |
| 124 zlib_stream_.get()->avail_in = input_buffer->BytesRemaining(); |
| 125 zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data()); |
| 126 zlib_stream_.get()->avail_out = output_buffer_size; |
| 127 |
| 128 ret = inflate(zlib_stream_.get(), Z_NO_FLUSH); |
| 129 // TODO(xunjieli): add a histogram to see how often this happens. The |
| 130 // original bug for this behavior was ancient and maybe it doesn't happen |
| 131 // in the wild any more? |
| 132 } |
| 133 |
| 134 size_t bytes_used = |
| 135 input_buffer->BytesRemaining() - zlib_stream_.get()->avail_in; |
| 136 size_t bytes_out = output_buffer_size - zlib_stream_.get()->avail_out; |
| 137 |
| 138 input_buffer->DidConsume(bytes_used); |
| 139 |
| 140 if (ret != Z_STREAM_END && ret != Z_OK) |
| 141 return ERR_CONTENT_DECODING_FAILED; |
| 142 |
| 143 // The zlib stream can end before the input stream ends. If this happens, |
| 144 // |Decompress| will pass any further data on untouched. |
| 145 if (ret == Z_STREAM_END) { |
| 146 zlib_eof_ = true; |
| 147 return bytes_out + Passthrough(output_buffer->data() + bytes_out, |
| 148 output_buffer_size - bytes_out, |
| 149 input_buffer); |
| 150 } |
| 151 return bytes_out; |
| 152 } |
| 153 |
| 154 size_t GzipStreamSource::Passthrough(char* output_buffer, |
| 155 size_t output_buffer_size, |
| 156 DrainableIOBuffer* input_buffer) { |
| 157 SkipGzipFooterIfNeeded(input_buffer); |
| 158 size_t to_copy = input_buffer->BytesRemaining(); |
| 159 if (to_copy > output_buffer_size) |
| 160 to_copy = output_buffer_size; |
| 161 memcpy(output_buffer, input_buffer->data(), to_copy); |
| 162 input_buffer->DidConsume(to_copy); |
| 163 return to_copy; |
| 164 } |
| 165 |
| 166 bool GzipStreamSource::InsertZlibHeader() { |
| 167 char dummy_header[] = {0x78, 0x01}; |
| 168 char dummy_output[4]; |
| 169 |
| 170 inflateReset(zlib_stream_.get()); |
| 171 zlib_stream_.get()->next_in = bit_cast<Bytef*>(&dummy_header[0]); |
| 172 zlib_stream_.get()->avail_in = sizeof(dummy_header); |
| 173 zlib_stream_.get()->next_out = bit_cast<Bytef*>(&dummy_output[0]); |
| 174 zlib_stream_.get()->avail_out = sizeof(dummy_output); |
| 175 |
| 176 int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH); |
| 177 return ret == Z_OK; |
| 178 } |
| 179 |
| 180 bool GzipStreamSource::IsGzipHeaderInvalid(DrainableIOBuffer* input_buffer) { |
| 181 const size_t kGzipFooterBytes = 8; |
| 182 const char* end = nullptr; |
| 183 GZipHeader::Status status = gzip_header_.ReadMore( |
| 184 input_buffer->data(), input_buffer->BytesRemaining(), &end); |
| 185 if (status == GZipHeader::INCOMPLETE_HEADER) { |
| 186 input_buffer->DidConsume(input_buffer->BytesRemaining()); |
| 187 return false; |
| 188 } |
| 189 |
| 190 should_check_gzip_header_ = false; |
| 191 if (status == GZipHeader::COMPLETE_HEADER) { |
| 192 // If there is a valid header, there should also be a valid footer. |
| 193 gzip_footer_bytes_left_ = kGzipFooterBytes; |
| 194 input_buffer->DidConsume(end - input_buffer->data()); |
| 195 } |
| 196 |
| 197 return status == GZipHeader::INVALID_HEADER; |
| 198 } |
| 199 |
| 200 // Dumb heuristic. Gzip files always start with a two-byte magic value per RFC |
| 201 // 1952 2.3.1, so if the first byte isn't the first byte of the gzip magic, and |
| 202 // this filter is checking whether it should fallback, then fallback. |
| 203 bool GzipStreamSource::ShouldFallbackToPlain(DrainableIOBuffer* input_buffer) { |
| 204 static const char kGzipFirstByte = 0x1f; |
| 205 if (mode_ != GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK) |
| 206 return false; |
| 207 if (!should_check_gzip_header_) |
| 208 return false; |
| 209 if (input_buffer->BytesRemaining() == 0) |
| 210 return false; |
| 211 char d = input_buffer->data()[0]; |
| 212 return d != kGzipFirstByte; |
| 213 } |
| 214 |
| 215 void GzipStreamSource::SkipGzipFooterIfNeeded(DrainableIOBuffer* input_buffer) { |
| 216 if (gzip_footer_bytes_left_ == 0) |
| 217 return; |
| 218 size_t to_read = gzip_footer_bytes_left_; |
| 219 if (to_read > base::checked_cast<size_t>(input_buffer->BytesRemaining())) |
| 220 to_read = input_buffer->BytesRemaining(); |
| 221 input_buffer->DidConsume(to_read); |
| 222 gzip_footer_bytes_left_ -= to_read; |
| 223 } |
| 224 |
| 225 } // namespace net |
| OLD | NEW |