Skip to content

Commit 2c6b119

Browse files
committed
Merge pull request #159 from VBA-tools/follow-redirects
Follow redirects
2 parents 4b66747 + 8a06904 commit 2c6b119

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

specs/Specs_WebClient.bas

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ Public Function Specs() As SpecSuite
8888
Client.Insecure = False
8989
End With
9090

91+
' FollowRedirects
92+
' --------------------------------------------- '
93+
With Specs.It(" should FollowRedirects")
94+
Set Request = New WebRequest
95+
Request.Resource = "redirect/5"
96+
Request.Format = WebFormat.PlainText
97+
98+
Client.FollowRedirects = True
99+
Set Response = Client.Execute(Request)
100+
.Expect(Response.StatusCode).ToEqual WebStatusCode.Ok
101+
102+
Client.FollowRedirects = False
103+
Set Response = Client.Execute(Request)
104+
.Expect(Response.StatusCode).ToEqual 302
105+
End With
106+
91107
' ============================================= '
92108
' Public Methods
93109
' ============================================= '
@@ -242,18 +258,6 @@ Public Function Specs() As SpecSuite
242258

243259
' SetProxy
244260

245-
' GetRedirectLocation
246-
' --------------------------------------------- '
247-
With Specs.It("should GetRedirectLocation of Request")
248-
Set Request = New WebRequest
249-
Request.Resource = "redirect/1"
250-
251-
Dim RedirectLocation As String
252-
RedirectLocation = Client.GetRedirectLocation(Request)
253-
254-
.Expect(RedirectLocation).ToEqual "/get"
255-
End With
256-
257261
' GetFullUrl
258262
' --------------------------------------------- '
259263
With Specs.It("should GetFullUrl of Request")

src/WebClient.cls

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ Public EnableAutoProxy As Boolean
183183
''
184184
Public Insecure As Boolean
185185

186+
''
187+
' Follow redirects (301, 302, 307) using Location header
188+
'
189+
' @property FollowRedirects
190+
' @type Boolean
191+
' @default True
192+
''
193+
Public FollowRedirects As Boolean
194+
186195
''
187196
' Proxy server to pass requests through (except for those that match `ProxyBypassList`).
188197
'
@@ -449,26 +458,6 @@ Public Sub SetProxy(ProxyServer As String, _
449458
Me.ProxyBypassList = BypassList
450459
End Sub
451460

452-
''
453-
' If the given Request is a redirect (301, 302, or 307),
454-
' then get value of the "Location" header
455-
'
456-
' @param {WebRequest} Request
457-
' @return {String}
458-
''
459-
Public Function GetRedirectLocation(Request As WebRequest) As String
460-
Dim RedirectRequest As WebRequest
461-
Set RedirectRequest = Request.Clone
462-
RedirectRequest.Method = WebMethod.HttpHead
463-
464-
Dim Response As WebResponse
465-
Set Response = Me.Execute(RedirectRequest)
466-
467-
If Response.StatusCode = 301 Or Response.StatusCode = 302 Or Response.StatusCode = 307 Then
468-
GetRedirectLocation = WebHelpers.FindInKeyValues(Response.Headers, "Location")
469-
End If
470-
End Function
471-
472461
''
473462
' Get full url by joining given `WebRequest.FormattedResource` and `BaseUrl`.
474463
'
@@ -535,24 +524,23 @@ Public Function PrepareHttpRequest(Request As WebRequest, Optional Async As Bool
535524
' Invalid common name (CN), 0x1000
536525
' Invalid date or certificate expired, 0x2000
537526
' = 0x3300 = 13056
538-
' - Enable redirects
539527
' - Enable https-to-http redirects
540528
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableCertificateRevocationCheck) = False
541529
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_SslErrorIgnoreFlags) = 13056
542-
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableRedirects) = True
543530
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableHttpsToHttpRedirects) = True
544531
Else
545532
' By default:
546533
' - Enable certificate revocation check (especially useful after HeartBleed)
547534
' - Ignore no SLL erros
548-
' - Disable redirects (matches cURL behavior)
549535
' - Disable https-to-http redirects
550536
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableCertificateRevocationCheck) = True
551537
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_SslErrorIgnoreFlags) = 0
552-
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableRedirects) = False
553538
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableHttpsToHttpRedirects) = False
554539
End If
555540

541+
' Setup redirects
542+
web_Http.Option(web_WinHttpRequestOption.web_WinHttpRequestOption_EnableRedirects) = Me.FollowRedirects
543+
556544
' Set headers on http request (after open)
557545
For Each web_KeyValue In Request.Headers
558546
web_Http.SetRequestHeader web_KeyValue("Key"), web_KeyValue("Value")
@@ -623,6 +611,11 @@ Public Function PrepareCurlRequest(Request As WebRequest) As String
623611
web_Curl = web_Curl & " --insecure"
624612
End If
625613

614+
' Setup redirects
615+
If Me.FollowRedirects Then
616+
web_Curl = web_Curl & " --location"
617+
End If
618+
626619
' Set headers and cookies
627620
For Each web_KeyValue In Request.Headers
628621
web_Curl = web_Curl & " -H '" & web_KeyValue("Key") & ": " & web_KeyValue("Value") & "'"
@@ -734,4 +727,5 @@ Private Sub Class_Initialize()
734727
Me.TimeoutMs = web_DefaultTimeoutMs
735728
Me.EnableAutoProxy = False
736729
Me.Insecure = False
730+
Me.FollowRedirects = True
737731
End Sub

0 commit comments

Comments
 (0)