~romaimperator/keryx/devel

« back to all changes in this revision

Viewing changes to libkeryx/urlgrabber/sslfactory.py

  • Committer: Chris Oliver
  • Date: 2009-09-08 03:34:12 UTC
  • Revision ID: excid3@gmail.com-20090908033412-uaml2aaxcxxa7ryp
Added urlgrabber to libkeryx

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#   This library is free software; you can redistribute it and/or
 
2
#   modify it under the terms of the GNU Lesser General Public
 
3
#   License as published by the Free Software Foundation; either
 
4
#   version 2.1 of the License, or (at your option) any later version.
 
5
#
 
6
#   This library is distributed in the hope that it will be useful,
 
7
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
9
#   Lesser General Public License for more details.
 
10
#
 
11
#   You should have received a copy of the GNU Lesser General Public
 
12
#   License along with this library; if not, write to the 
 
13
#      Free Software Foundation, Inc., 
 
14
#      59 Temple Place, Suite 330, 
 
15
#      Boston, MA  02111-1307  USA
 
16
 
 
17
# This file is part of urlgrabber, a high-level cross-protocol url-grabber
 
18
 
 
19
import httplib
 
20
import urllib2
 
21
 
 
22
try:
 
23
    from M2Crypto import SSL
 
24
    from M2Crypto import httpslib
 
25
    from M2Crypto import m2urllib2
 
26
 
 
27
    have_m2crypto = True
 
28
except ImportError:
 
29
    have_m2crypto = False
 
30
 
 
31
DEBUG = None
 
32
 
 
33
if have_m2crypto:
 
34
    
 
35
    class M2SSLFactory:
 
36
 
 
37
        def __init__(self, ssl_ca_cert, ssl_context):
 
38
            self.ssl_context = self._get_ssl_context(ssl_ca_cert, ssl_context)
 
39
 
 
40
        def _get_ssl_context(self, ssl_ca_cert, ssl_context):
 
41
            """
 
42
            Create an ssl context using the CA cert file or ssl context.
 
43
 
 
44
            The CA cert is used first if it was passed as an option. If not,
 
45
            then the supplied ssl context is used. If no ssl context was supplied,
 
46
            None is returned.
 
47
            """
 
48
            if ssl_ca_cert:
 
49
                context = SSL.Context()
 
50
                context.load_verify_locations(ssl_ca_cert)
 
51
                context.set_verify(SSL.verify_peer, -1)
 
52
                return context
 
53
            else:
 
54
                return ssl_context
 
55
 
 
56
        def create_https_connection(self, host, response_class = None):
 
57
            connection = httplib.HTTPSConnection(host, self.ssl_context)
 
58
            if response_class:
 
59
                connection.response_class = response_class
 
60
            return connection
 
61
 
 
62
        def create_opener(self, *handlers):
 
63
            return m2urllib2.build_opener(self.ssl_context, *handlers)
 
64
 
 
65
 
 
66
class SSLFactory:
 
67
 
 
68
    def create_https_connection(self, host, response_class = None):
 
69
        connection = httplib.HTTPSConnection(host)
 
70
        if response_class:
 
71
            connection.response_class = response_class
 
72
        return connection
 
73
 
 
74
    def create_opener(self, *handlers):
 
75
        return urllib2.build_opener(*handlers)
 
76
 
 
77
   
 
78
 
 
79
def get_factory(ssl_ca_cert = None, ssl_context = None):
 
80
    """ Return an SSLFactory, based on if M2Crypto is available. """
 
81
    if have_m2crypto:
 
82
        return M2SSLFactory(ssl_ca_cert, ssl_context)
 
83
    else:
 
84
        # Log here if someone provides the args but we don't use them.
 
85
        if ssl_ca_cert or ssl_context:
 
86
            if DEBUG:
 
87
                DEBUG.warning("SSL arguments supplied, but M2Crypto is not available. "
 
88
                        "Using Python SSL.")
 
89
        return SSLFactory()