| 1 |
from __future__ import division |
|---|
| 2 |
|
|---|
| 3 |
"""HTTPS servers for CherryPy using the M2Crypto package""" |
|---|
| 4 |
|
|---|
| 5 |
import M2Crypto.SSL |
|---|
| 6 |
from cherrypy import cpg, _cphttpserver, _cputil |
|---|
| 7 |
import SocketServer, BaseHTTPServer |
|---|
| 8 |
import traceback, sys |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
class SslCherryConnection(M2Crypto.SSL.Connection): |
|---|
| 12 |
def __init__(self, socket): |
|---|
| 13 |
"""Create an M2Crypto.SSL.Connection using the key file specified in the config""" |
|---|
| 14 |
|
|---|
| 15 |
context = M2Crypto.SSL.Context() |
|---|
| 16 |
context.load_cert(cpg.configOption.sslKeyFile) |
|---|
| 17 |
M2Crypto.SSL.Connection.__init__(self, context, socket) |
|---|
| 18 |
|
|---|
| 19 |
self._cpLogMessage = _cputil.getSpecialFunction('_cpLogMessage') |
|---|
| 20 |
|
|---|
| 21 |
def settimeout(self, timeout): |
|---|
| 22 |
"""Add settimeout method, which is missing M2Crypto.SSL.Connection""" |
|---|
| 23 |
|
|---|
| 24 |
self.socket.settimeout(timeout) |
|---|
| 25 |
|
|---|
| 26 |
def accept(self): |
|---|
| 27 |
while 1: |
|---|
| 28 |
try: |
|---|
| 29 |
return M2Crypto.SSL.Connection.accept(self) |
|---|
| 30 |
except M2Crypto.SSL.SSLError, e: |
|---|
| 31 |
self._cpLogMessage('m2crypto ssl exception "%s"' % str(e), 'SSL', 1) |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class SslCherryServer(_cphttpserver.CherryHTTPServer): |
|---|
| 35 |
"""Single threaded HTTPS server""" |
|---|
| 36 |
|
|---|
| 37 |
def server_bind(self): |
|---|
| 38 |
"""Wrap the socket with a SslCherryConnection object before proceeding as normal""" |
|---|
| 39 |
|
|---|
| 40 |
self.socket = SslCherryConnection(self.socket) |
|---|
| 41 |
_cphttpserver.CherryHTTPServer.server_bind(self) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
class SslCherryPooledThreadServer(_cphttpserver.PooledThreadServer): |
|---|
| 45 |
"""Thread pooled HTTPS server""" |
|---|
| 46 |
|
|---|
| 47 |
def server_bind(self): |
|---|
| 48 |
"""Wrap the socket with a SslCherryConnection object before proceeding as normal""" |
|---|
| 49 |
|
|---|
| 50 |
self.socket = SslCherryConnection(self.socket) |
|---|
| 51 |
_cphttpserver.PooledThreadServer.server_bind(self) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
def start(configFile=None, parsedConfigFile=None, configDict={}): |
|---|
| 55 |
"""Start an HTTPS server instead of an HTTP server""" |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
cpg.server.start(configFile, parsedConfigFile, configDict, True) |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
if cpg.configOption.threadPool > 1: |
|---|
| 62 |
MyCherryHTTPServer = SslCherryPooledThreadServer |
|---|
| 63 |
else: |
|---|
| 64 |
MyCherryHTTPServer = SslCherryServer |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
if (cpg.configOption.threadPool > 1) and \ |
|---|
| 68 |
cpg.configOption.sessionStorageType == 'file': |
|---|
| 69 |
cpg._sessionFileLock = threading.RLock() |
|---|
| 70 |
|
|---|
| 71 |
MyCherryHTTPServer.request_queue_size = cpg.configOption.socketQueueSize |
|---|
| 72 |
|
|---|
| 73 |
_cphttpserver.CherryHTTPRequestHandler.protocol_version = cpg.configOption.protocolVersion |
|---|
| 74 |
|
|---|
| 75 |
_cphttpserver.run_server(_cphttpserver.CherryHTTPRequestHandler, MyCherryHTTPServer, |
|---|
| 76 |
(cpg.configOption.socketHost, cpg.configOption.socketPort), |
|---|
| 77 |
cpg.configOption.socketFile) |
|---|