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 |
||
130
by mattgiuca
server: Imports os module correctly (needed by serve_file_directly). |
27 |
import os |
93
by mattgiuca
New directory hierarchy. |
28 |
import mimetypes |
29 |
||
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
30 |
from ivle import (util, studpath, interpret) |
31 |
import ivle.conf |
|
32 |
||
33 |
serveservice_path = os.path.join(ivle.conf.share_path, 'services/serveservice') |
|
34 |
interpretservice_path = os.path.join(ivle.conf.share_path, |
|
35 |
'services/interpretservice') |
|
657
by drtomc
serve: use the trampoline to serve all files. |
36 |
|
720
by dcoles
download: Fixing the download button. |
37 |
# Serve all files as application/octet-stream so the browser presents them as
|
38 |
# a download.
|
|
39 |
default_mimetype = "application/octet-stream" |
|
40 |
zip_mimetype = "application/zip" |
|
41 |
||
93
by mattgiuca
New directory hierarchy. |
42 |
def handle(req): |
43 |
"""Handler for the Server application which serves pages."""
|
|
44 |
req.write_html_head_foot = False |
|
45 |
||
46 |
# Get the username of the student whose work we are browsing, and the path
|
|
47 |
# on the local machine where the file is stored.
|
|
48 |
(user, path) = studpath.url_to_local(req.path) |
|
49 |
||
1053
by wagrant
www.apps.server: Bail out early if the user doesn't exist. This |
50 |
try: |
51 |
interpret.get_uid(user) |
|
52 |
except KeyError: |
|
53 |
# There is no user.
|
|
609
by mattgiuca
browser: Now stats the file requested before doing anything else. |
54 |
req.throw_error(req.HTTP_NOT_FOUND, |
55 |
"The path specified is invalid.") |
|
93
by mattgiuca
New directory hierarchy. |
56 |
|
57 |
serve_file(req, user, path) |
|
58 |
||
262
by mattgiuca
studpath: Added "authorize" function which checks the logged in user against |
59 |
def authorize(req): |
60 |
"""Given a request, checks whether req.username is allowed to
|
|
61 |
access req.path. Returns None on authorization success. Raises
|
|
62 |
HTTP_FORBIDDEN on failure.
|
|
63 |
"""
|
|
64 |
if req.publicmode: |
|
65 |
# Public mode authorization: any user can access any other user's
|
|
266
by mattgiuca
Added Publishing feature. This feature is complete except it currently isn't |
66 |
# files, BUT the accessed file needs to have its "ivle:published" flag
|
262
by mattgiuca
studpath: Added "authorize" function which checks the logged in user against |
67 |
# turned on in the SVN status.
|
266
by mattgiuca
Added Publishing feature. This feature is complete except it currently isn't |
68 |
studpath.authorize_public(req) |
262
by mattgiuca
studpath: Added "authorize" function which checks the logged in user against |
69 |
else: |
70 |
# Private mode authorization: standard (only logged in user can access
|
|
71 |
# their own files, and can access all of them).
|
|
72 |
studpath.authorize(req) |
|
73 |
||
720
by dcoles
download: Fixing the download button. |
74 |
def serve_file(req, owner, filename, download=False): |
93
by mattgiuca
New directory hierarchy. |
75 |
"""Serves a file, using one of three possibilities: interpreting the file,
|
76 |
serving it directly, or denying it and returning a 403 Forbidden error.
|
|
77 |
No return value. Writes to req (possibly throwing a server error exception
|
|
78 |
using req.throw_error).
|
|
79 |
|
|
80 |
req: An IVLE request object.
|
|
81 |
owner: Username of the user who owns the file being served.
|
|
82 |
filename: Filename in the local file system.
|
|
720
by dcoles
download: Fixing the download button. |
83 |
download: Should the file be viewed in browser or downloaded
|
93
by mattgiuca
New directory hierarchy. |
84 |
"""
|
969
by wagrant
server: Use the noop interpreter to mount the user's jail so that authorisation |
85 |
|
86 |
# We need a no-op trampoline run to ensure that the jail is mounted.
|
|
87 |
# Otherwise we won't be able to authorise for public mode!
|
|
88 |
noop_object = interpret.interpreter_objects["noop"] |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
89 |
user_jail_dir = os.path.join(ivle.conf.jail_base, owner) |
969
by wagrant
server: Use the noop interpreter to mount the user's jail so that authorisation |
90 |
interpret.interpret_file(req, owner, user_jail_dir, '', noop_object) |
91 |
||
262
by mattgiuca
studpath: Added "authorize" function which checks the logged in user against |
92 |
# Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
|
93 |
authorize(req) |
|
913
by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already |
94 |
|
95 |
# Jump into the jail
|
|
96 |
interp_object = interpret.interpreter_objects["cgi-python"] |
|
720
by dcoles
download: Fixing the download button. |
97 |
if download: |
964
by mattgiuca
server: Download now serves files with Content-Disposition: attachment (to |
98 |
req.headers_out["Content-Disposition"] = "attachment" |
724
by dcoles
public mode: Backend changes to enable public mode. So long as the folder is |
99 |
interpret.interpret_file(req, owner, user_jail_dir, |
843
by wagrant
Give interpret_file a gentle mode (on by default, to avoid change in |
100 |
serveservice_path, interp_object, gentle=False) |
913
by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already |
101 |
else: |
102 |
interpret.interpret_file(req, owner, user_jail_dir, |
|
103 |
interpretservice_path, interp_object, gentle=True) |
|
93
by mattgiuca
New directory hierarchy. |
104 |
|
105 |
def serve_file_direct(req, filename, type): |
|
106 |
"""Serves a file by directly writing it out to the response.
|
|
107 |
||
108 |
req: An IVLE request object.
|
|
109 |
filename: Filename in the local file system.
|
|
110 |
type: String. Mime type to serve the file with.
|
|
111 |
"""
|
|
112 |
if not os.access(filename, os.R_OK): |
|
610
by mattgiuca
server, download: More fixes to error messages. |
113 |
req.throw_error(req.HTTP_NOT_FOUND, |
114 |
"The specified file does not exist.") |
|
93
by mattgiuca
New directory hierarchy. |
115 |
req.content_type = type |
116 |
req.sendfile(filename) |