<commit_before># Module:   static
# Date:     13th September 2007
# Author:   <NAME>, prologic at shortcircuit dot net dot au

"""Static

This modStatic implements a Static dispatcher used to serve up static
resources and an optional apache-style directory listing.
"""

import os
from string import Template

from circuits import handler, BaseComponent

from circuits.web.tools import expires, serve_file

DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />
        <meta name="robots" content="NONE,NOARCHIVE" />
        <title>Index of $directory</title>
    </head>
    <body>
        <h1>Index of $directory</h1>
        <ul>
            $url_up
            $listing
        </ul>
    </body>
</html>
"""

_dirlisting_template = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE)


class Static(BaseComponent):

    channel = "web"

    def __init__(self, path=None, docroot=None,
                 defaults=("index.html", "index.xhtml",), dirlisting=False):
        super(Static, self).__init__()

        self.path = path
        self.docroot = docroot or os.getcwd()
        self.defaults = defaults
        self.dirlisting = dirlisting

    @handler("request", filter=True, priority=0.9)
    def _on_request(self, request, response):
        if self.path is not None and not request.path.startswith(self.path):
            return

        path = request.path

        if self.path is not None:
            path = path[len(self.path):]
        path = path.strip("/")

        if path:
            location = os.path.abspath(os.path.join(self.docroot, path))
        else:
            location = os.path.abspath(os.path.join(self.docroot, "."))

        if not os.path.exists(location):
            return

        # Is it a file we can serve directly?
        if os.path.isfile(location):
            # Don't set cookies for static content
            response.cookie.clear()
            expires(request, response, (3600 * 24 * 30), force=True)<commit_msg>Fixed a bug with the static dispatcher for circuits.web whereby the path was not being unquoted<commit_after># Module:   static
# Date:     13th September 2007
# Author:   <NAME>, prologic at shortcircuit dot net dot au

"""Static

This modStatic implements a Static dispatcher used to serve up static
resources and an optional apache-style directory listing.
"""

import os
from urllib import unquote
from string import Template

from circuits import handler, BaseComponent

from circuits.web.tools import expires, serve_file

DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />
        <meta name="robots" content="NONE,NOARCHIVE" />
        <title>Index of $directory</title>
    </head>
    <body>
        <h1>Index of $directory</h1>
        <ul>
            $url_up
            $listing
        </ul>
    </body>
</html>
"""

_dirlisting_template = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE)


class Static(BaseComponent):

    channel = "web"

    def __init__(self, path=None, docroot=None,
                 defaults=("index.html", "index.xhtml",), dirlisting=False):
        super(Static, self).__init__()

        self.path = path
        self.docroot = docroot or os.getcwd()
        self.defaults = defaults
        self.dirlisting = dirlisting

    @handler("request", filter=True, priority=0.9)
    def _on_request(self, request, response):
        if self.path is not None and not request.path.startswith(self.path):
            return

        path = request.path

        if self.path is not None:
            path = path[len(self.path):]

        path = unquote(path.strip("/"))

        if path:
            location = os.path.abspath(os.path.join(self.docroot, path))
        else:
            location = os.path.abspath(os.path.join(self.docroot, "."))

        if not os.path.exists(location):
            return

        # Is it a file we can serve directly?
        if os.path.isfile(location):
            # Don't set cookies for static content
            response.cookie.clear()
            expires(request, response, (3600 * 24 * 30), force=True)
{'fraction_non_alphanumeric': 0.12305395606739176, 'fraction_numerical': 0.012795905310300703, 'mean_word_length': 2.585626911314985, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7038931', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1406, 'n_words': 370}