OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "net/base/net_api.h" | 14 #include "net/base/net_export.h" |
15 | 15 |
16 // Escaping -------------------------------------------------------------------- | 16 // Escaping -------------------------------------------------------------------- |
17 | 17 |
18 // Escape a file. This includes: | 18 // Escape a file. This includes: |
19 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} | 19 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} |
20 NET_API std::string EscapePath(const std::string& path); | 20 NET_EXPORT std::string EscapePath(const std::string& path); |
21 | 21 |
22 // Escape application/x-www-form-urlencoded content. This includes: | 22 // Escape application/x-www-form-urlencoded content. This includes: |
23 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} | 23 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} |
24 // Space is escaped as + (if use_plus is true) and other special characters | 24 // Space is escaped as + (if use_plus is true) and other special characters |
25 // as %XX (hex). | 25 // as %XX (hex). |
26 NET_API std::string EscapeUrlEncodedData(const std::string& path, | 26 NET_EXPORT std::string EscapeUrlEncodedData(const std::string& path, |
27 bool use_plus); | 27 bool use_plus); |
28 | 28 |
29 // Escape all non-ASCII input. | 29 // Escape all non-ASCII input. |
30 NET_API std::string EscapeNonASCII(const std::string& input); | 30 NET_EXPORT std::string EscapeNonASCII(const std::string& input); |
31 | 31 |
32 // Escapes characters in text suitable for use as an external protocol handler | 32 // Escapes characters in text suitable for use as an external protocol handler |
33 // command. | 33 // command. |
34 // We %XX everything except alphanumerics and %-_.!~*'() and the restricted | 34 // We %XX everything except alphanumerics and %-_.!~*'() and the restricted |
35 // chracters (;/?:@&=+$,). | 35 // chracters (;/?:@&=+$,). |
36 NET_API std::string EscapeExternalHandlerValue(const std::string& text); | 36 NET_EXPORT std::string EscapeExternalHandlerValue(const std::string& text); |
37 | 37 |
38 // Append the given character to the output string, escaping the character if | 38 // Append the given character to the output string, escaping the character if |
39 // the character would be interpretted as an HTML delimiter. | 39 // the character would be interpretted as an HTML delimiter. |
40 NET_API void AppendEscapedCharForHTML(char c, std::string* output); | 40 NET_EXPORT void AppendEscapedCharForHTML(char c, std::string* output); |
41 | 41 |
42 // Escape chars that might cause this text to be interpretted as HTML tags. | 42 // Escape chars that might cause this text to be interpretted as HTML tags. |
43 NET_API std::string EscapeForHTML(const std::string& text); | 43 NET_EXPORT std::string EscapeForHTML(const std::string& text); |
44 NET_API string16 EscapeForHTML(const string16& text); | 44 NET_EXPORT string16 EscapeForHTML(const string16& text); |
45 | 45 |
46 // Unescaping ------------------------------------------------------------------ | 46 // Unescaping ------------------------------------------------------------------ |
47 | 47 |
48 class UnescapeRule { | 48 class UnescapeRule { |
49 public: | 49 public: |
50 // A combination of the following flags that is passed to the unescaping | 50 // A combination of the following flags that is passed to the unescaping |
51 // functions. | 51 // functions. |
52 typedef uint32 Type; | 52 typedef uint32 Type; |
53 | 53 |
54 enum { | 54 enum { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 // Unescapes |escaped_text| and returns the result. | 88 // Unescapes |escaped_text| and returns the result. |
89 // Unescaping consists of looking for the exact pattern "%XX", where each X is | 89 // Unescaping consists of looking for the exact pattern "%XX", where each X is |
90 // a hex digit, and converting to the character with the numerical value of | 90 // a hex digit, and converting to the character with the numerical value of |
91 // those digits. Thus "i%20=%203%3b" unescapes to "i = 3;". | 91 // those digits. Thus "i%20=%203%3b" unescapes to "i = 3;". |
92 // | 92 // |
93 // Watch out: this doesn't necessarily result in the correct final result, | 93 // Watch out: this doesn't necessarily result in the correct final result, |
94 // because the encoding may be unknown. For example, the input might be ASCII, | 94 // because the encoding may be unknown. For example, the input might be ASCII, |
95 // which, after unescaping, is supposed to be interpreted as UTF-8, and then | 95 // which, after unescaping, is supposed to be interpreted as UTF-8, and then |
96 // converted into full UTF-16 chars. This function won't tell you if any | 96 // converted into full UTF-16 chars. This function won't tell you if any |
97 // conversions need to take place, it only unescapes. | 97 // conversions need to take place, it only unescapes. |
98 NET_API std::string UnescapeURLComponent(const std::string& escaped_text, | 98 NET_EXPORT std::string UnescapeURLComponent(const std::string& escaped_text, |
| 99 UnescapeRule::Type rules); |
| 100 NET_EXPORT string16 UnescapeURLComponent(const string16& escaped_text, |
99 UnescapeRule::Type rules); | 101 UnescapeRule::Type rules); |
100 NET_API string16 UnescapeURLComponent(const string16& escaped_text, | |
101 UnescapeRule::Type rules); | |
102 | 102 |
103 // Unescapes the given substring as a URL, and then tries to interpret the | 103 // Unescapes the given substring as a URL, and then tries to interpret the |
104 // result as being encoded as UTF-8. If the result is convertable into UTF-8, it | 104 // result as being encoded as UTF-8. If the result is convertable into UTF-8, it |
105 // will be returned as converted. If it is not, the original escaped string will | 105 // will be returned as converted. If it is not, the original escaped string will |
106 // be converted into a string16 and returned. (|offset[s]_for_adjustment|) | 106 // be converted into a string16 and returned. (|offset[s]_for_adjustment|) |
107 // specifies one or more offsets into the source strings; each offset will be | 107 // specifies one or more offsets into the source strings; each offset will be |
108 // adjusted to point at the same logical place in the result strings during | 108 // adjusted to point at the same logical place in the result strings during |
109 // decoding. If this isn't possible because an offset points past the end of | 109 // decoding. If this isn't possible because an offset points past the end of |
110 // the source strings or into the middle of a multibyte sequence, the offending | 110 // the source strings or into the middle of a multibyte sequence, the offending |
111 // offset will be set to string16::npos. |offset[s]_for_adjustment| may be NULL. | 111 // offset will be set to string16::npos. |offset[s]_for_adjustment| may be NULL. |
112 NET_API string16 UnescapeAndDecodeUTF8URLComponent( | 112 NET_EXPORT string16 UnescapeAndDecodeUTF8URLComponent( |
113 const std::string& text, | 113 const std::string& text, |
114 UnescapeRule::Type rules, | 114 UnescapeRule::Type rules, |
115 size_t* offset_for_adjustment); | 115 size_t* offset_for_adjustment); |
116 NET_API string16 UnescapeAndDecodeUTF8URLComponentWithOffsets( | 116 NET_EXPORT string16 UnescapeAndDecodeUTF8URLComponentWithOffsets( |
117 const std::string& text, | 117 const std::string& text, |
118 UnescapeRule::Type rules, | 118 UnescapeRule::Type rules, |
119 std::vector<size_t>* offsets_for_adjustment); | 119 std::vector<size_t>* offsets_for_adjustment); |
120 | 120 |
121 // Unescape the following ampersand character codes from |text|: | 121 // Unescape the following ampersand character codes from |text|: |
122 // < > & " ' | 122 // < > & " ' |
123 NET_API string16 UnescapeForHTML(const string16& text); | 123 NET_EXPORT string16 UnescapeForHTML(const string16& text); |
124 | 124 |
125 // Deprecated ------------------------------------------------------------------ | 125 // Deprecated ------------------------------------------------------------------ |
126 | 126 |
127 // Escapes characters in text suitable for use as a query parameter value. | 127 // Escapes characters in text suitable for use as a query parameter value. |
128 // We %XX everything except alphanumerics and -_.!~*'() | 128 // We %XX everything except alphanumerics and -_.!~*'() |
129 // Spaces change to "+" unless you pass usePlus=false. | 129 // Spaces change to "+" unless you pass usePlus=false. |
130 // This is basically the same as encodeURIComponent in javascript. | 130 // This is basically the same as encodeURIComponent in javascript. |
131 // For the string16 version, we do a conversion to charset before encoding the | 131 // For the string16 version, we do a conversion to charset before encoding the |
132 // string. If the charset doesn't exist, we return false. | 132 // string. If the charset doesn't exist, we return false. |
133 NET_API std::string EscapeQueryParamValue(const std::string& text, | 133 NET_EXPORT std::string EscapeQueryParamValue(const std::string& text, |
134 bool use_plus); | 134 bool use_plus); |
135 NET_API bool EscapeQueryParamValue(const string16& text, const char* codepage, | 135 NET_EXPORT bool EscapeQueryParamValue(const string16& text, |
136 bool use_plus, string16* escaped); | 136 const char* codepage, |
| 137 bool use_plus, |
| 138 string16* escaped); |
137 | 139 |
138 // A specialized version of EscapeQueryParamValue for string16s that | 140 // A specialized version of EscapeQueryParamValue for string16s that |
139 // assumes the codepage is UTF8. This is provided as a convenience. | 141 // assumes the codepage is UTF8. This is provided as a convenience. |
140 NET_API string16 EscapeQueryParamValueUTF8(const string16& text, bool use_plus); | 142 NET_EXPORT string16 EscapeQueryParamValueUTF8(const string16& text, |
| 143 bool use_plus); |
141 | 144 |
142 // Private Functions (Exposed for Unit Testing) -------------------------------- | 145 // Private Functions (Exposed for Unit Testing) -------------------------------- |
143 | 146 |
144 // A function called by std::for_each that will adjust any offset which occurs | 147 // A function called by std::for_each that will adjust any offset which occurs |
145 // after one or more encoded characters. | 148 // after one or more encoded characters. |
146 struct NET_TEST AdjustEncodingOffset { | 149 struct NET_EXPORT_PRIVATE AdjustEncodingOffset { |
147 typedef std::vector<size_t> Adjustments; | 150 typedef std::vector<size_t> Adjustments; |
148 | 151 |
149 explicit AdjustEncodingOffset(const Adjustments& adjustments); | 152 explicit AdjustEncodingOffset(const Adjustments& adjustments); |
150 void operator()(size_t& offset); | 153 void operator()(size_t& offset); |
151 | 154 |
152 const Adjustments& adjustments; | 155 const Adjustments& adjustments; |
153 }; | 156 }; |
154 | 157 |
155 #endif // NET_BASE_ESCAPE_H_ | 158 #endif // NET_BASE_ESCAPE_H_ |
OLD | NEW |