Skip to content

Commit 288e58f

Browse files
committed
Add encoding for cookie name
1 parent 160959d commit 288e58f

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

specs/Specs_WebRequest.bas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ Public Function Specs() As SpecSuite
338338
With Specs.It("should AddCookie")
339339
Set Request = New WebRequest
340340

341-
Request.AddCookie "A", "cookie"
341+
Request.AddCookie "A[1]", "cookie"
342342
Request.AddCookie "B", "cookie 2"
343343

344344
.Expect(Request.Cookies.Count).ToEqual 2
345-
.Expect(Request.Cookies(1)("Key")).ToEqual "A"
345+
.Expect(Request.Cookies(1)("Key")).ToEqual "A%5B1%5D"
346346
.Expect(Request.Cookies(2)("Value")).ToEqual "cookie%202"
347347
End With
348348

src/WebRequest.cls

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ End Sub
678678
''
679679
Public Sub AddCookie(Key As String, Value As Variant)
680680
Me.Cookies.Add WebHelpers.CreateKeyValue( _
681-
Key, _
681+
web_EncodeCookieName(Key), _
682682
WebHelpers.UrlEncode(Value, EncodingMode:=UrlEncodingMode.CookieUrlEncoding) _
683683
)
684684
End Sub
@@ -810,6 +810,55 @@ End Sub
810810
' Private Functions
811811
' ============================================= '
812812

813+
' Encode cookie name
814+
'
815+
' References:
816+
' - RFC 6265 https://tools.ietf.org/html/rfc6265
817+
Private Function web_EncodeCookieName(web_CookieName As Variant) As String
818+
Dim web_CookieVal As String
819+
Dim web_StringLen As Long
820+
821+
web_CookieVal = VBA.CStr(web_CookieName)
822+
web_StringLen = VBA.Len(web_CookieVal)
823+
824+
If web_StringLen > 0 Then
825+
Dim web_Result() As String
826+
Dim web_i As Long
827+
Dim web_CharCode As Integer
828+
Dim web_Char As String
829+
ReDim web_Result(web_StringLen)
830+
831+
' ALPHA / DIGIT / "!" / "#" / "$" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
832+
' Note: "%" is allowed in spec, but is currently excluded due to parsing issues
833+
834+
' Loop through string characters
835+
For web_i = 1 To web_StringLen
836+
' Get character and ascii code
837+
web_Char = VBA.Mid$(web_CookieVal, web_i, 1)
838+
web_CharCode = VBA.Asc(web_Char)
839+
840+
Select Case web_CharCode
841+
Case 65 To 90, 97 To 122
842+
' ALPHA
843+
web_Result(web_i) = web_Char
844+
Case 48 To 57
845+
' DIGIT
846+
web_Result(web_i) = web_Char
847+
Case 33, 35, 36, 38, 39, 42, 43, 45, 46, 94, 95, 96, 124, 126
848+
' "!" / "#" / "$" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
849+
web_Result(web_i) = web_Char
850+
851+
Case 0 To 15
852+
web_Result(web_i) = "%0" & VBA.Hex(web_CharCode)
853+
Case Else
854+
web_Result(web_i) = "%" & VBA.Hex(web_CharCode)
855+
End Select
856+
Next web_i
857+
858+
web_EncodeCookieName = VBA.Join$(web_Result, "")
859+
End If
860+
End Function
861+
813862
Private Sub Class_Initialize()
814863
' Set default values
815864
Me.RequestFormat = WebFormat.Json

0 commit comments

Comments
 (0)