Evict resources from resource pool after timeout
[chromium-blink-merge.git] / net / filter / gzip_filter.h
blob2306c9b05fc8fd1dd946374ca0ae9f62057e5d34
1 // Copyright 2014 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.
5 // GZipFilter applies gzip and deflate content encoding/decoding to a data
6 // stream. As specified by HTTP 1.1, with gzip encoding the content is
7 // wrapped with a gzip header, and with deflate encoding the content is in
8 // a raw, headerless DEFLATE stream.
9 //
10 // Internally GZipFilter uses zlib inflate to do decoding.
12 // GZipFilter is a subclass of Filter. See the latter's header file filter.h
13 // for sample usage.
15 #ifndef NET_FILTER_GZIP_FILTER_H_
16 #define NET_FILTER_GZIP_FILTER_H_
18 #include "base/basictypes.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "net/filter/filter.h"
22 typedef struct z_stream_s z_stream;
24 namespace net {
26 class GZipHeader;
28 class GZipFilter : public Filter {
29 public:
30 ~GZipFilter() override;
32 // Initializes filter decoding mode and internal control blocks.
33 // Parameter filter_type specifies the type of filter, which corresponds to
34 // either gzip or deflate decoding. The function returns true if success and
35 // false otherwise.
36 // The filter can only be initialized once.
37 bool InitDecoding(Filter::FilterType filter_type);
39 // Decodes the pre-filter data and writes the output into the dest_buffer
40 // passed in.
41 // The function returns FilterStatus. See filter.h for its description.
43 // Upon entry, *dest_len is the total size (in number of chars) of the
44 // destination buffer. Upon exit, *dest_len is the actual number of chars
45 // written into the destination buffer.
47 // This function will fail if there is no pre-filter data in the
48 // stream_buffer_. On the other hand, *dest_len can be 0 upon successful
49 // return. For example, the internal zlib may process some pre-filter data
50 // but not produce output yet.
51 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override;
53 private:
54 enum DecodingStatus {
55 DECODING_UNINITIALIZED,
56 DECODING_IN_PROGRESS,
57 DECODING_DONE,
58 DECODING_ERROR
61 enum DecodingMode {
62 DECODE_MODE_GZIP,
63 DECODE_MODE_DEFLATE,
64 DECODE_MODE_UNKNOWN
67 enum GZipCheckHeaderState {
68 GZIP_CHECK_HEADER_IN_PROGRESS,
69 GZIP_GET_COMPLETE_HEADER,
70 GZIP_GET_INVALID_HEADER
73 static const int kGZipFooterSize = 8;
75 // Only to be instantiated by Filter::Factory.
76 explicit GZipFilter(FilterType type);
77 friend class Filter;
79 // Parses and verifies the GZip header.
80 // Upon exit, the function updates gzip_header_status_ accordingly.
82 // The function returns Filter::FILTER_OK if it gets a complete header and
83 // there are more data in the pre-filter buffer.
84 // The function returns Filter::FILTER_NEED_MORE_DATA if it parses all data
85 // in the pre-filter buffer, either getting a complete header or a partial
86 // header. The caller needs to check gzip_header_status_ and call this
87 // function again for partial header.
88 // The function returns Filter::FILTER_ERROR if error occurs.
89 FilterStatus CheckGZipHeader();
91 // Internal function to decode the pre-filter data and writes the output into
92 // the dest_buffer passed in.
94 // This is the internal version of ReadFilteredData. See the latter's
95 // comments for the use of function.
96 FilterStatus DoInflate(char* dest_buffer, int* dest_len);
98 // Inserts a zlib header to the data stream before calling zlib inflate.
99 // This is used to work around server bugs. See more comments at the place
100 // it is called in gzip_filter.cc.
101 // The function returns true on success and false otherwise.
102 bool InsertZlibHeader();
104 // Skip the 8 byte GZip footer after z_stream_end
105 void SkipGZipFooter();
107 // Tracks the status of decoding.
108 // This variable is initialized by InitDecoding and updated only by
109 // ReadFilteredData.
110 DecodingStatus decoding_status_;
112 // Indicates the type of content decoding the GZipFilter is performing.
113 // This variable is set only once by InitDecoding.
114 DecodingMode decoding_mode_;
116 // Used to parse the gzip header in gzip stream.
117 // It is used when the decoding_mode_ is DECODE_MODE_GZIP.
118 scoped_ptr<GZipHeader> gzip_header_;
120 // Tracks the progress of parsing gzip header.
121 // This variable is maintained by gzip_header_.
122 GZipCheckHeaderState gzip_header_status_;
124 // A flag used by InsertZlibHeader to record whether we've successfully added
125 // a zlib header to this stream.
126 bool zlib_header_added_;
128 // Tracks how many bytes of gzip footer have been received.
129 int gzip_footer_bytes_;
131 // The control block of zlib which actually does the decoding.
132 // This data structure is initialized by InitDecoding and updated only by
133 // DoInflate, with InsertZlibHeader being the exception as a workaround.
134 scoped_ptr<z_stream> zlib_stream_;
136 // For robustness, when we see the solo sdch filter, we chain in a gzip filter
137 // in front of it, with this flag to indicate that the gzip decoding might not
138 // be needed. This handles a strange case where "Content-Encoding: sdch,gzip"
139 // is reduced by an errant proxy to "Content-Encoding: sdch", while the
140 // content is indeed really gzipped result of sdch :-/.
141 // If this flag is set, then we will revert to being a pass through filter if
142 // we don't get a valid gzip header.
143 bool possible_sdch_pass_through_;
145 DISALLOW_COPY_AND_ASSIGN(GZipFilter);
148 } // namespace net
150 #endif // NET_FILTER_GZIP_FILTER_H__