3 gY@sdZddlZddlZddlZddlZddlZddlTddlTddlTddl Tddl Tej rhddl m Zn ddl mZddd gZGd ddeZd dZd d ZddZddZddZddZddZdd ZdS)aL websocket - WebSocket client library for Python Copyright (C) 2010 Hiroki Ohtani(liris) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA N)*) encodebytes) encodestring proxy_infoconnect read_headersc@seZdZddZdS)rcKsV|jdd|_|jr@|jdd|_|jdd|_|jdd|_nd|_d|_d|_dS)Nhttp_proxy_hosthttp_proxy_portrhttp_proxy_auth http_no_proxy)gethostportauthno_proxy)selfoptionsr@/Users/olari/OneDrive/sandbox/awsBlog2/lambda/websocket/_http.py__init__-szproxy_info.__init__N)__name__ __module__ __qualname__rrrrrr+sc Cst|\}}}}|r"||||ffSt||||\}} } |sRtd|dt|d} yPt||j|j} | rzt| ||| } |rtrt | |j |} ntd| |||ffS| r| j YnXdS)NzHost not found.: :zSSL not available.) parse_url_get_addrinfo_listWebSocketExceptionstr _open_socketsockopttimeout_tunnelZHAVE_SSL _ssl_socketssloptclose) urlrproxysockethostnamerresource is_secure addrinfo_listZ need_tunnelrsockrrrr9s,cCspt|||j|j|j|j\}}}|sBtj||ddtj}|ddfS|rJ|pLd}tj||ddtj}|d|fSdS)NrFPT)Zget_proxy_inforrrrr( getaddrinfoSOL_TCP)r)rr+r'ZphostZpportZpauthr,rrrrXs  rc Csd}x|D]}|d}tj|}|j|xtD]}|j|q0Wx|D]}|j|qHW|d}y|j|WnJtjk r} z,t|d| _| jtj fkr| }w nWYdd} ~ Xq XPq W||S)Nr) r( settimeoutZDEFAULT_SOCKET_OPTION setsockoptrerrorrZ remote_iperrnoZ ECONNREFUSED) r,r r!errZaddrinfofamilyr-optsaddressr4rrrres*     rcCstjrtjdkptjdkS)N )r:r;r<)r=r:)sixPY2sys version_inforrrr _can_use_snisrBcCstj|jdtj}|jdtjtjkr<|j|jddd|jddrj|j|d|jdd|jdd|d|_tr~||_ d|kr|j |dd |kr|d \}}}|j||||j ||jd d |jd d |d S)N ssl_version cert_reqsca_certs)cafilecertfilekeyfilepasswordciphersZ cert_chaindo_handshake_on_connectTsuppress_ragged_eofs)rKrLserver_hostname) ssl SSLContextr PROTOCOL_SSLv23 CERT_NONEload_verify_locationsload_cert_chain verify_modeHAVE_CONTEXT_CHECK_HOSTNAMEcheck_hostname set_ciphers wrap_socket)r-r$r)rVcontextrGrHrIrrr_wrap_sni_sockets*     rZcCsttjd}|j|tjjdr0tjjd}ntjjtjj t d}tjj |rj|jdddkrj||d<|dtj ko|j dd}trt||||}n|j ddtj|f|}t r|rt|j||S)N)rDZWEBSOCKET_CLIENT_CA_BUNDLEz cacert.pemrErDrVT)dictrN CERT_REQUIREDupdateosenvironr pathjoindirname__file__isfilerQpoprBrZrXrUmatch_hostname getpeercert)r-Z user_ssloptr)r$ZcertPathrVrrrr#s"     r#c Cstdd||f}|r`|dr`|d}|dr@|d|d7}t|jjj}|d|7}|d7}td|t||yt|\}}Wn.tk r} zt t | WYdd} ~ XnX|d krt d ||S) NzConnecting proxy...zCONNECT %s:%d HTTP/1.0 rrrzProxy-Authorization: Basic %s z zrequest headerz#failed CONNECT via proxy status: %r) debug base64encodeencodestripdecodedumpsendr ExceptionWebSocketProxyExceptionr) r-rrrZconnect_headerauth_strZ encoded_strstatus resp_headerserrrr"s&      r"cCsd}i}tdxt|}|jdj}|s.Pt||sT|jdd}t|d}q|jdd}t|dkr|\}}|j||j<qtdqWtd||fS) Nz--- response header ---zutf-8 r:rrzInvalid headerz-----------------------) traceZ recv_linermrlsplitintlenlowerr)r-rsheaderslineZ status_infokvkeyvaluerrrrs&    )__doc__r5r^r(r@r> _exceptions_logging_socketZ _ssl_compat_urlPY3base64rrjr__all__objectrrrrrBrZr#r"rrrrrs.