90
91
path_segs = req.path.split(os.sep)
93
if len(path_segs) > 2:
94
req.throw_error(req.HTTP_NOT_FOUND)
95
elif len(req.path) > 0:
96
95
subject = path_segs[0]
96
if subject == "media":
97
# Special case: "tutorial/media" will plainly serve any path
98
# relative to "subjects/media".
99
handle_media_path(req)
101
if len(path_segs) > 2:
102
req.throw_error(req.HTTP_NOT_FOUND)
97
103
if len(path_segs) == 2:
98
104
worksheet = path_segs[1]
105
111
handle_worksheet(req, subject, worksheet)
106
112
plugins.console.present(req, windowpane=True)
114
def handle_media_path(req):
116
Urls in "tutorial/media" will just be served directly, relative to
117
subjects. So if we came here, we just want to serve a file relative to the
118
subjects directory on the local file system.
120
# First normalise the path
121
urlpath = os.path.normpath(req.path)
122
# Now if it begins with ".." or separator, then it's illegal
123
if urlpath.startswith("..") or urlpath.startswith(os.sep):
124
req.throw_error(req.HTTP_FORBIDDEN)
125
filename = os.path.join(conf.subjects_base, urlpath)
126
(type, _) = mimetypes.guess_type(filename)
128
type = conf.mimetypes.default_mimetype
129
## THIS CODE taken from apps/server/__init__.py
130
if not os.access(filename, os.R_OK):
131
req.throw_error(req.HTTP_NOT_FOUND)
132
if os.path.isdir(filename):
133
req.throw_error(req.HTTP_FORBIDDEN,
134
"The requested file is a directory.")
135
req.content_type = type
136
req.sendfile(filename)
108
138
def handle_toplevel_menu(req):
109
139
# This is represented as a directory. Redirect and add a slash if it is