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

913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
1
#!/usr/bin/python
2
3
# IVLE - Informatics Virtual Learning Environment
4
# Copyright (C) 2007-2008 The University of Melbourne
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20
# Script: interpretservice
21
# Author: David Coles
22
# Date:   6/3/2007
23
24
# A CGI script for interpreting files.
25
26
import mimetypes
27
import os
28
import sys
29
import StringIO
30
import urlparse
31
import subprocess
32
33
from common import (cgirequest, studpath)
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
34
import conf
35
import conf.mimetypes, conf.app, conf.app.server
36
37
serveservice_path = "/opt/ivle/scripts/serveservice"
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
38
39
req = cgirequest.CGIRequest()
40
req.install_error_handler()
41
42
# Work out the parts of the URL
43
url = urlparse.urlparse(req.path)
44
querystr = url[4]
45
urlpath = url[2]
962 by dcoles
Serve: Fix the working directory used by interpretservice so that paths are
46
username, _, filename = studpath.url_to_jailpaths(urlpath)
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
47
path = os.path.split(filename)[0]
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
48
49
python = "/usr/bin/python"
50
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
51
# Everything should be done from the same directory as the script
993 by wagrant
interpretservice: Don't die horribly if we can't chdir into the path's parent.
52
# If we can't chdir, a 404 will be thrown later when we can't access the file.
53
try:
54
    os.chdir(path)
55
except OSError:
56
    pass
57
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
58
# Search the path for modules first
59
sys.path[0] = path
962 by dcoles
Serve: Fix the working directory used by interpretservice so that paths are
60
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
61
(type, _) = mimetypes.guess_type(filename)
62
if type is None:
63
    type = conf.mimetypes.default_mimetype
64
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
65
if filename is None:
66
    req.throw_error(req.HTTP_NOT_FOUND, "The path specified is invalid.")
67
elif not os.access(filename, os.R_OK):
68
    req.throw_error(req.HTTP_NOT_FOUND,
69
        "The specified file (%s) does not exist." % urlpath)
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
70
elif os.path.isdir(filename):
71
    # 403 Forbidden error for visiting a directory
72
    # (Not giving a directory listing, since this can be seen by
73
    # the world at large. Directory contents are private).
74
    req.throw_error(req.HTTP_FORBIDDEN,
75
        "The path specified is a directory.")
76
elif type in conf.app.server.interpreters:
77
    # We'll save on a fork and execute in this python process
78
    # Let exceptions blow up normally again
79
    sys.excepthook = sys.__excepthook__
80
    execfile(filename, {})
81
    # Non-Python process should probably use something like
82
    # subprocess.call([python, filename])
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
83
else:
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
84
    # Otherwise, use the blacklist/whitelist to see if this file should be
85
    # served or disallowed
86
    if (conf.app.server.blacklist_served_filetypes and \
87
            type in conf.app.server.served_filetypes_blacklist) or \
88
        (conf.app.server.served_filetypes_whitelist and \
89
            type not in conf.app.server.served_filetypes_whitelist):
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
90
        req.throw_error(req.HTTP_FORBIDDEN,
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
91
            "Files of this type are not allowed to be served.")
92
93
    execfile(serveservice_path)