]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #22366: urllib.request.urlopen will accept a context object (SSLContext)
authorSenthil Kumaran <senthil@uthcode.com>
Fri, 19 Sep 2014 07:23:30 +0000 (15:23 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Fri, 19 Sep 2014 07:23:30 +0000 (15:23 +0800)
as an argument which will then used be for HTTPS connection.

Patch by Alex Gaynor.

Doc/library/urllib.request.rst
Lib/test/test_urllib.py
Lib/urllib/request.py
Misc/NEWS

index b588dad3a3442cbf23011c3bd27be48368e31b8a..07928eda2d3601a07bcd65031964f5ee2462e6b7 100644 (file)
@@ -16,7 +16,7 @@ authentication, redirections, cookies and more.
 The :mod:`urllib.request` module defines the following functions:
 
 
-.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None, cadefault=False)
+.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None, cadefault=False, context=None)
 
    Open the URL *url*, which can be either a string or a
    :class:`Request` object.
@@ -47,6 +47,10 @@ The :mod:`urllib.request` module defines the following functions:
    the global default timeout setting will be used).  This actually
    only works for HTTP, HTTPS and FTP connections.
 
+   If *context* is specified, it must be a :class:`ssl.SSLContext` instance
+   describing the various SSL options. See
+   :class:`~http.client.HTTPSConnection` for more details.
+
    The optional *cafile* and *capath* parameters specify a set of trusted
    CA certificates for HTTPS requests.  *cafile* should point to a single
    file containing a bundle of CA certificates, whereas *capath* should
@@ -111,6 +115,9 @@ The :mod:`urllib.request` module defines the following functions:
    .. versionchanged:: 3.3
       *cadefault* was added.
 
+   .. versionchanged:: 3.5
+      *context* was added.
+
 .. function:: install_opener(opener)
 
    Install an :class:`OpenerDirector` instance as the default global opener.
index 547883710305a3dbd97431f7633967147c3c64a1..962ef73a37bab28fefc997d97afdc08897ea3dd0 100644 (file)
@@ -10,6 +10,7 @@ import unittest
 from unittest.mock import patch
 from test import support
 import os
+import ssl
 import sys
 import tempfile
 from nturl2path import url2pathname, pathname2url
@@ -379,6 +380,13 @@ Content-Type: text/html; charset=iso-8859-1
         with support.check_warnings(('',DeprecationWarning)):
             urllib.request.URLopener()
 
+    def test_cafile_and_context(self):
+        context = ssl.create_default_context()
+        with self.assertRaises(ValueError):
+            urllib.request.urlopen(
+                "https://localhost", cafile="/nonexistent/path", context=context
+            )
+
 class urlopen_DataTests(unittest.TestCase):
     """Test urlopen() opening a data URL."""
 
index 67c7566c5b02413eddf359cfbd0ed0b954068fe7..e0c8116373299819dc4cc10e0f9bbd3a5c4ef35a 100644 (file)
@@ -136,9 +136,14 @@ __version__ = sys.version[:3]
 
 _opener = None
 def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
-            *, cafile=None, capath=None, cadefault=False):
+            *, cafile=None, capath=None, cadefault=False, context=None):
     global _opener
     if cafile or capath or cadefault:
+        if context is not None:
+            raise ValueError(
+                "You can't pass both context and any of cafile, capath, and "
+                "cadefault"
+            )
         if not _have_ssl:
             raise ValueError('SSL support not available')
         context = ssl._create_stdlib_context(cert_reqs=ssl.CERT_REQUIRED,
@@ -146,6 +151,9 @@ def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                                              capath=capath)
         https_handler = HTTPSHandler(context=context, check_hostname=True)
         opener = build_opener(https_handler)
+    elif context:
+        https_handler = HTTPSHandler(context=context)
+        opener = build_opener(https_handler)
     elif _opener is None:
         _opener = opener = build_opener()
     else:
index 8ca38329fe946c799c7dc04aebb86c496fa890e7..83e0dbd8afa74a72aa30edab5c89b522fe02a288 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #22366: urllib.request.urlopen will accept a context object
+  (SSLContext) as an argument which will then used be for HTTPS connection.
+  Patch by Alex Gaynor.
+
 - Issue #4180: The warnings registries are now reset when the filters
   are modified.