OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Filter performs filtering on data streams. Sample usage: | 5 // Filter performs filtering on data streams. Sample usage: |
6 // | 6 // |
7 // IStream* pre_filter_source; | 7 // IStream* pre_filter_source; |
8 // ... | 8 // ... |
9 // Filter* filter = Filter::Factory(filter_type, size); | 9 // Filter* filter = Filter::Factory(filter_type, size); |
10 // int pre_filter_data_len = filter->stream_buffer_size(); | 10 // int pre_filter_data_len = filter->stream_buffer_size(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // filtering. At this point, the caller is free to reuse the filter | 48 // filtering. At this point, the caller is free to reuse the filter |
49 // buffer to provide more data. | 49 // buffer to provide more data. |
50 FILTER_NEED_MORE_DATA, | 50 FILTER_NEED_MORE_DATA, |
51 // Read filtered data successfully, and filter reaches the end of the data | 51 // Read filtered data successfully, and filter reaches the end of the data |
52 // stream. | 52 // stream. |
53 FILTER_DONE, | 53 FILTER_DONE, |
54 // There is an error during filtering. | 54 // There is an error during filtering. |
55 FILTER_ERROR | 55 FILTER_ERROR |
56 }; | 56 }; |
57 | 57 |
| 58 // Specifies type of filters that can be created. |
| 59 enum FilterType { |
| 60 FILTER_TYPE_DEFLATE, |
| 61 FILTER_TYPE_GZIP, |
| 62 FILTER_TYPE_BZIP2, |
| 63 FILTER_TYPE_GZIP_HELPING_SDCH, |
| 64 FILTER_TYPE_SDCH, |
| 65 FILTER_TYPE_UNSUPPORTED, |
| 66 }; |
| 67 |
| 68 |
58 virtual ~Filter(); | 69 virtual ~Filter(); |
59 | 70 |
60 // Creates a Filter object. | 71 // Creates a Filter object. |
61 // Parameters: Filter_types specifies the type of filter created; Buffer_size | 72 // Parameters: Filter_types specifies the type of filter created; Buffer_size |
62 // specifies the size (in number of chars) of the buffer the filter should | 73 // specifies the size (in number of chars) of the buffer the filter should |
63 // allocate to hold pre-filter data. | 74 // allocate to hold pre-filter data. |
64 // If success, the function returns the pointer to the Filter object created. | 75 // If success, the function returns the pointer to the Filter object created. |
65 // If failed or a filter is not needed, the function returns NULL. | 76 // If failed or a filter is not needed, the function returns NULL. |
66 // | 77 // |
67 // Note: filter_types is an array of filter names (content encoding types as | 78 // Note: filter_types is an array of filter names (content encoding types as |
68 // provided in an HTTP header), which will be chained together serially do | 79 // provided in an HTTP header), which will be chained together serially do |
69 // successive filtering of data. The names in the vector are ordered based on | 80 // successive filtering of data. The names in the vector are ordered based on |
70 // encoding order, and the filters are chained to operate in the reverse | 81 // encoding order, and the filters are chained to operate in the reverse |
71 // (decoding) order. For example, types[0] = "sdch", types[1] = "gzip" will | 82 // (decoding) order. For example, types[0] = "sdch", types[1] = "gzip" will |
72 // cause data to first be gunizip filtered, and the resulting output from that | 83 // cause data to first be gunizip filtered, and the resulting output from that |
73 // filter will be sdch decoded. | 84 // filter will be sdch decoded. |
74 static Filter* Factory(const std::vector<std::string>& filter_types, | 85 static Filter* Factory(const std::vector<FilterType>& filter_types, |
75 const std::string& mime_type, | |
76 int buffer_size); | 86 int buffer_size); |
77 | 87 |
78 // External call to obtain data from this filter chain. If ther is no | 88 // External call to obtain data from this filter chain. If ther is no |
79 // next_filter_, then it obtains data from this specific filter. | 89 // next_filter_, then it obtains data from this specific filter. |
80 FilterStatus ReadData(char* dest_buffer, int* dest_len); | 90 FilterStatus ReadData(char* dest_buffer, int* dest_len); |
81 | 91 |
82 // Returns a pointer to the beginning of stream_buffer_. | 92 // Returns a pointer to the beginning of stream_buffer_. |
83 char* stream_buffer() const { return stream_buffer_.get(); } | 93 char* stream_buffer() const { return stream_buffer_.get(); } |
84 | 94 |
85 // Returns the maximum size of stream_buffer_ in number of chars. | 95 // Returns the maximum size of stream_buffer_ in number of chars. |
(...skipping 14 matching lines...) Expand all Loading... |
100 // out by ReadFilteredData. | 110 // out by ReadFilteredData. |
101 // | 111 // |
102 // The input stream_data_len is the length (in number of chars) of valid | 112 // The input stream_data_len is the length (in number of chars) of valid |
103 // data in stream_buffer_. It can not be greater than stream_buffer_size_. | 113 // data in stream_buffer_. It can not be greater than stream_buffer_size_. |
104 // The function returns true if success, and false otherwise. | 114 // The function returns true if success, and false otherwise. |
105 bool FlushStreamBuffer(int stream_data_len); | 115 bool FlushStreamBuffer(int stream_data_len); |
106 | 116 |
107 void SetURL(const GURL& url); | 117 void SetURL(const GURL& url); |
108 const GURL& url() const { return url_; } | 118 const GURL& url() const { return url_; } |
109 | 119 |
110 void SetMimeType(std::string& mime_type); | 120 void SetMimeType(const std::string& mime_type); |
111 const std::string& mime_type() const { return mime_type_; } | 121 const std::string& mime_type() const { return mime_type_; } |
112 | 122 |
| 123 // Translate the text of a filter name (from Content-Encoding header) into a |
| 124 // FilterType. |
| 125 static FilterType ConvertEncodingToType(const std::string& filter_type); |
| 126 |
| 127 // Given a array of encoding_types, try to do some error recovery adjustment |
| 128 // to the list. This includes handling known bugs in the Apache server (where |
| 129 // redundant gzip encoding is specified), as well as issues regarding SDCH |
| 130 // encoding, where various proxies and anti-virus products modify or strip the |
| 131 // encodings. These fixups require context, which includes whether this |
| 132 // response was made to an SDCH request (i.e., an available dictionary was |
| 133 // advertised in the GET), as well as the mime type of the content. |
| 134 static void FixupEncodingTypes(bool is_sdch_response, |
| 135 const std::string& mime_type, |
| 136 std::vector<FilterType>* encoding_types); |
113 protected: | 137 protected: |
114 // Specifies type of filters that can be created. | |
115 enum FilterType { | |
116 FILTER_TYPE_DEFLATE, | |
117 FILTER_TYPE_GZIP, | |
118 FILTER_TYPE_BZIP2, | |
119 FILTER_TYPE_GZIP_HELPING_SDCH, | |
120 FILTER_TYPE_SDCH, // open-vcdiff compression relative to a dictionary. | |
121 FILTER_TYPE_UNSUPPORTED | |
122 }; | |
123 | |
124 Filter(); | 138 Filter(); |
125 | 139 |
126 FRIEND_TEST(SdchFilterTest, ContentTypeId); | 140 FRIEND_TEST(SdchFilterTest, ContentTypeId); |
127 // Filters the data stored in stream_buffer_ and writes the output into the | 141 // Filters the data stored in stream_buffer_ and writes the output into the |
128 // dest_buffer passed in. | 142 // dest_buffer passed in. |
129 // | 143 // |
130 // Upon entry, *dest_len is the total size (in number of chars) of the | 144 // Upon entry, *dest_len is the total size (in number of chars) of the |
131 // destination buffer. Upon exit, *dest_len is the actual number of chars | 145 // destination buffer. Upon exit, *dest_len is the actual number of chars |
132 // written into the destination buffer. | 146 // written into the destination buffer. |
133 // | 147 // |
134 // This function will fail if there is no pre-filter data in the | 148 // This function will fail if there is no pre-filter data in the |
135 // stream_buffer_. On the other hand, *dest_len can be 0 upon successful | 149 // stream_buffer_. On the other hand, *dest_len can be 0 upon successful |
136 // return. For example, a decoding filter may process some pre-filter data | 150 // return. For example, a decoding filter may process some pre-filter data |
137 // but not produce output yet. | 151 // but not produce output yet. |
138 virtual FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len); | 152 virtual FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len); |
139 | 153 |
140 // Copy pre-filter data directly to destination buffer without decoding. | 154 // Copy pre-filter data directly to destination buffer without decoding. |
141 FilterStatus CopyOut(char* dest_buffer, int* dest_len); | 155 FilterStatus CopyOut(char* dest_buffer, int* dest_len); |
142 | 156 |
143 // Allocates and initializes stream_buffer_. | 157 // Allocates and initializes stream_buffer_. |
144 // Buffer_size is the maximum size of stream_buffer_ in number of chars. | 158 // Buffer_size is the maximum size of stream_buffer_ in number of chars. |
145 bool InitBuffer(int buffer_size); | 159 bool InitBuffer(int buffer_size); |
146 | 160 |
147 // Translate the text of a filter name (from Content-Encoding header) into a | |
148 // FilterType, in the context of a mime type. | |
149 static FilterType ConvertEncodingToType(const std::string& filter_type, | |
150 const std::string& mime_type); | |
151 | |
152 // A factory helper for creating filters for within a chain of potentially | 161 // A factory helper for creating filters for within a chain of potentially |
153 // multiple encodings. If a chain of filters is created, then this may be | 162 // multiple encodings. If a chain of filters is created, then this may be |
154 // called multiple times during the filter creation process. In most simple | 163 // called multiple times during the filter creation process. In most simple |
155 // cases, this is only called once. Returns NULL and cleans up (deleting | 164 // cases, this is only called once. Returns NULL and cleans up (deleting |
156 // filter_list) if a new filter can't be constructed. | 165 // filter_list) if a new filter can't be constructed. |
157 static Filter* PrependNewFilter(FilterType type_id, int buffer_size, | 166 static Filter* PrependNewFilter(FilterType type_id, int buffer_size, |
158 Filter* filter_list); | 167 Filter* filter_list); |
159 | 168 |
160 FilterStatus last_status() const { return last_status_; } | 169 FilterStatus last_status() const { return last_status_; } |
161 | 170 |
(...skipping 23 matching lines...) Expand all Loading... |
185 scoped_ptr<Filter> next_filter_; | 194 scoped_ptr<Filter> next_filter_; |
186 // Remember what status or local filter last returned so we can better handle | 195 // Remember what status or local filter last returned so we can better handle |
187 // chained filters. | 196 // chained filters. |
188 FilterStatus last_status_; | 197 FilterStatus last_status_; |
189 | 198 |
190 DISALLOW_COPY_AND_ASSIGN(Filter); | 199 DISALLOW_COPY_AND_ASSIGN(Filter); |
191 }; | 200 }; |
192 | 201 |
193 #endif // NET_BASE_FILTER_H__ | 202 #endif // NET_BASE_FILTER_H__ |
194 | 203 |
OLD | NEW |