Chromium Code Reviews| 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_SOURCE_STREAM_H_ | |
| 6 #define NET_FILTER_GZIP_SOURCE_STREAM_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "net/base/net_export.h" | |
| 14 #include "net/filter/filter_source_stream.h" | |
| 15 #include "net/filter/gzip_header.h" | |
| 16 | |
| 17 typedef struct z_stream_s z_stream; | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 class IOBuffer; | |
| 22 | |
| 23 // GZipSourceStream applies gzip and deflate content encoding/decoding to a data | |
| 24 // stream. As specified by HTTP 1.1, with gzip encoding the content is | |
| 25 // wrapped with a gzip header, and with deflate encoding the content is in | |
| 26 // a raw, headerless DEFLATE stream. | |
| 27 // | |
| 28 // Internally GZipSourceStream uses zlib inflate to do decoding. | |
| 29 // | |
| 30 class NET_EXPORT_PRIVATE GzipSourceStream : public FilterSourceStream { | |
| 31 public: | |
| 32 ~GzipSourceStream() override; | |
| 33 | |
| 34 // Creates a GzipSourceStream. Return nullptr if initialization fails. | |
| 35 static std::unique_ptr<GzipSourceStream> Create( | |
| 36 std::unique_ptr<SourceStream> previous, | |
| 37 SourceStream::SourceType type); | |
| 38 | |
| 39 private: | |
| 40 enum InputState { | |
| 41 // Starts processing the input stream. Checks whether the stream is valid | |
| 42 // and whether a fallback to plain data is needed. | |
| 43 STATE_START, | |
| 44 // Gzip header of the input stream is being processed. | |
| 45 STATE_GZIP_HEADER, | |
| 46 // The input stream is being decoded. | |
| 47 STATE_COMPRESSED_BODY, | |
| 48 // The input stream is being passed through undecoded. | |
| 49 STATE_UNCOMPRESSED_BODY, | |
| 50 }; | |
| 51 | |
| 52 GzipSourceStream(std::unique_ptr<SourceStream> previous, | |
| 53 SourceStream::SourceType type); | |
| 54 | |
| 55 // Returns true if initialization is successful, false otherwise. | |
| 56 // For instance, this method returns false if there is not enough memory or | |
| 57 // if there is a version mismatch. | |
| 58 bool Init(); | |
| 59 | |
| 60 // SourceStream implementation | |
| 61 std::string GetTypeAsString() const override; | |
| 62 int FilterData(IOBuffer* output_buffer, | |
| 63 int output_buffer_size, | |
| 64 IOBuffer* input_buffer, | |
| 65 int input_buffer_size, | |
| 66 int* consumed_bytes, | |
| 67 bool upstream_end_reached) override; | |
| 68 | |
| 69 // Synchronous decompressor. This function consumes bytes from |input_buffer| | |
| 70 // and decompresses them into |output_buffer| until it consumes all of | |
| 71 // |input_buffer|. This decompressor will decompress a zlib stream (either | |
|
Randy Smith (Not in Mondays)
2016/09/21 20:57:06
Presumably it will also stop when it's filled |*ou
xunjieli
2016/09/22 17:20:49
Done. Added to the doc.
| |
| 72 // gzip or deflate) until the zlib reaches EOF, then will pass any further | |
| 73 // input through untouched. | |
| 74 int Decompress(IOBuffer* output_buffer, | |
| 75 int output_buffer_size, | |
| 76 char* input_buffer, | |
| 77 int input_buffer_size, | |
| 78 int* consumed_bytes); | |
| 79 | |
| 80 // Passes through as many bytes as possible from from |input_buffer| to | |
| 81 // |output_buffer|. It might additionally drains footer bytes from the | |
| 82 // |input_buffer| without writing the footer bytes to |output_buffer|. The | |
|
Randy Smith (Not in Mondays)
2016/09/21 20:57:06
I don't find this documentation complete; can this
xunjieli
2016/09/22 17:20:49
Done. I have inlined this method. After applying y
| |
| 83 // total number of consumed bytes from |input_buffer| will be written to | |
| 84 // |consumed_bytes|. | |
| 85 size_t Passthrough(char* output_buffer, | |
| 86 int output_buffer_size, | |
| 87 char* input_buffer, | |
| 88 int input_buffer_size, | |
| 89 int* consumed_bytes); | |
| 90 | |
| 91 // Inserts a zlib header to the data stream before calling zlib inflate. | |
| 92 // This is used to work around server bugs. | |
| 93 // The function returns true on success and false otherwise. | |
|
Randy Smith (Not in Mondays)
2016/09/21 20:57:06
nit, suggestion: I don't think the comments needs
xunjieli
2016/09/22 17:20:49
Done.
| |
| 94 bool InsertZlibHeader(); | |
| 95 | |
| 96 // Returns whether an invalid GZip header has been observed. This method | |
| 97 // returns true as soon as an invalid GZip header is observed; it returns | |
| 98 // false if a complete, valid header has been observed, or not enough bytes | |
| 99 // have been seen to be sure. This method can be called multiple times with | |
| 100 // multiple |input_buffer|. The number of consumed bytes will be written to | |
| 101 // |consumed_bytes|. | |
| 102 bool IsGzipHeaderInvalid(char* input_buffer, | |
| 103 int input_buffer_size, | |
| 104 int* consumed_bytes); | |
|
Randy Smith (Not in Mondays)
2016/09/21 20:57:06
I don't see this function in the source file anymo
xunjieli
2016/09/22 17:20:49
Done.
| |
| 105 | |
| 106 // Returns whether this stream looks like it could be plain text (ie, not | |
| 107 // actually gzipped). Right now this uses an extremely simple heuristic; see | |
| 108 // the source for details. This method checks the first byte of the stream. | |
| 109 bool ShouldFallbackToPlain(char first_byte); | |
| 110 | |
| 111 // Returns number of footer bytes to skip. | |
| 112 size_t NumGzipFooterBytesToSkip(int input_buffer_size); | |
| 113 | |
| 114 // The control block of zlib which actually does the decoding. | |
| 115 // This data structure is initialized by Init and updated only by | |
| 116 // Decompress, with InsertZlibHeader being the exception as a workaround. | |
| 117 std::unique_ptr<z_stream> zlib_stream_; | |
| 118 | |
| 119 // A flag used by Decompress to record whether we've successfully added | |
| 120 // a zlib header to this stream. | |
| 121 bool zlib_header_added_; | |
| 122 | |
| 123 // Used to parse the gzip header in gzip stream. | |
| 124 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. | |
| 125 GZipHeader gzip_header_; | |
| 126 | |
| 127 // Tracks how many bytes of gzip footer are yet to be filtered. | |
| 128 size_t gzip_footer_bytes_left_; | |
| 129 | |
| 130 // Tracks the state of the input stream. | |
| 131 InputState input_state_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); | |
| 134 }; | |
| 135 | |
| 136 } // namespace net | |
| 137 | |
| 138 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ | |
| OLD | NEW |