|
8 | 8 | import socket
|
9 | 9 | import struct
|
10 | 10 | import ssl
|
11 |
| -import time |
12 | 11 | import sys
|
13 | 12 | import errno
|
14 | 13 | import codecs
|
@@ -151,75 +150,74 @@ def _handlePacket(self):
|
151 | 150 | status = 1002
|
152 | 151 |
|
153 | 152 | self.close(status, reason)
|
154 |
| - #raise Exception("received client close") |
| 153 | + return |
155 | 154 |
|
156 |
| - if self.fin == 0: |
157 |
| - if self.opcode != STREAM: |
158 |
| - if self.opcode == PING or self.opcode == PONG: |
159 |
| - raise Exception('control messages can not be fragmented') |
160 |
| - |
161 |
| - self.frag_type = self.opcode |
162 |
| - self.frag_start = True |
163 |
| - self.frag_decoder.reset() |
164 |
| - |
165 |
| - if self.frag_type == TEXT: |
166 |
| - self.frag_buffer = u'' |
167 |
| - utf_str = self.frag_decoder.decode(self.data, final = False) |
168 |
| - if utf_str: |
169 |
| - self.frag_buffer += utf_str |
170 |
| - else: |
171 |
| - self.frag_buffer = bytearray() |
172 |
| - self.frag_buffer += self.data |
173 |
| - |
174 |
| - else: |
175 |
| - if self.frag_start is False: |
176 |
| - raise Exception('fragmentation protocol error') |
177 |
| - |
178 |
| - if self.frag_type == TEXT: |
179 |
| - utf_str = self.frag_decoder.decode(self.data, final = False) |
180 |
| - if utf_str: |
181 |
| - self.frag_buffer += utf_str |
182 |
| - else: |
183 |
| - self.frag_buffer += self.data |
| 155 | + elif self.fin == 0: |
| 156 | + if self.opcode != STREAM: |
| 157 | + if self.opcode == PING or self.opcode == PONG: |
| 158 | + raise Exception('control messages can not be fragmented') |
| 159 | + |
| 160 | + self.frag_type = self.opcode |
| 161 | + self.frag_start = True |
| 162 | + self.frag_decoder.reset() |
| 163 | + |
| 164 | + if self.frag_type == TEXT: |
| 165 | + self.frag_buffer = [] |
| 166 | + utf_str = self.frag_decoder.decode(self.data, final = False) |
| 167 | + if utf_str: |
| 168 | + self.frag_buffer.append(utf_str) |
| 169 | + else: |
| 170 | + self.frag_buffer = bytearray() |
| 171 | + self.frag_buffer.extend(self.data) |
| 172 | + |
| 173 | + else: |
| 174 | + if self.frag_start is False: |
| 175 | + raise Exception('fragmentation protocol error') |
| 176 | + |
| 177 | + if self.frag_type == TEXT: |
| 178 | + utf_str = self.frag_decoder.decode(self.data, final = False) |
| 179 | + if utf_str: |
| 180 | + self.frag_buffer.append(utf_str) |
| 181 | + else: |
| 182 | + self.frag_buffer.extend(self.data) |
184 | 183 |
|
185 |
| - else: |
186 |
| - |
187 |
| - if self.opcode == STREAM: |
188 |
| - if self.frag_start is False: |
189 |
| - raise Exception('fragmentation protocol error') |
190 |
| - |
191 |
| - if self.frag_type == TEXT: |
192 |
| - utf_str = self.frag_decoder.decode(self.data, final = True) |
193 |
| - self.frag_buffer += utf_str |
194 |
| - else: |
195 |
| - self.frag_buffer += self.data |
196 |
| - |
197 |
| - self.data = self.frag_buffer |
| 184 | + else: |
| 185 | + if self.opcode == STREAM: |
| 186 | + if self.frag_start is False: |
| 187 | + raise Exception('fragmentation protocol error') |
| 188 | + |
| 189 | + if self.frag_type == TEXT: |
| 190 | + utf_str = self.frag_decoder.decode(self.data, final = True) |
| 191 | + self.frag_buffer.append(utf_str) |
| 192 | + self.data = u''.join(self.frag_buffer) |
| 193 | + else: |
| 194 | + self.frag_buffer.extend(self.data) |
| 195 | + self.data = self.frag_buffer |
| 196 | + |
| 197 | + self.handleMessage() |
| 198 | + |
| 199 | + self.frag_decoder.reset() |
| 200 | + self.frag_type = BINARY |
| 201 | + self.frag_start = False |
| 202 | + self.frag_buffer = None |
| 203 | + |
| 204 | + elif self.opcode == PING: |
| 205 | + self._sendMessage(False, PONG, self.data) |
| 206 | + |
| 207 | + elif self.opcode == PONG: |
| 208 | + pass |
198 | 209 |
|
199 |
| - self.handleMessage() |
200 |
| - |
201 |
| - self.frag_decoder.reset() |
202 |
| - self.frag_type = BINARY |
203 |
| - self.frag_start = False |
204 |
| - self.frag_buffer = None |
205 |
| - |
206 |
| - elif self.opcode == PING: |
207 |
| - self._sendMessage(False, PONG, self.data) |
208 |
| - |
209 |
| - elif self.opcode == PONG: |
210 |
| - pass |
211 |
| - |
212 |
| - else: |
213 |
| - if self.frag_start is True: |
214 |
| - raise Exception('fragmentation protocol error') |
215 |
| - |
216 |
| - if self.opcode == TEXT: |
217 |
| - try: |
218 |
| - self.data = self.data.decode('utf-8', errors='strict') |
219 |
| - except Exception as exp: |
220 |
| - raise Exception('invalid utf-8 payload') |
221 |
| - |
222 |
| - self.handleMessage() |
| 210 | + else: |
| 211 | + if self.frag_start is True: |
| 212 | + raise Exception('fragmentation protocol error') |
| 213 | + |
| 214 | + if self.opcode == TEXT: |
| 215 | + try: |
| 216 | + self.data = self.data.decode('utf-8', errors='strict') |
| 217 | + except Exception as exp: |
| 218 | + raise Exception('invalid utf-8 payload') |
| 219 | + |
| 220 | + self.handleMessage() |
223 | 221 |
|
224 | 222 |
|
225 | 223 | def _handleData(self):
|
@@ -255,14 +253,13 @@ def _handleData(self):
|
255 | 253 |
|
256 | 254 | # else do normal data
|
257 | 255 | else:
|
258 |
| - data = self.client.recv(2048) |
| 256 | + data = self.client.recv(8192) |
259 | 257 | if not data:
|
260 | 258 | raise Exception("remote socket closed")
|
261 | 259 |
|
262 | 260 | for d in data:
|
263 | 261 | self._parseMessage(ord(d))
|
264 | 262 |
|
265 |
| - |
266 | 263 | def close(self, status = 1000, reason = u''):
|
267 | 264 | """
|
268 | 265 | Send Close frame to the client. The underlying socket is only closed
|
@@ -303,8 +300,7 @@ def _sendBuffer(self, buff):
|
303 | 300 |
|
304 | 301 | except socket.error as e:
|
305 | 302 | # if we have full buffers then wait for them to drain and try again
|
306 |
| - if e.errno == errno.EAGAIN: |
307 |
| - #time.sleep(0.001) |
| 303 | + if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]: |
308 | 304 | return buff[already_sent:]
|
309 | 305 | else:
|
310 | 306 | raise e
|
|
0 commit comments