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 enum GzipSourceStreamMode { | |
33 GZIP_SOURCE_STREAM_DEFLATE, | |
34 GZIP_SOURCE_STREAM_GZIP, | |
35 // Same as GZIP_SOURCE_STREAM_GZIP, but allow fallback to plain text. | |
36 GZIP_SOURCE_STREAM_GZIP_WITH_FALLBACK, | |
37 }; | |
38 | |
39 ~GzipSourceStream() override; | |
40 | |
41 // Creates a GzipSourceStream. Return nullptr if initialization fails. | |
42 static std::unique_ptr<GzipSourceStream> Create( | |
43 std::unique_ptr<SourceStream> previous, | |
44 GzipSourceStreamMode mode); | |
45 | |
46 private: | |
47 GzipSourceStream(std::unique_ptr<SourceStream> previous, | |
Randy Smith (Not in Mondays)
2016/09/14 22:25:24
nit, suggestion: "previous -> upstream"?
xunjieli
2016/09/19 13:57:03
Done.
| |
48 GzipSourceStreamMode mode); | |
49 | |
50 // Returns true if initialization is successful, false otherwise. | |
Randy Smith (Not in Mondays)
2016/09/14 22:25:23
Under what circumstances can initialization fail?
xunjieli
2016/09/19 13:57:03
Done. I am not sure if it's a good idea to DCHECK
| |
51 bool Init(); | |
52 | |
53 // SourceStream implementation | |
54 std::string GetTypeAsString() const override; | |
55 int FilterData(IOBuffer* output_buffer, | |
56 int output_buffer_size, | |
57 IOBuffer* input_buffer, | |
58 int input_buffer_size, | |
59 int* consumed_bytes, | |
60 bool upstream_end_reached) override; | |
61 | |
62 // Synchronous decompressor. This function consumes bytes from |input_buffer| | |
63 // and decompresses them into |output_buffer| until it consumes all of | |
64 // |input_buffer|. This decompressor will decompress a zlib stream (either | |
65 // gzip or deflate) until the zlib reaches EOF, then will pass any further | |
66 // input through untouched. | |
67 int Decompress(IOBuffer* output_buffer, | |
68 int output_buffer_size, | |
69 char* input_buffer, | |
70 int input_buffer_size, | |
71 int* consumed_bytes); | |
72 | |
73 // Returns how many bytes are copied from |input_buffer| to |output_buffer|. | |
74 size_t Passthrough(char* output_buffer, | |
75 int output_buffer_size, | |
76 char* input_buffer, | |
77 int input_buffer_size, | |
78 int* consumed_bytes); | |
79 | |
80 // Inserts a zlib header to the data stream before calling zlib inflate. | |
81 // This is used to work around server bugs. | |
82 // The function returns true on success and false otherwise. | |
83 bool InsertZlibHeader(); | |
84 | |
85 // Returns whether an invalid GZip header has been observed. This method | |
86 // returns true as soon as an invalid GZip header is observed; it returns | |
87 // false if a complete, valid header has been observed, or not enough bytes | |
88 // have been seen to be sure. This method can be called multiple times with | |
89 // multiple |input_buffer|. The number of consumed bytes will be written to | |
90 // |consumed_bytes|. | |
91 bool IsGzipHeaderInvalid(char* input_buffer, | |
92 int input_buffer_size, | |
93 int* consumed_bytes); | |
94 | |
95 // Returns whether this stream looks like it could be plain text (ie, not | |
96 // actually gzipped). Right now this uses an extremely simple heuristic; see | |
97 // the source for details. This method checks the first byte of the stream. | |
98 bool ShouldFallbackToPlain(char first_byte); | |
99 | |
100 // Returns number of footer bytes to skip. | |
101 size_t NumGzipFooterBytesToSkip(int input_buffer_size); | |
102 | |
103 // Indicates the type of content decoding the stream source is performing. | |
104 // This variable is set only once by Init. | |
Randy Smith (Not in Mondays)
2016/09/14 22:25:23
nit: No longer set by Init, correct? And "const"
xunjieli
2016/09/19 13:57:03
Done.
| |
105 const GzipSourceStreamMode mode_; | |
106 | |
107 // The control block of zlib which actually does the decoding. | |
108 // This data structure is initialized by Init and updated only by | |
109 // Decompress, with InsertZlibHeader being the exception as a workaround. | |
110 std::unique_ptr<z_stream> zlib_stream_; | |
111 | |
112 // Whether end of stream has been reached. | |
113 bool zlib_eof_; | |
114 | |
115 // A flag used by Decompress to record whether we've successfully added | |
116 // a zlib header to this stream. | |
117 bool zlib_header_added_; | |
118 | |
119 // Used to parse the gzip header in gzip stream. | |
120 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. | |
121 GZipHeader gzip_header_; | |
122 | |
123 // Whether gzip header should be checked for validity. | |
124 bool should_check_gzip_header_; | |
125 | |
126 // Whether the first byte of the input stream should be checked. | |
127 bool should_check_first_byte_; | |
128 | |
129 // Tracks how many bytes of gzip footer are yet to be filtered. | |
130 size_t gzip_footer_bytes_left_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); | |
133 }; | |
134 | |
135 } // namespace net | |
136 | |
137 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ | |
OLD | NEW |