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