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" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "net/base/mime_util.h" | 18 #include "net/base/mime_util.h" |
19 #include "net/base/platform_mime_util.h" | 19 #include "net/base/platform_mime_util.h" |
20 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
21 | 21 |
22 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
23 #include "base/android/build_info.h" | 23 #include "base/android/build_info.h" |
24 #endif | 24 #endif |
25 | 25 |
26 using std::string; | 26 using std::string; |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 | 29 |
30 namespace { | |
31 | |
32 const base::FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0'); | |
eroman
2015/05/18 15:45:24
nit: I think it would be shorter to just inline th
yawano
2015/05/19 02:26:40
Done.
| |
33 | |
34 } // namespace | |
35 | |
30 // Singleton utility class for mime types. | 36 // Singleton utility class for mime types. |
31 class MimeUtil : public PlatformMimeUtil { | 37 class MimeUtil : public PlatformMimeUtil { |
32 public: | 38 public: |
33 enum Codec { | 39 enum Codec { |
34 INVALID_CODEC, | 40 INVALID_CODEC, |
35 PCM, | 41 PCM, |
36 MP3, | 42 MP3, |
37 MPEG2_AAC_LC, | 43 MPEG2_AAC_LC, |
38 MPEG2_AAC_MAIN, | 44 MPEG2_AAC_MAIN, |
39 MPEG2_AAC_SSR, | 45 MPEG2_AAC_SSR, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 bool allow_proprietary_codecs_; | 163 bool allow_proprietary_codecs_; |
158 | 164 |
159 // Lookup table for string compare based string -> Codec mappings. | 165 // Lookup table for string compare based string -> Codec mappings. |
160 StringToCodecMappings string_to_codec_map_; | 166 StringToCodecMappings string_to_codec_map_; |
161 }; // class MimeUtil | 167 }; // class MimeUtil |
162 | 168 |
163 // This variable is Leaky because we need to access it from WorkerPool threads. | 169 // This variable is Leaky because we need to access it from WorkerPool threads. |
164 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 170 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
165 LAZY_INSTANCE_INITIALIZER; | 171 LAZY_INSTANCE_INITIALIZER; |
166 | 172 |
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[] = { | 173 static const MimeInfo primary_mappings[] = { |
173 { "text/html", "html,htm,shtml,shtm" }, | 174 { "text/html", "html,htm,shtml,shtm" }, |
174 { "text/css", "css" }, | 175 { "text/css", "css" }, |
175 { "text/xml", "xml" }, | 176 { "text/xml", "xml" }, |
176 { "image/gif", "gif" }, | 177 { "image/gif", "gif" }, |
177 { "image/jpeg", "jpeg,jpg" }, | 178 { "image/jpeg", "jpeg,jpg" }, |
178 { "image/webp", "webp" }, | 179 { "image/webp", "webp" }, |
179 { "image/png", "png" }, | 180 { "image/png", "png" }, |
180 { "video/mp4", "mp4,m4v" }, | 181 { "video/mp4", "mp4,m4v" }, |
181 { "audio/x-m4a", "m4a" }, | 182 { "audio/x-m4a", "m4a" }, |
(...skipping 27 matching lines...) Expand all Loading... | |
209 { "text/plain", "txt,text" }, | 210 { "text/plain", "txt,text" }, |
210 { "text/html", "ehtml" }, | 211 { "text/html", "ehtml" }, |
211 { "application/rss+xml", "rss" }, | 212 { "application/rss+xml", "rss" }, |
212 { "application/rdf+xml", "rdf" }, | 213 { "application/rdf+xml", "rdf" }, |
213 { "text/xml", "xsl,xbl,xslt" }, | 214 { "text/xml", "xsl,xbl,xslt" }, |
214 { "application/vnd.mozilla.xul+xml", "xul" }, | 215 { "application/vnd.mozilla.xul+xml", "xul" }, |
215 { "application/x-shockwave-flash", "swf,swl" }, | 216 { "application/x-shockwave-flash", "swf,swl" }, |
216 { "application/pkcs7-mime", "p7m,p7c,p7z" }, | 217 { "application/pkcs7-mime", "p7m,p7c,p7z" }, |
217 { "application/pkcs7-signature", "p7s" }, | 218 { "application/pkcs7-signature", "p7s" }, |
218 { "application/x-mpegurl", "m3u8" }, | 219 { "application/x-mpegurl", "m3u8" }, |
219 { "application/epub+zip", "epub" }, | |
220 }; | 220 }; |
221 | 221 |
222 static const char* FindMimeType(const MimeInfo* mappings, | 222 const char* FindMimeType(const MimeInfo* mappings, |
223 size_t mappings_len, | 223 size_t mappings_len, |
224 const char* ext) { | 224 const char* ext) { |
225 size_t ext_len = strlen(ext); | 225 size_t ext_len = strlen(ext); |
226 | 226 |
227 for (size_t i = 0; i < mappings_len; ++i) { | 227 for (size_t i = 0; i < mappings_len; ++i) { |
228 const char* extensions = mappings[i].extensions; | 228 const char* extensions = mappings[i].extensions; |
229 for (;;) { | 229 for (;;) { |
230 size_t end_pos = strcspn(extensions, ","); | 230 size_t end_pos = strcspn(extensions, ","); |
231 if (end_pos == ext_len && | 231 if (end_pos == ext_len && |
232 base::strncasecmp(extensions, ext, ext_len) == 0) | 232 base::strncasecmp(extensions, ext, ext_len) == 0) |
233 return mappings[i].mime_type; | 233 return mappings[i].mime_type; |
234 extensions += end_pos; | 234 extensions += end_pos; |
(...skipping 26 matching lines...) Expand all Loading... | |
261 | 261 |
262 bool MimeUtil::GetMimeTypeFromExtensionHelper( | 262 bool MimeUtil::GetMimeTypeFromExtensionHelper( |
263 const base::FilePath::StringType& ext, | 263 const base::FilePath::StringType& ext, |
264 bool include_platform_types, | 264 bool include_platform_types, |
265 string* result) const { | 265 string* result) const { |
266 // Avoids crash when unable to handle a long file path. See crbug.com/48733. | 266 // Avoids crash when unable to handle a long file path. See crbug.com/48733. |
267 const unsigned kMaxFilePathSize = 65536; | 267 const unsigned kMaxFilePathSize = 65536; |
268 if (ext.length() > kMaxFilePathSize) | 268 if (ext.length() > kMaxFilePathSize) |
269 return false; | 269 return false; |
270 | 270 |
271 // Reject a string which contains null character. | |
272 base::FilePath::StringType::size_type nul_pos = ext.find(kStringTerminator); | |
eroman
2015/05/18 15:45:24
Thanks for adding. I have also filed https://code.
yawano
2015/05/19 02:26:40
Done.
| |
273 if (nul_pos != base::FilePath::StringType::npos) | |
274 return false; | |
275 | |
271 // We implement the same algorithm as Mozilla for mapping a file extension to | 276 // 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 | 277 // 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. | 278 // 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 | 279 // 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. | 280 // deduce but that we also want to allow the OS to override. |
276 | 281 |
277 base::FilePath path_ext(ext); | 282 base::FilePath path_ext(ext); |
278 const string ext_narrow_str = path_ext.AsUTF8Unsafe(); | 283 const string ext_narrow_str = path_ext.AsUTF8Unsafe(); |
279 const char* mime_type = FindMimeType(primary_mappings, | 284 const char* mime_type = FindMimeType(primary_mappings, |
280 arraysize(primary_mappings), | 285 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"); | 1213 post_data->append("\r\n" + value + "\r\n"); |
1209 } | 1214 } |
1210 | 1215 |
1211 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1216 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1212 std::string* post_data) { | 1217 std::string* post_data) { |
1213 DCHECK(post_data); | 1218 DCHECK(post_data); |
1214 post_data->append("--" + mime_boundary + "--\r\n"); | 1219 post_data->append("--" + mime_boundary + "--\r\n"); |
1215 } | 1220 } |
1216 | 1221 |
1217 } // namespace net | 1222 } // namespace net |
OLD | NEW |