def _send_response(self, interim=False): self._response.finalize() self._logger.info('< %s', self._response.status_line) self._log_headers(self._response.raw_headers) cls = h11.InformationalResponse if interim else h11.Response self._handler.send_event(cls( http_version=self._response.http_version, status_code=self._response.status_code, reason=force_bytes(self._response.reason), headers=_encode_headers(self._response.raw_headers), ))
def chunk(self, data): self.flush(body_too=False) self._response.body = None # So that `_send_body` skips it # Responses to HEAD can't have a message body. We magically skip # sending data in that case, so the user doesn't have to remember. # (204 and 304 responses also can't have a body, but those have to be # explicitly selected by the user, so it's their problem.) if self.method == 'HEAD': self._logger.debug('not sending %d bytes of response body ' 'because request was HEAD', len(data)) else: self._logger.debug('sending %d bytes of response body', len(data)) self._handler.send_event(h11.Data(data=force_bytes(data)))
def _parse_multipart(body, params): # Some ritual dance is required to get the `cgi` module work in 2017. body = io.BytesIO(body) params = {name: force_bytes(value) for (name, value) in params.items()} parsed = _single_values(cgi.parse_multipart(body, params)) return {name: value.decode('utf-8') for (name, value) in parsed.items()}
def _encode_headers(headers): return [(force_bytes(name), force_bytes(value)) for (name, value) in headers]
def send_raw(self, data): self._logger.info('sending %d bytes of raw data', len(data)) self._handler.send_raw(force_bytes(data, 'utf-8'))
def body(self, data): if hasattr(data, 'read'): # files data = data.read() self._response.body = force_bytes(data, 'utf-8')