Skip to content
Myr

Fix #1955: Do not use original request in redirect

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The original request was never being properly overriden in resolve_redirects. As such being having a POST request respond with a 303 would generate a GET request. If the GET request encountered another redirect to something like a 307, then it would use the original request and generate another POST request. There are two parts to this fix: - The fix itself - The test infrastructure to ensure it does not regress because HTTPBin is insufficient

authored by Ian Cordasco · committed by Ian Cordasco · parent 110048f · Browse files · 2 files+65 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−1 removed

  • other:+65 added−1 removed

2 files+65 added−1 removed

modified:requests/sessions.py+4 added-1 removed
166 unmodified lines
167 new_auth = get_netrc_auth(url) if self.trust_env else None168 if new_auth is not None:169 prepared_request.prepare_auth(new_auth)170171 # Override the original request.172 req = prepared_request173174 resp = self.send(172 prepared_request,175 req,176 stream=stream,177 timeout=timeout,178 verify=verify,179 cert=cert,
395 unmodified lines
modified:test_requests.py+61 added
6 unmodified lines
7import json8import os9import pickle10import unittest11import collections1213import requests14import pytest15from requests.adapters import HTTPAdapter
2 unmodified lines
18 Morsel, cookielib, getproxies, str, urljoin, urlparse)19from requests.cookies import cookiejar_from_dict, morsel_to_cookie20from requests.exceptions import InvalidURL, MissingSchema21from requests.structures import CaseInsensitiveDict22from requests.sessions import SessionRedirectMixin2324try:25 import StringIO26except ImportError:
1161 unmodified lines
1188 except requests.exceptions.Timeout as e:1189 assert 'Read timed out' in e.args[0].args[0]119011911192SendCall = collections.namedtuple('SendCall', ('args', 'kwargs'))119311941195class RedirectSession(SessionRedirectMixin):1196 def __init__(self, order_of_redirects):1197 self.redirects = order_of_redirects1198 self.calls = []1199 self.max_redirects = 301200 self.cookies = {}1201 self.trust_env = False12021203 def send(self, *args, **kwargs):1204 self.calls.append(SendCall(args, kwargs))1205 return self.build_response()12061207 def build_response(self):1208 request = self.calls[-1].args[0]1209 r = requests.Response()12101211 try:1212 r.status_code = int(self.redirects.pop(0))1213 except IndexError:1214 r.status_code = 20012151216 r.headers = CaseInsensitiveDict({'Location': '/'})1217 r.raw = self._build_raw()1218 r.request = request1219 return r12201221 def _build_raw(self):1222 string = StringIO.StringIO('')1223 setattr(string, 'release_conn', lambda *args: args)1224 return string122512261227class TestRedirects:1228 default_keyword_args = {1229 'stream': False,1230 'verify': True,1231 'cert': None,1232 'timeout': None,1233 'allow_redirects': False,1234 'proxies': None,1235 }12361237 def test_requests_are_updated_each_time(self):1238 session = RedirectSession([303, 307])1239 prep = requests.Request('POST', 'http://httpbin.org/post').prepare()1240 r0 = session.send(prep)1241 assert r0.request.method == 'POST'1242 assert session.calls[-1] == SendCall((r0.request,), {})1243 redirect_generator = session.resolve_redirects(r0, prep)1244 for response in redirect_generator:1245 assert response.request.method == 'GET'1246 send_call = SendCall((response.request,),1247 TestRedirects.default_keyword_args)1248 assert session.calls[-1] == send_call124912501251if __name__ == '__main__':1252 unittest.main()