Example #1
0
    def finish_headers(self):
        """ Finalize headers """
        self._set_state(ResponseBodyWaitState)
        request = self._request
        if request.protocol >= (1, 0):
            out, hdict, writer = [], self._hdict, request.connection.writer
            request.response_headers = hdict
            request.connection.compute_status()
            if request.connection.persist or request.sent_100:
                # suck the whole request body, before sending a response
                # (avoiding dead locks)

                if request.expects_100 and not request.sent_100:
                    request.send_continue = self._send_continue
                # pylint: disable = W0212
                stream = request._request_body_stream
                if stream is not None:
                    dummy, read = True, stream.read
                    while dummy:
                        dummy = read(0)
                if request.send_continue == self._send_continue:
                    del request.send_continue

            hdict.update({
                'server': ["WTF"], 'date': [_http_util.make_date()]
            })
            for key, value in request.connection.headers.iteritems():
                if key not in hdict:
                    self._headers.append(key)
                hdict[key] = [value]

            for name in self._headers:
                if name in hdict:
                    cname = name.title()
                    if name == 'set-cookie':
                        out.extend([(cname, val) for val in hdict[name]])
                    else:
                        out.append((cname, ", ".join(hdict[name])))

            writer.write(
                # pylint: disable = W0212
                "HTTP/%d.%d " % request.http_version +
                    request._response_status_line + CRLF
            )
            writer.writelines([
                "%s: %s%s" % (name, value, CRLF) for name, value in out
            ])
            writer.write(CRLF)
Example #2
0
    def error(self, status, message):
        """
        Emit a simple error

        :Parameters:
         - `status`: The status line to emit, it will be repeated in the body
           (which is labeled text/plain for >= HTTP/1.0 or wrapped into HTML
           for HTTP/0.9)
         - `message`: The message to emit

        :Types:
         - `status`: ``str``
         - `message`: ``str``
        """
        protocol, write = self.protocol, self.connection.writer.write
        if protocol >= (1, 0):
            out = status + CRLF + message + CRLF
            write("HTTP/%d.%d " % self.http_version + status + CRLF)
            write("Date: %s%s" % (_http_util.make_date(), CRLF))
            write("Content-Type: text/plain" + CRLF)
            write("Content-Length: %s%s" % (len(out), CRLF))
            if protocol >= (1, 1):
                write("Connection: close" + CRLF)
            write(CRLF)
            write(out)
        else:
            out = """
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>%(status)s</title></head>
<body><h1>%(status)s</h1><p>%(message)s</p></body>
</html>
            """.strip() % {
                'status': status.replace('&', '&amp;').replace('<', '&lt;'),
                'message': message.replace('&', '&amp;').replace('<', '&lt;'),
            }
            write(out + CRLF)