Fixed #174 and refactored urls quoting/concatenation in one function in utils.py.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jérémy Bethmont · committed by Jérémy Bethmont · parent cd01aa4 · Browse files · 2 files·+31 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+31 added−27 removed
- other:+31 added−27 removed
Provenance
- Commit
62d29d5- Tree
63cd053- Author
Jérémy Bethmont <jeremy.bethmont@gmail.com>- Committer
Jérémy Bethmont <jeremy.bethmont@gmail.com>- Parent
cd01aa4
2 files changed+31 added−27 removed
modified:requests/models.py+3 added-27 removed
20 unmodified lines21 from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler, HTTPDigestAuthHandler, HTTPRedirectHandler22 from .structures import CaseInsensitiveDict23 from .packages.poster.encode import multipart_encode24 from .packages.poster.streaminghttp import register_openers, get_handlers25-from .utils import dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip25+from .utils import get_clean_url, dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip26 from .status_codes import codes27 from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects28 29 184 unmodified lines214 raise TooManyRedirects()215 216 history.append(r)217 218- url = r.headers['location']218+ url = get_clean_url(r.headers['location'], parent_url=r.url)219 220- # Handle redirection without scheme (see: RFC 1808 Section 4)221- if url.startswith('//'):222- parsed_rurl = urlparse(r.url)223- url = '%s:%s' % (parsed_rurl.scheme, url)224-225- # Facilitate non-RFC2616-compliant 'location' headers226- # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')227- parsed_url = urlparse(url)228- if not parsed_url.netloc:229- parsed_url = list(parsed_url)230- parsed_url[2] = urllib.quote(parsed_url[2], safe="%/:=&?~#+!$,;'@()*[]")231- url = urljoin(r.url, str(urlunparse(parsed_url)))232-220 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4221 if r.status_code is codes.see_other:222 method = 'GET'223 else:38 unmodified lines262 263 def _build_url(self):264 """Build the actual URL to use."""265 279- # Support for unicode domain names and paths.280- scheme, netloc, path, params, query, fragment = urlparse(self.url)281- netloc = netloc.encode('idna')266+ self.url = get_clean_url(self.url)267 283- if isinstance(path, unicode):284- path = path.encode('utf-8')285-286- path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")287-288- self.url = str(urlunparse(289- [scheme, netloc, path, params, query, fragment]290- ))291-268 if self._enc_params:269 if urlparse(self.url).query:270 return '%s&%s' % (self.url, self._enc_params)271 else:364 unmodified linesmodified:requests/utils.py+28 added
11 unmodified lines12 import cgi13 import codecs14 import cookielib15 import re16+import urllib17 import zlib18 19+from urlparse import urlparse, urlunparse, urljoin20 21+def get_clean_url(url, parent_url=None):22+ # Handle redirection without scheme (see: RFC 1808 Section 4)23+ if url.startswith('//'):24+ parsed_rurl = urlparse(parent_url)25+ url = '%s:%s' % (parsed_rurl.scheme, url)26+27+ scheme, netloc, path, params, query, fragment = urlparse(url)28+ if netloc:29+ netloc = netloc.encode('idna')30+31+ if isinstance(path, unicode):32+ path = path.encode('utf-8')33+34+ path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")35+ params = urllib.quote(params, safe="%/:=&?~#+!$,;'@()*[]")36+ query = urllib.quote(query, safe="%/:=&?~#+!$,;'@()*[]")37+38+ url = str(urlunparse([scheme, netloc, path, params, query, fragment]))39+40+ # Facilitate non-RFC2616-compliant 'location' headers41+ # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')42+ if not netloc and parent_url:43+ url = urljoin(parent_url, url)44+45+ return url46+47 def header_expand(headers):48 """Returns an HTTP Header value string from a dictionary.49 50 Example expansion::305 unmodified lines