3 gY@sdZddlZddlZddlZddlZddlTddlTddlTddlTej rXddl m Z n ddl m Z ddgZeed r~ejZnd d Zd ZGd ddeZd dZddZdddZdddZddZddZdS)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) encodestringhandshake_response handshakecompare_digestcCs||kS)N)s1s2r r E/Users/olari/OneDrive/sandbox/awsBlog2/lambda/websocket/_handshake.pyr+s c@seZdZddZdS)rcCs||_||_||_dS)N)statusheaders subprotocol)selfrrrr r r __init__4szhandshake_response.__init__N)__name__ __module__ __qualname__rr r r r r2sc Ksjt||||\}}dj|}t||td|t|\}} t| ||jd\} } | s^tdt|| | S)Nz zrequest header subprotocolszInvalid WebSocket Header) _get_handshake_headersjoinsenddump_get_resp_headers _validategetWebSocketExceptionr) sockhostnameportresourceoptionsrkey header_strrrespsuccesssubprotor r r r:s    c CsLd|ddg}|dks|dkr$|}n d||f}d|krT|drT|jd|dn|jd|d |kr|d r|jd |d n|jd |t}|jd ||jd t|jd}|r|jddj|d|kr|d}t|trtdj|j}|j ||jdd} | r0|jd| |jd|jd||fS)NzGET %s HTTP/1.1zUpgrade: websocketzConnection: UpgradePiz%s:%dhostzHost: %soriginz Origin: %szOrigin: http://%szSec-WebSocket-Key: %szSec-WebSocket-Version: %srzSec-WebSocket-Protocol: %s,headerz: cookiez Cookie: %s) append_create_sec_websocket_keyVERSIONrr isinstancedictmapitemsextend) r"r*r!r#rhostportr$rr-r.r r r rIs:        recCs&t|\}}||krtd|||fS)NzHandshake status %d)Z read_headersWebSocketBadStatusException)rZsuccess_statusrZ resp_headersr r r rws  r websocketupgrade)r< connectionc Csd}xsz_validate..zInvalid subprotocol: zsec-websocket-acceptzutf-8z$258EAFA5-E914-47DA-95CA-C5AB0DC85B11T)FN)FN)FN)FN)FN)_HEADERS_TO_CHECKr6rr>errorstrr3six text_typeencode base64encodehashlibsha1digeststripr) rr$rr(kvrresultvalueZhashedr'r r r rs2     rcCstjd}t|jdjS)Nzutf-8)osurandomrHdecoderL)Z randomnessr r r r1s r1)r9)__doc__rIhmacrSrE _exceptions_http_logging_socketPY3base64rrHr__all__hasattrrr2objectrrrrrBrr1r r r r s0  . "