Chromium Code Reviews| 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 // 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" | 12 #include "base/basictypes.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
| 16 #include "net/base/mime_util.h" | 16 #include "net/base/mime_util.h" |
| 17 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 bool DataURL::Parse(const GURL& url, std::string* mime_type, | 23 bool DataURL::Parse(const GURL& url, |
| 24 std::string* charset, std::string* data) { | 24 std::string* mime_type, |
| 25 std::string* charset, | |
| 26 std::string* data) { | |
| 25 DCHECK(mime_type->empty()); | 27 DCHECK(mime_type->empty()); |
| 26 DCHECK(charset->empty()); | 28 DCHECK(charset->empty()); |
| 27 std::string::const_iterator begin = url.spec().begin(); | 29 std::string::const_iterator begin = url.spec().begin(); |
| 28 std::string::const_iterator end = url.spec().end(); | 30 std::string::const_iterator end = url.spec().end(); |
| 29 | 31 |
| 30 std::string::const_iterator after_colon = std::find(begin, end, ':'); | 32 std::string::const_iterator after_colon = std::find(begin, end, ':'); |
| 31 if (after_colon == end) | 33 if (after_colon == end) |
| 32 return false; | 34 return false; |
| 33 ++after_colon; | 35 ++after_colon; |
| 34 | 36 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 // (Spaces in a data URL should be escaped, which is handled below, so any | 93 // (Spaces in a data URL should be escaped, which is handled below, so any |
| 92 // spaces now are wrong. People expect to be able to enter them in the URL | 94 // spaces now are wrong. People expect to be able to enter them in the URL |
| 93 // bar for text, and it can't hurt, so we allow it.) | 95 // bar for text, and it can't hurt, so we allow it.) |
| 94 std::string temp_data = std::string(comma + 1, end); | 96 std::string temp_data = std::string(comma + 1, end); |
| 95 | 97 |
| 96 // For base64, we may have url-escaped whitespace which is not part | 98 // For base64, we may have url-escaped whitespace which is not part |
| 97 // of the data, and should be stripped. Otherwise, the escaped whitespace | 99 // of the data, and should be stripped. Otherwise, the escaped whitespace |
| 98 // could be part of the payload, so don't strip it. | 100 // could be part of the payload, so don't strip it. |
| 99 if (base64_encoded) { | 101 if (base64_encoded) { |
| 100 temp_data = UnescapeURLComponent(temp_data, | 102 temp_data = UnescapeURLComponent(temp_data, |
| 101 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS | | 103 UnescapeRule::SPACES | |
|
mef
2014/10/10 20:38:17
this is definitely wrong.
| |
| 102 UnescapeRule::CONTROL_CHARS); | 104 UnescapeRule::URL_SPECIAL_CHARS | |
| 105 UnescapeRule::CONTROL_CHARS); | |
| 103 } | 106 } |
| 104 | 107 |
| 105 // Strip whitespace. | 108 // Strip whitespace. |
| 106 if (base64_encoded || !(mime_type->compare(0, 5, "text/") == 0 || | 109 if (base64_encoded || |
| 107 mime_type->find("xml") != std::string::npos)) { | 110 !(mime_type->compare(0, 5, "text/") == 0 || |
| 108 temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(), | 111 mime_type->find("xml") != std::string::npos)) { |
| 109 IsAsciiWhitespace<wchar_t>), | 112 temp_data.erase( |
| 110 temp_data.end()); | 113 std::remove_if( |
| 114 temp_data.begin(), temp_data.end(), IsAsciiWhitespace<wchar_t>), | |
| 115 temp_data.end()); | |
| 111 } | 116 } |
| 112 | 117 |
| 113 if (!base64_encoded) { | 118 if (!base64_encoded) { |
| 114 temp_data = UnescapeURLComponent(temp_data, | 119 temp_data = UnescapeURLComponent(temp_data, |
| 115 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS | | 120 UnescapeRule::SPACES | |
| 116 UnescapeRule::CONTROL_CHARS); | 121 UnescapeRule::URL_SPECIAL_CHARS | |
| 122 UnescapeRule::CONTROL_CHARS); | |
| 117 } | 123 } |
| 118 | 124 |
| 119 if (base64_encoded) { | 125 if (base64_encoded) { |
| 120 size_t length = temp_data.length(); | 126 size_t length = temp_data.length(); |
| 121 size_t padding_needed = 4 - (length % 4); | 127 size_t padding_needed = 4 - (length % 4); |
| 122 // If the input wasn't padded, then we pad it as necessary until we have a | 128 // If the input wasn't padded, then we pad it as necessary until we have a |
| 123 // length that is a multiple of 4 as required by our decoder. We don't | 129 // length that is a multiple of 4 as required by our decoder. We don't |
| 124 // correct if the input was incorrectly padded. If |padding_needed| == 3, | 130 // correct if the input was incorrectly padded. If |padding_needed| == 3, |
| 125 // then the input isn't well formed and decoding will fail with or without | 131 // then the input isn't well formed and decoding will fail with or without |
| 126 // padding. | 132 // padding. |
| 127 if ((padding_needed == 1 || padding_needed == 2) && | 133 if ((padding_needed == 1 || padding_needed == 2) && |
| 128 temp_data[length - 1] != '=') { | 134 temp_data[length - 1] != '=') { |
| 129 temp_data.resize(length + padding_needed, '='); | 135 temp_data.resize(length + padding_needed, '='); |
| 130 } | 136 } |
| 131 return base::Base64Decode(temp_data, data); | 137 return base::Base64Decode(temp_data, data); |
| 132 } | 138 } |
| 133 | 139 |
| 134 temp_data.swap(*data); | 140 temp_data.swap(*data); |
| 135 return true; | 141 return true; |
| 136 } | 142 } |
| 137 | 143 |
| 138 } // namespace net | 144 } // namespace net |
| OLD | NEW |