summaryrefslogtreecommitdiff
path: root/pelican/server.py
blob: 913c3761f8a066a74b5dcdbaf28c2c4a80d6f106 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import argparse
import logging
import os
import posixpath
import ssl
import sys
import urllib
from http import server

try:
    from magic import from_file as magic_from_file
except ImportError:
    magic_from_file = None

from pelican.log import console  # noqa: F401
from pelican.log import init as init_logging
logger = logging.getLogger(__name__)


def parse_arguments():
    parser = argparse.ArgumentParser(
        description='Pelican Development Server',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("port", default=8000, type=int, nargs="?",
                        help="Port to Listen On")
    parser.add_argument("server", default="", nargs="?",
                        help="Interface to Listen On")
    parser.add_argument('--ssl', action="store_true",
                        help='Activate SSL listener')
    parser.add_argument('--cert', default="./cert.pem", nargs="?",
                        help='Path to certificate file. ' +
                        'Relative to current directory')
    parser.add_argument('--key', default="./key.pem", nargs="?",
                        help='Path to certificate key file. ' +
                        'Relative to current directory')
    parser.add_argument('--path', default=".",
                        help='Path to pelican source directory to serve. ' +
                        'Relative to current directory')
    return parser.parse_args()


class ComplexHTTPRequestHandler(server.SimpleHTTPRequestHandler):
    SUFFIXES = ['.html', '/index.html', '/', '']

    extensions_map = {
        **server.SimpleHTTPRequestHandler.extensions_map,
        ** {
            # web fonts
            ".oft": "font/oft",
            ".sfnt": "font/sfnt",
            ".ttf": "font/ttf",
            ".woff": "font/woff",
            ".woff2": "font/woff2",
        },
    }

    def translate_path(self, path):
        # abandon query parameters
        path = path.split('?', 1)[0]
        path = path.split('#', 1)[0]
        # Don't forget explicit trailing slash when normalizing. Issue17324
        trailing_slash = path.rstrip().endswith('/')
        path = urllib.parse.unquote(path)
        path = posixpath.normpath(path)
        words = path.split('/')
        words = filter(None, words)
        path = self.base_path
        for word in words:
            if os.path.dirname(word) or word in (os.curdir, os.pardir):
                # Ignore components that are not a simple file/directory name
                continue
            path = os.path.join(path, word)
        if trailing_slash:
            path += '/'
        return path

    def do_GET(self):
        # cut off a query string
        original_path = self.path.split('?', 1)[0]
        # try to find file
        self.path = self.get_path_that_exists(original_path)

        if not self.path:
            return

        server.SimpleHTTPRequestHandler.do_GET(self)

    def get_path_that_exists(self, original_path):
        # Try to strip trailing slash
        trailing_slash = original_path.endswith('/')
        original_path = original_path.rstrip('/')
        # Try to detect file by applying various suffixes
        tries = []
        for suffix in self.SUFFIXES:
            if not trailing_slash and suffix == '/':
                # if original request does not have trailing slash, skip the '/' suffix
                # so that base class can redirect if needed
                continue
            path = original_path + suffix
            if os.path.exists(self.translate_path(path)):
                return path
            tries.append(path)
        logger.warning("Unable to find `%s` or variations:\n%s",
                       original_path,
                       '\n'.join(tries))
        return None

    def guess_type(self, path):
        """Guess at the mime type for the specified file.
        """
        mimetype = server.SimpleHTTPRequestHandler.guess_type(self, path)

        # If the default guess is too generic, try the python-magic library
        if mimetype == 'application/octet-stream' and magic_from_file:
            mimetype = magic_from_file(path, mime=True)

        return mimetype

    def log_message(self, format, *args):
        logger.info(format, *args)


class RootedHTTPServer(server.HTTPServer):
    def __init__(self, base_path, *args, **kwargs):
        server.HTTPServer.__init__(self, *args, **kwargs)
        self.RequestHandlerClass.base_path = base_path


if __name__ == '__main__':
    init_logging(level=logging.INFO)
    logger.warning("'python -m pelican.server' is deprecated.\nThe "
                   "Pelican development server should be run via "
                   "'pelican --listen' or 'pelican -l'.\nThis can be combined "
                   "with regeneration as 'pelican -lr'.\nRerun 'pelican-"
                   "quickstart' to get new Makefile and tasks.py files.")
    args = parse_arguments()
    RootedHTTPServer.allow_reuse_address = True
    try:
        httpd = RootedHTTPServer(
            args.path, (args.server, args.port), ComplexHTTPRequestHandler)
        if args.ssl:
            httpd.socket = ssl.wrap_socket(
                httpd.socket, keyfile=args.key,
                certfile=args.cert, server_side=True)
    except ssl.SSLError as e:
        logger.error("Couldn't open certificate file %s or key file %s",
                     args.cert, args.key)
        logger.error("Could not listen on port %s, server %s.",
                     args.port, args.server)
        sys.exit(getattr(e, 'exitcode', 1))

    logger.info("Serving at port %s, server %s.",
                args.port, args.server)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        logger.info("Shutting down server.")
        httpd.socket.close()