| 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 // See https://crbug.com/677001 |
| 54 STATE_REPLAY_DATA, |
| 47 // The input stream is being decoded. | 55 // The input stream is being decoded. |
| 48 STATE_COMPRESSED_BODY, | 56 STATE_COMPRESSED_BODY, |
| 49 // Gzip footer of the input stream is being processed. | 57 // Gzip footer of the input stream is being processed. |
| 50 STATE_GZIP_FOOTER, | 58 STATE_GZIP_FOOTER, |
| 51 // The input stream is being passed through undecoded. | 59 // The input stream is being passed through undecoded. |
| 52 STATE_UNCOMPRESSED_BODY, | 60 STATE_UNCOMPRESSED_BODY, |
| 53 }; | 61 }; |
| 54 | 62 |
| 55 GzipSourceStream(std::unique_ptr<SourceStream> previous, | 63 GzipSourceStream(std::unique_ptr<SourceStream> previous, |
| 56 SourceStream::SourceType type); | 64 SourceStream::SourceType type); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 77 // Returns whether this stream looks like it could be plain text (ie, not | 85 // 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 | 86 // 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. | 87 // the source for details. This method checks the first byte of the stream. |
| 80 bool ShouldFallbackToPlain(char first_byte); | 88 bool ShouldFallbackToPlain(char first_byte); |
| 81 | 89 |
| 82 // The control block of zlib which actually does the decoding. | 90 // The control block of zlib which actually does the decoding. |
| 83 // This data structure is initialized by Init and updated only by | 91 // This data structure is initialized by Init and updated only by |
| 84 // FilterData(), with InsertZlibHeader() being the exception as a workaround. | 92 // FilterData(), with InsertZlibHeader() being the exception as a workaround. |
| 85 std::unique_ptr<z_stream> zlib_stream_; | 93 std::unique_ptr<z_stream> zlib_stream_; |
| 86 | 94 |
| 87 // A flag used by FilterData() to record whether we've successfully added | 95 // While in STATE_SNIFFING_DEFLATE_HEADER, it may be determined that a zlib |
| 88 // a zlib header to this stream. | 96 // header needs to be added, and all received data needs to be replayed. In |
| 89 bool zlib_header_added_; | 97 // that case, this buffer holds the data to be replayed. |
| 98 std::string replay_data_; |
| 90 | 99 |
| 91 // Used to parse the gzip header in gzip stream. | 100 // Used to parse the gzip header in gzip stream. |
| 92 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. | 101 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. |
| 93 GZipHeader gzip_header_; | 102 GZipHeader gzip_header_; |
| 94 | 103 |
| 95 // Tracks how many bytes of gzip footer are yet to be filtered. | 104 // Tracks how many bytes of gzip footer are yet to be filtered. |
| 96 size_t gzip_footer_bytes_left_; | 105 size_t gzip_footer_bytes_left_; |
| 97 | 106 |
| 98 // Tracks the state of the input stream. | 107 // Tracks the state of the input stream. |
| 99 InputState input_state_; | 108 InputState input_state_; |
| 100 | 109 |
| 110 // Used when replaying data. |
| 111 InputState replay_state_; |
| 112 |
| 101 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); | 113 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); |
| 102 }; | 114 }; |
| 103 | 115 |
| 104 } // namespace net | 116 } // namespace net |
| 105 | 117 |
| 106 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ | 118 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ |
| OLD | NEW |