| |
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| <html><head><title>Python: module gdata.tlslite.integration.TLSAsyncDispatcherMixIn</title> |
| </head><body bgcolor="#f0f0f8"> |
| |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| <tr bgcolor="#7799ee"> |
| <td valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="gdata.html"><font color="#ffffff">gdata</font></a>.<a href="gdata.tlslite.html"><font color="#ffffff">tlslite</font></a>.<a href="gdata.tlslite.integration.html"><font color="#ffffff">integration</font></a>.TLSAsyncDispatcherMixIn</strong></big></big></font></td |
| ><td align=right valign=bottom |
| ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/usr/local/google/home/afshar/src/external-gdata-release/google3/src/gdata/tlslite/integration/TLSAsyncDispatcherMixIn.py">/usr/local/google/home/afshar/src/external-gdata-release/google3/src/gdata/tlslite/integration/TLSAsyncDispatcherMixIn.py</a></font></td></tr></table> |
| <p><tt>TLS Lite + asyncore.</tt></p> |
| <p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#aa55cc"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> |
| <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="asyncore.html">asyncore</a><br> |
| </td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#ee77aa"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| <td width="100%"><dl> |
| <dt><font face="helvetica, arial"><a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a> |
| </font></dt><dd> |
| <dl> |
| <dt><font face="helvetica, arial"><a href="gdata.tlslite.integration.TLSAsyncDispatcherMixIn.html#TLSAsyncDispatcherMixIn">TLSAsyncDispatcherMixIn</a> |
| </font></dt></dl> |
| </dd> |
| </dl> |
| <p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#ffc8d8"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#000000" face="helvetica, arial"><a name="TLSAsyncDispatcherMixIn">class <strong>TLSAsyncDispatcherMixIn</strong></a>(<a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>)</font></td></tr> |
| |
| <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| <td colspan=2><tt>This class can be "mixed in" with an<br> |
| L{asyncore.dispatcher} to add TLS support.<br> |
| <br> |
| This class essentially sits between the dispatcher and the select<br> |
| loop, intercepting events and only calling the dispatcher when<br> |
| applicable.<br> |
| <br> |
| In the case of <a href="#TLSAsyncDispatcherMixIn-handle_read">handle_read</a>(), a read operation will be activated,<br> |
| and when it completes, the bytes will be placed in a buffer where<br> |
| the dispatcher can retrieve them by calling <a href="#TLSAsyncDispatcherMixIn-recv">recv</a>(), and the<br> |
| dispatcher's <a href="#TLSAsyncDispatcherMixIn-handle_read">handle_read</a>() will be called.<br> |
| <br> |
| In the case of <a href="#TLSAsyncDispatcherMixIn-handle_write">handle_write</a>(), the dispatcher's <a href="#TLSAsyncDispatcherMixIn-handle_write">handle_write</a>() will<br> |
| be called, and when it calls <a href="#TLSAsyncDispatcherMixIn-send">send</a>(), a write operation will be<br> |
| activated.<br> |
| <br> |
| To use this class, you must combine it with an asyncore.dispatcher,<br> |
| and pass in a handshake operation with <a href="#TLSAsyncDispatcherMixIn-setServerHandshakeOp">setServerHandshakeOp</a>().<br> |
| <br> |
| Below is an example of using this class with medusa. This class is<br> |
| mixed in with http_channel to create http_tls_channel. Note:<br> |
| 1. the mix-in is listed first in the inheritance list<br> |
| <br> |
| 2. the input buffer size must be at least 16K, otherwise the<br> |
| dispatcher might not read all the bytes from the TLS layer,<br> |
| leaving some bytes in limbo.<br> |
| <br> |
| 3. IE seems to have a problem receiving a whole HTTP response in a<br> |
| single TLS record, so HTML pages containing '\r\n\r\n' won't<br> |
| be displayed on IE.<br> |
| <br> |
| Add the following text into 'start_medusa.py', in the 'HTTP Server'<br> |
| section::<br> |
| <br> |
| from tlslite.api import *<br> |
| s = open("./serverX509Cert.pem").read()<br> |
| x509 = X509()<br> |
| x509.parse(s)<br> |
| certChain = X509CertChain([x509])<br> |
| <br> |
| s = open("./serverX509Key.pem").read()<br> |
| privateKey = parsePEMKey(s, private=True)<br> |
| <br> |
| class http_tls_channel(<a href="#TLSAsyncDispatcherMixIn">TLSAsyncDispatcherMixIn</a>,<br> |
| http_server.http_channel):<br> |
| ac_in_buffer_size = 16384<br> |
| <br> |
| def __init__ (self, server, conn, addr):<br> |
| http_server.http_channel.<a href="#TLSAsyncDispatcherMixIn-__init__">__init__</a>(self, server, conn, addr)<br> |
| <a href="#TLSAsyncDispatcherMixIn">TLSAsyncDispatcherMixIn</a>.<a href="#TLSAsyncDispatcherMixIn-__init__">__init__</a>(self, conn)<br> |
| self.<strong>tlsConnection</strong>.ignoreAbruptClose = True<br> |
| <a href="#TLSAsyncDispatcherMixIn-setServerHandshakeOp">setServerHandshakeOp</a>(certChain=certChain,<br> |
| privateKey=privateKey)<br> |
| <br> |
| hs.channel_class = http_tls_channel<br> |
| <br> |
| If the TLS layer raises an exception, the exception will be caught<br> |
| in asyncore.dispatcher, which will call <a href="#TLSAsyncDispatcherMixIn-close">close</a>() on this class. The<br> |
| TLS layer always closes the TLS connection before raising an<br> |
| exception, so the close operation will complete right away, causing<br> |
| asyncore.dispatcher.<a href="#TLSAsyncDispatcherMixIn-close">close</a>() to be called, which closes the socket<br> |
| and removes this instance from the asyncore loop.<br> </tt></td></tr> |
| <tr><td> </td> |
| <td width="100%">Methods defined here:<br> |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-__init__"><strong>__init__</strong></a>(self, sock<font color="#909090">=None</font>)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-close"><strong>close</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-handle_read"><strong>handle_read</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-handle_write"><strong>handle_write</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-outCloseEvent"><strong>outCloseEvent</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-outConnectEvent"><strong>outConnectEvent</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-outReadEvent"><strong>outReadEvent</strong></a>(self, readBuffer)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-outWriteEvent"><strong>outWriteEvent</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-readable"><strong>readable</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-recv"><strong>recv</strong></a>(self, bufferSize<font color="#909090">=16384</font>)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-send"><strong>send</strong></a>(self, writeBuffer)</dt></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-writable"><strong>writable</strong></a>(self)</dt></dl> |
| |
| <hr> |
| Methods inherited from <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>:<br> |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-inReadEvent"><strong>inReadEvent</strong></a>(self)</dt><dd><tt>Tell the state machine it can read from the socket.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-inWriteEvent"><strong>inWriteEvent</strong></a>(self)</dt><dd><tt>Tell the state machine it can write to the socket.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-setCloseOp"><strong>setCloseOp</strong></a>(self)</dt><dd><tt>Start a close operation.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-setHandshakeOp"><strong>setHandshakeOp</strong></a>(self, handshaker)</dt><dd><tt>Start a handshake operation.<br> |
| <br> |
| @type handshaker: generator<br> |
| @param handshaker: A generator created by using one of the<br> |
| asynchronous handshake functions (i.e. handshakeServerAsync, or<br> |
| handshakeClientxxx(..., async=True).</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-setServerHandshakeOp"><strong>setServerHandshakeOp</strong></a>(self, **args)</dt><dd><tt>Start a handshake operation.<br> |
| <br> |
| The arguments passed to this function will be forwarded to<br> |
| L{tlslite.TLSConnection.TLSConnection.handshakeServerAsync}.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-setWriteOp"><strong>setWriteOp</strong></a>(self, writeBuffer)</dt><dd><tt>Start a write operation.<br> |
| <br> |
| @type writeBuffer: str<br> |
| @param writeBuffer: The string to transmit.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-wantsReadEvent"><strong>wantsReadEvent</strong></a>(self)</dt><dd><tt>If the state machine wants to read.<br> |
| <br> |
| If an operation is active, this returns whether or not the<br> |
| operation wants to read from the socket. If an operation is<br> |
| not active, this returns None.<br> |
| <br> |
| @rtype: bool or None<br> |
| @return: If the state machine wants to read.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSAsyncDispatcherMixIn-wantsWriteEvent"><strong>wantsWriteEvent</strong></a>(self)</dt><dd><tt>If the state machine wants to write.<br> |
| <br> |
| If an operation is active, this returns whether or not the<br> |
| operation wants to write to the socket. If an operation is<br> |
| not active, this returns None.<br> |
| <br> |
| @rtype: bool or None<br> |
| @return: If the state machine wants to write.</tt></dd></dl> |
| |
| </td></tr></table></td></tr></table> |
| </body></html> |