1 # Copyright (C) Research In Motion Limited 2011. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4 # following conditions are met:
6 # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9 # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10 # following disclaimer in the documentation and/or other materials provided with the distribution.
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS “AS IS” AND ANY EXPRESS OR
13 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
15 # APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
19 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 from mod_pywebsocket
import stream
25 from mod_pywebsocket
.handshake
.hybi
import compute_accept
28 def web_socket_do_extra_handshake(request
):
29 # This simulates a broken server that sends a WebSocket frame before the
30 # handshake, and more frames afterwards. It is important that if this
31 # happens the client does not buffer all the frames as the server continues
32 # to send more data - it should abort after reading a reasonable number of
33 # bytes (set arbitrarily to 1024).
34 frame
= stream
.create_text_frame('\0Frame-contains-thirty-two-bytes')
37 msg
+= 'HTTP/1.1 101 Switching Protocols\r\n'
38 msg
+= 'Upgrade: websocket\r\n'
39 msg
+= 'Connection: Upgrade\r\n'
40 msg
+= 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request
.headers_in
['Sec-WebSocket-Key'])[0]
42 request
.connection
.write(msg
)
43 # continue writing data until the client disconnects
46 # write over 1024 bytes including the above handshake
47 numFrames
= 1024 / len(frame
)
48 for i
in range(0, numFrames
):
49 request
.connection
.write(frame
)
52 def web_socket_transfer_data(request
):