OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <algorithm> | 5 #include <algorithm> |
6 #include <iterator> | 6 #include <iterator> |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 bool allow_proprietary_codecs_; | 157 bool allow_proprietary_codecs_; |
158 | 158 |
159 // Lookup table for string compare based string -> Codec mappings. | 159 // Lookup table for string compare based string -> Codec mappings. |
160 StringToCodecMappings string_to_codec_map_; | 160 StringToCodecMappings string_to_codec_map_; |
161 }; // class MimeUtil | 161 }; // class MimeUtil |
162 | 162 |
163 // This variable is Leaky because we need to access it from WorkerPool threads. | 163 // This variable is Leaky because we need to access it from WorkerPool threads. |
164 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 164 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
165 LAZY_INSTANCE_INITIALIZER; | 165 LAZY_INSTANCE_INITIALIZER; |
166 | 166 |
167 struct MimeInfo { | |
168 const char* const mime_type; | |
169 const char* const extensions; // comma separated list | |
170 }; | |
171 | |
172 static const MimeInfo primary_mappings[] = { | 167 static const MimeInfo primary_mappings[] = { |
173 { "text/html", "html,htm,shtml,shtm" }, | 168 { "text/html", "html,htm,shtml,shtm" }, |
174 { "text/css", "css" }, | 169 { "text/css", "css" }, |
175 { "text/xml", "xml" }, | 170 { "text/xml", "xml" }, |
176 { "image/gif", "gif" }, | 171 { "image/gif", "gif" }, |
177 { "image/jpeg", "jpeg,jpg" }, | 172 { "image/jpeg", "jpeg,jpg" }, |
178 { "image/webp", "webp" }, | 173 { "image/webp", "webp" }, |
179 { "image/png", "png" }, | 174 { "image/png", "png" }, |
180 { "video/mp4", "mp4,m4v" }, | 175 { "video/mp4", "mp4,m4v" }, |
181 { "audio/x-m4a", "m4a" }, | 176 { "audio/x-m4a", "m4a" }, |
(...skipping 27 matching lines...) Expand all Loading... |
209 { "text/plain", "txt,text" }, | 204 { "text/plain", "txt,text" }, |
210 { "text/html", "ehtml" }, | 205 { "text/html", "ehtml" }, |
211 { "application/rss+xml", "rss" }, | 206 { "application/rss+xml", "rss" }, |
212 { "application/rdf+xml", "rdf" }, | 207 { "application/rdf+xml", "rdf" }, |
213 { "text/xml", "xsl,xbl,xslt" }, | 208 { "text/xml", "xsl,xbl,xslt" }, |
214 { "application/vnd.mozilla.xul+xml", "xul" }, | 209 { "application/vnd.mozilla.xul+xml", "xul" }, |
215 { "application/x-shockwave-flash", "swf,swl" }, | 210 { "application/x-shockwave-flash", "swf,swl" }, |
216 { "application/pkcs7-mime", "p7m,p7c,p7z" }, | 211 { "application/pkcs7-mime", "p7m,p7c,p7z" }, |
217 { "application/pkcs7-signature", "p7s" }, | 212 { "application/pkcs7-signature", "p7s" }, |
218 { "application/x-mpegurl", "m3u8" }, | 213 { "application/x-mpegurl", "m3u8" }, |
219 { "application/epub+zip", "epub" }, | |
220 }; | 214 }; |
221 | 215 |
222 static const char* FindMimeType(const MimeInfo* mappings, | 216 const char* FindMimeType(const MimeInfo* mappings, |
223 size_t mappings_len, | 217 size_t mappings_len, |
224 const char* ext) { | 218 const char* ext) { |
225 size_t ext_len = strlen(ext); | 219 size_t ext_len = strlen(ext); |
226 | 220 |
227 for (size_t i = 0; i < mappings_len; ++i) { | 221 for (size_t i = 0; i < mappings_len; ++i) { |
228 const char* extensions = mappings[i].extensions; | 222 const char* extensions = mappings[i].extensions; |
229 for (;;) { | 223 for (;;) { |
230 size_t end_pos = strcspn(extensions, ","); | 224 size_t end_pos = strcspn(extensions, ","); |
231 if (end_pos == ext_len && | 225 if (end_pos == ext_len && |
232 base::strncasecmp(extensions, ext, ext_len) == 0) | 226 base::strncasecmp(extensions, ext, ext_len) == 0) |
233 return mappings[i].mime_type; | 227 return mappings[i].mime_type; |
234 extensions += end_pos; | 228 extensions += end_pos; |
(...skipping 26 matching lines...) Expand all Loading... |
261 | 255 |
262 bool MimeUtil::GetMimeTypeFromExtensionHelper( | 256 bool MimeUtil::GetMimeTypeFromExtensionHelper( |
263 const base::FilePath::StringType& ext, | 257 const base::FilePath::StringType& ext, |
264 bool include_platform_types, | 258 bool include_platform_types, |
265 string* result) const { | 259 string* result) const { |
266 // Avoids crash when unable to handle a long file path. See crbug.com/48733. | 260 // Avoids crash when unable to handle a long file path. See crbug.com/48733. |
267 const unsigned kMaxFilePathSize = 65536; | 261 const unsigned kMaxFilePathSize = 65536; |
268 if (ext.length() > kMaxFilePathSize) | 262 if (ext.length() > kMaxFilePathSize) |
269 return false; | 263 return false; |
270 | 264 |
| 265 // Reject a string which contains null character. |
| 266 base::FilePath::StringType::size_type nul_pos = |
| 267 ext.find(FILE_PATH_LITERAL('\0')); |
| 268 if (nul_pos != base::FilePath::StringType::npos) |
| 269 return false; |
| 270 |
271 // We implement the same algorithm as Mozilla for mapping a file extension to | 271 // We implement the same algorithm as Mozilla for mapping a file extension to |
272 // a mime type. That is, we first check a hard-coded list (that cannot be | 272 // a mime type. That is, we first check a hard-coded list (that cannot be |
273 // overridden), and then if not found there, we defer to the system registry. | 273 // overridden), and then if not found there, we defer to the system registry. |
274 // Finally, we scan a secondary hard-coded list to catch types that we can | 274 // Finally, we scan a secondary hard-coded list to catch types that we can |
275 // deduce but that we also want to allow the OS to override. | 275 // deduce but that we also want to allow the OS to override. |
276 | 276 |
277 base::FilePath path_ext(ext); | 277 base::FilePath path_ext(ext); |
278 const string ext_narrow_str = path_ext.AsUTF8Unsafe(); | 278 const string ext_narrow_str = path_ext.AsUTF8Unsafe(); |
279 const char* mime_type = FindMimeType(primary_mappings, | 279 const char* mime_type = FindMimeType(primary_mappings, |
280 arraysize(primary_mappings), | 280 arraysize(primary_mappings), |
(...skipping 927 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1208 post_data->append("\r\n" + value + "\r\n"); | 1208 post_data->append("\r\n" + value + "\r\n"); |
1209 } | 1209 } |
1210 | 1210 |
1211 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1211 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1212 std::string* post_data) { | 1212 std::string* post_data) { |
1213 DCHECK(post_data); | 1213 DCHECK(post_data); |
1214 post_data->append("--" + mime_boundary + "--\r\n"); | 1214 post_data->append("--" + mime_boundary + "--\r\n"); |
1215 } | 1215 } |
1216 | 1216 |
1217 } // namespace net | 1217 } // namespace net |
OLD | NEW |