mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Fail if unsupported schemas are used.
requests only supports http and https. This change enforces that.
This commit is contained in:
+4
-1
@@ -26,7 +26,7 @@ from .exceptions import (
|
||||
URLRequired, SSLError)
|
||||
from .utils import (
|
||||
get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,
|
||||
dict_from_string)
|
||||
dict_from_string, supported_schemes)
|
||||
|
||||
from .compat import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, str, bytes, SimpleCookie, is_py3, is_py2
|
||||
|
||||
@@ -316,6 +316,9 @@ class Request(object):
|
||||
if not scheme:
|
||||
raise ValueError("Invalid URL %r: No schema supplied" % url)
|
||||
|
||||
if not scheme in supported_schemes():
|
||||
raise ValueError("Invalid scheme %r" % scheme)
|
||||
|
||||
netloc = netloc.encode('idna').decode('utf-8')
|
||||
|
||||
if not path:
|
||||
|
||||
@@ -416,3 +416,10 @@ def requote_uri(uri):
|
||||
# or '%')
|
||||
return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")
|
||||
return "/".join(parts)
|
||||
|
||||
def supported_schemes():
|
||||
"""A list of schemes supported by requests.
|
||||
|
||||
return: a list of strings.
|
||||
"""
|
||||
return ["http","https"]
|
||||
|
||||
@@ -815,15 +815,12 @@ class RequestsTestSuite(TestSetup, unittest.TestCase):
|
||||
|
||||
def test_useful_exception_for_invalid_schema(self):
|
||||
|
||||
try:
|
||||
self.assertRaises(
|
||||
requests.exceptions.URLRequired,
|
||||
# If we pass a legitimate URL with a schema not supported
|
||||
# by requests, we should fail.
|
||||
self.assertRaises(
|
||||
ValueError,
|
||||
get,
|
||||
'http://http://')
|
||||
# To make this test as minimal as possible, only catch the
|
||||
# exception raised in issue #380.
|
||||
except ValueError:
|
||||
self.fail()
|
||||
'ftp://ftp.kernel.org/pub/')
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user