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

« back to all changes in this revision

Viewing changes to scripts/serveservice

  • Committer: wagrant
  • Date: 2008-12-20 03:46:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1060
Make IVLE work fine in Firefox 3.1 (ie. Gecko/XULRunner 1.9.1). Gecko 1.9.1 has
a built-in JSON object, so our json2.js isn't used, and compatibility problems
arise. The only one significant to us is that Gecko's doesn't support having
a primitive as the root object. Unfortunately we use that sort of thing in a
few places, so IVLE ends up pretty much broken.

A few adjustments were needed both client- and server-side for proto changes:

 - consoleservice and the JS interacting with it now deal with the key returned
   by start_server in a dict like {'key': ...}.
 - tutorialservice and interacting JS deal with code returned by getattempt in
   a dict like {'code': ...}.


The JS repr() implementation used JSON.stringify on the given object, and was
used only with primitive objects in order to create function calls. This needed
to be eliminated for compatibility with the new Gecko, so these call
constructions were replaced with better events:

 - listing.js constructed events for generated file listings by repr()ing some
   strings. That is foul, so we instead use DOM operations from 'this' in a
   somewhat nicer event handler.
 - tutorial.js previously created save_exercise() call strings by repr()ing
   lots of stuff. That was really bad, and broke like the rest. I replaced it
   with something that's still bad, but not quite as awful - grabbing the call
   string from the save button. This should be fixed at some point.
 - console.js had some strange Firebug logging stuff which used repr(). I
   removed it, as it shouldn't have been in released code in the first place.

repr() itself was removed after all references were.
   

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: serveservice
 
21
# Author: Thomas Conway
 
22
# Date:   6/3/2007
 
23
 
 
24
# A CGI script for serving files.
 
25
 
 
26
import mimetypes
 
27
import os
 
28
import conf
 
29
import StringIO
 
30
import urlparse
 
31
 
 
32
from common import (cgirequest, studpath)
 
33
from common import zip as zipmod
 
34
 
 
35
req = cgirequest.CGIRequest()
 
36
req.install_error_handler()
 
37
 
 
38
# Work out the parts of the URL
 
39
url = urlparse.urlparse(req.path)
 
40
querystr = url[4]
 
41
urlpath = url[2]
 
42
filename = studpath.url_to_jailpaths(urlpath)[2]
 
43
 
 
44
default_mimetype = "application/octet-stream"
 
45
zip_mimetype = "application/zip"
 
46
 
 
47
zipmode = False
 
48
zipbasepath = None
 
49
zipfilename = None
 
50
path = None
 
51
 
 
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")
 
56
if len(paths) > 0:
 
57
    zipmode = True
 
58
    zipbasepath = filename
 
59
    zipfilename = os.path.basename(zipbasepath)
 
60
    #for i in range(0, len(paths)):
 
61
        #paths[i] = paths[i].value
 
62
else:
 
63
    if filename is None:
 
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
 
70
    if os.path.isdir(filename):
 
71
        zipmode = True
 
72
        # Zip it from the perspective of its own parent.
 
73
        # That way it will be a directory in the top level of the zip
 
74
        # file.
 
75
        if filename[-1] == os.sep: filename = filename[:-1]
 
76
        splitpath = filename.rsplit(os.sep, 1)
 
77
        if len(splitpath) == 1:
 
78
            zipbasepath = ''
 
79
            paths = [filename]
 
80
        else:
 
81
            zipbasepath = splitpath[0]
 
82
            paths = [splitpath[1]]
 
83
        zipfilename = paths[0]
 
84
 
 
85
if zipmode:
 
86
    req.content_type = zip_mimetype
 
87
    # zipfilename is some filename. Strip trailing slash or extension,
 
88
    # and add ".zip".
 
89
    if zipfilename == '':
 
90
        zipfilename = "files"
 
91
    elif zipfilename[-1] == '/':
 
92
        zipfilename = zipfilename[:-1]
 
93
    elif '.' in zipfilename:
 
94
        zipfilename = zipfilename[:zipfilename.rindex('.')]
 
95
    zipfilename += ".zip"
 
96
    req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
 
97
        zipfilename)
 
98
    zipfile = StringIO.StringIO()
 
99
    zipmod.make_zip(zipbasepath, paths, zipfile)
 
100
        
 
101
    req.write(zipfile.getvalue())
 
102
else:
 
103
    #req.content_type = default_mimetype
 
104
    req.sendfile(filename)