Login

RandomFileExtensionMiddleware

Author:
jezdez
Posted:
June 2, 2008
Language:
Python
Version:
.96
Tags:
middleware
Score:
3 (after 3 ratings)

If you feel nostalgic for the old days of crufty URLs, put this middleware somewhere in your Django app and add the first entry in settings.MIDDLEWARE_CLASSES as shown below. Keep in mind that you need to replace 'path.to' with the correct Python path to the middleware.

MIDDLEWARE_CLASSES = (
    'path.to.RandomFileExtensionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
)

Note: Although this works it's still a joke.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
from django.http import HttpResponseRedirect
from django.conf import settings

extensions = (
    ".do",
    ".pl",
    ".py",
    ".asp",
    ".cfm",
    ".cgi",
    ".dll",
    ".htm",
    ".jsp",
    ".xml",
    ".exe",
    ".php",
    ".aspx",
    ".html",
    ".php3",
    ".php4",
    ".shtml",
    ".action",
)

indexes = (
    "/index",
    "/main",
)

class RandomFileExtensionMiddleware(object):
    """
    A middleware that replaces the request path automatically with a random
    file extension to make you feel nostalgic for old crufty URLs. This
    will respect settings.APPEND_SLASH when it converts the request path back
    to normal.
    """
    def _is_valid_extension(self, path):
        for i, ext in enumerate(extensions):
            if path.endswith(ext):
                return True
        return False
    
    def _is_valid_index(self, path):
        for i, index in enumerate(indexes):
            if path.startswith(index):
                return True
        return False
    
    def process_request(self, request):
        if request.path == "/":
            request.path = random.choice(indexes) + random.choice(extensions)
            return HttpResponseRedirect(request.build_absolute_uri())

        elif request.path.endswith("/"):
            request.path = request.path[:-1] + random.choice(extensions)
            return HttpResponseRedirect(request.build_absolute_uri())

        elif self._is_valid_extension(request.path):
            if self._is_valid_index(request.path):
                request.path = "/"
            else:
                path = request.path.split("/")
                path[-1] = "".join(path[-1].split(".")[:-1])
                if settings.APPEND_SLASH:
                    path[-1] = path[-1] + '/'
                request.path = "/".join(path)

More like this

  1. Gzip decorator by SmileyChris 6 years, 8 months ago
  2. Clean Reversion History: Remove unimportant Changes by guettli 3 years, 1 month ago
  3. Smart append slash middleware by akaihola 7 years, 1 month ago
  4. Class-based coverage test runner by brutasse 4 years, 9 months ago
  5. Basic Auth Middleware by joshsharp 3 years, 9 months ago

Comments

Please login first before commenting.