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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: William Grant
  • Date: 2010-02-23 08:08:27 UTC
  • Revision ID: grantw@unimelb.edu.au-20100223080827-wklsx122pcw79wi7
Reject off-site non-GET requests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
 
89
89
    # HTTP status codes
90
90
 
91
 
    HTTP_CONTINUE                     = 100
92
 
    HTTP_SWITCHING_PROTOCOLS          = 101
93
 
    HTTP_PROCESSING                   = 102
94
91
    HTTP_OK                           = 200
95
 
    HTTP_CREATED                      = 201
96
 
    HTTP_ACCEPTED                     = 202
97
 
    HTTP_NON_AUTHORITATIVE            = 203
98
 
    HTTP_NO_CONTENT                   = 204
99
 
    HTTP_RESET_CONTENT                = 205
100
 
    HTTP_PARTIAL_CONTENT              = 206
101
 
    HTTP_MULTI_STATUS                 = 207
102
 
    HTTP_MULTIPLE_CHOICES             = 300
103
 
    HTTP_MOVED_PERMANENTLY            = 301
104
92
    HTTP_MOVED_TEMPORARILY            = 302
105
 
    HTTP_SEE_OTHER                    = 303
106
 
    HTTP_NOT_MODIFIED                 = 304
107
 
    HTTP_USE_PROXY                    = 305
108
 
    HTTP_TEMPORARY_REDIRECT           = 307
109
 
    HTTP_BAD_REQUEST                  = 400
110
 
    HTTP_UNAUTHORIZED                 = 401
111
 
    HTTP_PAYMENT_REQUIRED             = 402
112
93
    HTTP_FORBIDDEN                    = 403
113
94
    HTTP_NOT_FOUND                    = 404
114
 
    HTTP_METHOD_NOT_ALLOWED           = 405
115
 
    HTTP_NOT_ACCEPTABLE               = 406
116
 
    HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
117
 
    HTTP_REQUEST_TIME_OUT             = 408
118
 
    HTTP_CONFLICT                     = 409
119
 
    HTTP_GONE                         = 410
120
 
    HTTP_LENGTH_REQUIRED              = 411
121
 
    HTTP_PRECONDITION_FAILED          = 412
122
 
    HTTP_REQUEST_ENTITY_TOO_LARGE     = 413
123
 
    HTTP_REQUEST_URI_TOO_LARGE        = 414
124
 
    HTTP_UNSUPPORTED_MEDIA_TYPE       = 415
125
 
    HTTP_RANGE_NOT_SATISFIABLE        = 416
126
 
    HTTP_EXPECTATION_FAILED           = 417
127
 
    HTTP_UNPROCESSABLE_ENTITY         = 422
128
 
    HTTP_LOCKED                       = 423
129
 
    HTTP_FAILED_DEPENDENCY            = 424
130
95
    HTTP_INTERNAL_SERVER_ERROR        = 500
131
 
    HTTP_NOT_IMPLEMENTED              = 501
132
 
    HTTP_BAD_GATEWAY                  = 502
133
 
    HTTP_SERVICE_UNAVAILABLE          = 503
134
 
    HTTP_GATEWAY_TIME_OUT             = 504
135
 
    HTTP_VERSION_NOT_SUPPORTED        = 505
136
 
    HTTP_VARIANT_ALSO_VARIES          = 506
137
 
    HTTP_INSUFFICIENT_STORAGE         = 507
138
 
    HTTP_NOT_EXTENDED                 = 510
139
96
 
140
97
    def __init__(self, req, config):
141
98
        """Create an IVLE request from a mod_python one.
161
118
        self.uri = req.uri
162
119
        # Split the given path into the app (top-level dir) and sub-path
163
120
        # (after first stripping away the root directory)
164
 
        path = self.unmake_path(req.uri)
165
 
        (self.app, self.path) = (ivle.util.split_path(path))
 
121
        (self.app, self.path) = (ivle.util.split_path(req.uri))
166
122
        self.user = None
167
123
        self.hostname = req.hostname
168
124
        self.headers_in = req.headers_in
281
237
        """
282
238
        return os.path.join(self.config['urls']['root'], path)
283
239
 
284
 
    def unmake_path(self, path):
285
 
        """Strip the IVLE URL prefix from the given path, if present.
286
 
 
287
 
        Also normalises the path.
288
 
 
289
 
        This method is DEPRECATED. We no longer support use of a prefix.
290
 
        """
291
 
        path = os.path.normpath(path)
292
 
        root = os.path.normpath(self.config['urls']['root'])
293
 
 
294
 
        if path.startswith(root):
295
 
            path = path[len(root):]
296
 
            # Take out the slash as well
297
 
            if len(path) > 0 and path[0] == os.sep:
298
 
                path = path[1:]
299
 
 
300
 
        return path
301
 
 
302
240
    def get_session(self):
303
241
        """Returns a mod_python Session object for this request.
304
242
        Note that this is dependent on mod_python and may need to change
331
269
            self.got_common_vars = True
332
270
        return self.apache_req.subprocess_env
333
271
 
334
 
    @staticmethod
335
 
    def get_http_codename(code):
336
 
        """Given a HTTP error code int, returns a (name, description)
337
 
        pair, suitable for displaying to the user.
338
 
        May return (None,None) if code is unknown.
339
 
        Only lists common 4xx and 5xx codes (since this is just used
340
 
        to display throw_error error messages).
341
 
        """
342
 
        try:
343
 
            return http_codenames[code]
344
 
        except KeyError:
345
 
            return None, None
346
 
 
347
 
# Human strings for HTTP response codes
348
 
http_codenames = {
349
 
    Request.HTTP_BAD_REQUEST:
350
 
        ("Bad Request",
351
 
        "Your browser sent a request IVLE did not understand."),
352
 
    Request.HTTP_UNAUTHORIZED:
353
 
        ("Unauthorized",
354
 
        "You are not allowed to view this part of IVLE."),
355
 
    Request.HTTP_FORBIDDEN:
356
 
        ("Forbidden",
357
 
        "You are not allowed to view this part of IVLE."),
358
 
    Request.HTTP_NOT_FOUND:
359
 
        ("Not Found",
360
 
        "The application or file you requested does not exist."),
361
 
    Request.HTTP_METHOD_NOT_ALLOWED:
362
 
        ("Method Not Allowed",
363
 
        "Your browser is interacting with IVLE in the wrong way."
364
 
        "This is probably a bug in IVLE. "
365
 
        "Please report it to the administrators."),
366
 
    Request.HTTP_INTERNAL_SERVER_ERROR:
367
 
        ("Internal Server Error",
368
 
        "An unknown error occured in IVLE."),
369
 
    Request.HTTP_NOT_IMPLEMENTED:
370
 
        ("Not Implemented",
371
 
        "The application or file you requested has not been implemented "
372
 
        "in IVLE."),
373
 
    Request.HTTP_SERVICE_UNAVAILABLE:
374
 
        ("Service Unavailable",
375
 
        "IVLE is currently experiencing technical difficulties. "
376
 
        "Please try again later."),
377
 
}