| 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/basictypes.h" |
| 13 #include "base/string_split.h" |
| 12 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 13 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
| 15 | 17 |
| 16 namespace net { | 18 namespace net { |
| 17 | 19 |
| 18 // static | 20 // static |
| 19 bool DataURL::Parse(const GURL& url, std::string* mime_type, | 21 bool DataURL::Parse(const GURL& url, std::string* mime_type, |
| 20 std::string* charset, std::string* data) { | 22 std::string* charset, std::string* data) { |
| 23 DCHECK(mime_type->empty()); |
| 24 DCHECK(charset->empty()); |
| 21 std::string::const_iterator begin = url.spec().begin(); | 25 std::string::const_iterator begin = url.spec().begin(); |
| 22 std::string::const_iterator end = url.spec().end(); | 26 std::string::const_iterator end = url.spec().end(); |
| 23 | 27 |
| 24 std::string::const_iterator after_colon = std::find(begin, end, ':'); | 28 std::string::const_iterator after_colon = std::find(begin, end, ':'); |
| 25 if (after_colon == end) | 29 if (after_colon == end) |
| 26 return false; | 30 return false; |
| 27 ++after_colon; | 31 ++after_colon; |
| 28 | 32 |
| 29 // first, find the start of the data | |
| 30 std::string::const_iterator comma = std::find(after_colon, end, ','); | 33 std::string::const_iterator comma = std::find(after_colon, end, ','); |
| 31 if (comma == end) | 34 if (comma == end) |
| 32 return false; | 35 return false; |
| 33 | 36 |
| 34 const char kBase64Tag[] = ";base64"; | 37 std::vector<std::string> meta_data; |
| 35 std::string::const_iterator it = | 38 std::string unparsed_meta_data(after_colon, comma); |
| 36 std::search(after_colon, comma, kBase64Tag, | 39 base::SplitString(unparsed_meta_data, ';', &meta_data); |
| 37 kBase64Tag + sizeof(kBase64Tag)-1); | |
| 38 | 40 |
| 39 bool base64_encoded = (it != comma); | 41 std::vector<std::string>::iterator iter = meta_data.begin(); |
| 42 if (iter != meta_data.end()) { |
| 43 mime_type->swap(*iter); |
| 44 StringToLowerASCII(mime_type); |
| 45 ++iter; |
| 46 } |
| 40 | 47 |
| 41 if (comma != after_colon) { | 48 static const char kBase64Tag[] = "base64"; |
| 42 // everything else is content type | 49 static const char kCharsetTag[] = "charset="; |
| 43 std::string::const_iterator semi_colon = std::find(after_colon, comma, ';'); | 50 const size_t kCharsetTagLength = arraysize(kCharsetTag) - 1; |
| 44 if (semi_colon != after_colon) { | 51 |
| 45 mime_type->assign(after_colon, semi_colon); | 52 bool base64_encoded = false; |
| 46 StringToLowerASCII(mime_type); | 53 for (; iter != meta_data.end(); ++iter) { |
| 47 } | 54 if (!base64_encoded && *iter == kBase64Tag) { |
| 48 if (semi_colon != comma) { | 55 base64_encoded = true; |
| 49 const char kCharsetTag[] = "charset="; | 56 } else if (charset->empty() && |
| 50 it = std::search(semi_colon + 1, comma, kCharsetTag, | 57 iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) { |
| 51 kCharsetTag + sizeof(kCharsetTag)-1); | 58 charset->assign(iter->substr(kCharsetTagLength)); |
| 52 if (it != comma) | |
| 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 |