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

« back to all changes in this revision

Viewing changes to www/apps/server/__init__.py

  • Committer: dcoles
  • Date: 2008-07-18 07:48:20 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:913
Serve: Broke apart Serve into two parts - a download service (basically already 
implemented inside the jail as serveservice) and a new interpret service.
This prevents us from having to rely on the "hopefully works" inspection 
methods from outside the jail (which were a bit of a mess).

It now might cost us one extra fork for non-python processes, but if that ever  
turns out to be too expensive we can always wrap the dispatch functionality of 
interpret sevice into trampoline. (It's pretty straight forward)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import mimetypes
33
33
 
34
34
serveservice_path = "/opt/ivle/scripts/serveservice"
 
35
interpretservice_path = "/opt/ivle/scripts/interpretservice"
35
36
 
36
37
# Serve all files as application/octet-stream so the browser presents them as
37
38
# a download.
80
81
    """
81
82
    # Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
82
83
    authorize(req)
83
 
 
84
 
    # First get the mime type of this file
85
 
    # If download then use "application/octet-stream" type to force the browser 
86
 
    # to download the file.
87
 
    # (Note that importing common.util has already initialised mime types)
 
84
    
 
85
    # Jump into the jail
 
86
    interp_object = interpret.interpreter_objects["cgi-python"]
 
87
    user_jail_dir = os.path.join(conf.jail_base, owner)
88
88
    if download:
89
 
        if os.path.isdir(filename):
90
 
            type = zip_mimetype
91
 
        else:
92
 
            type = default_mimetype
93
 
        req.headers_out["Content-Disposition"] = "attachment"
94
 
    else:
95
 
        (type, _) = mimetypes.guess_type(filename)
96
 
        if type is None:
97
 
            type = conf.mimetypes.default_mimetype
98
 
 
99
 
    # If this type is to be interpreted
100
 
    if os.path.isdir(filename) and not download:
101
 
        # 403 Forbidden error for visiting a directory
102
 
        # (Not giving a directory listing, since this can be seen by
103
 
        # the world at large. Directory contents are private).
104
 
        req.throw_error(req.HTTP_FORBIDDEN,
105
 
            "The path specified is a directory.")
106
 
    elif type in conf.app.server.interpreters:
107
 
        interp_name = conf.app.server.interpreters[type]
108
 
        try:
109
 
            # Get the interpreter function object
110
 
            interp_object = interpret.interpreter_objects[interp_name]
111
 
            (_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
112
 
        except KeyError:
113
 
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
114
 
                "The interpreter for this file has not been "
115
 
                "configured correctly.")
116
 
        interpret.interpret_file(req, owner, jail_dir, path, interp_object)
117
 
 
118
 
    else:
119
 
        # Otherwise, use the blacklist/whitelist to see if this file should be
120
 
        # served or disallowed
121
 
        if (conf.app.server.blacklist_served_filetypes and \
122
 
                type in conf.app.server.served_filetypes_blacklist) or \
123
 
           (conf.app.server.served_filetypes_whitelist and \
124
 
                type not in conf.app.server.served_filetypes_whitelist): 
125
 
            req.throw_error(req.HTTP_FORBIDDEN,
126
 
                "Files of this type are not allowed to be served.")
127
 
 
128
 
        interp_object = interpret.interpreter_objects["cgi-python"]
129
 
        user_jail_dir = os.path.join(conf.jail_base, owner)
130
89
        interpret.interpret_file(req, owner, user_jail_dir,
131
90
            serveservice_path, interp_object, gentle=False)
 
91
    else:
 
92
        interpret.interpret_file(req, owner, user_jail_dir,
 
93
            interpretservice_path, interp_object, gentle=True)
132
94
 
133
95
def serve_file_direct(req, filename, type):
134
96
    """Serves a file by directly writing it out to the response.