3
ò\ð]Á  ã               @   sp   d dl Z d dlZd dlmZ d dlmZmZ d dlZdd„ Zdd„ Z	dd	„ Z
G d
d„ deƒZdee	e
edœZdS )é    N)Úurlsplit)ÚResponseÚWebSocketResponsec             C   s6   | j j||jƒ | j j||jƒ | j jd||jƒ dS )z°This function sets up the engine.io endpoint as a route for the
    application.

    Note that both GET and POST requests must be hooked up on the engine.io
    endpoint.
    ÚOPTIONSN)ZrouterZadd_getÚhandle_requestZadd_postZ	add_route)ZappZengineio_serverZengineio_endpoint© r   úI/tmp/pip-build-mqc4i71p/python-engineio/engineio/async_drivers/aiohttp.pyÚcreate_route	   s    
r	   c       	      C   sð   | j }| j}t|jƒ}|tjdddddd|j|jp4d|jd|j dd	dd	| d
œ}xt|j	j
ƒ D ]f\}}|jƒ }|dkr€||d< q\n|dkr’||d< q\d|jddƒ }||krºd|| |f }|||< q\W |jddƒ|d< |j}||d< d|d< |S )zˆThis function takes the arguments passed to the request handler and
    uses them to generate a WSGI compatible environ dictionary.
    é   r   TFZaiohttpÚ z
HTTP/%s.%sz	127.0.0.1Ú0)z
wsgi.inputzwsgi.errorszwsgi.versionz
wsgi.asynczwsgi.multithreadzwsgi.multiprocesszwsgi.run_onceZSERVER_SOFTWAREÚREQUEST_METHODÚQUERY_STRINGZRAW_URIZSERVER_PROTOCOLZREMOTE_ADDRZREMOTE_PORTZSERVER_NAMEZSERVER_PORTzaiohttp.requestzCONTENT-TYPEÚCONTENT_TYPEzCONTENT-LENGTHÚCONTENT_LENGTHzHTTP_%sú-Ú_z%s,%sZHTTP_X_FORWARDED_PROTOÚhttpzwsgi.url_schemeZ	PATH_INFOZSCRIPT_NAME)r
   r   )Ú_messageÚ_payloadr   ÚpathÚsysÚstderrÚmethodÚqueryÚversionÚheadersÚitemsÚupperÚreplaceÚget)	ÚrequestÚmessageÚpayloadZ	uri_partsÚenvironZhdr_nameZ	hdr_valueÚkeyZ	path_infor   r   r   Útranslate_request   sJ    
r&   c             C   s   t |t| jƒ d ƒ|d�S )zTThis function generates an appropriate response object for this async
    mode.
    r   )ÚbodyÚstatusr   )r   ÚintÚsplit)r(   r   r#   r$   r   r   r   Úmake_responseK   s    r+   c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú	WebSocketzƒ
    This wrapper class provides a aiohttp WebSocket interface that is
    somewhat compatible with eventlet's implementation.
    c             C   s   || _ d | _d S )N)ÚhandlerÚ_sock)Úselfr-   r   r   r   Ú__init__X   s    zWebSocket.__init__c             Ã   s>   |d }t ƒ | _| jj|ƒI d H  || _| j| ƒI d H  | jS )Nzaiohttp.request)r   r.   Úpreparer$   r-   )r/   r$   r!   r   r   r   Ú__call__\   s    zWebSocket.__call__c             Ã   s   | j jƒ I d H  d S )N)r.   Úclose)r/   r   r   r   r3   e   s    zWebSocket.closec             Ã   sB   t |tƒr| jj}n| jj}tj|ƒr6||ƒI d H  n||ƒ d S )N)Ú
isinstanceÚbytesr.   Z
send_bytesZsend_strÚasyncioÚiscoroutinefunction)r/   r"   Úfr   r   r   Úsendh   s    


zWebSocket.sendc             Ã   s<   | j jƒ I d H }t|jtjƒ r6t|jtjƒ r6tƒ ‚|jS )N)r.   Zreceiver4   ÚdataÚsixÚbinary_typeÚ	text_typeÚIOError)r/   Úmsgr   r   r   Úwaitr   s
    zWebSocket.waitN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r2   r3   r9   r@   r   r   r   r   r,   S   s   	
r,   T)r6   r	   r&   r+   Z	websocket)r6   r   Úurllib.parser   Zaiohttp.webr   r   r;   r	   r&   r+   Úobjectr,   Z_asyncr   r   r   r   Ú<module>   s   5(