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
Provenance
- Commit
d2e1130- Tree
424ee69- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
f89c325- Parent 2
f60324a
3 files changed+86 added−1 removed
modified:requests/sessions.py+6 added-1 removed
121 unmodified lines122 123 hist = [] # keep track of history124 125 url = self.get_redirect_target(resp)126+ previous_fragment = urlparse(req.url).fragment127 while url:128 prepared_request = req.copy()129 130 # Update history and keep track of redirects.16 unmodified lines147 if url.startswith('//'):148 parsed_rurl = urlparse(resp.url)149 url = '%s:%s' % (to_native_string(parsed_rurl.scheme), url)150 150- # 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()158 159 # 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 linesmodified:tests/test_lowlevel.py+72 added
233 unmodified lines234 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'))236 237 close_server.set()238+239+def 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+ )248+249+ close_server = threading.Event()250+ server = Server(response_handler, wait_to_close_event=close_server)251+252+ 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.content256+257+ 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)260+261+ 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 body265+266+ close_server.set()267+268+def 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+ """273+274+ 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+ )291+292+ close_server = threading.Event()293+ server = Server(response_handler, wait_to_close_event=close_server)294+295+ 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.content299+300+ assert r.status_code == 200301+ assert len(r.history) == 2302+ assert r.history[0].request.url == url303+304+ # 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)308+309+ close_server.set()modified:tests/test_requests.py+8 added
292 unmodified lines293 assert next_resp.request.body is None294 for header in purged_headers:295 assert header not in next_resp.request.headers296 297+ def test_fragment_maintained_on_redirect(self, httpbin):298+ fragment = "#view=edit&token=hunter2"299+ r = requests.get(httpbin('redirect-to?url=get')+fragment)300+301+ assert len(r.history) > 0302+ assert r.history[0].request.url == httpbin('redirect-to?url=get')+fragment303+ assert r.url == httpbin('get')+fragment304+305 def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):306 heads = {'User-agent': 'Mozilla/5.0'}307 308 r = requests.get(httpbin('user-agent'), headers=heads)2140 unmodified lines