Skip to content

Commit 62cdf5e

Browse files
svenvcsvenvc
authored andcommitted
make ZnTooManyRedirects more intelligent (add a trail of followed URLs and two resume behaviors)
1 parent fc59039 commit 62cdf5e

File tree

11 files changed

+84
-19
lines changed

11 files changed

+84
-19
lines changed

repository/Zinc-HTTP.package/ZnClient.class/instance/executeWithRedirectsRemaining..st

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
private - protocol
2+
executeWithRedirectsRemaining: redirectCount trail: collectionOfUrls
3+
self getConnectionAndExecute.
4+
response isRedirect
5+
ifTrue: [
6+
(redirectCount > 0 and: [ self followRedirects ])
7+
ifTrue: [
8+
self prepareRedirect.
9+
collectionOfUrls add: self request url.
10+
self
11+
executeWithRedirectsRemaining: redirectCount - 1
12+
trail: collectionOfUrls ]
13+
ifFalse: [
14+
self followRedirects
15+
ifTrue: [ | exception |
16+
(exception := ZnTooManyRedirects new)
17+
trail: collectionOfUrls.
18+
exception signal = exception defaultResumeValue
19+
ifTrue: [
20+
"when resumed with default resume value, start over"
21+
self
22+
executeWithRedirectsRemaining: self maxNumberOfRedirects
23+
trail: collectionOfUrls ] ] ] ].
24+
^ self handleResponse

repository/Zinc-HTTP.package/ZnClient.class/instance/executeWithRetriesRemaining..st

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
private - protocol
22
executeWithRetriesRemaining: retryCount
3-
^ [ self executeWithRedirectsRemaining: self maxNumberOfRedirects ]
3+
^ [ self
4+
executeWithRedirectsRemaining: self maxNumberOfRedirects
5+
trail: OrderedCollection new ]
46
on: self retryExceptionSet
57
do: [ :exception |
68
retryCount > 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
ZnTooManyRedirects is signalled when an HTTP client has been following more redirects than allowed.
22

3+
The default resume behavior is to retry, signal with any other value to give up just return the redirect.
4+
35
Part of Zinc HTTP Components.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
private
2+
defaultResumeValue
3+
^ #retry
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
accessing
2+
trail: aCollectionOfUrls
3+
trail := aCollectionOfUrls
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
accessing
2+
trail
3+
^ trail
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
2-
"commentStamp" : "",
2+
"commentStamp" : "<historical>",
33
"super" : "Error",
44
"category" : "Zinc-HTTP-Exceptions",
55
"classinstvars" : [ ],
66
"pools" : [ ],
77
"classvars" : [ ],
8-
"instvars" : [ ],
8+
"instvars" : [
9+
"trail"
10+
],
911
"name" : "ZnTooManyRedirects",
1012
"type" : "normal"
1113
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
self packageOrganizer ensurePackage: #'Zinc-HTTP' withTags: #(#'Client-Server' #Core #Exceptions #Logging #Streaming #Support #Variables)!
1+
SystemOrganization addCategory: #'Zinc-HTTP'!
2+
SystemOrganization addCategory: #'Zinc-HTTP-Client-Server'!
3+
SystemOrganization addCategory: #'Zinc-HTTP-Core'!
4+
SystemOrganization addCategory: #'Zinc-HTTP-Exceptions'!
5+
SystemOrganization addCategory: #'Zinc-HTTP-Logging'!
6+
SystemOrganization addCategory: #'Zinc-HTTP-Streaming'!
7+
SystemOrganization addCategory: #'Zinc-HTTP-Support'!
8+
SystemOrganization addCategory: #'Zinc-HTTP-Variables'!

repository/Zinc-Tests.package/ZnClientTest.class/instance/testRedirect.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ testRedirect
1616
beOneShot;
1717
maxNumberOfRedirects: 0;
1818
get: target;
19-
response ] on: ZnTooManyRedirects do: [ :exception | exception resume ].
19+
response ] on: ZnTooManyRedirects do: [ :exception | exception resume: #doNotRetry ].
2020
self assert: response isRedirect
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
testing
2+
testRedirectLoopAndTrail
3+
self withServerDo: [ :server | | client count |
4+
server onRequestRespond: [ :request |
5+
request uri firstPathSegment = 'follow'
6+
ifTrue: [ ZnResponse redirect: 'follow' ] ].
7+
8+
(client := ZnClient new)
9+
url: server localUrl; addPath: 'follow';
10+
maxNumberOfRedirects: 10.
11+
self should: [ client get ] raise: ZnTooManyRedirects.
12+
client close.
13+
14+
(client := ZnClient new)
15+
url: server localUrl; addPath: 'follow';
16+
maxNumberOfRedirects: 10.
17+
[ client get ] on: ZnTooManyRedirects do: [ :exception |
18+
self assert: exception isResumable.
19+
self assert: exception trail size equals: 10.
20+
self assert: (exception trail allSatisfy: [ :each | each = (server localUrl / 'follow') ]) ].
21+
client close.
22+
23+
(client := ZnClient new)
24+
url: server localUrl; addPath: 'follow';
25+
maxNumberOfRedirects: 10.
26+
count := 0.
27+
[ client get ] on: ZnTooManyRedirects do: [ :exception |
28+
count := count + 1.
29+
exception trail size <= 30
30+
ifTrue: [ exception resume ]
31+
ifFalse: [ exception resume: #doNotRetry ] ].
32+
self assert: count equals: 4.
33+
client close ]

0 commit comments

Comments
 (0)