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

« back to all changes in this revision

Viewing changes to services/interpretservice

Merge my migration of serve and download to the new framework.

Poor fileservice is all alone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 ivle import (cgirequest, studpath)
34
 
import ivle.conf
35
 
import ivle.conf.mimetypes
36
 
import ivle.conf.app
37
 
import ivle.conf.app.server
38
 
 
39
 
# XXX: using dirname because we don't have path config in the jailconf.
40
 
serveservice_path = os.path.join(os.path.dirname(__file__), 'serveservice')
41
 
 
42
 
def determine_file_type(filename):
43
 
    filetype = mimetypes.guess_type(filename)[0]
44
 
    if filetype is None:
45
 
         filetype = ivle.conf.mimetypes.default_mimetype
46
 
    return filetype
47
 
 
48
 
req = cgirequest.CGIRequest()
49
 
req.install_error_handler()
50
 
 
51
 
python = "/usr/bin/python"
52
 
 
53
 
# Work out the parts of the URL
54
 
urlpath = urlparse.urlparse(req.path)[2]
55
 
filename = studpath.url_to_jailpaths(urlpath)[2]
56
 
path_info = ''
57
 
 
58
 
if filename is None:
59
 
    req.throw_error(req.HTTP_NOT_FOUND, "The path specified is invalid.")
60
 
 
61
 
if not os.access(filename, os.F_OK):
62
 
    # The given path doesn't exist. CGI lets us backtrack and put the path
63
 
    # elements through which we pass into PATH_INFO, so we try that.
64
 
    while not os.access(filename, os.F_OK):
65
 
        filename, path_info_frag = os.path.split(filename)
66
 
 
67
 
        # os.path.join tacks a / on the end even if the second fragment is
68
 
        # empty, which we don't want.
69
 
        if len(path_info) > 0:
70
 
            path_info = os.path.join(path_info_frag, path_info)
71
 
        else:
72
 
            path_info = path_info_frag
73
 
 
74
 
    # PATH_INFO is meant to start with a /.
75
 
    path_info = os.path.join(os.sep, path_info)
76
 
 
77
 
    # We now have a file that exists, but is it something that we're allowed
78
 
    # to execute? If not, we should 404 anyway.
79
 
    if determine_file_type(filename) not in ivle.conf.app.server.interpreters:
80
 
        req.throw_error(req.HTTP_NOT_FOUND,
81
 
            "The specified file (%s) does not exist." % urlpath)
82
 
 
83
 
path = os.path.split(filename)[0]
84
 
 
85
 
# Everything should be done from the same directory as the script
86
 
try:
87
 
    os.chdir(path)
88
 
except OSError:
89
 
    req.throw_error(req.HTTP_NOT_FOUND,
90
 
        "The specified file (%s) does not exist." % urlpath)
91
 
 
92
 
# Search the path for modules first
93
 
sys.path[0] = path
94
 
 
95
 
# Now we have lots of fun mangling environment variables to fix PATH_INFO
96
 
# and related stuff.
97
 
os.environ['PATH_INFO'] = path_info
98
 
if len(path_info) > 0:
99
 
    os.environ['SCRIPT_NAME'] = os.environ['SCRIPT_NAME'][:-len(path_info)]
100
 
 
101
 
# Set PATH_TRANSLATED. We trim the leading / from path_info or things go bad.
102
 
path_translated = studpath.url_to_jailpaths(path_info[1:])[2]
103
 
if path_translated is not None:
104
 
    if len(path_translated) == 0 or path_translated[0] != os.sep:
105
 
        path_translated = os.sep + path_translated
106
 
    os.environ['PATH_TRANSLATED'] = path_translated
107
 
 
108
 
if not os.access(filename, os.R_OK):
109
 
    req.throw_error(req.HTTP_NOT_FOUND,
110
 
        "The specified file (%s) does not exist." % urlpath)
111
 
elif os.path.isdir(filename):
112
 
    # 403 Forbidden error for visiting a directory
113
 
    # (Not giving a directory listing, since this can be seen by
114
 
    # the world at large. Directory contents are private).
115
 
    req.throw_error(req.HTTP_FORBIDDEN,
116
 
        "The path specified is a directory.")
117
 
elif determine_file_type(filename) in ivle.conf.app.server.interpreters:
118
 
    # We'll save on a fork and execute in this python process
119
 
    # Let exceptions blow up normally again
120
 
    sys.excepthook = sys.__excepthook__
121
 
    execfile(filename, {})
122
 
    # Non-Python process should probably use something like
123
 
    # subprocess.call([python, filename])
124
 
else:
125
 
    # Otherwise, use the blacklist/whitelist to see if this file should be
126
 
    # served or disallowed
127
 
    if (ivle.conf.app.server.blacklist_served_filetypes and \
128
 
            determine_file_type(filename) in \
129
 
            ivle.conf.app.server.served_filetypes_blacklist) or \
130
 
        (ivle.conf.app.server.served_filetypes_whitelist and \
131
 
            determine_file_type(filename) not in \
132
 
            ivle.conf.app.server.served_filetypes_whitelist):
133
 
        req.throw_error(req.HTTP_FORBIDDEN,
134
 
            "Files of this type are not allowed to be served.")
135
 
 
136
 
    execfile(serveservice_path)