17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
# Script: serveservice
21
# Author: Thomas Conway
24
# A CGI script for serving files.
20
# Author: Thomas Conway, Will Grant
32
from common import (cgirequest, studpath)
33
from common import zip as zipmod
35
req = cgirequest.CGIRequest()
36
req.install_error_handler()
38
# Work out the parts of the URL
39
url = urlparse.urlparse(req.path)
42
filename = studpath.url_to_jailpaths(urlpath)[2]
26
from optparse import OptionParser
30
from ivle import zip as zipmod
31
import ivle.conf.app.server
34
def determine_file_type(filename):
35
filetype = mimetypes.guess_type(filename)[0]
37
filetype = ivle.mimetypes.DEFAULT_MIMETYPE
40
def throw_error(message, extra={}):
41
error = {'error': message}
43
print cjson.encode(error)
46
parser = OptionParser()
47
parser.add_option('-d', '--download', dest='download', action='store_true',
48
help='force download, not execution, of the paths')
49
(options, args) = parser.parse_args()
51
# Detect download mode. Download mode zips any multiple selection,
52
# and does not execute CGI scripts.
55
# paths is filled later.
44
60
default_mimetype = "application/octet-stream"
45
61
zip_mimetype = "application/zip"
52
# If any "path=" variables have been supplied, bring these into a list and
53
# make a zip file instead.
54
fields = req.get_fieldstorage()
55
paths = fields.getlist("path")
67
# If multiple paths have been specified, zip them up.
69
# Mangle the paths - we want the basename of their dirname in front.
71
dir = os.path.dirname(args[0])
73
assert os.path.dirname(path) == dir
74
paths.append(os.path.join(os.path.basename(dir),
75
os.path.basename(path)))
76
dir = os.path.dirname(dir)
58
zipbasepath = filename
59
79
zipfilename = os.path.basename(zipbasepath)
60
#for i in range(0, len(paths)):
61
#paths[i] = paths[i].value
64
req.throw_error(req.HTTP_NOT_FOUND,
65
"The path specified is invalid.")
66
elif not os.access(filename, os.R_OK):
67
req.throw_error(req.HTTP_NOT_FOUND,
68
"The specified file (%s) does not exist." % urlpath)
69
# If it's a directory, serve as a zip file
83
if not os.access(filename, os.F_OK):
84
# The given path doesn't exist. CGI lets us backtrack and put the path
85
# elements through which we pass into PATH_INFO, so we try that.
86
while not os.access(filename, os.F_OK):
87
filename, path_info_frag = os.path.split(filename)
89
# We now have a file that exists, but is it something that we're allowed
90
# to execute? If not, we should 404 anyway.
91
if determine_file_type(filename) not in ivle.conf.app.server.interpreters:
92
throw_error('not-found')
94
# If it's a directory, serve as a zip file
70
95
if os.path.isdir(filename):
97
# Not giving a directory listing - this is visible to everyone.
98
throw_error('is-directory')
72
100
# Zip it from the perspective of its own parent.
73
101
# That way it will be a directory in the top level of the zip
81
109
zipbasepath = splitpath[0]
82
110
paths = [splitpath[1]]
83
zipfilename = paths[0]
111
zipfilename = filename
113
if not download and \
114
determine_file_type(filename) in ivle.conf.app.server.interpreters:
115
throw_error('is-executable', {'path': filename})
117
if not download and (
118
(ivle.conf.app.server.blacklist_served_filetypes and \
119
determine_file_type(filename) in \
120
ivle.conf.app.server.served_filetypes_blacklist) or \
121
(ivle.conf.app.server.served_filetypes_whitelist and \
122
determine_file_type(filename) not in \
123
ivle.conf.app.server.served_filetypes_whitelist)):
124
throw_error('forbidden')
86
req.content_type = zip_mimetype
87
127
# zipfilename is some filename. Strip trailing slash or extension,
89
129
if zipfilename == '':
93
133
elif '.' in zipfilename:
94
134
zipfilename = zipfilename[:zipfilename.rindex('.')]
95
135
zipfilename += ".zip"
96
req.headers_out["Content-Disposition"] = ("attachment; filename=" +
136
#req.headers_out["Content-Disposition"] = ("attachment; filename=" +
137
# zipfilename) # TODO
98
138
zipfile = StringIO.StringIO()
99
139
zipmod.make_zip(zipbasepath, paths, zipfile)
101
req.write(zipfile.getvalue())
141
print cjson.encode({'type': zip_mimetype,
142
'name': zipfilename})
103
#req.content_type = default_mimetype
104
req.sendfile(filename)
148
print cjson.encode({'type': determine_file_type(filename),
149
'name': os.path.basename(filename)})
150
stream = open(filename)
152
next = stream.read(1024)
154
sys.stdout.write(next)
155
next = stream.read(1024)