Skip to content
Myr

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

2 files+31 added−27 removed

modified:requests/models.py+3 added-27 removed
20 unmodified lines
21from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler, HTTPDigestAuthHandler, HTTPRedirectHandler22from .structures import CaseInsensitiveDict23from .packages.poster.encode import multipart_encode24from .packages.poster.streaminghttp import register_openers, get_handlers25from .utils import dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip25from .utils import get_clean_url, dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip26from .status_codes import codes27from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects2829
184 unmodified lines
214 raise TooManyRedirects()215216 history.append(r)217218 url = r.headers['location']218 url = get_clean_url(r.headers['location'], parent_url=r.url)219220 # 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)224225 # 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)))232220 # 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 lines
262263 def _build_url(self):264 """Build the actual URL to use."""265279 # 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)267283 if isinstance(path, unicode):284 path = path.encode('utf-8')285286 path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")287288 self.url = str(urlunparse(289 [scheme, netloc, path, params, query, fragment]290 ))291268 if self._enc_params:269 if urlparse(self.url).query:270 return '%s&%s' % (self.url, self._enc_params)271 else:
364 unmodified lines
modified:requests/utils.py+28 added
11 unmodified lines
12import cgi13import codecs14import cookielib15import re16import urllib17import zlib1819from urlparse import urlparse, urlunparse, urljoin2021def 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)2627 scheme, netloc, path, params, query, fragment = urlparse(url)28 if netloc:29 netloc = netloc.encode('idna')3031 if isinstance(path, unicode):32 path = path.encode('utf-8')3334 path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")35 params = urllib.quote(params, safe="%/:=&?~#+!$,;'@()*[]")36 query = urllib.quote(query, safe="%/:=&?~#+!$,;'@()*[]")3738 url = str(urlunparse([scheme, netloc, path, params, query, fragment]))3940 # 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)4445 return url4647def header_expand(headers):48 """Returns an HTTP Header value string from a dictionary.4950 Example expansion::
305 unmodified lines