def _generate_headers(self): lines = [' '.join((self.request.version, str(self._status_code), httplib.responses[self._status_code]))] lines.extend(['{}: {}'.format(n, v) for n, v in self._headers.items()]) for cookie in self._cookies.values(): LOGGER.debug(to_str('Set-Cookie: ' + cookie.OutputString(None))) lines.append(to_str('Set-Cookie: ' + cookie.OutputString(None))) return '\r\n'.join(lines) + '\r\n\r\n'
def _generate_headers(self): lines = [self.request.version + " " + str(self._status_code) + " " + httplib.responses[self._status_code]] lines.extend(['%s: %s' % (n, v) for n, v in self._headers.iteritems()]) for cookie in self._cookies.values(): LOGGER.debug(to_str("Set-Cookie: " + cookie.OutputString(None))) lines.append(to_str("Set-Cookie: " + cookie.OutputString(None))) return '\r\n'.join(lines) + '\r\n\r\n'
def redirect_to(url, permanent=False, response=None): response = response or get_current_http_response() if response.headers_written: raise Exception("Cannot redirect after headers have been written") response.status_code = httplib.MOVED_PERMANENTLY if permanent else httplib.FOUND # Remove whitespace url = re.sub(br"[\x00-\x20]+", "", to_str(url)) response.set_header("Location", urlparse.urljoin(to_str(response.request.uri), url)) end_http_request_processing()
def clear_header(self, name): """Clears an outgoing header, undoing a previous `set_header` call. Note that this method does not apply to multi-valued headers set by `add_header`. """ name = to_str(name) if name in self._headers: del self._headers[name]
def set_header(self, name, value): if self.headers_written: LOGGER.warn('setting header too late: header to set is %(name)s=%(value)s\n%(headers_written)s', { 'name': name, 'value': value, 'headers_written': self.headers_written }) if isinstance(value, datetime): t = calendar.timegm(value.utctimetuple()) value = email.utils.formatdate(t, localtime=False, usegmt=True) elif isinstance(value, int) or isinstance(value, long): value = str(value) else: value = to_str(value) # If \n is allowed into the header, it is possible to inject # additional headers or split the request. Also cap length to # prevent obviously erroneous values. if INVALID_HEADER_CHAR_RE.search(value): raise ValueError('Unsafe header value %r', value) self._headers[to_str(name)] = value
def write(self, chunk): assert not self._finished chunk = to_str(chunk) self._write_buffer.append(chunk)