Пример #1
0
def defaultErrorHandler(request, response):
    if response.stream is not None:
        # Already got an error message
        return response
    if response.code < 300:
        # We only do error messages
        return response

    message = ERROR_MESSAGES.get(response.code, None)
    if message is None:
        # No message specified for that code
        return response

    message = message % {
        'uri': _escape(request.uri),
        'location': _escape(response.headers.getHeader('location')),
        'method': _escape(request.method)
    }

    title = RESPONSES.get(response.code, "")
    body = ("<html><head><title>%d %s</title></head>"
            "<body><h1>%s</h1>%s</body></html>") % (response.code, title,
                                                    title, message)

    response.headers.setHeader("content-type",
                               http_headers.MimeType('text', 'html'))
    response.stream = stream.MemoryStream(body)

    return response
Пример #2
0
    def renderHTTP(self, req):
        responder = getattr(logic_tree['__val__'], self.mode,
                            logic_tree['__val__'].http)(req)
        view = view_tree['__val__']()
        view.req = req

        response = http.Response(200,
                                 stream=stream.MemoryStream(
                                     view.respond().encode('utf8')))
        response.headers.setRawHeaders('Content-Type',
                                       ['text/html; charset=utf-8'])

        if responder:
            responder(response)
        return response
Пример #3
0
    def __init__(self, site, method, uri, headers=None, content=None):
        if not headers:
            headers = http_headers.Headers(headers)

        super(SimpleRequest, self).__init__(site=site,
                                            chanRequest=None,
                                            command=method,
                                            path=uri,
                                            version=self.clientproto,
                                            contentLength=len(content or ''),
                                            headers=headers)

        self.stream = stream.MemoryStream(content or '')

        self.remoteAddr = address.IPv4Address('TCP', '127.0.0.1', 0)
        self._parseURL()
        self.host = 'localhost'
        self.port = 8080
Пример #4
0
        def renderCallback(responder):
            if isinstance(responder, HardResponder):
                return responder.response(getattr(req, 'cookies', None))

            view = self.view['__val__']()
            view.req = req
            responseData = view.respond().encode('utf8')

            response = http.Response(200,
                                     stream=stream.MemoryStream(responseData))
            response.headers.setRawHeaders('Content-Type',
                                           ['text/html; charset=utf-8'])

            if getattr(req, 'cookies', None):
                response.headers.setHeader('Set-Cookie', req.cookies)

            if responder:
                responder(response)

            return response
Пример #5
0
 def responseStream(self):
     return stream.MemoryStream(self.responseText)
Пример #6
0
 def runCode(self, code, inputStream=None):
     if inputStream is None:
         inputStream = stream.MemoryStream("")
     return stream.ProcessStreamer(inputStream, sys.executable,
                                   [sys.executable, "-u", "-c", code],
                                   os.environ)
Пример #7
0
 def setUp(self):
     self.data = testdata.replace('\n', '\r\n')
     s = stream.MemoryStream(self.data)
     self.s = stream.BufferedStream(s)
Пример #8
0
 def setUp(self):
     self.data = testdata
     self.s = stream.MemoryStream(self.data)
Пример #9
0
 def makeStream(self, *args, **kw):
     return stream.MemoryStream(self.text, *args, **kw)
Пример #10
0
 def renderHTTP(self, req):
     if os.path.isfile(self.path):
         return static.File(self.path)
     else:
         return http.Response(200, stream=stream.MemoryStream('forbidden'))