Esempio n. 1
0
    def __call__(self, environ, start_response):
        """ Apple WSGI-interface. """

        try:
            environ['bottle.app'] = self
            request.bind(environ)
            response.bind(self)
            handler, args = self.match_url(request.path, request.method)
            # If its a Websocket or Async call
            if isinstance( handler, Async ):
                return handler( environ, start_response, args )

            result = handler( **args )
            status = '%d %s' % (response.status, HTTP_CODES[response.status])
            start_response(status, response.headerlist)
            # RFC2616 Section 4.3
            if response.status in (100, 101, 204, 304) or request.method == 'HEAD':
                return []

            socket = Socket( environ['eventlet.input'].get_socket(), environ )
            for value in iter(result):
                socket.write( self._cast( value, request, response ) )

            return []

        except (KeyboardInterrupt, SystemExit):
            raise
        except HTTPResponse, e:
            start_response( '%s %s' % (e.status, HTTP_CODES[e.status]), [('Content-Type', 'text/html')] )
            return e
Esempio n. 2
0
    def _handle(self, environ):
        path = environ['bottle.raw_path'] = environ['PATH_INFO']
        try:
            environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
        except UnicodeError:
            return HTTPError(400, 'Invalid path string. Expected UTF-8')

        try:
            environ['bottle.app'] = self
            request.bind(environ)
            response.bind()
            try:
                self.trigger_hook("before_request")
                route, args = self.router.match(environ)
                environ['route.handle'] = route
                environ['bottle.route'] = route
                environ['route.url_args'] = args
                out = route.call(**args)
                if isinstance(out, asyncio.Future) or inspect.isgenerator(out):
                    out = yield from out
                return out
            finally:
                self.trigger_hook("after_request")
        except HTTPResponse:
            return _e()
        except RouteReset:
            route.reset()
            return (yield from self._handle(environ))
        except (KeyboardInterrupt, SystemExit, MemoryError):
            raise
        except Exception:
            if not self.catchall: raise
            stacktrace = format_exc()
            environ['wsgi.errors'].write(stacktrace)
            return HTTPError(500, "Internal Server Error", _e(), stacktrace)
Esempio n. 3
0
 def _handle(self, environ):
     try:
         environ['bottle.app'] = self
         request.bind(environ)
         response.bind()
         route, args = self.router.match(environ)
         environ['route.handle'] = route
         environ['bottle.route'] = route
         environ['route.url_args'] = args
         out = route.call(**args)
         if isinstance(out, asyncio.Future) or inspect.isgenerator(out):
             out = yield from out
         return out
     except HTTPResponse:
         return _e()
     except RouteReset:
         route.reset()
         return (yield from self._handle(environ))
     except (KeyboardInterrupt, SystemExit, MemoryError):
         raise
     except Exception:
         if not self.catchall: raise
         stacktrace = format_exc()
         environ['wsgi.errors'].write(stacktrace)
         return HTTPError(500, "Internal Server Error", _e(), stacktrace)
Esempio n. 4
0
    def _handle(self, environ):
        path = environ['bottle.raw_path'] = environ['PATH_INFO']
        try:
            environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
        except UnicodeError:
            return HTTPError(400, 'Invalid path string. Expected UTF-8')

        try:
            environ['bottle.app'] = self
            request.bind(environ)
            response.bind()
            try:
                self.trigger_hook("before_request")
                route, args = self.router.match(environ)
                environ['route.handle'] = route
                environ['bottle.route'] = route
                environ['route.url_args'] = args
                out = route.call(**args)
                if isinstance(out, asyncio.Future) or inspect.isgenerator(out):
                    out = yield from out
                return out
            finally:
                self.trigger_hook("after_request")
        except HTTPResponse:
            return _e()
        except RouteReset:
            route.reset()
            return (yield from self._handle(environ))
        except (KeyboardInterrupt, SystemExit, MemoryError):
            raise
        except Exception:
            if not self.catchall: raise
            stacktrace = format_exc()
            environ['wsgi.errors'].write(stacktrace)
            return HTTPError(500, "Internal Server Error", _e(), stacktrace)
Esempio n. 5
0
 def test_ims(self):
     """ SendFile: If-Modified-Since"""
     request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime(
         "%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
     response.bind()
     try:
         send_file(os.path.basename(__file__), root='./')
     except HTTPError, e:
         self.assertEqual(304, e.http_status)
Esempio n. 6
0
 def serve():
     '''Puppet Web Trading Interface'''
     task = request.json
     if task:
         try:
             return getattr(acc, task.pop('action'))(**task)
         except Exception as e:
             response.bind(status=502)
             return {'puppet': str(e)}
     return {'puppet': '仅支持json格式'}
Esempio n. 7
0
 def setUp(self):
     response.bind()
Esempio n. 8
0
 def setUp(self):
     response.bind()
Esempio n. 9
0
 def setUp(self):
     e = dict()
     wsgiref.util.setup_testing_defaults(e)
     b = Bottle()
     request.bind(e)
     response.bind()
Esempio n. 10
0
 def setUp(self):
     e = dict()
     wsgiref.util.setup_testing_defaults(e)
     b = Bottle()
     request.bind(e)
     response.bind()