Esempio n. 1
0
    def run(self, environ, start_response):
        try:
            path_info = urllib.unquote_plus(environ["PATH_INFO"])
        except KeyError:
            path_info = ""

        if not self.meltscriptname:
            try:
                path_info = "%s%s" % (environ["SCRIPT_NAME"], path_info)
            except KeyError:
                raise WSGIKeyNotPresentError("SCRIPT_NAME", source="root.py")

        xenviron = Struct()
        xenviron.path = Struct()
        xenviron.path.endswithslash = path_info.endswith("/")

        path = [p for p in path_info.split("/") if p] or [""]
        params = dict([(x[0], x[1] or True) for x in parse_qsl(environ["QUERY_STRING"], keep_blank_values=True)])

        # a dirty little trick to deny FieldStorage to use QUERY_STRING
        environ["QUERY_STRING"] = ""

        xenviron.path.args = path
        xenviron.path.scriptname = environ["SCRIPT_NAME"]
        xenviron.path.kwargs = params

        # use Python's ``cgi`` module to parse contents of POST
        try:
            xenviron.fields = BizFieldStorage(environ=environ, fp=environ["wsgi.input"])
        except KeyError:
            raise WSGIKeyNotPresentError("wsgi.input")

        xenviron.cookies = CookieJar(environ.get("HTTP_COOKIE", ""))
        xenviron.env = environ

        # TODO: Find a way to check whether the client browser can use cookies
        try:
            sessionman_lock.acquire()
            try:
                xenviron.cookies, xenviron.session = self.sessionman.get_session(xenviron.cookies)
            except SessionError:
                xenviron.session = self.sessionman.new_session()
        finally:
            sessionman_lock.release()

        appname = path[0]

        # if no application name given in the URL (i.e., it is ``/``),
        # ... call the index/default index application
        if not appname:
            if not self._index:
                return self._default_index(start_response)

            appname = self._index

        try:
            app = self._applist[appname](xenviron)
        except KeyError, e:
            xenviron.error_code = 404
            if self.debug:
                xenviron.error_message = unicode(e)
            else:
                xenviron.error_message = _(u"Method not found")

            return self._default_error(start_response, xenviron.error_code, xenviron.error_message)
Esempio n. 2
0
    def run(self, environ, start_response):
        def tuplize(x):
            l = x.split("=")[:2]
            if len(l) == 1:
                l.append(True)
            return tuple(l)

        self.environ = environ
        self.start_response = start_response

        try:
            path_info = urllib.unquote_plus(environ["PATH_INFO"])
        except KeyError:
            raise WSGIKeyNotPresentError("PATH_INFO")

        path = [p for p in path_info.split("/") if p] or [""]
        if "QUERY_STRING" in environ:
            query_string = urllib.unquote_plus(environ["QUERY_STRING"])
            params = dict([tuplize(x) for x in query_string.split("&") if x])
        else:
            params = {}

            # a dirty little trick to deny FieldStorage to use QUERY_STRING
        self.environ["QUERY_STRING"] = ""

        xenviron = Struct()
        xenviron.args = path
        xenviron.kwargs = params
        try:
            xenviron.fields = FieldStorage(environ=self.environ, fp=self.environ["wsgi.input"])
        except KeyError:
            raise WSGIKeyNotPresentError("wsgi.input")
        xenviron.cookies = SimpleCookie(environ.get("HTTP_COOKIE", ""))

        try:
            xenviron.cookies, xenviron.session = self.sessionman.get_session(xenviron.cookies)
        except SessionError:
            xenviron.session = self.sessionman.new_session()

        if not path[0]:
            if not self._index:
                return self._default_index()

            app = self._index(xenviron)
        else:
            try:
                name = path[0]
                app = self._applist[name](xenviron)
            except KeyError:
                xenviron.error_code = 404
                xenviron.error_message = _(u"Method not found")

                if self._error:
                    app = self._error(xenviron)
                else:
                    return self._default_error(xenviron.error_code, xenviron.error_message)

        app.body.refresh(xenviron)
        app.body.run()
        app_xenviron, response = app.body.get()

        # further process the app_xenviron
        self.sessionman.update(app_xenviron.session)

        # return preparations
        cookies = SimpleCookie()
        cookies.update(app_xenviron.cookies)
        cookies.update(app_xenviron.session.sidcookie)
        cookies = str(cookies).split()[1]
        response.heads.set_cookie = cookies
        return self._prepare_response(response)
Esempio n. 3
0
    def run(self, environ, start_response):
        def tuplize(x):
            l = x.split("=")[:2]
            if len(l) == 1:
                l.append(True)
            return tuple(l)

        try:
            path_info = urllib.unquote_plus(environ["PATH_INFO"])
        except KeyError:
            path_info = ""

        if not self.meltscriptname:
            try:
                path_info = "%s%s" % (environ["SCRIPT_NAME"], path_info)
            except KeyError:
                raise WSGIKeyNotPresentError("SCRIPT_NAME", source="root.py")

        xenviron = Struct()
        xenviron.path = Struct()
        xenviron.path.endswithslash = path_info.endswith("/")

        path = [p for p in path_info.split("/") if p] or [""]
        if "QUERY_STRING" in environ:
            query_string = urllib.unquote_plus(environ["QUERY_STRING"])
            params = dict([tuplize(x) for x in query_string.split("&") if x])
        else:
            params = {}

            # a dirty little trick to deny FieldStorage to use QUERY_STRING
        environ["QUERY_STRING"] = ""

        if self.meltscriptname:
            try:
                xenviron.path.args = path
                xenviron.path.scriptname = environ["SCRIPT_NAME"]
            except KeyError:
                raise WSGIKeyNotPresentError("SCRIPT_NAME")
        else:
            xenviron.path.args = path
            xenviron.path.scriptname = ""

        xenviron.path.kwargs = params
        try:
            xenviron.fields = FieldStorage(environ=environ, fp=environ["wsgi.input"])
        except KeyError:
            raise WSGIKeyNotPresentError("wsgi.input")

        xenviron.cookies = SimpleCookie(environ.get("HTTP_COOKIE", ""))

        try:
            sessionman_lock.acquire()
            try:
                xenviron.cookies, xenviron.session = self.sessionman.get_session(xenviron.cookies)
            except SessionError:
                xenviron.session = self.sessionman.new_session()
        finally:
            sessionman_lock.release()

        appname = path[0]

        if not appname:
            if not self._index:
                return self._default_index(start_response)

            app = self._index(xenviron)
        else:
            try:
                name = appname
                app = self._applist[name](xenviron)

            except KeyError:
                xenviron.error_code = 404
                xenviron.error_message = _(u"Method not found")

                if self._error:
                    app = self._error(xenviron)
                else:
                    return self._default_error(start_response, xenviron.error_code, xenviron.error_message)

        response = app.body(xenviron)

        try:
            sessionman_lock.acquire()
            # further process the app_xenviron
            self.sessionman.update(response.session)
        finally:
            sessionman_lock.release()

        return self._prepare_response(start_response, Response(response))