~azzar1/unity/add-show-desktop-key

93 by mattgiuca
New directory hierarchy.
1
# IVLE
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# App: server
19
# Author: Tom Conway, Matt Giuca
20
# Date: 13/12/2007
21
22
# Serves content to the user (acting as a web server for students files).
23
# For most file types we just serve the static file, but
24
# for python files, we evaluate the python script inside
25
# our safe execution environment.
26
27
from common import (util, studpath, interpret)
28
import conf
29
import conf.app.server
30
31
import mimetypes
32
33
def handle(req):
34
    """Handler for the Server application which serves pages."""
35
    req.write_html_head_foot = False
36
37
    # Get the username of the student whose work we are browsing, and the path
38
    # on the local machine where the file is stored.
39
    (user, path) = studpath.url_to_local(req.path)
40
41
    if user is None:
42
        # TODO: Nicer 404 message?
43
        req.throw_error(req.HTTP_NOT_FOUND)
44
45
    serve_file(req, user, path)
46
47
def serve_file(req, owner, filename):
48
    """Serves a file, using one of three possibilities: interpreting the file,
49
    serving it directly, or denying it and returning a 403 Forbidden error.
50
    No return value. Writes to req (possibly throwing a server error exception
51
    using req.throw_error).
52
    
53
    req: An IVLE request object.
54
    owner: Username of the user who owns the file being served.
55
    filename: Filename in the local file system.
56
    """
57
58
    # First get the mime type of this file
59
    # (Note that importing common.util has already initialised mime types)
60
    (type, _) = mimetypes.guess_type(filename)
61
    if type is None:
62
        type = conf.app.server.default_mimetype
63
64
    # If this type is to be interpreted
65
    if type in conf.app.server.interpreters:
66
        interp_name = conf.app.server.interpreters[type]
67
        try:
68
            # Get the interpreter function object
69
            interp_object = interpret.interpreter_objects[interp_name]
70
        except KeyError:
71
            # TODO: Nicer 500 message (this is due to bad configuration in
72
            # conf/app/server.py)
73
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
74
        interpret.interpret_file(req, owner, filename, interp_object)
75
76
    else:
77
        # Otherwise, use the blacklist/whitelist to see if this file should be
78
        # served or disallowed
79
        if conf.app.server.blacklist_served_filetypes:
80
            toserve = type not in conf.app.server.served_filetypes_blacklist
81
        else:
82
            toserve = type in conf.app.server.served_filetypes_whitelist
83
84
        # toserve or not toserve
85
        if not toserve:
86
            # TODO: Nicer 403 message
87
            req.throw_error(req.HTTP_FORBIDDEN)
88
        else:
89
            serve_file_direct(req, filename, type)
90
91
def serve_file_direct(req, filename, type):
92
    """Serves a file by directly writing it out to the response.
93
94
    req: An IVLE request object.
95
    filename: Filename in the local file system.
96
    type: String. Mime type to serve the file with.
97
    """
98
    if not os.access(filename, os.R_OK):
99
        req.throw_error(req.HTTP_NOT_FOUND)
100
    req.content_type = type
101
    req.sendfile(filename)