-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathURI.rbbas
299 lines (265 loc) · 7.67 KB
/
URI.rbbas
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#tag Class
Protected Class URI
#tag Method, Flags = &h1
Protected Sub Constructor(URL As String)
' Pass a URI string to parse. e.g. http://user:password@www.example.com:8080/?foo=bar&bat=baz#Top
Dim isIPv6 As Boolean
If NthField(URL, ":", 1) <> "mailto" Then
If InStr(URL, "://") > 0 Then
Me.Scheme = NthField(URL, "://", 1)
URL = URL.Replace(Me.Scheme + "://", "")
End If
If Instr(URL, "@") > 0 Then // USER:PASS@Domain
Me.Username = NthField(URL, ":", 1)
URL = URL.Replace(Me.Username + ":", "")
Me.Password = NthField(URL, "@", 1)
URL = URL.Replace(Me.Password + "@", "")
End If
If Instr(URL, ":") > 0 And Left(URL, 1) <> "[" Then // Domain:Port
Dim s As String = NthField(URL, ":", 2)
s = NthField(s, "?", 1)
If Val(s) > 0 Then
Me.Port = Val(s)
URL = URL.Replace(":" + Format(Me.Port, "######"), "")
End If
ElseIf Left(URL, 1) = "[" And InStr(URL, "]:") > 0 Then ' ipv6 with port
isIPv6 = True
Dim s As String = NthField(URL, "]:", 2)
s = NthField(s, "?", 1)
Me.Port = Val(s)
URL = URL.Replace("]:" + Format(Me.Port, "######"), "]")
ElseIf Left(URL, 1) = "[" And InStr(URL, "]/") > 0 Then ' ipv6 with path
isIPv6 = True
'URL = URL.Replace("]/", "]")
End If
If Instr(URL, "#") > 0 Then
Me.Fragment = NthField(URL, "#", 2) // #fragment
URL = URL.Replace("#" + Me.Fragment, "")
End If
Me.Host = NthField(URL, "/", 1) // [sub.]domain.tld
URL = URL.Replace(Me.Host, "")
If InStr(URL, "?") > 0 Then
Dim tmp As String = NthField(URL, "?", 1)
Path = tmp // /foo/bar.php
URL = URL.Replace(tmp + "?", "")
Me.Arguments = Split(URL, "&")
ElseIf URL.Trim <> "" Then
Path = URL.Trim
URL = Replace(URL, Me.Path, "")
End If
Else
Me.Scheme = "mailto"
URL = Replace(URL, "mailto:", "")
Me.Username = NthField(URL, "@", 1)
URL = Replace(URL, Me.Username + "@", "")
If InStr(URL, "?") > 0 Then
Me.Host = NthField(URL, "?", 1)
Me.Arguments = Split(NthField(URL, "?", 2), "&")
Else
Me.Host = URL
End If
End If
Me.Scheme = EncodeURLComponent(Me.Scheme)
Me.Username = EncodeURLComponent(Me.Username)
Me.Password = EncodeURLComponent(Me.Password)
If Not isIPv6 Then Me.Host = EncodeURLComponent(Me.Host)
For Each arg As String In Me.Arguments
arg = EncodeURLComponent(arg)
Next
Me.Fragment = EncodeURLComponent(Me.Fragment)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub Constructor(BaseURL As URI, Relative As String)
Dim u1, u2 As URI
u1 = New URI(BaseURL.ToString)
u2 = New URI(Relative)
u1.Path = u2.Path
u1.Arguments = u2.Arguments
u1.Fragment = u2.Fragment
Me.Constructor(u1.ToString)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub Operator_Convert(NewValue As String)
Me.Constructor(NewValue)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function Path() As String
Dim s As String
For i As Integer = 0 To UBound(mPath)
If mPath(i).Trim = "" Then Continue
s = s + "/" + EncodeURLComponent(mPath(i))
Next
If mHasEndSlash Then s = s + "/"
Return s
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub Path(Assigns NewPath As String)
Dim s() As String = Split(DecodeURLComponent(NewPath), "/")
ReDim mPath(-1)
For i As Integer = 0 To UBound(s)
If s(i).Trim <> "" Then mPath.Append(s(i))
Next
If s.Ubound > -1 Then mHasEndSlash = (s(s.Ubound) = "") Else mHasEndSlash = False
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function ToString() As String
Dim URL As String
If Scheme = "mailto" Then
URL = "mailto:"
Else
If Scheme <> "" Then URL = EncodeURLComponent(Scheme) + "://"
End If
If Username <> "" Then
URL = URL + EncodeURLComponent(Username)
If Scheme <> "mailto" Then URL = URL + ":"
If Password <> "" Then URL = URL + EncodeURLComponent(Password)
URL = URL + "@"
End If
If Left(Host, 1) = "[" And Right(Host, 1) = "]" Then ' IPv6 literal
URL = URL + Host
Else
URL = URL + EncodeURLComponent(Host)
End If
If Port > -1 Then
URL = URL + ":" + Format(Port, "####0")
End If
URL = URL + Me.Path
If Arguments.Ubound > -1 Then
Dim args As String = "?"
Dim acount As Integer = UBound(Arguments)
For i As Integer = 0 To acount
If i > 0 Then args = args + "&"
If EncodeArguments Then
args = args + EncodeURLComponent(Arguments(i))
Else
args = args + Arguments(i)
End If
Next
URL = URL + args
End If
If Fragment <> "" Then
URL = URL + "#" + EncodeURLComponent(Fragment)
End If
If URL.Trim = "" Then URL = "/"
Return URL
End Function
#tag EndMethod
#tag Property, Flags = &h0
Arguments() As String
#tag EndProperty
#tag Property, Flags = &h0
EncodeArguments As Boolean = False
#tag EndProperty
#tag Property, Flags = &h0
Fragment As String
#tag EndProperty
#tag Property, Flags = &h0
Host As String
#tag EndProperty
#tag Property, Flags = &h21
Private mHasEndSlash As Boolean
#tag EndProperty
#tag Property, Flags = &h1
Protected mPath() As String
#tag EndProperty
#tag Property, Flags = &h0
Password As String
#tag EndProperty
#tag Property, Flags = &h0
Port As Integer = -1
#tag EndProperty
#tag Property, Flags = &h0
Scheme As String
#tag EndProperty
#tag Property, Flags = &h0
Username As String
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="EncodeArguments"
Group="Behavior"
InitialValue="False"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="Fragment"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Host"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
Type="String"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Password"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Port"
Group="Behavior"
InitialValue="80"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Scheme"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
Type="String"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Username"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass