| 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 #ifndef NET_FILTER_GZIP_STREAM_SOURCE_H_ |
| 6 #define NET_FILTER_GZIP_STREAM_SOURCE_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "net/filter/filter_stream_source.h" |
| 11 #include "net/filter/gzip_header.h" |
| 12 |
| 13 typedef struct z_stream_s z_stream; |
| 14 |
| 15 namespace net { |
| 16 |
| 17 class BlockBuffer; |
| 18 |
| 19 class GzipStreamSource : public FilterStreamSource { |
| 20 public: |
| 21 enum GzipStreamSourceMode { |
| 22 GZIP_STREAM_SOURCE_DEFLATE, |
| 23 GZIP_STREAM_SOURCE_GZIP, |
| 24 // Same as GZIP_STREAM_SOURCE_GZIP, but allow fallback to plain text. |
| 25 GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK, |
| 26 }; |
| 27 |
| 28 explicit GzipStreamSource(std::unique_ptr<StreamSource> previous, |
| 29 GzipStreamSourceMode mode); |
| 30 ~GzipStreamSource() override; |
| 31 |
| 32 bool Init() override; |
| 33 |
| 34 private: |
| 35 enum GzipStreamState { |
| 36 GZIP_STREAM_ERROR, |
| 37 GZIP_STREAM_MORE_INPUT, |
| 38 GZIP_STREAM_MORE_OUTPUT_SPACE, |
| 39 }; |
| 40 |
| 41 // StreamSource implementation |
| 42 Error ReadInternal(IOBuffer* dest_buffer, |
| 43 size_t buffer_size, |
| 44 size_t* bytes_read) override; |
| 45 |
| 46 GzipStreamState Decompress(IOBuffer* dest_buffer, |
| 47 size_t buffer_size, |
| 48 size_t* bytes_read); |
| 49 |
| 50 GzipStreamState Passthrough(IOBuffer* dest_buffer, |
| 51 size_t buffer_size, |
| 52 size_t* bytes_read); |
| 53 |
| 54 void OnReadComplete(const OnReadCompleteCallback& callback, |
| 55 IOBuffer* dest_buffer, |
| 56 size_t dest_buffer_size, |
| 57 Error error, |
| 58 size_t bytes_read); |
| 59 |
| 60 bool InsertZlibHeader(); |
| 61 |
| 62 // Returns whether an invalid GZip header has been observed. This method |
| 63 // returns true as soon as an invalid GZip header is observed; it returns |
| 64 // false if a complete, valid header has been observed, or not enough bytes |
| 65 // have been seen to be sure. This method pulls its input from |buffer_|. |
| 66 bool IsGzipHeaderInvalid(); |
| 67 |
| 68 // Returns whether this stream looks like it could be plain text (ie, not |
| 69 // actually gzipped). Right now this uses an extremely simple heuristic; see |
| 70 // the source for details. This method checks the input in |buffer_|, but does |
| 71 // not drain from it. |
| 72 bool ShouldFallbackToPlain(); |
| 73 |
| 74 // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if |
| 75 // there are still footer bytes to read. |
| 76 void SkipGzipFooterIfNeeded(); |
| 77 |
| 78 scoped_refptr<IOBuffer> pending_read_buffer_; |
| 79 |
| 80 // Indicates the type of content decoding the stream source is performing. |
| 81 // This variable is set only once by Init. |
| 82 GzipStreamSourceMode mode_; |
| 83 // The control block of zlib which actually does the decoding. |
| 84 // This data structure is initialized by Init and updated only by |
| 85 // Decompress, with InsertZlibHeader being the exception as a workaround. |
| 86 std::unique_ptr<z_stream> zlib_stream_; |
| 87 // Whether end of stream has been reached. |
| 88 bool zlib_eof_; |
| 89 // A flag used by Decompress to record whether we've successfully added |
| 90 // a zlib header to this stream. |
| 91 bool zlib_header_added_; |
| 92 // Used to parse the gzip header in gzip stream. |
| 93 // It is used when the decoding mode is GZIP_STREAM_SOURCE_GZIP. |
| 94 GZipHeader gzip_header_; |
| 95 // Whether gzip header should be checked for validity. |
| 96 bool should_check_gzip_header_; |
| 97 // Tracks how many bytes of gzip footer are yet to be filtered. |
| 98 size_t gzip_footer_bytes_left_; |
| 99 }; |
| 100 |
| 101 } // namespace net |
| 102 |
| 103 #endif // NET_FILTER_GZIP_STREAM_SOURCE_H__ |
| OLD | NEW |