81
82
# Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
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)
86
interp_object = interpret.interpreter_objects["cgi-python"]
87
user_jail_dir = os.path.join(conf.jail_base, owner)
89
if os.path.isdir(filename):
92
type = default_mimetype
93
req.headers_out["Content-Disposition"] = "attachment"
95
(type, _) = mimetypes.guess_type(filename)
97
type = conf.mimetypes.default_mimetype
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]
109
# Get the interpreter function object
110
interp_object = interpret.interpreter_objects[interp_name]
111
(_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
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)
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.")
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)
92
interpret.interpret_file(req, owner, user_jail_dir,
93
interpretservice_path, interp_object, gentle=True)
133
95
def serve_file_direct(req, filename, type):
134
96
"""Serves a file by directly writing it out to the response.