Valgrind Mac: Remove many obsolete suppressions.
[chromium-blink-merge.git] / net / base / data_url.cc
blob0ec345ea6dbf4f89ed5701e132109e6c7e411a41
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
3 // found in the LICENSE file.
5 // NOTE: based loosely on mozilla's nsDataChannel.cpp
7 #include <algorithm>
9 #include "net/base/data_url.h"
11 #include "base/base64.h"
12 #include "base/basictypes.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "net/base/escape.h"
16 #include "net/base/mime_util.h"
17 #include "net/http/http_util.h"
18 #include "url/gurl.h"
20 namespace net {
22 // static
23 bool DataURL::Parse(const GURL& url, std::string* mime_type,
24 std::string* charset, std::string* data) {
25 DCHECK(mime_type->empty());
26 DCHECK(charset->empty());
27 std::string::const_iterator begin = url.spec().begin();
28 std::string::const_iterator end = url.spec().end();
30 std::string::const_iterator after_colon = std::find(begin, end, ':');
31 if (after_colon == end)
32 return false;
33 ++after_colon;
35 std::string::const_iterator comma = std::find(after_colon, end, ',');
36 if (comma == end)
37 return false;
39 std::vector<std::string> meta_data;
40 std::string unparsed_meta_data(after_colon, comma);
41 base::SplitString(unparsed_meta_data, ';', &meta_data);
43 std::vector<std::string>::iterator iter = meta_data.begin();
44 if (iter != meta_data.end()) {
45 mime_type->swap(*iter);
46 base::StringToLowerASCII(mime_type);
47 ++iter;
50 static const char kBase64Tag[] = "base64";
51 static const char kCharsetTag[] = "charset=";
52 const size_t kCharsetTagLength = arraysize(kCharsetTag) - 1;
54 bool base64_encoded = false;
55 for (; iter != meta_data.end(); ++iter) {
56 if (!base64_encoded && *iter == kBase64Tag) {
57 base64_encoded = true;
58 } else if (charset->empty() &&
59 iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) {
60 charset->assign(iter->substr(kCharsetTagLength));
61 // The grammar for charset is not specially defined in RFC2045 and
62 // RFC2397. It just needs to be a token.
63 if (!net::HttpUtil::IsToken(*charset))
64 return false;
68 if (mime_type->empty()) {
69 // fallback to defaults if nothing specified in the URL:
70 mime_type->assign("text/plain");
71 } else if (!ParseMimeTypeWithoutParameter(*mime_type, NULL, NULL)) {
72 return false;
74 if (charset->empty())
75 charset->assign("US-ASCII");
77 // The caller may not be interested in receiving the data.
78 if (!data)
79 return true;
81 // Preserve spaces if dealing with text or xml input, same as mozilla:
82 // https://bugzilla.mozilla.org/show_bug.cgi?id=138052
83 // but strip them otherwise:
84 // https://bugzilla.mozilla.org/show_bug.cgi?id=37200
85 // (Spaces in a data URL should be escaped, which is handled below, so any
86 // spaces now are wrong. People expect to be able to enter them in the URL
87 // bar for text, and it can't hurt, so we allow it.)
88 std::string temp_data = std::string(comma + 1, end);
90 // For base64, we may have url-escaped whitespace which is not part
91 // of the data, and should be stripped. Otherwise, the escaped whitespace
92 // could be part of the payload, so don't strip it.
93 if (base64_encoded) {
94 temp_data = UnescapeURLComponent(temp_data,
95 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS |
96 UnescapeRule::CONTROL_CHARS);
99 // Strip whitespace.
100 if (base64_encoded || !(mime_type->compare(0, 5, "text/") == 0 ||
101 mime_type->find("xml") != std::string::npos)) {
102 temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(),
103 IsAsciiWhitespace<wchar_t>),
104 temp_data.end());
107 if (!base64_encoded) {
108 temp_data = UnescapeURLComponent(temp_data,
109 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS |
110 UnescapeRule::CONTROL_CHARS);
113 if (base64_encoded) {
114 size_t length = temp_data.length();
115 size_t padding_needed = 4 - (length % 4);
116 // If the input wasn't padded, then we pad it as necessary until we have a
117 // length that is a multiple of 4 as required by our decoder. We don't
118 // correct if the input was incorrectly padded. If |padding_needed| == 3,
119 // then the input isn't well formed and decoding will fail with or without
120 // padding.
121 if ((padding_needed == 1 || padding_needed == 2) &&
122 temp_data[length - 1] != '=') {
123 temp_data.resize(length + padding_needed, '=');
125 return base::Base64Decode(temp_data, data);
128 temp_data.swap(*data);
129 return true;
132 } // namespace net