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 // NOTE: based loosely on mozilla's nsDataChannel.cpp | 5 // NOTE: based loosely on mozilla's nsDataChannel.cpp |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "net/base/data_url.h" | 9 #include "net/base/data_url.h" |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/string_split.h" | |
willchan no longer on Chromium
2011/05/18 21:00:14
nit: sort alphabetically
| |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 // static | 19 // static |
19 bool DataURL::Parse(const GURL& url, std::string* mime_type, | 20 bool DataURL::Parse(const GURL& url, std::string* mime_type, |
20 std::string* charset, std::string* data) { | 21 std::string* charset, std::string* data) { |
22 DCHECK(mime_type->empty()); | |
23 DCHECK(charset->empty()); | |
21 std::string::const_iterator begin = url.spec().begin(); | 24 std::string::const_iterator begin = url.spec().begin(); |
22 std::string::const_iterator end = url.spec().end(); | 25 std::string::const_iterator end = url.spec().end(); |
23 | 26 |
24 std::string::const_iterator after_colon = std::find(begin, end, ':'); | 27 std::string::const_iterator after_colon = std::find(begin, end, ':'); |
25 if (after_colon == end) | 28 if (after_colon == end) |
26 return false; | 29 return false; |
27 ++after_colon; | 30 ++after_colon; |
28 | 31 |
29 // first, find the start of the data | |
30 std::string::const_iterator comma = std::find(after_colon, end, ','); | 32 std::string::const_iterator comma = std::find(after_colon, end, ','); |
31 if (comma == end) | 33 if (comma == end) |
32 return false; | 34 return false; |
33 | 35 |
34 const char kBase64Tag[] = ";base64"; | 36 std::vector<std::string> meta_data; |
35 std::string::const_iterator it = | 37 std::string unparsed_meta_data(after_colon, comma); |
36 std::search(after_colon, comma, kBase64Tag, | 38 base::SplitString(unparsed_meta_data, ';', &meta_data); |
37 kBase64Tag + sizeof(kBase64Tag)-1); | |
38 | 39 |
39 bool base64_encoded = (it != comma); | 40 std::vector<std::string>::iterator iter = meta_data.begin(); |
41 if (iter != meta_data.end()) { | |
42 mime_type->swap(*iter); | |
43 StringToLowerASCII(mime_type); | |
44 ++iter; | |
45 } | |
40 | 46 |
41 if (comma != after_colon) { | 47 const char kBase64Tag[] = "base64"; |
willchan no longer on Chromium
2011/05/18 21:00:14
static, otherwise this gets initialized again on e
| |
42 // everything else is content type | 48 const char kCharsetTag[] = "charset="; |
willchan no longer on Chromium
2011/05/18 21:00:14
ditto
| |
43 std::string::const_iterator semi_colon = std::find(after_colon, comma, ';'); | 49 const size_t kCharsetTagLength = sizeof(kCharsetTag) - 1; |
willchan no longer on Chromium
2011/05/18 21:00:14
arraysize or strlen (compilers understand strlen a
Peter Kasting
2011/05/18 21:05:04
I think we've typically used arraysize() for this
| |
44 if (semi_colon != after_colon) { | 50 |
45 mime_type->assign(after_colon, semi_colon); | 51 bool base64_encoded = false; |
46 StringToLowerASCII(mime_type); | 52 for (; iter != meta_data.end(); ++iter) { |
47 } | 53 if (!base64_encoded && *iter == kBase64Tag) { |
48 if (semi_colon != comma) { | 54 base64_encoded = true; |
49 const char kCharsetTag[] = "charset="; | 55 } else if (charset->empty() && |
50 it = std::search(semi_colon + 1, comma, kCharsetTag, | 56 iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) { |
51 kCharsetTag + sizeof(kCharsetTag)-1); | 57 charset->assign(iter->substr(kCharsetTagLength)); |
52 if (it != comma) | 58 StringToLowerASCII(charset); |
53 charset->assign(it + sizeof(kCharsetTag)-1, comma); | |
54 } | 59 } |
55 } | 60 } |
56 | 61 |
57 // fallback to defaults if nothing specified in the URL: | 62 // fallback to defaults if nothing specified in the URL: |
58 if (mime_type->empty()) | 63 if (mime_type->empty()) |
59 mime_type->assign("text/plain"); | 64 mime_type->assign("text/plain"); |
60 if (charset->empty()) | 65 if (charset->empty()) |
61 charset->assign("US-ASCII"); | 66 charset->assign("US-ASCII"); |
62 | 67 |
63 // The caller may not be interested in receiving the data. | 68 // The caller may not be interested in receiving the data. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 } | 102 } |
98 | 103 |
99 if (base64_encoded) | 104 if (base64_encoded) |
100 return base::Base64Decode(temp_data, data); | 105 return base::Base64Decode(temp_data, data); |
101 | 106 |
102 temp_data.swap(*data); | 107 temp_data.swap(*data); |
103 return true; | 108 return true; |
104 } | 109 } |
105 | 110 |
106 } // namespace net | 111 } // namespace net |
OLD | NEW |