Ejemplo n.º 1
0
 def __init__(self, conn, server, request=None):
     self._buffer = b""
     self.request = request
     self.alive = True
     self.connection = conn
     self.server = server
     self.headers = Headers()
     self.status = 200
     self.protocol = b"HTTP/1.1"
     self.charset = "utf-8"
Ejemplo n.º 2
0
    def send_error(self, code, body=None, headers={}):
        self.check_self()
        if body is not None:
            if isinstance(body, str):
                body = body.encode("utf-8")
            if not isinstance(body, bytes):
                raise TypeError(
                    "res.send_error accepts only str or bytes or None as body!"
                )

        self.status = code
        self.headers = Headers()

        for key, value in headers:
            self.headers[key] = value

        self.send_all(body, buffer=False)
Ejemplo n.º 3
0
def parse_headers(raw):
    data = raw.strip().split(CRLF)

    head = data.pop(0).split()

    method = validators.method(head[0])
    url = validators.url(head[1])
    query = url[1]
    url = url[0]
    protocol = validators.protocol(head[2])

    headers = Headers()
    for line in data:
        try:
            key, value = line.split(HTTP_HEADER_SEPARATOR)
        except Exception:
            continue

        headers[key.strip()] = value.strip()

    return headers, method, url, query, protocol