1 // Copyright (c) 2011 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.
4 
5 #ifndef NET_BASE_DATA_URL_H_
6 #define NET_BASE_DATA_URL_H_
7 
8 #include <string>
9 
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_export.h"
15 
16 class GURL;
17 
18 namespace net {
19 
20 class HttpResponseHeaders;
21 
22 // See RFC 2397 for a complete description of the 'data' URL scheme.
23 //
24 // Briefly, a 'data' URL has the form:
25 //
26 //   data:[<mediatype>][;base64],<data>
27 //
28 // The <mediatype> is an Internet media type specification (with optional
29 // parameters.)  The appearance of ";base64" means that the data is encoded as
30 // base64.  Without ";base64", the data (as a sequence of octets) is represented
31 // using ASCII encoding for octets inside the range of safe URL characters and
32 // using the standard %xx hex encoding of URLs for octets outside that range.
33 // If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII.  As a
34 // shorthand, "text/plain" can be omitted but the charset parameter supplied.
35 //
36 class NET_EXPORT DataURL {
37  public:
38   // This method can be used to parse a 'data' URL into its component pieces.
39   //
40   // |mime_type| and |charset| must be non-null and point to empty strings.
41   //
42   // If |data| is null, then the <data> section will not be parsed or validated.
43   // If non-null, it must point to an empty string.
44   //
45   // The resulting mime_type is normalized to lowercase.  The data is the
46   // decoded data (e.g.., if the data URL specifies base64 encoding, then the
47   // returned data is base64 decoded, and any %-escaped bytes are unescaped).
48   //
49   // If the media type value doesn't match the media-type production defined in
50   // RFC 7231, mime_type will be set to the default value "text/plain". We
51   // don't simply fail for this grammar violation since Chromium had been
52   // accepting such invalid values. For example, <img> element with the src
53   // attribute set to a data URL with an invalid media type "image" (without a
54   // slash and subtype) had been displayed. However, the value this method will
55   // store in mime_type argument can be used for generating other headers, etc.
56   // This could lead to security vulnerability. We don't want to accept
57   // arbitrary value and ask each caller to validate the return value.
58   //
59   // If the charset parameter is specified but its value doesn't match the
60   // token production defined in RFC 7230, this method simply fails and returns
61   // false.
62   //
63   // If there's any other grammar violation in the URL, then this method will
64   // return false, and all passed in pointers will be unmodified. On success,
65   // true is returned.
66   static bool Parse(const GURL& url,
67                     std::string* mime_type,
68                     std::string* charset,
69                     std::string* data) WARN_UNUSED_RESULT;
70 
71   // Similar to parse, except that it also generates a bogus set of response
72   // headers, with Content-Type populated, and takes a method. Only the "HEAD"
73   // method modifies the response, resulting in a 0-length body. All arguments
74   // except must be non-null. All std::string pointers must point to empty
75   // strings, and |*headers| must be nullptr. Returns net::OK on success.
76   static Error BuildResponse(const GURL& url,
77                              base::StringPiece method,
78                              std::string* mime_type,
79                              std::string* charset,
80                              std::string* data,
81                              scoped_refptr<HttpResponseHeaders>* headers)
82       WARN_UNUSED_RESULT;
83 };
84 
85 }  // namespace net
86 
87 #endif  // NET_BASE_DATA_URL_H_
88