| OLD | NEW |
| 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 #ifndef NET_FILTER_GZIP_SOURCE_STREAM_H_ | 5 #ifndef NET_FILTER_GZIP_SOURCE_STREAM_H_ |
| 6 #define NET_FILTER_GZIP_SOURCE_STREAM_H_ | 6 #define NET_FILTER_GZIP_SOURCE_STREAM_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 15 #include "net/filter/filter_source_stream.h" | 13 #include "net/filter/filter_source_stream.h" |
| 16 #include "net/filter/gzip_header.h" | 14 #include "net/filter/gzip_header.h" |
| 17 | 15 |
| 18 typedef struct z_stream_s z_stream; | 16 typedef struct z_stream_s z_stream; |
| 19 | 17 |
| 20 namespace net { | 18 namespace net { |
| 21 | 19 |
| 22 class IOBuffer; | 20 class IOBuffer; |
| 23 | 21 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 37 std::unique_ptr<SourceStream> previous, | 35 std::unique_ptr<SourceStream> previous, |
| 38 SourceStream::SourceType type); | 36 SourceStream::SourceType type); |
| 39 | 37 |
| 40 private: | 38 private: |
| 41 enum InputState { | 39 enum InputState { |
| 42 // Starts processing the input stream. Checks whether the stream is valid | 40 // Starts processing the input stream. Checks whether the stream is valid |
| 43 // and whether a fallback to plain data is needed. | 41 // and whether a fallback to plain data is needed. |
| 44 STATE_START, | 42 STATE_START, |
| 45 // Gzip header of the input stream is being processed. | 43 // Gzip header of the input stream is being processed. |
| 46 STATE_GZIP_HEADER, | 44 STATE_GZIP_HEADER, |
| 45 // Deflate responses may or may not have a zlib header. In this state until |
| 46 // enough has been inflated that this stream most likely has a zlib header, |
| 47 // or until a zlib header has been added. Data is appended to |replay_data_| |
| 48 // in case it needs to be replayed after adding a header. |
| 49 STATE_SNIFFING_DEFLATE_HEADER, |
| 50 // If a zlib header has to be added to the response, this state will replay |
| 51 // data passed to inflate before it was determined that no zlib header was |
| 52 // present. |
| 53 STATE_REPLAY_DATA, |
| 47 // The input stream is being decoded. | 54 // The input stream is being decoded. |
| 48 STATE_COMPRESSED_BODY, | 55 STATE_COMPRESSED_BODY, |
| 49 // Gzip footer of the input stream is being processed. | 56 // Gzip footer of the input stream is being processed. |
| 50 STATE_GZIP_FOOTER, | 57 STATE_GZIP_FOOTER, |
| 51 // The input stream is being passed through undecoded. | 58 // The input stream is being passed through undecoded. |
| 52 STATE_UNCOMPRESSED_BODY, | 59 STATE_UNCOMPRESSED_BODY, |
| 53 }; | 60 }; |
| 54 | 61 |
| 55 GzipSourceStream(std::unique_ptr<SourceStream> previous, | 62 GzipSourceStream(std::unique_ptr<SourceStream> previous, |
| 56 SourceStream::SourceType type); | 63 SourceStream::SourceType type); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 77 // Returns whether this stream looks like it could be plain text (ie, not | 84 // Returns whether this stream looks like it could be plain text (ie, not |
| 78 // actually gzipped). Right now this uses an extremely simple heuristic; see | 85 // actually gzipped). Right now this uses an extremely simple heuristic; see |
| 79 // the source for details. This method checks the first byte of the stream. | 86 // the source for details. This method checks the first byte of the stream. |
| 80 bool ShouldFallbackToPlain(char first_byte); | 87 bool ShouldFallbackToPlain(char first_byte); |
| 81 | 88 |
| 82 // The control block of zlib which actually does the decoding. | 89 // The control block of zlib which actually does the decoding. |
| 83 // This data structure is initialized by Init and updated only by | 90 // This data structure is initialized by Init and updated only by |
| 84 // FilterData(), with InsertZlibHeader() being the exception as a workaround. | 91 // FilterData(), with InsertZlibHeader() being the exception as a workaround. |
| 85 std::unique_ptr<z_stream> zlib_stream_; | 92 std::unique_ptr<z_stream> zlib_stream_; |
| 86 | 93 |
| 87 // A flag used by FilterData() to record whether we've successfully added | 94 // While in STATE_SNIFFING_DEFLATE_HEADER, it may be determined that a zlib |
| 88 // a zlib header to this stream. | 95 // header needs to be added, and all received data needs to be replayed. In |
| 89 bool zlib_header_added_; | 96 // that case, this buffer holds the data to be replayed. |
| 97 std::string replay_data_; |
| 90 | 98 |
| 91 // Used to parse the gzip header in gzip stream. | 99 // Used to parse the gzip header in gzip stream. |
| 92 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. | 100 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. |
| 93 GZipHeader gzip_header_; | 101 GZipHeader gzip_header_; |
| 94 | 102 |
| 95 // Tracks how many bytes of gzip footer are yet to be filtered. | 103 // Tracks how many bytes of gzip footer are yet to be filtered. |
| 96 size_t gzip_footer_bytes_left_; | 104 size_t gzip_footer_bytes_left_; |
| 97 | 105 |
| 98 // Tracks the state of the input stream. | 106 // Tracks the state of the input stream. |
| 99 InputState input_state_; | 107 InputState input_state_; |
| 100 | 108 |
| 109 // Used when replaying data. |
| 110 InputState replay_state_; |
| 111 |
| 101 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); | 112 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); |
| 102 }; | 113 }; |
| 103 | 114 |
| 104 } // namespace net | 115 } // namespace net |
| 105 | 116 |
| 106 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ | 117 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ |
| OLD | NEW |