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 #ifndef NET_BASE_ESCAPE_H_ | 5 #ifndef NET_BASE_ESCAPE_H_ |
6 #define NET_BASE_ESCAPE_H_ | 6 #define NET_BASE_ESCAPE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | |
12 | 11 |
13 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_piece.h" |
14 #include "base/strings/utf_offset_string_conversions.h" | 14 #include "base/strings/utf_offset_string_conversions.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 // Escaping -------------------------------------------------------------------- | 19 // Escaping -------------------------------------------------------------------- |
20 | 20 |
21 // Escapes characters in text suitable for use as a query parameter value. | 21 // Escapes characters in text suitable for use as a query parameter value. |
22 // We %XX everything except alphanumerics and -_.!~*'() | 22 // We %XX everything except alphanumerics and -_.!~*'() |
23 // Spaces change to "+" unless you pass usePlus=false. | 23 // Spaces change to "+" unless you pass usePlus=false. |
24 // This is basically the same as encodeURIComponent in javascript. | 24 // This is basically the same as encodeURIComponent in javascript. |
25 NET_EXPORT std::string EscapeQueryParamValue(const std::string& text, | 25 NET_EXPORT std::string EscapeQueryParamValue(base::StringPiece text, |
26 bool use_plus); | 26 bool use_plus); |
27 | 27 |
28 // Escapes a partial or complete file/pathname. This includes: | 28 // Escapes a partial or complete file/pathname. This includes: |
29 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} | 29 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} |
30 // For the base::string16 version, we attempt a conversion to |codepage| before | 30 // For the base::string16 version, we attempt a conversion to |codepage| before |
31 // encoding the string. If this conversion fails, we return false. | 31 // encoding the string. If this conversion fails, we return false. |
32 NET_EXPORT std::string EscapePath(const std::string& path); | 32 NET_EXPORT std::string EscapePath(base::StringPiece path); |
33 | 33 |
34 #if defined(OS_MACOSX) | 34 #if defined(OS_MACOSX) |
35 // Escapes characters as per expectations of NSURL. This includes: | 35 // Escapes characters as per expectations of NSURL. This includes: |
36 // non-printable, non-7bit, and (including space) "#%<>[\]^`{|} | 36 // non-printable, non-7bit, and (including space) "#%<>[\]^`{|} |
37 NET_EXPORT std::string EscapeNSURLPrecursor(const std::string& precursor); | 37 NET_EXPORT std::string EscapeNSURLPrecursor(base::StringPiece precursor); |
38 #endif // defined(OS_MACOSX) | 38 #endif // defined(OS_MACOSX) |
39 | 39 |
40 // Escapes application/x-www-form-urlencoded content. This includes: | 40 // Escapes application/x-www-form-urlencoded content. This includes: |
41 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} | 41 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} |
42 // Space is escaped as + (if use_plus is true) and other special characters | 42 // Space is escaped as + (if use_plus is true) and other special characters |
43 // as %XX (hex). | 43 // as %XX (hex). |
44 NET_EXPORT std::string EscapeUrlEncodedData(const std::string& path, | 44 NET_EXPORT std::string EscapeUrlEncodedData(base::StringPiece path, |
45 bool use_plus); | 45 bool use_plus); |
46 | 46 |
47 // Escapes all non-ASCII input. | 47 // Escapes all non-ASCII input. |
48 NET_EXPORT std::string EscapeNonASCII(const std::string& input); | 48 NET_EXPORT std::string EscapeNonASCII(base::StringPiece input); |
49 | 49 |
50 // Escapes characters in text suitable for use as an external protocol handler | 50 // Escapes characters in text suitable for use as an external protocol handler |
51 // command. | 51 // command. |
52 // We %XX everything except alphanumerics and -_.!~*'() and the restricted | 52 // We %XX everything except alphanumerics and -_.!~*'() and the restricted |
53 // chracters (;/?:@&=+$,#[]) and a valid percent escape sequence (%XX). | 53 // chracters (;/?:@&=+$,#[]) and a valid percent escape sequence (%XX). |
54 NET_EXPORT std::string EscapeExternalHandlerValue(const std::string& text); | 54 NET_EXPORT std::string EscapeExternalHandlerValue(base::StringPiece text); |
55 | 55 |
56 // Appends the given character to the output string, escaping the character if | 56 // Appends the given character to the output string, escaping the character if |
57 // the character would be interpretted as an HTML delimiter. | 57 // the character would be interpretted as an HTML delimiter. |
58 NET_EXPORT void AppendEscapedCharForHTML(char c, std::string* output); | 58 NET_EXPORT void AppendEscapedCharForHTML(char c, std::string* output); |
59 | 59 |
60 // Escapes chars that might cause this text to be interpretted as HTML tags. | 60 // Escapes chars that might cause this text to be interpretted as HTML tags. |
61 NET_EXPORT std::string EscapeForHTML(const std::string& text); | 61 NET_EXPORT std::string EscapeForHTML(base::StringPiece text); |
62 NET_EXPORT base::string16 EscapeForHTML(const base::string16& text); | 62 NET_EXPORT base::string16 EscapeForHTML(base::StringPiece16 text); |
63 | 63 |
64 // Unescaping ------------------------------------------------------------------ | 64 // Unescaping ------------------------------------------------------------------ |
65 | 65 |
66 class UnescapeRule { | 66 class UnescapeRule { |
67 public: | 67 public: |
68 // A combination of the following flags that is passed to the unescaping | 68 // A combination of the following flags that is passed to the unescaping |
69 // functions. | 69 // functions. |
70 typedef uint32_t Type; | 70 typedef uint32_t Type; |
71 | 71 |
72 enum { | 72 enum { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // Unescapes |escaped_text| and returns the result. | 118 // Unescapes |escaped_text| and returns the result. |
119 // Unescaping consists of looking for the exact pattern "%XX", where each X is | 119 // Unescaping consists of looking for the exact pattern "%XX", where each X is |
120 // a hex digit, and converting to the character with the numerical value of | 120 // a hex digit, and converting to the character with the numerical value of |
121 // those digits. Thus "i%20=%203%3b" unescapes to "i = 3;". | 121 // those digits. Thus "i%20=%203%3b" unescapes to "i = 3;". |
122 // | 122 // |
123 // Watch out: this doesn't necessarily result in the correct final result, | 123 // Watch out: this doesn't necessarily result in the correct final result, |
124 // because the encoding may be unknown. For example, the input might be ASCII, | 124 // because the encoding may be unknown. For example, the input might be ASCII, |
125 // which, after unescaping, is supposed to be interpreted as UTF-8, and then | 125 // which, after unescaping, is supposed to be interpreted as UTF-8, and then |
126 // converted into full UTF-16 chars. This function won't tell you if any | 126 // converted into full UTF-16 chars. This function won't tell you if any |
127 // conversions need to take place, it only unescapes. | 127 // conversions need to take place, it only unescapes. |
128 NET_EXPORT std::string UnescapeURLComponent(const std::string& escaped_text, | 128 NET_EXPORT std::string UnescapeURLComponent(base::StringPiece escaped_text, |
129 UnescapeRule::Type rules); | 129 UnescapeRule::Type rules); |
130 NET_EXPORT base::string16 UnescapeURLComponent( | 130 NET_EXPORT base::string16 UnescapeURLComponent(base::StringPiece16 escaped_text, |
131 const base::string16& escaped_text, | 131 UnescapeRule::Type rules); |
132 UnescapeRule::Type rules); | |
133 | 132 |
134 // Unescapes the given substring as a URL, and then tries to interpret the | 133 // Unescapes the given substring as a URL, and then tries to interpret the |
135 // result as being encoded as UTF-8. If the result is convertable into UTF-8, it | 134 // result as being encoded as UTF-8. If the result is convertable into UTF-8, it |
136 // will be returned as converted. If it is not, the original escaped string will | 135 // will be returned as converted. If it is not, the original escaped string will |
137 // be converted into a base::string16 and returned. |adjustments| provides | 136 // be converted into a base::string16 and returned. |adjustments| provides |
138 // information on how the original string was adjusted to get the string | 137 // information on how the original string was adjusted to get the string |
139 // returned. | 138 // returned. |
140 NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponent( | 139 NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponent( |
141 const std::string& text, | 140 base::StringPiece text, |
142 UnescapeRule::Type rules); | 141 UnescapeRule::Type rules); |
143 NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( | 142 NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
144 const std::string& text, | 143 base::StringPiece text, |
145 UnescapeRule::Type rules, | 144 UnescapeRule::Type rules, |
146 base::OffsetAdjuster::Adjustments* adjustments); | 145 base::OffsetAdjuster::Adjustments* adjustments); |
147 | 146 |
148 // Unescapes the following ampersand character codes from |text|: | 147 // Unescapes the following ampersand character codes from |text|: |
149 // < > & " ' | 148 // < > & " ' |
150 NET_EXPORT base::string16 UnescapeForHTML(const base::string16& text); | 149 NET_EXPORT base::string16 UnescapeForHTML(base::StringPiece16 text); |
151 | 150 |
152 } // namespace net | 151 } // namespace net |
153 | 152 |
154 #endif // NET_BASE_ESCAPE_H_ | 153 #endif // NET_BASE_ESCAPE_H_ |
OLD | NEW |