Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / filter / gzip_filter.h
blob0ae284a187a751dd002cf66a15ec430708e33a91
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 virtual ~GZipFilter();
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 virtual FilterStatus ReadFilteredData(char* dest_buffer,
52 int* dest_len) OVERRIDE;
54 private:
55 enum DecodingStatus {
56 DECODING_UNINITIALIZED,
57 DECODING_IN_PROGRESS,
58 DECODING_DONE,
59 DECODING_ERROR
62 enum DecodingMode {
63 DECODE_MODE_GZIP,
64 DECODE_MODE_DEFLATE,
65 DECODE_MODE_UNKNOWN
68 enum GZipCheckHeaderState {
69 GZIP_CHECK_HEADER_IN_PROGRESS,
70 GZIP_GET_COMPLETE_HEADER,
71 GZIP_GET_INVALID_HEADER
74 static const int kGZipFooterSize = 8;
76 // Only to be instantiated by Filter::Factory.
77 GZipFilter();
78 friend class Filter;
80 // Parses and verifies the GZip header.
81 // Upon exit, the function updates gzip_header_status_ accordingly.
83 // The function returns Filter::FILTER_OK if it gets a complete header and
84 // there are more data in the pre-filter buffer.
85 // The function returns Filter::FILTER_NEED_MORE_DATA if it parses all data
86 // in the pre-filter buffer, either getting a complete header or a partial
87 // header. The caller needs to check gzip_header_status_ and call this
88 // function again for partial header.
89 // The function returns Filter::FILTER_ERROR if error occurs.
90 FilterStatus CheckGZipHeader();
92 // Internal function to decode the pre-filter data and writes the output into
93 // the dest_buffer passed in.
95 // This is the internal version of ReadFilteredData. See the latter's
96 // comments for the use of function.
97 FilterStatus DoInflate(char* dest_buffer, int* dest_len);
99 // Inserts a zlib header to the data stream before calling zlib inflate.
100 // This is used to work around server bugs. See more comments at the place
101 // it is called in gzip_filter.cc.
102 // The function returns true on success and false otherwise.
103 bool InsertZlibHeader();
105 // Skip the 8 byte GZip footer after z_stream_end
106 void SkipGZipFooter();
108 // Tracks the status of decoding.
109 // This variable is initialized by InitDecoding and updated only by
110 // ReadFilteredData.
111 DecodingStatus decoding_status_;
113 // Indicates the type of content decoding the GZipFilter is performing.
114 // This variable is set only once by InitDecoding.
115 DecodingMode decoding_mode_;
117 // Used to parse the gzip header in gzip stream.
118 // It is used when the decoding_mode_ is DECODE_MODE_GZIP.
119 scoped_ptr<GZipHeader> gzip_header_;
121 // Tracks the progress of parsing gzip header.
122 // This variable is maintained by gzip_header_.
123 GZipCheckHeaderState gzip_header_status_;
125 // A flag used by InsertZlibHeader to record whether we've successfully added
126 // a zlib header to this stream.
127 bool zlib_header_added_;
129 // Tracks how many bytes of gzip footer have been received.
130 int gzip_footer_bytes_;
132 // The control block of zlib which actually does the decoding.
133 // This data structure is initialized by InitDecoding and updated only by
134 // DoInflate, with InsertZlibHeader being the exception as a workaround.
135 scoped_ptr<z_stream> zlib_stream_;
137 // For robustness, when we see the solo sdch filter, we chain in a gzip filter
138 // in front of it, with this flag to indicate that the gzip decoding might not
139 // be needed. This handles a strange case where "Content-Encoding: sdch,gzip"
140 // is reduced by an errant proxy to "Content-Encoding: sdch", while the
141 // content is indeed really gzipped result of sdch :-/.
142 // If this flag is set, then we will revert to being a pass through filter if
143 // we don't get a valid gzip header.
144 bool possible_sdch_pass_through_;
146 DISALLOW_COPY_AND_ASSIGN(GZipFilter);
149 } // namespace net
151 #endif // NET_FILTER_GZIP_FILTER_H__