Skip to content

Commit 6c23248

Browse files
committed
Delegate get_ssl_certificate to IOStream
1 parent 9da7cf8 commit 6c23248

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

tornado/httputil.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class _NormalizedHeaderCache(dict):
8383
>>> normalized_headers["coNtent-TYPE"]
8484
'Content-Type'
8585
"""
86+
8687
def __init__(self, size):
8788
super(_NormalizedHeaderCache, self).__init__()
8889
self.size = size
@@ -132,6 +133,7 @@ class HTTPHeaders(collections.MutableMapping):
132133
Set-Cookie: A=B
133134
Set-Cookie: C=D
134135
"""
136+
135137
def __init__(self, *args, **kwargs):
136138
self._dict = {} # type: typing.Dict[str, str]
137139
self._as_list = {} # type: typing.Dict[str, typing.List[str]]
@@ -349,6 +351,7 @@ class HTTPServerRequest(object):
349351
.. versionchanged:: 4.0
350352
Moved from ``tornado.httpserver.HTTPRequest``.
351353
"""
354+
352355
def __init__(self, method=None, uri=None, version="HTTP/1.0", headers=None,
353356
body=None, host=None, files=None, connection=None,
354357
start_line=None, server_connection=None):
@@ -451,30 +454,19 @@ def request_time(self):
451454
else:
452455
return self._finish_time - self._start_time
453456

454-
def get_ssl_certificate(self, binary_form=False):
457+
def get_ssl_certificate(self, **kwargs):
455458
"""Returns the client's SSL certificate, if any.
459+
See the documentation of the IOStream method for details
456460
457-
To use client certificates, the HTTPServer's
458-
`ssl.SSLContext.verify_mode` field must be set, e.g.::
459-
460-
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
461-
ssl_ctx.load_cert_chain("foo.crt", "foo.key")
462-
ssl_ctx.load_verify_locations("cacerts.pem")
463-
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
464-
server = HTTPServer(app, ssl_options=ssl_ctx)
465-
466-
By default, the return value is a dictionary (or None, if no
467-
client certificate is present). If ``binary_form`` is true, a
468-
DER-encoded form of the certificate is returned instead. See
469-
SSLSocket.getpeercert() in the standard library for more
470-
details.
471-
http://docs.python.org/library/ssl.html#sslsocket-objects
472461
"""
473-
try:
474-
return self.connection.stream.socket.getpeercert(
475-
binary_form=binary_form)
476-
except SSLError:
477-
return None
462+
return self.connection.stream.get_ssl_certificate(**kwargs)
463+
464+
def get_ssl_certificate_chain(self, **kwargs):
465+
"""Returns the client's SSL certificate chain, if any.
466+
See the documentation of the IOStream method for details
467+
468+
"""
469+
return self.connection.stream.get_ssl_certificate_chain(**kwargs)
478470

479471
def _parse_body(self):
480472
parse_body_arguments(
@@ -513,6 +505,7 @@ class HTTPServerConnectionDelegate(object):
513505
514506
.. versionadded:: 4.0
515507
"""
508+
516509
def start_request(self, server_conn, request_conn):
517510
"""This method is called by the server when a new request has started.
518511
@@ -539,6 +532,7 @@ class HTTPMessageDelegate(object):
539532
540533
.. versionadded:: 4.0
541534
"""
535+
542536
def headers_received(self, start_line, headers):
543537
"""Called when the HTTP headers have been received and parsed.
544538
@@ -579,6 +573,7 @@ class HTTPConnection(object):
579573
580574
.. versionadded:: 4.0
581575
"""
576+
582577
def write_headers(self, start_line, headers, chunk=None, callback=None):
583578
"""Write an HTTP header block.
584579

tornado/iostream.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,37 @@ def _is_connreset(self, e):
17431743
return True
17441744
return super(SSLIOStream, self)._is_connreset(e)
17451745

1746+
def get_ssl_certificate(self, **kwargs):
1747+
"""" Returns the client's SSL certificate, if any.
1748+
1749+
To use client certificates, the HTTPServer's
1750+
`ssl.SSLContext.verify_mode` field must be set, e.g.::
1751+
1752+
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
1753+
ssl_ctx.load_cert_chain("foo.crt", "foo.key")
1754+
ssl_ctx.load_verify_locations("cacerts.pem")
1755+
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
1756+
server = HTTPServer(app, ssl_options=ssl_ctx)
1757+
1758+
By default, the return value is a dictionary (or None, if no
1759+
client certificate is present). If ``binary_form`` is true, a
1760+
DER-encoded form of the certificate is returned instead. See
1761+
SSLSocket.getpeercert() in the standard library for more
1762+
details.
1763+
http://docs.python.org/library/ssl.html#sslsocket-objects
1764+
"""
1765+
binary_form = kwargs.get('binary_form', False)
1766+
try:
1767+
return self.socket.getpeercert(binary_form=binary_form)
1768+
except ssl.SSLError:
1769+
return None
1770+
1771+
def get_ssl_certificate_chain(self, **kwargs):
1772+
""" Should returns the client's SSL Certificate chain.
1773+
However, the python ssl library does not expose this
1774+
"""
1775+
raise NotImplemented
1776+
17461777

17471778
class PipeIOStream(BaseIOStream):
17481779
"""Pipe-based `IOStream` implementation.

0 commit comments

Comments
 (0)