Skip to content
Myr

Merge pull request #4452 from nateprewitt/append_fragment_on_redirect

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

append previous url fragment on redirect

authored by Nate Prewitt · committed by GitHub · parents f89c325, f60324a · Browse files · 3 files+86 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

3 files+86 added−1 removed

  • tests:+80 added−0 removed
  • other:+6 added−1 removed

3 files+86 added−1 removed

modified:requests/sessions.py+6 added-1 removed
121 unmodified lines
122123 hist = [] # keep track of history124125 url = self.get_redirect_target(resp)126 previous_fragment = urlparse(req.url).fragment127 while url:128 prepared_request = req.copy()129130 # Update history and keep track of redirects.
16 unmodified lines
147 if url.startswith('//'):148 parsed_rurl = urlparse(resp.url)149 url = '%s:%s' % (to_native_string(parsed_rurl.scheme), url)150150 # The scheme should be lower case...151 # Normalize url case and attach previous fragment if needed (RFC 7231 7.1.2)152 parsed = urlparse(url)153 if parsed.fragment == '' and previous_fragment:154 parsed = parsed._replace(fragment=previous_fragment)155 elif parsed.fragment:156 previous_fragment = parsed.fragment157 url = parsed.geturl()158159 # Facilitate relative 'location' headers, as allowed by RFC 7231.160 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
582 unmodified lines
modified:tests/test_lowlevel.py+72 added
233 unmodified lines
234 assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')235 assert r.url == u'{0}/{1}'.format(url, expected_path.decode('ascii'))236237 close_server.set()238239def test_fragment_not_sent_with_request():240 """Verify that the fragment portion of a URI isn't sent to the server."""241 def response_handler(sock):242 req = consume_socket_content(sock, timeout=0.5)243 sock.send(244 b'HTTP/1.1 200 OK\r\n'245 b'Content-Length: '+bytes(len(req))+b'\r\n'246 b'\r\n'+req247 )248249 close_server = threading.Event()250 server = Server(response_handler, wait_to_close_event=close_server)251252 with server as (host, port):253 url = 'http://{0}:{1}/path/to/thing/#view=edit&token=hunter2'.format(host, port)254 r = requests.get(url)255 raw_request = r.content256257 assert r.status_code == 200258 headers, body = raw_request.split(b'\r\n\r\n', 1)259 status_line, headers = headers.split(b'\r\n', 1)260261 assert status_line == b'GET /path/to/thing/ HTTP/1.1'262 for frag in (b'view', b'edit', b'token', b'hunter2'):263 assert frag not in headers264 assert frag not in body265266 close_server.set()267268def test_fragment_update_on_redirect():269 """Verify we only append previous fragment if one doesn't exist on new270 location. If a new fragment is encounterd in a Location header, it should271 be added to all subsequent requests.272 """273274 def response_handler(sock):275 consume_socket_content(sock, timeout=0.5)276 sock.send(277 b'HTTP/1.1 302 FOUND\r\n'278 b'Content-Length: 0\r\n'279 b'Location: /get#relevant-section\r\n\r\n'280 )281 consume_socket_content(sock, timeout=0.5)282 sock.send(283 b'HTTP/1.1 302 FOUND\r\n'284 b'Content-Length: 0\r\n'285 b'Location: /final-url/\r\n\r\n'286 )287 consume_socket_content(sock, timeout=0.5)288 sock.send(289 b'HTTP/1.1 200 OK\r\n\r\n'290 )291292 close_server = threading.Event()293 server = Server(response_handler, wait_to_close_event=close_server)294295 with server as (host, port):296 url = 'http://{0}:{1}/path/to/thing/#view=edit&token=hunter2'.format(host, port)297 r = requests.get(url)298 raw_request = r.content299300 assert r.status_code == 200301 assert len(r.history) == 2302 assert r.history[0].request.url == url303304 # Verify we haven't overwritten the location with our previous fragment.305 assert r.history[1].request.url == 'http://{0}:{1}/get#relevant-section'.format(host, port)306 # Verify previous fragment is used and not the original.307 assert r.url == 'http://{0}:{1}/final-url/#relevant-section'.format(host, port)308309 close_server.set()
modified:tests/test_requests.py+8 added
292 unmodified lines
293 assert next_resp.request.body is None294 for header in purged_headers:295 assert header not in next_resp.request.headers296297 def test_fragment_maintained_on_redirect(self, httpbin):298 fragment = "#view=edit&token=hunter2"299 r = requests.get(httpbin('redirect-to?url=get')+fragment)300301 assert len(r.history) > 0302 assert r.history[0].request.url == httpbin('redirect-to?url=get')+fragment303 assert r.url == httpbin('get')+fragment304305 def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):306 heads = {'User-agent': 'Mozilla/5.0'}307308 r = requests.get(httpbin('user-agent'), headers=heads)
2140 unmodified lines