Ejemplo n.º 1
0
 def read_upgrade_response(self):
     from calibre.srv.http_request import read_headers
     st = monotonic()
     buf, idx = b'', -1
     while idx == -1:
         data = self.socket.recv(1024)
         if not data:
             raise ValueError('Server did not respond with a valid HTTP upgrade response')
         buf += data
         if len(buf) > 4096:
             raise ValueError('Server responded with too much data to HTTP upgrade request')
         if monotonic() - st > self.timeout:
             raise ValueError('Timed out while waiting for server response to HTTP upgrade')
         idx = buf.find(b'\r\n\r\n')
     response, rest = buf[:idx+4], buf[idx+4:]
     if rest:
         self.read_buf.append(rest)
     lines = (x + b'\r\n' for x in response.split(b'\r\n')[:-1])
     rl = next(lines)
     if rl != b'HTTP/1.1 101 Switching Protocols\r\n':
         raise ValueError('Server did not respond with correct switching protocols line')
     headers = read_headers(partial(next, lines))
     key = standard_b64encode(sha1(self.key + GUID_STR).digest())
     if headers.get('Sec-WebSocket-Accept') != key:
         raise ValueError('Server did not respond with correct key in Sec-WebSocket-Accept')
Ejemplo n.º 2
0
 def read_upgrade_response(self):
     from calibre.srv.http_request import read_headers
     st = monotonic()
     buf, idx = b'', -1
     while idx == -1:
         data = self.socket.recv(1024)
         if not data:
             raise ValueError('Server did not respond with a valid HTTP upgrade response')
         buf += data
         if len(buf) > 4096:
             raise ValueError('Server responded with too much data to HTTP upgrade request')
         if monotonic() - st > self.timeout:
             raise ValueError('Timed out while waiting for server response to HTTP upgrade')
         idx = buf.find(b'\r\n\r\n')
     response, rest = buf[:idx+4], buf[idx+4:]
     if rest:
         self.read_buf.append(rest)
     lines = (x + b'\r\n' for x in response.split(b'\r\n')[:-1])
     rl = next(lines)
     if rl != b'HTTP/1.1 101 Switching Protocols\r\n':
         raise ValueError('Server did not respond with correct switching protocols line')
     headers = read_headers(partial(next, lines))
     key = as_base64_unicode(sha1(self.key + GUID_STR).digest())
     if headers.get('Sec-WebSocket-Accept') != key:
         raise ValueError('Server did not respond with correct key in Sec-WebSocket-Accept')
Ejemplo n.º 3
0
 def parse_part():
     line = buf.readline()
     if not line:
         raise ValueError('Premature end of message')
     if not line.startswith(b'--' + sep):
         raise ValueError('Malformed start of multipart message: %s' %
                          reprlib.repr(line))
     if line.endswith(b'--'):
         return None
     headers = read_headers(buf.readline)
     cr = headers.get('Content-Range')
     if not cr:
         raise ValueError('Missing Content-Range header in sub-part')
     if not cr.startswith('bytes '):
         raise ValueError(
             'Malformed Content-Range header in sub-part, no prefix')
     try:
         start, stop = map(
             lambda x: int(x.strip()),
             cr.partition(' ')[-1].partition('/')[0].partition('-')[::2])
     except Exception:
         raise ValueError(
             'Malformed Content-Range header in sub-part, failed to parse byte range'
         )
     content_length = stop - start + 1
     ret = buf.read(content_length)
     if len(ret) != content_length:
         raise ValueError(
             'Malformed sub-part, length of body not equal to length specified in Content-Range'
         )
     buf.readline()
     return (start, ret)
Ejemplo n.º 4
0
 def parse_part():
     line = buf.readline()
     if not line:
         raise ValueError('Premature end of message')
     if not line.startswith(b'--' + sep):
         raise ValueError('Malformed start of multipart message: %s' % reprlib.repr(line))
     if line.endswith(b'--'):
         return None
     headers = read_headers(buf.readline)
     cr = headers.get('Content-Range')
     if not cr:
         raise ValueError('Missing Content-Range header in sub-part')
     if not cr.startswith('bytes '):
         raise ValueError('Malformed Content-Range header in sub-part, no prefix')
     try:
         start, stop = map(lambda x: int(x.strip()), cr.partition(' ')[-1].partition('/')[0].partition('-')[::2])
     except Exception:
         raise ValueError('Malformed Content-Range header in sub-part, failed to parse byte range')
     content_length = stop - start + 1
     ret = buf.read(content_length)
     if len(ret) != content_length:
         raise ValueError('Malformed sub-part, length of body not equal to length specified in Content-Range')
     buf.readline()
     return (start, ret)