~ubuntu-branches/ubuntu/maverick/m2crypto/maverick

« back to all changes in this revision

Viewing changes to demo/Zope27/install_dir/lib/python/ZServer/medusa/https_server.py

  • Committer: Bazaar Package Importer
  • Author(s): Dima Barsky
  • Date: 2004-03-30 21:54:28 UTC
  • Revision ID: james.westby@ubuntu.com-20040330215428-7zulfxz4xt31w2xr
Tags: upstream-0.13
ImportĀ upstreamĀ versionĀ 0.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""A https server built on Medusa's http_server. 
 
4
 
 
5
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
 
6
 
 
7
RCS_id='$Id: https_server.py,v 1.2 2004/03/28 12:00:11 ngps Exp $'
 
8
 
 
9
import asynchat, asyncore, http_server, socket, sys
 
10
from M2Crypto import SSL, version
 
11
 
 
12
VERSION_STRING=version
 
13
 
 
14
class https_channel(http_server.http_channel):
 
15
 
 
16
    ac_in_buffer_size = 1 << 16
 
17
 
 
18
    def __init__(self, server, conn, addr):
 
19
        http_server.http_channel.__init__(self, server, conn, addr)
 
20
 
 
21
    def send(self, data):
 
22
        try:
 
23
            result = self.socket._write_nbio(data)
 
24
            if result <= 0:
 
25
                return 0
 
26
            else:
 
27
                self.server.bytes_out.increment(result)
 
28
                return result
 
29
        except SSL.SSLError, why:
 
30
            self.close()
 
31
            self.log_info('send: closing channel %s %s' % (repr(self), why))
 
32
            return 0
 
33
 
 
34
    def recv(self, buffer_size):
 
35
        try:
 
36
            result = self.socket._read_nbio(buffer_size)
 
37
            if result is None:
 
38
                return ''
 
39
            elif result == '':
 
40
                self.close()
 
41
                return ''
 
42
            else:
 
43
                self.server.bytes_in.increment(len(result))
 
44
                return result
 
45
        except SSL.SSLError, why:
 
46
            self.close()
 
47
            self.log_info('recv: closing channel %s %s' % (repr(self), why))
 
48
            return ''
 
49
 
 
50
 
 
51
class https_server(http_server.http_server):
 
52
 
 
53
    SERVER_IDENT='M2Crypto HTTPS Server (v%s)' % VERSION_STRING
 
54
 
 
55
    channel_class=https_channel
 
56
 
 
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)
 
59
        self.ssl_ctx=ssl_ctx
 
60
        
 
61
    def handle_accept(self):
 
62
        # Cribbed from http_server.
 
63
        self.total_clients.increment()
 
64
        try:
 
65
            conn, addr = self.accept()
 
66
        except socket.error:
 
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')
 
72
            return
 
73
 
 
74
        # Turn the vanilla socket into an SSL connection.
 
75
        try:
 
76
            ssl_conn=SSL.Connection(self.ssl_ctx, conn)
 
77
            ssl_conn._setup_ssl(addr)
 
78
            ssl_conn.accept_ssl()
 
79
            self.channel_class(self, ssl_conn, addr)
 
80
        except SSL.SSLError:
 
81
            pass
 
82
 
 
83
    def writeable(self):
 
84
        return 0
 
85