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