aboutsummaryrefslogtreecommitdiff
path: root/requests/models.py
diff options
context:
space:
mode:
authorSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:41:22 -0700
committerSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:41:22 -0700
commit40337989ba5056432c9f2af3c42267e5ee9e3e18 (patch)
tree25a680529c68fcdd7886b4b064845c3e371e167e /requests/models.py
parente75853fc04102c7f72f2e955b63f9692c472f64a (diff)
downloadpython-requests-40337989ba5056432c9f2af3c42267e5ee9e3e18.tar
python-requests-40337989ba5056432c9f2af3c42267e5ee9e3e18.tar.gz
Imported Upstream version 0.11.1
Diffstat (limited to 'requests/models.py')
-rw-r--r--requests/models.py79
1 files changed, 40 insertions, 39 deletions
diff --git a/requests/models.py b/requests/models.py
index 753e83a..70e3503 100644
--- a/requests/models.py
+++ b/requests/models.py
@@ -24,7 +24,7 @@ from .packages.urllib3.filepost import encode_multipart_formdata
from .defaults import SCHEMAS
from .exceptions import (
ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,
- URLRequired, SSLError)
+ URLRequired, SSLError, MissingSchema, InvalidSchema)
from .utils import (
get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,
dict_from_string, stream_decode_response_unicode, get_netrc_auth)
@@ -63,7 +63,8 @@ class Request(object):
config=None,
_poolmanager=None,
verify=None,
- session=None):
+ session=None,
+ cert=None):
#: Dictionary of configurations for this request.
self.config = dict(config or [])
@@ -143,6 +144,9 @@ class Request(object):
#: SSL Verification.
self.verify = verify
+ #: SSL Certificate
+ self.cert = cert
+
if headers:
headers = CaseInsensitiveDict(self.headers)
else:
@@ -212,6 +216,7 @@ class Request(object):
self.cookies.update(r.cookies)
if r.status_code in REDIRECT_STATI and not self.redirect:
+
while (('location' in r.headers) and
((r.status_code is codes.see_other) or (self.allow_redirects))):
@@ -226,6 +231,7 @@ class Request(object):
history.append(r)
url = r.headers['location']
+ data = self.data
# Handle redirection without scheme (see: RFC 1808 Section 4)
if url.startswith('//'):
@@ -243,9 +249,21 @@ class Request(object):
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
if r.status_code is codes.see_other:
method = 'GET'
+ data = None
else:
method = self.method
+ # Do what the browsers do if strict_mode is off...
+ if (not self.config.get('strict_mode')):
+
+ if r.status_code in (codes.moved, codes.found) and self.method == 'POST':
+ method = 'GET'
+ data = None
+
+ if (r.status_code == 303) and self.method != 'HEAD':
+ method = 'GET'
+ data = None
+
# Remove the cookie headers that were sent.
headers = self.headers
try:
@@ -262,12 +280,14 @@ class Request(object):
auth=self.auth,
cookies=self.cookies,
redirect=True,
+ data=data,
config=self.config,
timeout=self.timeout,
_poolmanager=self._poolmanager,
proxies=self.proxies,
verify=self.verify,
- session=self.session
+ session=self.session,
+ cert=self.cert
)
request.send()
@@ -321,10 +341,10 @@ class Request(object):
scheme, netloc, path, params, query, fragment = urlparse(url)
if not scheme:
- raise ValueError("Invalid URL %r: No schema supplied" % url)
+ raise MissingSchema("Invalid URL %r: No schema supplied" % url)
if not scheme in SCHEMAS:
- raise ValueError("Invalid scheme %r" % scheme)
+ raise InvalidSchema("Invalid scheme %r" % scheme)
netloc = netloc.encode('idna').decode('utf-8')
@@ -507,6 +527,13 @@ class Request(object):
conn.cert_reqs = 'CERT_NONE'
conn.ca_certs = None
+ if self.cert and self.verify:
+ if len(self.cert) == 2:
+ conn.cert_file = self.cert[0]
+ conn.key_file = self.cert[1]
+ else:
+ conn.cert_file = self.cert
+
if not self.sent or anyway:
if self.cookies:
@@ -648,7 +675,7 @@ class Response(object):
def ok(self):
try:
self.raise_for_status()
- except HTTPError:
+ except RequestException:
return False
return True
@@ -671,39 +698,7 @@ class Response(object):
yield chunk
self._content_consumed = True
- def generate_chunked():
- resp = self.raw._original_response
- fp = resp.fp
- if resp.chunk_left is not None:
- pending_bytes = resp.chunk_left
- while pending_bytes:
- chunk = fp.read(min(chunk_size, pending_bytes))
- pending_bytes -= len(chunk)
- yield chunk
- fp.read(2) # throw away crlf
- while 1:
- #XXX correct line size? (httplib has 64kb, seems insane)
- pending_bytes = fp.readline(40).strip()
- if not len(pending_bytes):
- # No content, like a HEAD request. Break out.
- break
- pending_bytes = int(pending_bytes, 16)
- if pending_bytes == 0:
- break
- while pending_bytes:
- chunk = fp.read(min(chunk_size, pending_bytes))
- pending_bytes -= len(chunk)
- yield chunk
- fp.read(2) # throw away crlf
- self._content_consumed = True
- fp.close()
-
- if getattr(getattr(self.raw, '_original_response', None), 'chunked', False):
- gen = generate_chunked()
- else:
- gen = generate()
-
- gen = stream_untransfer(gen, self)
+ gen = stream_untransfer(generate(), self)
if decode_unicode:
gen = stream_decode_response_unicode(gen, self)
@@ -788,6 +783,12 @@ class Response(object):
# Decode unicode from given encoding.
try:
content = str(self.content, encoding, errors='replace')
+ except LookupError:
+ # A LookupError is raised if the encoding was not found which could
+ # indicate a misspelling or similar mistake.
+ #
+ # So we try blindly encoding.
+ content = str(self.content, errors='replace')
except (UnicodeError, TypeError):
pass