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

« back to all changes in this revision

Viewing changes to services/serveservice

Merge from new-dispatch.

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