Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection handling changes #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions qgiscommons2/network/networkaccessmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
QgsMessageLog
)

# FIXME: ignored
DEFAULT_MAX_REDIRECTS = 4

class RequestsException(Exception):
Expand All @@ -52,6 +51,9 @@ class RequestsExceptionConnectionError(RequestsException):
class RequestsExceptionUserAbort(RequestsException):
pass

class RequestsMaxRedirects(RequestsException):
pass

class Map(dict):
"""
Example:
Expand Down Expand Up @@ -174,8 +176,10 @@ def auth_manager(self):
def request(self, url, method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None, blocking=True):
"""
Make a network request by calling QgsNetworkAccessManager.
redirections argument is ignored and is here only for httplib2 compatibility.
"""
self.calling_args = locals()
del self.calling_args['self']

self.msg_log(u'http_call request: {0}'.format(url))

self.blocking_mode = blocking
Expand Down Expand Up @@ -335,7 +339,19 @@ def replyFinished(self):

self.reply.deleteLater()
self.reply = None
self.request(redirectionUrl.toString())

if self.calling_args['redirections'] <= 0:
self.http_call_result.exception = RequestsMaxRedirects("Network error: Exceeded maximum redirects")
return

new_request = {}
request_changes = { 'url': redirectionUrl.toString(),
'redirections': self.calling_args['redirections']-1 }
new_request.update(self.calling_args)
new_request.update(request_changes)

# we want to skip the "normal" end of request on a redirect
return self.request(**new_request)

# really end request
else:
Expand Down