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, | |
48 GzipSourceStreamMode mode); | |
49 bool Init(); | |
Randy Smith (Not in Mondays)
2016/09/12 20:54:01
Documentation as to the return value of Init()?
xunjieli
2016/09/14 16:44:01
Done.
| |
50 | |
51 // SourceStream implementation | |
52 std::string GetTypeAsString() const override; | |
53 int FilterData(IOBuffer* output_buffer, | |
54 int output_buffer_size, | |
55 DrainableIOBuffer* input_buffer) override; | |
56 | |
57 // Synchronous decompressor. This function consumes bytes from |input_buffer| | |
58 // and decompresses them into |output_buffer| until it consumes all of | |
59 // |input_buffer|. This decompressor will decompress a zlib stream (either | |
60 // gzip or deflate) until the zlib reaches EOF, then will pass any further | |
61 // input through untouched. | |
62 int Decompress(IOBuffer* output_buffer, | |
63 size_t output_buffer_size, | |
64 DrainableIOBuffer* input_buffer); | |
65 | |
66 // Returns how many bytes are drained from |input_buffer|. | |
Randy Smith (Not in Mondays)
2016/09/12 20:54:01
I don't understand based on the comment what this
xunjieli
2016/09/14 16:44:01
Done.
Randy Smith (Not in Mondays)
2016/09/14 22:25:23
Not quite what I meant. The documentation is stil
xunjieli
2016/09/19 13:57:02
Done. It additionally drains footer bytes from the
| |
67 size_t Passthrough(char* output_buffer, | |
68 size_t output_buffer_size, | |
69 DrainableIOBuffer* input_buffer); | |
70 | |
71 // Inserts a zlib header to the data stream before calling zlib inflate. | |
72 // This is used to work around server bugs. | |
73 // The function returns true on success and false otherwise. | |
74 bool InsertZlibHeader(); | |
75 | |
76 // Returns whether an invalid GZip header has been observed. This method | |
77 // returns true as soon as an invalid GZip header is observed; it returns | |
78 // false if a complete, valid header has been observed, or not enough bytes | |
79 // have been seen to be sure. This method pulls its input from |buffer_|. | |
Randy Smith (Not in Mondays)
2016/09/12 20:54:01
I presume this can be called multiple times with m
xunjieli
2016/09/14 16:44:01
Done. Yes, I believe you are right. I will add thi
| |
80 bool IsGzipHeaderInvalid(DrainableIOBuffer* input_buffer); | |
81 | |
82 // Returns whether this stream looks like it could be plain text (ie, not | |
83 // actually gzipped). Right now this uses an extremely simple heuristic; see | |
84 // the source for details. This method checks the input in |buffer_|, but does | |
85 // not drain from it. | |
86 bool ShouldFallbackToPlain(DrainableIOBuffer* input_buffer); | |
87 | |
88 // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if | |
89 // there are still footer bytes to read. | |
90 void SkipGzipFooterIfNeeded(DrainableIOBuffer* input_buffer); | |
91 | |
92 // Indicates the type of content decoding the stream source is performing. | |
93 // This variable is set only once by Init. | |
94 GzipSourceStreamMode mode_; | |
Randy Smith (Not in Mondays)
2016/09/12 20:54:01
It looks to me as if this is set in the constructo
Randy Smith (Not in Mondays)
2016/09/12 20:54:01
nit, suggestion: I prefer whitespace before explan
xunjieli
2016/09/14 16:44:01
Done.
xunjieli
2016/09/14 16:44:01
Done.
| |
95 // The control block of zlib which actually does the decoding. | |
96 // This data structure is initialized by Init and updated only by | |
97 // Decompress, with InsertZlibHeader being the exception as a workaround. | |
98 std::unique_ptr<z_stream> zlib_stream_; | |
99 // Whether end of stream has been reached. | |
100 bool zlib_eof_; | |
101 // A flag used by Decompress to record whether we've successfully added | |
102 // a zlib header to this stream. | |
103 bool zlib_header_added_; | |
104 // Used to parse the gzip header in gzip stream. | |
105 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP. | |
106 GZipHeader gzip_header_; | |
107 // Whether gzip header should be checked for validity. | |
108 bool should_check_gzip_header_; | |
109 // Tracks how many bytes of gzip footer are yet to be filtered. | |
110 size_t gzip_footer_bytes_left_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream); | |
113 }; | |
114 | |
115 } // namespace net | |
116 | |
117 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__ | |
OLD | NEW |