예제 #1
0
    def handle_request(self, request):
        pad = self.get_pad()
        filename = None

        # We start with trying to resolve a source and then use the
        # primary
        source = pad.resolve_url_path(request.path)
        if source is not None:
            # If the request path does not end with a slash but we
            # requested a URL that actually wants a trailing slash, we
            # append it.  This is consistent with what apache and nginx do
            # and it ensures our relative urls work.
            if not request.path.endswith('/') and \
               source.url_path != '/' and \
               source.url_path.endswith('/'):
                return append_slash_redirect(request.environ)

            with CliReporter(self.env, verbosity=self.verbosity):
                builder = self.get_builder(pad)
                prog = builder.build(source)

            artifact = prog.primary_artifact
            if artifact is not None:
                filename = artifact.dst_filename

        # If there is no primary artifact or the url does not point to a
        # known artifact at all, then we just look directly into the
        # output directory and serve from there.  This will for instance
        # pick up thumbnails.
        if filename is None:
            filename = os.path.join(self.output_path, request.path.strip('/'))

        return send_file(request, filename)
예제 #2
0
 def get_response(self, environ, start_response):
     request = Request(environ)
     # last slash required.
     if not request.url.endswith('/'):
         return append_slash_redirect(environ)
     # retrieve original document.
     try:
         url = build_url(request.path)
         doc = urllib2.urlopen(url).read()
     except urllib2.URLError, e:
         logging.error("%s: %s", e.__class__.__name__, str(e))
         raise NotFound
예제 #3
0
 def wsgi(environ, start_response):
     path_info = environ['PATH_INFO']
     if path_info in {'/', '/ipa', '/ipa/'}:
         response = redirect('/ipa/ui/')
         return response(environ, start_response)
     # Redirect to append slash to some routes
     if path_info in {'/ipa/ui', '/ipa/ui/test'}:
         response = append_slash_redirect(environ)
         return response(environ, start_response)
     if path_info == '/favicon.ico':
         response = redirect('/ipa/ui/favicon.ico')
         return response(environ, start_response)
     return app(environ, start_response)
예제 #4
0
 def wsgi(environ, start_response):
     path_info = environ['PATH_INFO']
     if path_info in {'/', '/ipa', '/ipa/'}:
         response = redirect('/ipa/ui/')
         return response(environ, start_response)
     # Redirect to append slash to some routes
     if path_info in {'/ipa/ui', '/ipa/ui/test'}:
         response = append_slash_redirect(environ)
         return response(environ, start_response)
     if path_info == '/favicon.ico':
         response = redirect('/ipa/ui/favicon.ico')
         return response(environ, start_response)
     return app(environ, start_response)
예제 #5
0
    def wsgi_app(self, environ, start_response):

        request_method = self.request.environ.get("REQUEST_METHOD")
        last_character_in_path = self.request.environ.get("PATH_INFO", "")[-1:]

        if self.config.get("append_slash") and last_character_in_path != "/" and request_method != "POST":
            response = append_slash_redirect(self.request.environ)
        else:
            response = self.dispatch_request()

        self.request.end(response)

        return response(environ, start_response)
예제 #6
0
    def __call__(self, environ, start_response):
        path = environ.get('PATH_INFO', '')
        if len(path) == 0:
            return append_slash_redirect(environ)(environ, start_response)
        elif path[-1] != "/":
            return append_slash_redirect(environ)(environ, start_response)

        path = path[1:].split('/')

        if len(path) < 1:
            return Forbidden().get_response(environ)(environ, start_response)
        if path[0] == 'font-roboto':
            return self._instances['font-roboto'](environ, start_response)
        if path[0] != self.token:
            return Forbidden().get_response(environ)(environ, start_response)

        if len(path) < 2 or len(path[1]) == 0:
            body = "<h1>TensorBoard Instances</h1><br /><ul>"
            for name in self._instances.keys():
                if name == 'font-roboto':
                    continue
                body += f'<li><a href="{name}/">{name}</a>'
            body += "</ul>"
            response = Response(body, mimetype='text/html')
            return response(environ, start_response)

        token, target = path[:2]
        tb_path = "/".join(path[2:])

        if token != self.token:
            return Forbidden().get_response(environ)(environ, start_response)
        if target not in self._instances:
            return NotFound().get_response(environ)(environ, start_response)

        environ['PATH_INFO'] = tb_path
        return self._instances[target](environ, start_response)
예제 #7
0
파일: webui.py 프로젝트: turalaksel/lektor
    def resolve_artifact(self, url_path, pad=None, redirect_slash=True):
        """Resolves an artifact and also triggers a build if necessary.
        Returns a tuple in the form ``(artifact_name, filename)`` where
        `artifact_name` can be `None` in case a file was targeted explicitly.
        """
        if pad is None:
            pad = self.get_pad()

        artifact_name = filename = record_path = alt = None

        # We start with trying to resolve a source and then use the
        # primary
        source = pad.resolve_url_path(url_path)
        if source is not None:
            # If the request path does not end with a slash but we
            # requested a URL that actually wants a trailing slash, we
            # append it.  This is consistent with what apache and nginx do
            # and it ensures our relative urls work.
            if (
                not url_path.endswith("/")
                and source.url_path != "/"
                and source.url_path != url_path
            ):
                return abort(append_slash_redirect(request.environ))

            with CliReporter(self.env, verbosity=self.verbosity):
                builder = self.get_builder(pad)
                prog, _ = builder.build(source)

            artifact = prog.primary_artifact
            if artifact is not None:
                artifact_name = artifact.artifact_name
                filename = artifact.dst_filename
            alt = source.alt
            if isinstance(source, Record):
                record_path = source.record.path

        if filename is None:
            path_list = url_path.strip("/").split("/")
            if sys.platform == "win32":
                filename = os.path.join(self.output_path, *path_list)
            else:
                filename = safe_join(self.output_path, *path_list)

        return ResolveResult(artifact_name, filename, record_path, alt)
예제 #8
0
파일: webui.py 프로젝트: item4/lektor
    def resolve_artifact(self, path, pad=None, redirect_slash=True):
        """Resolves an artifact and also triggers a build if necessary.
        Returns a tuple in the form ``(artifact_name, filename)`` where
        `artifact_name` can be `None` in case a file was targeted explicitly.
        """
        if pad is None:
            pad = self.get_pad()

        artifact_name = filename = None

        # We start with trying to resolve a source and then use the
        # primary
        source = pad.resolve_url_path(path)
        if source is not None:
            # If the request path does not end with a slash but we
            # requested a URL that actually wants a trailing slash, we
            # append it.  This is consistent with what apache and nginx do
            # and it ensures our relative urls work.
            if not path.endswith('/') and \
               source.url_path != '/' and \
               source.url_path != path:
                return abort(append_slash_redirect(request.environ))

            with CliReporter(self.env, verbosity=self.verbosity):
                builder = self.get_builder(pad)
                prog, _ = builder.build(source)

            artifact = prog.primary_artifact
            if artifact is not None:
                artifact_name = artifact.artifact_name
                filename = artifact.dst_filename

        if filename is None:
            filename = safe_join(self.output_path, path.strip('/'))
            if os.path.isdir(filename):
                if os.path.isfile(safe_join(filename, 'index.html')):
                    filename = safe_join(filename, 'index.html')
                elif os.path.isfile(safe_join(filename, 'index.htm')):
                    filename = safe_join(filename, 'index.htm')

        return artifact_name, filename
예제 #9
0
    def resolve_artifact(self, path, pad=None, redirect_slash=True):
        """Resolves an artifact and also triggers a build if necessary.
        Returns a tuple in the form ``(artifact_name, filename)`` where
        `artifact_name` can be `None` in case a file was targeted explicitly.
        """
        if pad is None:
            pad = self.get_pad()

        artifact_name = filename = None

        # We start with trying to resolve a source and then use the
        # primary
        source = pad.resolve_url_path(path)
        if source is not None:
            # If the request path does not end with a slash but we
            # requested a URL that actually wants a trailing slash, we
            # append it.  This is consistent with what apache and nginx do
            # and it ensures our relative urls work.
            if not path.endswith('/') and \
               source.url_path != '/' and \
               source.url_path != path:
                return abort(append_slash_redirect(request.environ))

            with CliReporter(self.env, verbosity=self.verbosity):
                builder = self.get_builder(pad)
                prog, _ = builder.build(source)

            artifact = prog.primary_artifact
            if artifact is not None:
                artifact_name = artifact.artifact_name
                filename = artifact.dst_filename

        if filename is None:
            filename = safe_join(self.output_path, path.strip('/'))

        return artifact_name, filename
예제 #10
0
 def app(env, sr):
     return utils.append_slash_redirect(env)(env, sr)
예제 #11
0
 def app(env, sr):
     return utils.append_slash_redirect(env)(env, sr)
예제 #12
0
 def app(request):
     rv = utils.append_slash_redirect(request.environ)
     rv.autocorrect_location_header = autocorrect
     return rv