Ticket #2346: 2346-gnutls.patch

File 2346-gnutls.patch, 3.6 KB (added by robertwb, 14 years ago)
  • new file sage/server/notebook/gnutls_socket_ssl.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1207456587 25200
    # Node ID 6d666243f6660f80cd0330d8a7e4fce608aba5bf
    # Parent  0361af1564fbd612443e3bc8905bcb86105a90d7
    Fall back to gnutls for testing in case openssl is not detected at compile time.
    
    diff -r 0361af1564fb -r 6d666243f666 sage/server/notebook/gnutls_socket_ssl.py
    - +  
     1import socket
     2import gnutls
     3import gnutls.connection
     4import gnutls.errors
     5
     6class GnuTLSSocketSSL:
     7    """
     8    This class provides a bare-bones replacement to socket.ssl in the case
     9    that openssl is not installed/detected when Python is built.
     10   
     11    socket.ssl MUST be set before urllib is imported.
     12    """
     13    def __init__(self, sock, key_file=None, cert_file=None):
     14        self.creds = gnutls.connection.X509Credentials()
     15        self.session = gnutls.connection.ClientSession(sock, self.creds)
     16        self.session.handshake()
     17       
     18    def send(self, *args, **kwds):
     19        self.session.send(*args, **kwds)
     20       
     21    write = send
     22   
     23    def recv(self, *args, **kwds):
     24        try:
     25            s = self.session.recv(*args, **kwds)
     26        except gnutls.errors.GNUTLSError:
     27            raise socket.sslerror(socket.SSL_ERROR_EOF)
     28        return s
     29       
     30    read = recv
     31   
     32    def close(self):
     33        self.session.bye()
     34        self.session.shutdown()
     35        self.session.close()
     36       
  • sage/server/notebook/notebook_object.py

    diff -r 0361af1564fb -r 6d666243f666 sage/server/notebook/notebook_object.py
    a b def test_notebook(admin_passwd, director 
    159159    This function is used to test notebook server functions.
    160160   
    161161    EXAMPLE:
    162         sage: import urllib
    163162        sage: from sage.server.notebook.notebook_object import test_notebook
    164163        sage: passwd = str(randint(1,1<<128))
    165164        sage: nb = test_notebook(passwd, address='localhost', port=8060)
     165        sage: import urllib
    166166        sage: h = urllib.urlopen('https://localhost:8060')
    167167        sage: homepage = h.read()
    168168        sage: h.close()
    def test_notebook(admin_passwd, director 
    170170        True
    171171        sage: nb.dispose()
    172172        """
     173    import socket
     174    if not hasattr(socket, 'ssl'):
     175        import sage.server.notebook.gnutls_socket_ssl
     176        socket.ssl = sage.server.notebook.gnutls_socket_ssl.GnuTLSSocketSSL
     177        import urllib
     178        if not hasattr(urllib, 'open_https'):
     179            raise RuntimeError, "urllib imported before test_notebook called, can't be fixed now..."
     180       
    173181    if directory is None:
    174182        directory = tmp_dir = tempfile.mkdtemp()
    175183    else:
  • sage/server/simple/twist.py

    diff -r 0361af1564fb -r 6d666243f666 sage/server/simple/twist.py
    a b NOTE: 
    99
    1010
    1111TESTS:
     12Start the notebook:
    1213    sage: from sage.server.notebook.notebook_object import test_notebook
     14    sage: passwd = str(randint(1,1<<128))
     15    sage: nb = test_notebook(passwd, address='localhost', port=8095)
     16
     17Import urllib:
    1318    sage: import urllib, re
    1419    sage: def get_url(url): h = urllib.urlopen(url); data = h.read(); h.close(); return data
    1520
    16 Start the notebook:
    17     sage: passwd = str(randint(1,1<<128))
    18     sage: nb = test_notebook(passwd, address='localhost', port=8095)
    1921   
    2022Login to a new session:
    2123    sage: login_page = get_url('https://localhost:8095/simple/login?username=admin&password=%s' % passwd)