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

« back to all changes in this revision

Viewing changes to services/serveservice

  • Committer: William Grant
  • Date: 2009-01-22 04:47:42 UTC
  • mfrom: (1080.1.93 storm)
  • Revision ID: grantw@unimelb.edu.au-20090122044742-sa8gnww0ma2bm2rv
Merge Storm branch. ivle.db is dead. Watch out for the schema change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
19
19
 
20
 
# Author: Thomas Conway, Will Grant
 
20
# Script: serveservice
 
21
# Author: Thomas Conway
 
22
# Date:   6/3/2007
 
23
 
 
24
# A CGI script for serving files.
21
25
 
22
26
import mimetypes
23
27
import os
24
 
import sys
25
28
import StringIO
26
 
from optparse import OptionParser
27
 
 
28
 
try:
29
 
    import json
30
 
except ImportError:
31
 
    import simplejson as json
32
 
 
 
29
import urlparse
 
30
 
 
31
from ivle import (cgirequest, studpath)
33
32
from ivle import zip as zipmod
34
 
import ivle.conf.app.server
35
 
import ivle.mimetypes
36
 
 
37
 
def determine_file_type(filename):
38
 
    filetype = mimetypes.guess_type(filename)[0]
39
 
    if filetype is None:
40
 
        filetype = ivle.mimetypes.DEFAULT_MIMETYPE
41
 
    return filetype
42
 
 
43
 
def throw_error(message, extra={}):
44
 
    error = {'error': message}
45
 
    error.update(extra)
46
 
    print json.dumps(error)
47
 
    sys.exit(0)
48
 
 
49
 
parser = OptionParser()
50
 
parser.add_option('-d', '--download', dest='download', action='store_true',
51
 
                  help='force download, not execution, of the paths')
52
 
(options, args) = parser.parse_args()
53
 
 
54
 
# Detect download mode. Download mode zips any multiple selection,
55
 
# and does not execute CGI scripts.
56
 
if options.download:
57
 
    download = True
58
 
    # paths is filled later.
59
 
else:
60
 
    download = False
61
 
    assert len(args) == 1
 
33
 
 
34
req = cgirequest.CGIRequest()
 
35
req.install_error_handler()
 
36
 
 
37
# Work out the parts of the URL
 
38
url = urlparse.urlparse(req.path)
 
39
querystr = url[4]
 
40
urlpath = url[2]
 
41
filename = studpath.url_to_jailpaths(urlpath)[2]
62
42
 
63
43
default_mimetype = "application/octet-stream"
64
44
zip_mimetype = "application/zip"
66
46
zipmode = False
67
47
zipbasepath = None
68
48
zipfilename = None
 
49
path = None
69
50
 
70
 
# If multiple paths have been specified, zip them up.
71
 
if len(args) > 1:
72
 
    # Mangle the paths - we want the basename of their dirname in front.
73
 
    paths = []
74
 
    dir = os.path.dirname(args[0])
75
 
    for path in args:
76
 
        assert os.path.dirname(path) == dir
77
 
        paths.append(os.path.join(os.path.basename(dir),
78
 
                                  os.path.basename(path)))
79
 
    dir = os.path.dirname(dir)
 
51
# If any "path=" variables have been supplied, bring these into a list and
 
52
# make a zip file instead.
 
53
fields = req.get_fieldstorage()
 
54
paths = fields.getlist("path")
 
55
if len(paths) > 0:
80
56
    zipmode = True
81
 
    zipbasepath = dir
 
57
    zipbasepath = filename
82
58
    zipfilename = os.path.basename(zipbasepath)
 
59
    #for i in range(0, len(paths)):
 
60
        #paths[i] = paths[i].value
83
61
else:
84
 
    paths = args
85
 
    filename = paths[0]
86
 
    if not os.access(filename, os.F_OK):
87
 
        # The given path doesn't exist. CGI lets us backtrack and put the path
88
 
        # elements through which we pass into PATH_INFO, so we try that.
89
 
        while not os.access(filename, os.F_OK):
90
 
            filename, path_info_frag = os.path.split(filename)
91
 
 
92
 
        # We now have a file that exists, but is it something that we're allowed
93
 
        # to execute? If not, we should 404 anyway.
94
 
        if determine_file_type(filename) not in ivle.conf.app.server.interpreters:
95
 
            throw_error('not-found')
96
 
 
97
 
    # If it's a directory, serve as a zip file
 
62
    if filename is None:
 
63
        req.throw_error(req.HTTP_NOT_FOUND,
 
64
            "The path specified is invalid.")
 
65
    elif not os.access(filename, os.R_OK):
 
66
        req.throw_error(req.HTTP_NOT_FOUND,
 
67
            "The specified file (%s) does not exist." % urlpath)
 
68
     # If it's a directory, serve as a zip file
98
69
    if os.path.isdir(filename):
99
 
        if not download:
100
 
            # Not giving a directory listing - this is visible to everyone.
101
 
            throw_error('is-directory')
102
70
        zipmode = True
103
71
        # Zip it from the perspective of its own parent.
104
72
        # That way it will be a directory in the top level of the zip
111
79
        else:
112
80
            zipbasepath = splitpath[0]
113
81
            paths = [splitpath[1]]
114
 
        zipfilename = filename
115
 
    else:
116
 
        if not download and \
117
 
           determine_file_type(filename) in ivle.conf.app.server.interpreters:
118
 
            throw_error('is-executable', {'path': filename.decode('utf-8')})
119
 
 
120
 
        if not download and (
121
 
            (ivle.conf.app.server.blacklist_served_filetypes and \
122
 
                determine_file_type(filename) in \
123
 
                ivle.conf.app.server.served_filetypes_blacklist) or \
124
 
            (ivle.conf.app.server.served_filetypes_whitelist and \
125
 
                determine_file_type(filename) not in \
126
 
                ivle.conf.app.server.served_filetypes_whitelist)):
127
 
            throw_error('forbidden')
 
82
        zipfilename = paths[0]
128
83
 
129
84
if zipmode:
 
85
    req.content_type = zip_mimetype
130
86
    # zipfilename is some filename. Strip trailing slash or extension,
131
87
    # and add ".zip".
132
88
    if zipfilename == '':
136
92
    elif '.' in zipfilename:
137
93
        zipfilename = zipfilename[:zipfilename.rindex('.')]
138
94
    zipfilename += ".zip"
139
 
    #req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
140
 
    #    zipfilename) # TODO
 
95
    req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
 
96
        zipfilename)
141
97
    zipfile = StringIO.StringIO()
142
98
    zipmod.make_zip(zipbasepath, paths, zipfile)
143
 
 
144
 
    print json.dumps({'type': zip_mimetype,
145
 
                      'name': zipfilename.decode('utf-8'),
146
 
                      'size': len(zipfile.getvalue()),
147
 
                      })
148
 
 
149
 
    stream = zipfile
150
 
    stream.seek(0)
 
99
        
 
100
    req.write(zipfile.getvalue())
151
101
else:
152
 
 
153
 
    print json.dumps({'type': determine_file_type(filename),
154
 
                      'name': os.path.basename(filename).decode('utf-8'),
155
 
                      'size': os.path.getsize(filename),
156
 
                      })
157
 
    stream = open(filename)
158
 
 
159
 
next = stream.read(1024)
160
 
while next:
161
 
    sys.stdout.write(next)
162
 
    next = stream.read(1024)
 
102
    #req.content_type = default_mimetype
 
103
    req.sendfile(filename)