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
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"
22 bool DataURL::Parse(const GURL
& url
, std::string
* mime_type
,
23 std::string
* charset
, std::string
* data
) {
24 DCHECK(mime_type
->empty());
25 DCHECK(charset
->empty());
26 std::string::const_iterator begin
= url
.spec().begin();
27 std::string::const_iterator end
= url
.spec().end();
29 std::string::const_iterator after_colon
= std::find(begin
, end
, ':');
30 if (after_colon
== end
)
34 std::string::const_iterator comma
= std::find(after_colon
, end
, ',');
38 std::vector
<std::string
> meta_data
;
39 std::string
unparsed_meta_data(after_colon
, comma
);
40 base::SplitString(unparsed_meta_data
, ';', &meta_data
);
42 std::vector
<std::string
>::iterator iter
= meta_data
.begin();
43 if (iter
!= meta_data
.end()) {
44 mime_type
->swap(*iter
);
45 StringToLowerASCII(mime_type
);
49 static const char kBase64Tag
[] = "base64";
50 static const char kCharsetTag
[] = "charset=";
51 const size_t kCharsetTagLength
= arraysize(kCharsetTag
) - 1;
53 bool base64_encoded
= false;
54 for (; iter
!= meta_data
.end(); ++iter
) {
55 if (!base64_encoded
&& *iter
== kBase64Tag
) {
56 base64_encoded
= true;
57 } else if (charset
->empty() &&
58 iter
->compare(0, kCharsetTagLength
, kCharsetTag
) == 0) {
59 charset
->assign(iter
->substr(kCharsetTagLength
));
63 if (mime_type
->empty()) {
64 // fallback to defaults if nothing specified in the URL:
65 mime_type
->assign("text/plain");
66 } else if (!ParseMimeTypeWithoutParameter(*mime_type
, NULL
, NULL
)) {
70 charset
->assign("US-ASCII");
72 // The caller may not be interested in receiving the data.
76 // Preserve spaces if dealing with text or xml input, same as mozilla:
77 // https://bugzilla.mozilla.org/show_bug.cgi?id=138052
78 // but strip them otherwise:
79 // https://bugzilla.mozilla.org/show_bug.cgi?id=37200
80 // (Spaces in a data URL should be escaped, which is handled below, so any
81 // spaces now are wrong. People expect to be able to enter them in the URL
82 // bar for text, and it can't hurt, so we allow it.)
83 std::string temp_data
= std::string(comma
+ 1, end
);
85 // For base64, we may have url-escaped whitespace which is not part
86 // of the data, and should be stripped. Otherwise, the escaped whitespace
87 // could be part of the payload, so don't strip it.
89 temp_data
= UnescapeURLComponent(temp_data
,
90 UnescapeRule::SPACES
| UnescapeRule::URL_SPECIAL_CHARS
|
91 UnescapeRule::CONTROL_CHARS
);
95 if (base64_encoded
|| !(mime_type
->compare(0, 5, "text/") == 0 ||
96 mime_type
->find("xml") != std::string::npos
)) {
97 temp_data
.erase(std::remove_if(temp_data
.begin(), temp_data
.end(),
98 IsAsciiWhitespace
<wchar_t>),
102 if (!base64_encoded
) {
103 temp_data
= UnescapeURLComponent(temp_data
,
104 UnescapeRule::SPACES
| UnescapeRule::URL_SPECIAL_CHARS
|
105 UnescapeRule::CONTROL_CHARS
);
108 if (base64_encoded
) {
109 size_t length
= temp_data
.length();
110 size_t padding_needed
= 4 - (length
% 4);
111 // If the input wasn't padded, then we pad it as necessary until we have a
112 // length that is a multiple of 4 as required by our decoder. We don't
113 // correct if the input was incorrectly padded. If |padding_needed| == 3,
114 // then the input isn't well formed and decoding will fail with or without
116 if ((padding_needed
== 1 || padding_needed
== 2) &&
117 temp_data
[length
- 1] != '=') {
118 temp_data
.resize(length
+ padding_needed
, '=');
120 return base::Base64Decode(temp_data
, data
);
123 temp_data
.swap(*data
);