Source code for wptserve.response

from collections import OrderedDict
from datetime import datetime, timedelta
import Cookie
import json
import types
import uuid
import socket

from .constants import response_codes
from .logger import get_logger

missing = object()

[docs]class Response(object): """Object representing the response to a HTTP request :param handler: RequestHandler being used for this response :param request: Request that this is the response for .. attribute:: request Request associated with this Response. .. attribute:: encoding The encoding to use when converting unicode to strings for output. .. attribute:: add_required_headers Boolean indicating whether mandatory headers should be added to the response. .. attribute:: send_body_for_head_request Boolean, default False, indicating whether the body content should be sent when the request method is HEAD. .. attribute:: explicit_flush Boolean indicating whether output should be flushed automatically or only when requested. .. attribute:: writer The ResponseWriter for this response .. attribute:: status Status tuple (code, message). Can be set to an integer, in which case the message part is filled in automatically, or a tuple. .. attribute:: headers List of HTTP headers to send with the response. Each item in the list is a tuple of (name, value). .. attribute:: content The body of the response. This can either be a string or a iterable of response parts. If it is an iterable, any item may be a string or a function of zero parameters which, when called, returns a string.""" def __init__(self, handler, request): self.request = request self.encoding = "utf8" self.add_required_headers = True self.send_body_for_head_request = False self.explicit_flush = False self.close_connection = False self.writer = ResponseWriter(handler, self) self._status = (200, None) self.headers = ResponseHeaders() self.content = [] self.logger = get_logger() @property def status(self): return self._status @status.setter def status(self, value): if hasattr(value, "__len__"): if len(value) != 2: raise ValueError else: self._status = (int(value[0]), str(value[1])) else: self._status = (int(value), None)
[docs] def iter_content(self, read_file=False): """Iterator returning chunks of response body content. If any part of the content is a function, this will be called and the resulting value (if any) returned. :param read_file: - boolean controlling the behaviour when content is a file handle. When set to False the handle will be returned directly allowing the file to be passed to the output in small chunks. When set to True, the entire content of the file will be returned as a string facilitating non-streaming operations like template substitution. """ if isinstance(self.content, types.StringTypes): yield self.content elif hasattr(self.content, "read"): if read_file: yield else: yield self.content else: for item in self.content: if hasattr(item, "__call__"): value = item() else: value = item if value: yield value
[docs] def write_status_headers(self): """Write out the status line and headers for the response""" self.writer.write_status(*self.status) for item in self.headers: self.writer.write_header(*item) self.writer.end_headers()
[docs] def write_content(self): """Write out the response content""" if self.request.method != "HEAD" or self.send_body_for_head_request: for item in self.iter_content(): self.writer.write_content(item)
[docs] def write(self): """Write the whole response""" self.write_status_headers() self.write_content()
[docs] def set_error(self, code, message=""): """Set the response status headers and body to indicate an error""" err = {"code": code, "message": message} data = json.dumps({"error": err}) self.status = code self.headers = [("Content-Type", "application/json"), ("Content-Length", len(data))] self.content = data if code == 500: self.logger.error(message)
class MultipartContent(object): def __init__(self, boundary=None, default_content_type=None): self.items = [] if boundary is None: boundary = str(uuid.uuid4()) self.boundary = boundary self.default_content_type = default_content_type def __call__(self): boundary = "--" + self.boundary rv = ["", boundary] for item in self.items: rv.append(str(item)) rv.append(boundary) rv[-1] += "--" return "\r\n".join(rv) def append_part(self, data, content_type=None, headers=None): if content_type is None: content_type = self.default_content_type self.items.append(MultipartPart(data, content_type, headers)) def __iter__(self): #This is hackish; when writing the response we need an iterable #or a string. For a multipart/byterange response we want an #iterable that contains a single callable; the MultipartContent #object itself yield self class MultipartPart(object): def __init__(self, data, content_type=None, headers=None): self.headers = ResponseHeaders() if content_type is not None: self.headers.set("Content-Type", content_type) if headers is not None: for name, value in headers: if name.lower() == "content-type": func = self.headers.set else: func = self.headers.append func(name, value) = data def __str__(self): rv = [] for item in self.headers: rv.append("%s: %s" % item) rv.append("") rv.append( return "\r\n".join(rv)
[docs]class ResponseHeaders(object): """Dictionary-like object holding the headers for the response""" def __init__(self): = OrderedDict()
[docs] def set(self, key, value): """Set a header to a specific value, overwriting any previous header with the same name :param key: Name of the header to set :param value: Value to set the header to """[key.lower()] = (key, [value])
[docs] def append(self, key, value): """Add a new header with a given name, not overwriting any existing headers with the same name :param key: Name of the header to add :param value: Value to set for the header """ if key.lower() in[key.lower()][1].append(value) else: self.set(key, value)
[docs] def get(self, key, default=missing): """Get the set values for a particular header.""" try: return self[key] except KeyError: if default is missing: return [] return default
def __getitem__(self, key): """Get a list of values for a particular header """ return[key.lower()][1] def __delitem__(self, key): del[key.lower()] def __contains__(self, key): return key.lower() in def __setitem__(self, key, value): self.set(key, value) def __iter__(self): for key, values in for value in values: yield key, value def items(self): return list(self) def update(self, items_iter): for name, value in items_iter: self.append(name, value) def __repr__(self): return repr(
[docs]class ResponseWriter(object): """Object providing an API to write out a HTTP response. :param handler: The RequestHandler being used. :param response: The Response associated with this writer. After each part of the response is written, the output is flushed unless response.explicit_flush is False, in which case the user must call .flush() explicitly.""" def __init__(self, handler, response): self._wfile = handler.wfile self._response = response self._handler = handler self._headers_seen = set() self._headers_complete = False self.content_written = False self.request = response.request self.file_chunk_size = 32 * 1024
[docs] def write_status(self, code, message=None): """Write out the status line of a response. :param code: The integer status code of the response. :param message: The message of the response. Defaults to the message commonly used with the status code.""" if message is None: if code in response_codes: message = response_codes[code][0] else: message = '' self.write("%s %d %s\r\n" % (self._response.request.protocol_version, code, message))
[docs] def write_header(self, name, value): """Write out a single header for the response. :param name: Name of the header field :param value: Value of the header field """ self._headers_seen.add(name.lower()) self.write("%s: %s\r\n" % (name, value)) if not self._response.explicit_flush: self.flush()
def write_default_headers(self): for name, f in [("Server", self._handler.version_string), ("Date", self._handler.date_time_string)]: if name.lower() not in self._headers_seen: self.write_header(name, f()) if (type(self._response.content) in (str, unicode) and "content-length" not in self._headers_seen): #Would be nice to avoid double-encoding here self.write_header("Content-Length", len(self.encode(self._response.content)))
[docs] def end_headers(self): """Finish writing headers and write the separator. Unless add_required_headers on the response is False, this will also add HTTP-mandated headers that have not yet been supplied to the response headers""" if self._response.add_required_headers: self.write_default_headers() self.write("\r\n") if "content-length" not in self._headers_seen: self._response.close_connection = True if not self._response.explicit_flush: self.flush() self._headers_complete = True
[docs] def write_content(self, data): """Write the body of the response.""" if isinstance(data, types.StringTypes): self.write(data) else: self.write_content_file(data) if not self._response.explicit_flush: self.flush()
[docs] def write(self, data): """Write directly to the response, converting unicode to bytes according to response.encoding. Does not flush.""" self.content_written = True try: self._wfile.write(self.encode(data)) except socket.error: # This can happen if the socket got closed by the remote end pass
[docs] def write_content_file(self, data): """Write a file-like object directly to the response in chunks. Does not flush.""" self.content_written = True while True: buf = if not buf: break try: self._wfile.write(buf) except socket.error: break data.close()
[docs] def encode(self, data): """Convert unicode to bytes according to response.encoding.""" if isinstance(data, str): return data elif isinstance(data, unicode): return data.encode(self._response.encoding) else: raise ValueError
[docs] def flush(self): """Flush the output. Returns False if the flush failed due to the socket being closed by the remote end.""" try: self._wfile.flush() return True except socket.error: # This can happen if the socket got closed by the remote end return False