| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Runtime |
| | | 8 | | { |
| | | 9 | | internal static class UrlUtility |
| | | 10 | | { |
| | | 11 | | public static string UrlDecode(string str, Encoding e) |
| | | 12 | | { |
| | 494 | 13 | | if (str == null) |
| | | 14 | | { |
| | 0 | 15 | | return null; |
| | | 16 | | } |
| | 494 | 17 | | return UrlDecodeStringFromStringInternal(str, e); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | private static string UrlDecodeStringFromStringInternal(string s, Encoding e) |
| | | 21 | | { |
| | 494 | 22 | | int count = s.Length; |
| | 494 | 23 | | UrlDecoder helper = new UrlDecoder(count, e); |
| | | 24 | | |
| | | 25 | | // go through the string's chars collapsing %XX and %uXXXX and |
| | | 26 | | // appending each char as char, with exception of %XX constructs |
| | | 27 | | // that are appended as bytes |
| | | 28 | | |
| | 55926 | 29 | | for (int pos = 0; pos < count; pos++) |
| | | 30 | | { |
| | 27469 | 31 | | char ch = s[pos]; |
| | | 32 | | |
| | 27469 | 33 | | if (ch == '+') |
| | | 34 | | { |
| | 0 | 35 | | ch = ' '; |
| | | 36 | | } |
| | 27469 | 37 | | else if (ch == '%' && pos < count - 2) |
| | | 38 | | { |
| | 16 | 39 | | if (s[pos + 1] == 'u' && pos < count - 5) |
| | | 40 | | { |
| | 0 | 41 | | int h1 = HexToInt(s[pos + 2]); |
| | 0 | 42 | | int h2 = HexToInt(s[pos + 3]); |
| | 0 | 43 | | int h3 = HexToInt(s[pos + 4]); |
| | 0 | 44 | | int h4 = HexToInt(s[pos + 5]); |
| | | 45 | | |
| | 0 | 46 | | if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) |
| | | 47 | | { // valid 4 hex chars |
| | 0 | 48 | | ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4); |
| | 0 | 49 | | pos += 5; |
| | | 50 | | |
| | | 51 | | // only add as char |
| | 0 | 52 | | helper.AddChar(ch); |
| | 0 | 53 | | continue; |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 16 | 58 | | int h1 = HexToInt(s[pos + 1]); |
| | 16 | 59 | | int h2 = HexToInt(s[pos + 2]); |
| | | 60 | | |
| | 16 | 61 | | if (h1 >= 0 && h2 >= 0) |
| | | 62 | | { // valid 2 hex chars |
| | 16 | 63 | | byte b = (byte)((h1 << 4) | h2); |
| | 16 | 64 | | pos += 2; |
| | | 65 | | |
| | | 66 | | // don't add as char |
| | 16 | 67 | | helper.AddByte(b); |
| | 16 | 68 | | continue; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 27453 | 73 | | if ((ch & 0xFF80) == 0) |
| | | 74 | | { |
| | 27453 | 75 | | helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode |
| | | 76 | | } |
| | | 77 | | else |
| | | 78 | | { |
| | 0 | 79 | | helper.AddChar(ch); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 494 | 83 | | return helper.GetString(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // Private helpers for URL encoding/decoding |
| | | 87 | | private static int HexToInt(char h) |
| | | 88 | | { |
| | 32 | 89 | | return (h >= '0' && h <= '9') ? h - '0' : |
| | 32 | 90 | | (h >= 'a' && h <= 'f') ? h - 'a' + 10 : |
| | 32 | 91 | | (h >= 'A' && h <= 'F') ? h - 'A' + 10 : |
| | 32 | 92 | | -1; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // Internal class to facilitate URL decoding -- keeps char buffer and byte buffer, allows appending of either ch |
| | | 96 | | private class UrlDecoder |
| | | 97 | | { |
| | | 98 | | private readonly int _bufferSize; |
| | | 99 | | |
| | | 100 | | // Accumulate characters in a special array |
| | | 101 | | private int _numChars; |
| | | 102 | | private readonly char[] _charBuffer; |
| | | 103 | | |
| | | 104 | | // Accumulate bytes for decoding into characters in a special array |
| | | 105 | | private int _numBytes; |
| | | 106 | | private byte[] _byteBuffer; |
| | | 107 | | |
| | | 108 | | // Encoding to convert chars to bytes |
| | | 109 | | private readonly Encoding _encoding; |
| | | 110 | | |
| | | 111 | | private void FlushBytes() |
| | | 112 | | { |
| | 494 | 113 | | if (_numBytes > 0) |
| | | 114 | | { |
| | 494 | 115 | | _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars); |
| | 494 | 116 | | _numBytes = 0; |
| | | 117 | | } |
| | 494 | 118 | | } |
| | | 119 | | |
| | 494 | 120 | | internal UrlDecoder(int bufferSize, Encoding encoding) |
| | | 121 | | { |
| | 494 | 122 | | _bufferSize = bufferSize; |
| | 494 | 123 | | _encoding = encoding; |
| | | 124 | | |
| | 494 | 125 | | _charBuffer = new char[bufferSize]; |
| | | 126 | | // byte buffer created on demand |
| | 494 | 127 | | } |
| | | 128 | | |
| | | 129 | | internal void AddChar(char ch) |
| | | 130 | | { |
| | 0 | 131 | | if (_numBytes > 0) |
| | | 132 | | { |
| | 0 | 133 | | FlushBytes(); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | _charBuffer[_numChars++] = ch; |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | internal void AddByte(byte b) |
| | | 140 | | { |
| | | 141 | | // if there are no pending bytes treat 7 bit bytes as characters |
| | | 142 | | // this optimization is temp disable as it doesn't work for some encodings |
| | | 143 | | |
| | | 144 | | //if (_numBytes == 0 && ((b & 0x80) == 0)) { |
| | | 145 | | // AddChar((char)b); |
| | | 146 | | //} |
| | | 147 | | //else |
| | | 148 | | |
| | | 149 | | { |
| | 27469 | 150 | | if (_byteBuffer == null) |
| | | 151 | | { |
| | 494 | 152 | | _byteBuffer = new byte[_bufferSize]; |
| | | 153 | | } |
| | | 154 | | |
| | 27469 | 155 | | _byteBuffer[_numBytes++] = b; |
| | | 156 | | } |
| | 27469 | 157 | | } |
| | | 158 | | |
| | | 159 | | internal string GetString() |
| | | 160 | | { |
| | 494 | 161 | | if (_numBytes > 0) |
| | | 162 | | { |
| | 494 | 163 | | FlushBytes(); |
| | | 164 | | } |
| | | 165 | | |
| | 494 | 166 | | if (_numChars > 0) |
| | | 167 | | { |
| | 494 | 168 | | return new string(_charBuffer, 0, _numChars); |
| | | 169 | | } |
| | | 170 | | else |
| | | 171 | | { |
| | 0 | 172 | | return string.Empty; |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | } |