Merge branch 'refactor-urls' of https://github.com/jerem/requests into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 0b475dd, 2361a6f · Browse files · 2 files·+25 added−27 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+25 added−27 removed
- other:+25 added−27 removed
Provenance
- Commit
6e90aa7- Tree
abcb34a- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
0b475dd- Parent 2
2361a6f
2 files changed+25 added−27 removed
modified:requests/models.py+1 added-16 removed
197 unmodified lines198 199 # Add the old request to the history collector.200 history.append(r)201 202- # Redirect to...203- url = r.headers['location']202+ url = cleanup_url(r.headers['location'], parent_url=self.url)203 205- # Handle redirection without scheme (see: RFC 1808 Section 4)206- if url.startswith('//'):207- parsed_rurl = urlparse(r.url)208- url = '%s:%s' % (parsed_rurl.scheme, url)209-210- # Facilitate non-RFC2616-compliant 'location' headers211- # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')212- parsed_url = urlparse(url)213- if not parsed_url.netloc:214- parsed_url = list(parsed_url)215- parsed_url[2] = urllib.quote(parsed_url[2], safe="%/:=&?~#+!$,;'@()*[]")216- url = urljoin(r.url, str(urlunparse(parsed_url)))217-204 # If 303, convert to idempotent GET.205 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4206 if r.status_code is codes.see_other:207 method = 'GET'33 unmodified lines241 # Give Response some context.242 self.response.request = self243 244 259-245 def send(self, anyway=False):246 """Sends the shit."""247 248 # Safety check.242 unmodified linesmodified:requests/utils.py+24 added-11 removed
13 unmodified lines14 import cookielib15 import re16 import urllib17 import zlib18-from urlparse import urlparse, urlunparse18+from urlparse import urlparse, urlunparse, urljoin19 20 21 def encode_params(params):22 """Encode parameters in a piece of data.20 unmodified lines43 44 else:45 return params46 47+def cleanup_url(url, parent_url=None):48+ # Handle redirection without scheme (see: RFC 1808 Section 4)49+ if url.startswith('//'):50+ parsed_rurl = urlparse(parent_url)51+ url = '%s:%s' % (parsed_rurl.scheme, url)52 48-def build_url(url, query_params):49- """Build the actual URL to use."""50-51- # Support for unicode domain names and paths.53 scheme, netloc, path, params, query, fragment = urlparse(url)54+ if netloc:53- netloc = netloc.encode('idna')55+ netloc = netloc.encode('idna')56 57 if isinstance(path, unicode):56- path = path.encode('utf-8')58+ path = path.encode('utf-8')59 60 path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")61+ params = urllib.quote(params, safe="%/:=&?~#+!$,;'@()*[]")62+ query = urllib.quote(query, safe="%/:=&?~#+!$,;'@()*[]")63 60- url = str(urlunparse(61- [scheme, netloc, path, params, query, fragment]62- ))64+ url = str(urlunparse([scheme, netloc, path, params, query, fragment]))65 66+ # Facilitate non-RFC2616-compliant 'location' headers67+ # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')68+ if not netloc and parent_url:69+ url = urljoin(parent_url, url)70+71+ return url72+73+def build_url(url, query_params):74+ """Build the actual URL to use."""75+76+ url = cleanup_url(url)77+78 query_params = encode_params(query_params)79 80 if query_params:81 if urlparse(url).query:2 unmodified lines84 return '%s?%s' % (url, query_params)85 else:86 return url87 74-88 def header_expand(headers):89 """Returns an HTTP Header value string from a dictionary.90 91 Example expansion::304 unmodified lines