This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuri_encode_component.rb
180 lines (164 loc) · 5.55 KB
/
uri_encode_component.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ~*~ encoding: utf-8 ~*~
=begin
Copyright 2006-2008 the V8 project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=end
# This file provides the following methods:
# encodeURIComponent(componentString)
# string.charCodeAt(k)
# component must be String
def encodeURIComponent(componentString)
URI::URIEncodeComponent(componentString)
end
# define charCodeAt on String
class String
def charCodeAt(k)
# use scan, nil check, and unpack instead of ord for 1.8
# 1.9 can simply use self[k].ord
# http://stackoverflow.com/questions/7793177/split-utf8-string-regardless-of-ruby-version
c = self.scan(/./mu)[k]
return nil if c.nil?
c.unpack('U')[0]
end
end
module URI; class << self
# Does the char code correspond to an alpha-numeric char.
# isAlphaNumeric('a'.ord) => true
# isAlphaNumeric(''.ord) => false
def isAlphaNumeric(cc)
# a - z
if (97 <= cc && cc <= 122); return true end
# A - Z
if (65 <= cc && cc <= 90); return true end
# 0 - 9
if (48 <= cc && cc <= 57); return true end
return false
end
def unescapePredicate(cc)
if (isAlphaNumeric(cc)); return true end
# !
if (cc == 33); return true end
# '()*
if (39 <= cc && cc <= 42); return true end
# -.
if (45 <= cc && cc <= 46); return true end
# _
if (cc == 95); return true end
# ~
if (cc == 126); return true end
return false
end
def URIEncodeSingle(cc, result, index)
x = (cc >> 12) & 0xF;
y = (cc >> 6) & 63;
z = cc & 63;
octets = Array.new(3);
if (cc <= 0x007F)
octets[0] = cc;
elsif (cc <= 0x07FF)
octets[0] = y + 192;
octets[1] = z + 128;
else
octets[0] = x + 224;
octets[1] = y + 128;
octets[2] = z + 128;
end
return URIEncodeOctets(octets, result, index);
end
# Lazily initialized.
@@hexCharCodeArray = 0;
def URIAddEncodedOctetToBuffer(octet, result, index)
result[index] = 37; # Char code of '%'.
index += 1
result[index] = @@hexCharCodeArray[octet >> 4];
index += 1
result[index] = @@hexCharCodeArray[octet & 0x0F];
index += 1
return index;
end
def URIEncodeOctets(octets, result, index)
if (@@hexCharCodeArray == 0)
@@hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
65, 66, 67, 68, 69, 70];
end
index = URIAddEncodedOctetToBuffer(octets[0], result, index);
if (octets[1]); index = URIAddEncodedOctetToBuffer(octets[1], result, index) end
if (octets[2]); index = URIAddEncodedOctetToBuffer(octets[2], result, index) end
if (octets[3]); index = URIAddEncodedOctetToBuffer(octets[3], result, index) end
return index;
end
def URIEncodePair(cc1 , cc2, result, index)
u = ((cc1 >> 6) & 0xF) + 1;
w = (cc1 >> 2) & 0xF;
x = cc1 & 3;
y = (cc2 >> 6) & 0xF;
z = cc2 & 63;
octets = Array.new(4);
octets[0] = (u >> 2) + 240;
octets[1] = (((u & 3) << 4) | w) + 128;
octets[2] = ((x << 4) | y) + 128;
octets[3] = z + 128;
return URIEncodeOctets(octets, result, index);
end
# component must be String
def URIEncodeComponent(componentString)
Encode(componentString, :unescapePredicate);
end
# ECMA-262, section 15.1.3
def Encode(uri, unescape)
uriLength = uri.length;
# We are going to pass result to %StringFromCharCodeArray
# which does not expect any getters/setters installed
# on the incoming array.
result = Array.new(uriLength);
index = 0;
k = -1;
while ((k+=1) < uriLength) do
cc1 = uri.charCodeAt(k);
next if cc1.nil?
if (self.send(unescape, cc1))
result[index] = cc1;
index += 1
else
if (cc1 >= 0xDC00 && cc1 <= 0xDFFF); throw("URI malformed") end
if (cc1 < 0xD800 || cc1 > 0xDBFF)
index = URIEncodeSingle(cc1, result, index);
else
k+=1;
if (k == uriLength); throw("URI malformed") end
cc2 = uri.charCodeAt(k);
if (cc2 < 0xDC00 || cc2 > 0xDFFF); throw("URI malformed") end
index = URIEncodePair(cc1, cc2, result, index);
end
end
end
# use .compact to get rid of nils from charCodeAt
# return %StringFromCharCodeArray(result);
# 'c' = 8 bit signed char
# http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack
return result.compact.pack 'c*'
end
end # class << self
end # module