2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
19
# Author: Tom Conway, Matt Giuca
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.
27
from common import (util, studpath, interpret)
29
import conf.app.server
34
"""Handler for the Server application which serves pages."""
35
req.write_html_head_foot = False
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)
42
# TODO: Nicer 404 message?
43
req.throw_error(req.HTTP_NOT_FOUND)
45
serve_file(req, user, path)
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).
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.
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)
62
type = conf.app.server.default_mimetype
64
# If this type is to be interpreted
65
if type in conf.app.server.interpreters:
66
interp_name = conf.app.server.interpreters[type]
68
# Get the interpreter function object
69
interp_object = interpret.interpreter_objects[interp_name]
71
# TODO: Nicer 500 message (this is due to bad configuration in
73
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
74
interpret.interpret_file(req, owner, filename, interp_object)
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
82
toserve = type in conf.app.server.served_filetypes_whitelist
84
# toserve or not toserve
86
# TODO: Nicer 403 message
87
req.throw_error(req.HTTP_FORBIDDEN)
89
serve_file_direct(req, filename, type)
91
def serve_file_direct(req, filename, type):
92
"""Serves a file by directly writing it out to the response.
94
req: An IVLE request object.
95
filename: Filename in the local file system.
96
type: String. Mime type to serve the file with.
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)