def make( cls, status_code=200, # type: int content=b"", # type: AnyStr headers=( ) # type: Union[Dict[AnyStr, AnyStr], Iterable[Tuple[bytes, bytes]]] ): """ Simplified API for creating response objects. """ resp = cls(b"HTTP/1.1", status_code, status_codes.RESPONSES.get(status_code, "").encode(), (), None) # Assign this manually to update the content-length header. if isinstance(content, bytes): resp.content = content elif isinstance(content, str): resp.text = content else: raise TypeError( "Expected content to be str or bytes, but is {}.".format( type(content).__name__)) # Headers can be list or dict, we differentiate here. if isinstance(headers, dict): resp.headers = nheaders.Headers(**headers) elif isinstance(headers, Iterable): resp.headers = nheaders.Headers(headers) else: raise TypeError( "Expected headers to be an iterable or dict, but is {}.". format(type(headers).__name__)) return resp
def __init__(self, first_line_format, method, scheme, host, port, path, http_version, headers=(), content=None, timestamp_start=None, timestamp_end=None): if not isinstance(headers, nheaders.Headers): headers = nheaders.Headers(headers) self.first_line_format = first_line_format self.method = method self.scheme = scheme self.host = host self.port = port self.path = path self.http_version = http_version self.headers = headers self.content = content self.timestamp_start = timestamp_start self.timestamp_end = timestamp_end
def _read_headers(rfile): """ Read a set of headers. Stop once a blank line is reached. Returns: A headers object Raises: exceptions.HttpSyntaxException """ ret = [] while True: line = rfile.readline() if not line or line == b"\r\n" or line == b"\n": break if line[0] in b" \t": if not ret: raise exceptions.HttpSyntaxException("Invalid headers") # continued header ret[-1] = (ret[-1][0], ret[-1][1] + b'\r\n ' + line.strip()) else: try: name, value = line.split(b":", 1) value = value.strip() if not name: raise ValueError() ret.append((name, value)) except ValueError: raise exceptions.HttpSyntaxException( "Invalid header line: %s" % repr(line) ) return headers.Headers(ret)
def __init__(self, http_version, status_code, reason=None, headers=(), content=None, timestamp_start=None, timestamp_end=None): if isinstance(http_version, six.text_type): http_version = http_version.encode("ascii", "strict") if isinstance(reason, six.text_type): reason = reason.encode("ascii", "strict") if not isinstance(headers, nheaders.Headers): headers = nheaders.Headers(headers) if isinstance(content, six.text_type): raise ValueError("Content must be bytes, not {}".format( type(content).__name__)) self.http_version = http_version self.status_code = status_code self.reason = reason self.headers = headers self.content = content self.timestamp_start = timestamp_start self.timestamp_end = timestamp_end
def __init__(self, http_version, status_code, reason=None, headers=(), content=None, timestamp_start=None, timestamp_end=None): if not isinstance(headers, nheaders.Headers): headers = nheaders.Headers(headers) self.http_version = http_version self.status_code = status_code self.reason = reason self.headers = headers self.content = content self.timestamp_start = timestamp_start self.timestamp_end = timestamp_end
def __init__( self, first_line_format, method, scheme, host, port, path, http_version, headers=(), content=None, timestamp_start=None, timestamp_end=None ): if isinstance(method, six.text_type): method = method.encode("ascii", "strict") if isinstance(scheme, six.text_type): scheme = scheme.encode("ascii", "strict") if isinstance(host, six.text_type): host = host.encode("idna", "strict") if isinstance(path, six.text_type): path = path.encode("ascii", "strict") if isinstance(http_version, six.text_type): http_version = http_version.encode("ascii", "strict") if not isinstance(headers, nheaders.Headers): headers = nheaders.Headers(headers) if isinstance(content, six.text_type): raise ValueError("Content must be bytes, not {}".format(type(content).__name__)) self.first_line_format = first_line_format self.method = method self.scheme = scheme self.host = host self.port = port self.path = path self.http_version = http_version self.headers = headers self.content = content self.timestamp_start = timestamp_start self.timestamp_end = timestamp_end