3
"""A https server built on Medusa's http_server.
5
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
7
RCS_id='$Id: https_server.py,v 1.2 2004/03/28 12:00:11 ngps Exp $'
9
import asynchat, asyncore, http_server, socket, sys
10
from M2Crypto import SSL, version
12
VERSION_STRING=version
14
class https_channel(http_server.http_channel):
16
ac_in_buffer_size = 1 << 16
18
def __init__(self, server, conn, addr):
19
http_server.http_channel.__init__(self, server, conn, addr)
23
result = self.socket._write_nbio(data)
27
self.server.bytes_out.increment(result)
29
except SSL.SSLError, why:
31
self.log_info('send: closing channel %s %s' % (repr(self), why))
34
def recv(self, buffer_size):
36
result = self.socket._read_nbio(buffer_size)
43
self.server.bytes_in.increment(len(result))
45
except SSL.SSLError, why:
47
self.log_info('recv: closing channel %s %s' % (repr(self), why))
51
class https_server(http_server.http_server):
53
SERVER_IDENT='M2Crypto HTTPS Server (v%s)' % VERSION_STRING
55
channel_class=https_channel
57
def __init__(self, ip, port, ssl_ctx, resolver=None, logger_object=None):
58
http_server.http_server.__init__(self, ip, port, resolver, logger_object)
61
def handle_accept(self):
62
# Cribbed from http_server.
63
self.total_clients.increment()
65
conn, addr = self.accept()
67
# linux: on rare occasions we get a bogus socket back from
68
# accept. socketmodule.c:makesockaddr complains that the
69
# address family is unknown. We don't want the whole server
70
# to shut down because of this.
71
sys.stderr.write ('warning: server accept() threw an exception\n')
74
# Turn the vanilla socket into an SSL connection.
76
ssl_conn=SSL.Connection(self.ssl_ctx, conn)
77
ssl_conn._setup_ssl(addr)
79
self.channel_class(self, ssl_conn, addr)