Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 8b265e8

Browse files
authored
Merge pull request #258 from microsoft/shem/max_redirect_state_update
Shem/max redirect state update
2 parents f64b0f8 + 191afce commit 8b265e8

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.1] - 2024-01-22
9+
10+
### Added
11+
12+
### Changed
13+
14+
- Fixed bug with redirect handler maintaing `max_redirect` across requests.[#246](https://github.com/microsoft/kiota-http-python/issues/246)
15+
816
## [1.2.0] - 2023-11-29
917

1018
### Added

kiota_http/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION: str = '1.2.0'
1+
VERSION: str = '1.2.1'

kiota_http/middleware/options/redirect_handler_option.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def __init__(
2525
raise ValueError(
2626
"MaxLimitExceeded. Negative value for max_redirect property is invalid"
2727
)
28-
2928
self._max_redirect = max_redirect
3029
self._should_redirect = should_redirect
3130
self._allow_redirect_on_scheme_change = allow_redirect_on_scheme_change

kiota_http/middleware/redirect_handler.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ def __init__(self, options: RequestOption = RedirectHandlerOption()) -> None:
3131
super().__init__()
3232
self.options = options
3333
self.redirect_on_status_codes: typing.Set[int] = self.DEFAULT_REDIRECT_STATUS_CODES
34-
self.history: typing.List[httpx.Request] = []
3534

36-
def increment(self, response, max_redirect) -> bool:
35+
def increment(self, response, max_redirect, history) -> bool:
3736
"""Increment the redirect attempts for this request.
3837
Args
3938
response(httpx.Response): A httpx response object.
4039
Returns:
4140
bool: Whether further redirect attempts are remaining.
4241
False if exhausted; True if more redirect attempts available.
4342
"""
44-
45-
self.history.append(response.request)
43+
history.append(response.request)
4644
return max_redirect >= 0
4745

4846
def get_redirect_location(self, response: httpx.Response) -> typing.Union[str, bool, None]:
@@ -69,28 +67,35 @@ async def send(
6967
_enable_span.set_attribute(REDIRECT_ENABLE_KEY, True)
7068
_enable_span.end()
7169

72-
retryable = True
73-
_redirect_span = self._create_observability_span(
74-
request, f"RedirectHandler_send - redirect {len(self.history)}"
75-
)
76-
while retryable:
70+
max_redirect = current_options.max_redirect
71+
history: typing.List[httpx.Request] = []
72+
73+
while max_redirect >= 0:
74+
_redirect_span = self._create_observability_span(
75+
request, f"RedirectHandler_send - redirect {len(history)}"
76+
)
7777
response = await super().send(request, transport)
7878
_redirect_span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response.status_code)
7979
redirect_location = self.get_redirect_location(response)
80+
8081
if redirect_location and current_options.should_redirect:
81-
current_options.max_redirect -= 1
82-
retryable = self.increment(response, current_options.max_redirect)
83-
_redirect_span.set_attribute(REDIRECT_COUNT_KEY, len(self.history))
82+
max_redirect -= 1
83+
if not self.increment(response, max_redirect, history[:]):
84+
break
85+
_redirect_span.set_attribute(REDIRECT_COUNT_KEY, len(history))
8486
new_request = self._build_redirect_request(request, response)
8587
request = new_request
8688
continue
87-
response.history = self.history
89+
90+
response.history = history
8891
break
89-
if not retryable:
92+
93+
if max_redirect < 0:
9094
exc = RedirectError(f"Too many redirects. {response.history}")
9195
_redirect_span.record_exception(exc)
9296
_redirect_span.end()
9397
raise exc
98+
9499
return response
95100

96101
def _get_current_options(self, request: httpx.Request) -> RedirectHandlerOption:

tests/middleware_tests/test_redirect_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ def test_increment_redirects():
4444
"""
4545
request = httpx.Request('GET', BASE_URL)
4646
response = httpx.Response(301, request=request)
47+
history = []
4748

4849
handler = RedirectHandler()
49-
assert handler.increment(response, handler.options.max_redirect)
50+
assert handler.increment(response, handler.options.max_redirect, history=history)
5051

5152

5253
def test_same_origin(mock_redirect_handler):

0 commit comments

Comments
 (0)