Skip to content

Commit ce93ede

Browse files
committed
Small changes for Mac-to-Windows compatibility
1 parent f836331 commit ce93ede

6 files changed

+59
-35
lines changed

specs/Excel-REST - Specs - Async.xlsm

218 Bytes
Binary file not shown.

specs/Excel-REST - Specs.xlsm

-40.9 KB
Binary file not shown.

specs/OAuth1AuthenticatorSpecs.bas

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Public Function Specs() As SpecSuite
6161
Request.AddParameter "c", "Howdy!"
6262
Request.AddQuerystringParam "d", 789
6363

64-
.Expect(Auth.RequestParameters(Client, Request)).ToEqual "a=123&b=456&c=Howdy%21&d=789"
64+
.Expect(Auth.RequestParameters(Client, Request)).ToEqual "a=123&b=456&c=Howdy!&d=789"
6565
End With
6666

6767
With Specs.It("should handle spaces in parameters correctly")

specs/RestHelpersSpecs.bas

+8-7
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ Public Function Specs() As SpecSuite
131131
End With
132132

133133
With Specs.It("should url encode values")
134-
.Expect(RestHelpers.UrlEncode(" !""#$%&'")).ToEqual "%20%21%22%23%24%25%26%27"
134+
.Expect(RestHelpers.UrlEncode("$&+,/:;=?@", EncodeUnsafe:=False)).ToEqual "%24%26%2B%2C%2F%3A%3B%3D%3F%40"
135+
.Expect(RestHelpers.UrlEncode(" ""<>#%{}|\^~[]`")).ToEqual "%20%22%3C%3E%23%25%7B%7D%7C%5C%5E%7E%5B%5D%60"
135136
.Expect(RestHelpers.UrlEncode("A + B")).ToEqual "A%20%2B%20B"
136-
.Expect(RestHelpers.UrlEncode("A + B", True)).ToEqual "A+%2B+B"
137+
.Expect(RestHelpers.UrlEncode("A + B", SpaceAsPlus:=True)).ToEqual "A+%2B+B"
137138
End With
138139

139140
With Specs.It("should decode url values")
@@ -157,7 +158,7 @@ Public Function Specs() As SpecSuite
157158
B.Add "d & e", "A + B"
158159

159160
Encoded = RestHelpers.ConvertToUrlEncoded(RestHelpers.CombineObjects(A, B))
160-
.Expect(Encoded).ToEqual "a=1&b=4.14&c=Howdy%21&d+%26+e=A+%2B+B"
161+
.Expect(Encoded).ToEqual "a=1&b=4.14&c=Howdy!&d+%26+e=A+%2B+B"
161162
End With
162163

163164
With Specs.It("should parse url-encoded string")
@@ -223,22 +224,22 @@ Public Function Specs() As SpecSuite
223224
End With
224225

225226
With Specs.It("should extract parts from url")
226-
Set Parts = RestHelpers.UrlParts("https://www.google.com/dir/1/2/search.html?message=Howdy World!&other=123#hash")
227+
Set Parts = RestHelpers.UrlParts("https://www.google.com/dir/1/2/search.html?message=Howdy%20World!&other=123#hash")
227228

228229
.Expect(Parts("Protocol")).ToEqual "https"
229230
.Expect(Parts("Host")).ToEqual "www.google.com"
230231
.Expect(Parts("Port")).ToEqual "443"
231232
.Expect(Parts("Path")).ToEqual "/dir/1/2/search.html"
232-
.Expect(Parts("Querystring")).ToEqual "message=Howdy World!&other=123"
233+
.Expect(Parts("Querystring")).ToEqual "message=Howdy%20World!&other=123"
233234
.Expect(Parts("Hash")).ToEqual "hash"
234235

235-
Set Parts = RestHelpers.UrlParts("localhost:3000/dir/1/2/page%202.html?message=Howdy%20World%21&other=123#hash")
236+
Set Parts = RestHelpers.UrlParts("localhost:3000/dir/1/2/page%202.html?message=Howdy%20World!&other=123#hash")
236237

237238
.Expect(Parts("Protocol")).ToEqual ""
238239
.Expect(Parts("Host")).ToEqual "localhost"
239240
.Expect(Parts("Port")).ToEqual "3000"
240241
.Expect(Parts("Path")).ToEqual "/dir/1/2/page%202.html"
241-
.Expect(Parts("Querystring")).ToEqual "message=Howdy%20World%21&other=123"
242+
.Expect(Parts("Querystring")).ToEqual "message=Howdy%20World!&other=123"
242243
.Expect(Parts("Hash")).ToEqual "hash"
243244
End With
244245

specs/RestRequestSpecs.bas

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ Public Function Specs() As SpecSuite
109109
With Specs.It("should URL encode querystring")
110110
Set Request = New RestRequest
111111

112-
Request.AddParameter "A B", " !""#$%&'"
112+
Request.AddParameter "A B", "$&+,/:;=?@"
113113
Request.Method = httpGET
114114

115-
.Expect(Request.FormattedResource).ToEqual "?A+B=+%21%22%23%24%25%26%27"
115+
.Expect(Request.FormattedResource).ToEqual "?A+B=%24%26%2B%2C%2F%3A%3B%3D%3F%40"
116116
End With
117117

118118
With Specs.It("should use body string directly if no parameters")
@@ -257,7 +257,7 @@ Public Function Specs() As SpecSuite
257257
.Expect(Request.Body).ToEqual "{""A"":123,""B"":""Howdy!""}"
258258

259259
Request.Format = formurlencoded
260-
.Expect(Request.Body).ToEqual "A=123&B=Howdy%21"
260+
.Expect(Request.Body).ToEqual "A=123&B=Howdy!"
261261
End With
262262

263263
With Specs.It("should allow array/collection for body")

src/RestHelpers.bas

+47-24
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,14 @@ End Function
332332

333333
''
334334
' Url encode the given string
335+
' Reference: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
335336
'
336337
' @param {Variant} Text The raw string to encode
337338
' @param {Boolean} [SpaceAsPlus = False] Use plus sign for encoded spaces (otherwise %20)
339+
' @param {Boolean} [EncodeUnsafe = True] Encode unsafe characters
338340
' @return {String} Encoded string
339341
' --------------------------------------------- '
340-
Public Function UrlEncode(Text As Variant, Optional SpaceAsPlus As Boolean = False) As String
342+
Public Function UrlEncode(Text As Variant, Optional SpaceAsPlus As Boolean = False, Optional EncodeUnsafe As Boolean = True) As String
341343
Dim UrlVal As String
342344
Dim StringLen As Long
343345

@@ -346,34 +348,39 @@ Public Function UrlEncode(Text As Variant, Optional SpaceAsPlus As Boolean = Fal
346348

347349
If StringLen > 0 Then
348350
ReDim Result(StringLen) As String
349-
Dim i As Long, charCode As Integer
350-
Dim Char As String, space As String
351+
Dim i As Long
352+
Dim CharCode As Integer
353+
Dim Char As String
354+
Dim Space As String
351355

352356
' Set space value
353357
If SpaceAsPlus Then
354-
space = "+"
358+
Space = "+"
355359
Else
356-
space = "%20"
360+
Space = "%20"
357361
End If
358362

359363
' Loop through string characters
360364
For i = 1 To StringLen
361365
' Get character and ascii code
362366
Char = Mid$(UrlVal, i, 1)
363-
charCode = Asc(Char)
364-
Select Case charCode
365-
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
366-
' Use original for AZaz09-._~
367-
Result(i) = Char
368-
Case 32
369-
' Add space
370-
Result(i) = space
371-
Case 0 To 15
372-
' Convert to hex w/ leading 0
373-
Result(i) = "%0" & Hex(charCode)
367+
CharCode = Asc(Char)
368+
369+
Select Case CharCode
370+
Case 36, 38, 43, 44, 47, 58, 59, 61, 63, 64
371+
' Reserved characters
372+
Result(i) = "%" & Hex(CharCode)
373+
Case 32, 34, 35, 37, 60, 62, 91 To 94, 96, 123 To 126
374+
' Unsafe characters
375+
If EncodeUnsafe Then
376+
If CharCode = 32 Then
377+
Result(i) = Space
378+
Else
379+
Result(i) = "%" & Hex(CharCode)
380+
End If
381+
End If
374382
Case Else
375-
' Convert to hex
376-
Result(i) = "%" & Hex(charCode)
383+
Result(i) = Char
377384
End Select
378385
Next i
379386
UrlEncode = Join(Result, "")
@@ -533,18 +540,32 @@ Public Function UrlParts(Url As String) As Dictionary
533540
"print ""Protocol="" . $url->scheme;" & vbNewLine & _
534541
"print "" | Host="" . $url->host;" & vbNewLine & _
535542
"print "" | Port="" . $url->port;" & vbNewLine & _
536-
"print "" | Path="" . $url->path;" & vbNewLine & _
537-
"print "" | Querystring="" . $url->query;" & vbNewLine & _
543+
"print "" | FullPath="" . $url->full_path;" & vbNewLine & _
538544
"print "" | Hash="" . $url->frag;" & vbNewLine & _
539545
"}'"
540-
546+
541547
Results = Split(ExecuteInShell(Command).Output, " | ")
542548
For Each ResultPart In Results
543549
EqualsIndex = InStr(1, ResultPart, "=")
544550
Key = Trim(VBA.Mid$(ResultPart, 1, EqualsIndex - 1))
545551
Value = Trim(VBA.Mid$(ResultPart, EqualsIndex + 1))
546552

547-
Parts.Add Key, Value
553+
If Key = "FullPath" Then
554+
' For properly escaped path and querystring, need to use full_path
555+
' But, need to split FullPath into Path...?Querystring
556+
Dim QueryIndex As Integer
557+
558+
QueryIndex = InStr(1, Value, "?")
559+
If QueryIndex > 0 Then
560+
Parts.Add "Path", Mid$(Value, 1, QueryIndex - 1)
561+
Parts.Add "Querystring", Mid$(Value, QueryIndex + 1)
562+
Else
563+
Parts.Add "Path", Value
564+
Parts.Add "Querystring", ""
565+
End If
566+
Else
567+
Parts.Add Key, Value
568+
End If
548569
Next ResultPart
549570

550571
If AddedProtocol And Parts.Exists("Protocol") Then
@@ -792,7 +813,9 @@ Public Function CreateResponseFromCURL(Result As ShellResult, Optional Format As
792813
Dim ErrorNumber As Long
793814

794815
ErrorNumber = Result.ExitCode / 256
795-
If ErrorNumber = 28 Then
816+
' 7 - CURLE_COULDNT_CONNECT
817+
' 28 - CURLE_OPERATION_TIMEDOUT
818+
If ErrorNumber = 7 Or ErrorNumber = 28 Then
796819
Set CreateResponseFromCURL = CreateResponse(StatusCodes.RequestTimeout, "Request Timeout")
797820
Else
798821
LogError "cURL Error: " & ErrorNumber, "RestHelpers.CreateResponseFromCURL"
@@ -1151,7 +1174,7 @@ Public Function ExecuteInShell(Command As String) As ShellResult
11511174
End If
11521175

11531176
Do While feof(File) = 0
1154-
Chunk = VBA.space$(50)
1177+
Chunk = VBA.Space$(50)
11551178
Read = fread(Chunk, 1, Len(Chunk) - 1, File)
11561179
If Read > 0 Then
11571180
Chunk = VBA.Left$(Chunk, Read)

0 commit comments

Comments
 (0)