Skip to content

Commit

Permalink
make ZnTooManyRedirects more intelligent (add a trail of followed URL…
Browse files Browse the repository at this point in the history
…s and two resume behaviors)
  • Loading branch information
svenvc authored and svenvc committed May 10, 2024
1 parent fc59039 commit 62cdf5e
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 19 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
private - protocol
executeWithRedirectsRemaining: redirectCount trail: collectionOfUrls
self getConnectionAndExecute.
response isRedirect
ifTrue: [
(redirectCount > 0 and: [ self followRedirects ])
ifTrue: [
self prepareRedirect.
collectionOfUrls add: self request url.
self
executeWithRedirectsRemaining: redirectCount - 1
trail: collectionOfUrls ]
ifFalse: [
self followRedirects
ifTrue: [ | exception |
(exception := ZnTooManyRedirects new)
trail: collectionOfUrls.
exception signal = exception defaultResumeValue
ifTrue: [
"when resumed with default resume value, start over"
self
executeWithRedirectsRemaining: self maxNumberOfRedirects
trail: collectionOfUrls ] ] ] ].
^ self handleResponse
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
private - protocol
executeWithRetriesRemaining: retryCount
^ [ self executeWithRedirectsRemaining: self maxNumberOfRedirects ]
^ [ self
executeWithRedirectsRemaining: self maxNumberOfRedirects
trail: OrderedCollection new ]
on: self retryExceptionSet
do: [ :exception |
retryCount > 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ZnTooManyRedirects is signalled when an HTTP client has been following more redirects than allowed.

The default resume behavior is to retry, signal with any other value to give up just return the redirect.

Part of Zinc HTTP Components.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
private
defaultResumeValue
^ #retry
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
trail: aCollectionOfUrls
trail := aCollectionOfUrls
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
trail
^ trail
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"commentStamp" : "",
"commentStamp" : "<historical>",
"super" : "Error",
"category" : "Zinc-HTTP-Exceptions",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"instvars" : [
"trail"
],
"name" : "ZnTooManyRedirects",
"type" : "normal"
}
9 changes: 8 additions & 1 deletion repository/Zinc-HTTP.package/monticello.meta/categories.st
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
self packageOrganizer ensurePackage: #'Zinc-HTTP' withTags: #(#'Client-Server' #Core #Exceptions #Logging #Streaming #Support #Variables)!
SystemOrganization addCategory: #'Zinc-HTTP'!
SystemOrganization addCategory: #'Zinc-HTTP-Client-Server'!
SystemOrganization addCategory: #'Zinc-HTTP-Core'!
SystemOrganization addCategory: #'Zinc-HTTP-Exceptions'!
SystemOrganization addCategory: #'Zinc-HTTP-Logging'!
SystemOrganization addCategory: #'Zinc-HTTP-Streaming'!
SystemOrganization addCategory: #'Zinc-HTTP-Support'!
SystemOrganization addCategory: #'Zinc-HTTP-Variables'!
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ testRedirect
beOneShot;
maxNumberOfRedirects: 0;
get: target;
response ] on: ZnTooManyRedirects do: [ :exception | exception resume ].
response ] on: ZnTooManyRedirects do: [ :exception | exception resume: #doNotRetry ].
self assert: response isRedirect
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
testing
testRedirectLoopAndTrail
self withServerDo: [ :server | | client count |
server onRequestRespond: [ :request |
request uri firstPathSegment = 'follow'
ifTrue: [ ZnResponse redirect: 'follow' ] ].

(client := ZnClient new)
url: server localUrl; addPath: 'follow';
maxNumberOfRedirects: 10.
self should: [ client get ] raise: ZnTooManyRedirects.
client close.

(client := ZnClient new)
url: server localUrl; addPath: 'follow';
maxNumberOfRedirects: 10.
[ client get ] on: ZnTooManyRedirects do: [ :exception |
self assert: exception isResumable.
self assert: exception trail size equals: 10.
self assert: (exception trail allSatisfy: [ :each | each = (server localUrl / 'follow') ]) ].
client close.

(client := ZnClient new)
url: server localUrl; addPath: 'follow';
maxNumberOfRedirects: 10.
count := 0.
[ client get ] on: ZnTooManyRedirects do: [ :exception |
count := count + 1.
exception trail size <= 30
ifTrue: [ exception resume ]
ifFalse: [ exception resume: #doNotRetry ] ].
self assert: count equals: 4.
client close ]

0 comments on commit 62cdf5e

Please sign in to comment.