/usr/lib/python3/dist-packages/websockets/http.py is in python3-websockets 3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | """
The :mod:`websockets.http` module provides HTTP parsing functions. They're
merely adequate for the WebSocket handshake messages.
These functions cannot be imported from :mod:`websockets`; they must be
imported from :mod:`websockets.http`.
"""
__all__ = ['read_request', 'read_response', 'USER_AGENT']
import asyncio
import email.parser
import io
import sys
from .version import version as websockets_version
MAX_HEADERS = 256
MAX_LINE = 4096
USER_AGENT = ' '.join((
'Python/{}'.format(sys.version[:3]),
'websockets/{}'.format(websockets_version),
))
@asyncio.coroutine
def read_request(stream):
"""
Read an HTTP/1.1 request from ``stream``.
Return ``(path, headers)`` where ``path`` is a :class:`str` and
``headers`` is a :class:`~email.message.Message`. ``path`` isn't
URL-decoded.
Raise an exception if the request isn't well formatted.
The request is assumed not to contain a body.
"""
request_line, headers = yield from read_message(stream)
method, path, version = request_line[:-2].decode().split(None, 2)
if method != 'GET':
raise ValueError("Unsupported method")
if version != 'HTTP/1.1':
raise ValueError("Unsupported HTTP version")
return path, headers
@asyncio.coroutine
def read_response(stream):
"""
Read an HTTP/1.1 response from ``stream``.
Return ``(status, headers)`` where ``status`` is a :class:`int` and
``headers`` is a :class:`~email.message.Message`.
Raise an exception if the request isn't well formatted.
The response is assumed not to contain a body.
"""
status_line, headers = yield from read_message(stream)
version, status, reason = status_line[:-2].decode().split(None, 2)
if version != 'HTTP/1.1':
raise ValueError("Unsupported HTTP version")
return int(status), headers
@asyncio.coroutine
def read_message(stream):
"""
Read an HTTP message from ``stream``.
Return ``(start_line, headers)`` where ``start_line`` is :class:`bytes`
and ``headers`` is a :class:`~email.message.Message`.
The message is assumed not to contain a body.
"""
start_line = yield from read_line(stream)
header_lines = io.BytesIO()
for num in range(MAX_HEADERS):
header_line = yield from read_line(stream)
header_lines.write(header_line)
if header_line == b'\r\n':
break
else:
raise ValueError("Too many headers")
header_lines.seek(0)
headers = email.parser.BytesHeaderParser().parse(header_lines)
return start_line, headers
@asyncio.coroutine
def read_line(stream):
"""
Read a single line from ``stream``.
"""
line = yield from stream.readline()
if len(line) > MAX_LINE:
raise ValueError("Line too long")
if not line.endswith(b'\r\n'):
raise ValueError("Line without CRLF")
return line
|