~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
130 by mattgiuca
server: Imports os module correctly (needed by serve_file_directly).
31
import os
93 by mattgiuca
New directory hierarchy.
32
import mimetypes
33
657 by drtomc
serve: use the trampoline to serve all files.
34
serveservice_path = "/opt/ivle/scripts/serveservice"
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
35
interpretservice_path = "/opt/ivle/scripts/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
50
    if user is None:
609 by mattgiuca
browser: Now stats the file requested before doing anything else.
51
        req.throw_error(req.HTTP_NOT_FOUND,
52
            "The path specified is invalid.")
93 by mattgiuca
New directory hierarchy.
53
54
    serve_file(req, user, path)
55
262 by mattgiuca
studpath: Added "authorize" function which checks the logged in user against
56
def authorize(req):
57
    """Given a request, checks whether req.username is allowed to
58
    access req.path. Returns None on authorization success. Raises
59
    HTTP_FORBIDDEN on failure.
60
    """
61
    if req.publicmode:
62
        # 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
63
        # 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
64
        # turned on in the SVN status.
266 by mattgiuca
Added Publishing feature. This feature is complete except it currently isn't
65
        studpath.authorize_public(req)
262 by mattgiuca
studpath: Added "authorize" function which checks the logged in user against
66
    else:
67
        # Private mode authorization: standard (only logged in user can access
68
        # their own files, and can access all of them).
69
        studpath.authorize(req)
70
720 by dcoles
download: Fixing the download button.
71
def serve_file(req, owner, filename, download=False):
93 by mattgiuca
New directory hierarchy.
72
    """Serves a file, using one of three possibilities: interpreting the file,
73
    serving it directly, or denying it and returning a 403 Forbidden error.
74
    No return value. Writes to req (possibly throwing a server error exception
75
    using req.throw_error).
76
    
77
    req: An IVLE request object.
78
    owner: Username of the user who owns the file being served.
79
    filename: Filename in the local file system.
720 by dcoles
download: Fixing the download button.
80
    download:  Should the file be viewed in browser or downloaded
93 by mattgiuca
New directory hierarchy.
81
    """
262 by mattgiuca
studpath: Added "authorize" function which checks the logged in user against
82
    # Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
83
    authorize(req)
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
84
    
85
    # Jump into the jail
86
    interp_object = interpret.interpreter_objects["cgi-python"]
87
    user_jail_dir = os.path.join(conf.jail_base, owner)
720 by dcoles
download: Fixing the download button.
88
    if download:
724 by dcoles
public mode: Backend changes to enable public mode. So long as the folder is
89
        interpret.interpret_file(req, owner, user_jail_dir,
843 by wagrant
Give interpret_file a gentle mode (on by default, to avoid change in
90
            serveservice_path, interp_object, gentle=False)
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
91
    else:
92
        interpret.interpret_file(req, owner, user_jail_dir,
93
            interpretservice_path, interp_object, gentle=True)
93 by mattgiuca
New directory hierarchy.
94
95
def serve_file_direct(req, filename, type):
96
    """Serves a file by directly writing it out to the response.
97
98
    req: An IVLE request object.
99
    filename: Filename in the local file system.
100
    type: String. Mime type to serve the file with.
101
    """
102
    if not os.access(filename, os.R_OK):
610 by mattgiuca
server, download: More fixes to error messages.
103
        req.throw_error(req.HTTP_NOT_FOUND,
104
            "The specified file does not exist.")
93 by mattgiuca
New directory hierarchy.
105
    req.content_type = type
106
    req.sendfile(filename)