Skip to content

Commit 771fc76

Browse files
committed
Set Accept-Encoding to identity by default
Fixes #370
1 parent f400fbc commit 771fc76

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

specs/Specs_WebRequest.bas

+21-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ Public Function Specs() As SpecSuite
191191
.Expect(Request.Accept).ToEqual "x-custom/text"
192192
End With
193193

194+
' AcceptEncoding
195+
' --------------------------------------------- '
196+
With Specs.It("AcceptEncoding should be set to identity by default")
197+
Set Request = New WebRequest
198+
199+
.Expect(Request.AcceptEncoding).ToEqual "identity"
200+
End With
201+
202+
With Specs.It("AcceptEncoding should allow override")
203+
Set Request = New WebRequest
204+
205+
Request.AcceptEncoding = "gzip"
206+
207+
.Expect(Request.AcceptEncoding).ToEqual "gzip"
208+
End With
209+
194210
' ContentLength
195211
' --------------------------------------------- '
196212
With Specs.It("ContentLength should be set from length of Body")
@@ -502,15 +518,17 @@ Public Function Specs() As SpecSuite
502518
Request.Method = WebMethod.HttpPost
503519
Request.ContentType = "text/plain"
504520
Request.Accept = "text/csv"
521+
Request.AcceptEncoding = "gzip"
505522
Request.ContentLength = 100
506523

507524
.Expect(Request.Headers.Count).ToEqual 0
508525

509526
Request.Prepare
510527

511-
.Expect(Request.Headers.Count).ToBeGTE 3
528+
.Expect(Request.Headers.Count).ToBeGTE 4
512529
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Content-Type")).ToEqual "text/plain"
513530
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Accept")).ToEqual "text/csv"
531+
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Accept-Encoding")).ToEqual "gzip"
514532
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Content-Length")).ToEqual "100"
515533
End With
516534

@@ -538,12 +556,12 @@ Public Function Specs() As SpecSuite
538556

539557
Request.Prepare
540558

541-
.Expect(Request.Headers.Count).ToEqual 2
559+
.Expect(Request.Headers.Count).ToEqual 3
542560

543561
Request.Body = "non-empty"
544562
Request.Prepare
545563

546-
.Expect(Request.Headers.Count).ToEqual 4
564+
.Expect(Request.Headers.Count).ToEqual 5
547565

548566
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Content-Type")).ToEqual "text/plain"
549567
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Content-Length")).ToEqual "9"

src/WebClient.cls

+5
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,11 @@ Public Function PrepareCurlRequest(Request As WebRequest) As String
642642
If Me.FollowRedirects Then
643643
web_Curl = web_Curl & " --location"
644644
End If
645+
646+
' Enable compressed if Accept-Encoding != "identity"
647+
If Request.AcceptEncoding <> "identity" Then
648+
web_Curl = web_Curl & " --compressed"
649+
End If
645650

646651
' Set headers and cookies
647652
For Each web_KeyValue In Request.Headers

src/WebRequest.cls

+22
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Private web_pBody As Variant
5959
Private web_pConvertedBody As Variant
6060
Private web_pContentType As String
6161
Private web_pAccept As String
62+
Private web_pAcceptEncoding As String
6263
Private web_pContentLength As Long
6364
Private web_pId As String
6465

@@ -413,6 +414,26 @@ Public Property Let Accept(Value As String)
413414
web_pAccept = Value
414415
End Property
415416

417+
''
418+
' WinHTTP does not support decompression at this time (Jan. 2019).
419+
' If not Accept-Encoding is passed to the server, [RFC 7231](https://tools.ietf.org/html/rfc7231#section-5.3.4)
420+
' states that "any content-coding is considered acceptable by the user agent"
421+
' -> Explicitly set Accept-Encoding
422+
'
423+
' cURL supports --compressed, which automatically decompresses gzip and other compressed responses
424+
' -> If AcceptEncoding != "identity", enable --compressed flag
425+
''
426+
Public Property Get AcceptEncoding() As String
427+
If web_pAcceptEncoding <> "" Then
428+
AcceptEncoding = web_pAcceptEncoding
429+
Else
430+
AcceptEncoding = "identity"
431+
End If
432+
End Property
433+
Public Property Let AcceptEncoding(Value As String)
434+
web_pAcceptEncoding = Value
435+
End Property
436+
416437
''
417438
' Set automatically by length of `Body`,
418439
' but can be overriden to set `Content-Length` header for request.
@@ -741,6 +762,7 @@ Public Sub Prepare()
741762
' Add/replace general headers for request
742763
SetHeader "User-Agent", Me.UserAgent
743764
SetHeader "Accept", Me.Accept
765+
SetHeader "Accept-Encoding", Me.AcceptEncoding
744766
If Me.Method <> WebMethod.HttpGet Or Me.ContentLength > 0 Then
745767
SetHeader "Content-Type", Me.ContentType
746768
SetHeader "Content-Length", VBA.CStr(Me.ContentLength)

0 commit comments

Comments
 (0)