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

« back to all changes in this revision

Viewing changes to lib/common/interpret.py

  • Committer: mattgiuca
  • Date: 2008-07-15 07:20:30 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:876
common/db.py: Added add_enrolment method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
from common import studpath
31
31
from common import db
 
32
from common.util import IVLEError, IVLEJailError
32
33
import conf
33
34
import functools
34
35
 
61
62
 
62
63
    return uids[login]
63
64
 
64
 
def interpret_file(req, owner, jail_dir, filename, interpreter):
 
65
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
65
66
    """Serves a file by interpreting it using one of IVLE's builtin
66
67
    interpreters. All interpreters are intended to run in the user's jail. The
67
68
    jail location is provided as an argument to the interpreter but it is up
98
99
    # (Note that paths "relative" to the jail actually begin with a '/' as
99
100
    # they are absolute in the jailspace)
100
101
 
101
 
    return interpreter(uid, jail_dir, working_dir, filename_abs, req)
 
102
    return interpreter(uid, jail_dir, working_dir, filename_abs, req,
 
103
                       gentle)
102
104
 
103
105
class CGIFlags:
104
 
    """Stores flags regarding the state of reading CGI output."""
105
 
    def __init__(self):
 
106
    """Stores flags regarding the state of reading CGI output.
 
107
       If this is to be gentle, detection of invalid headers will result in an
 
108
       HTML warning."""
 
109
    def __init__(self, begentle=True):
 
110
        self.gentle = begentle
106
111
        self.started_cgi_body = False
107
112
        self.got_cgi_headers = False
108
113
        self.wrote_html_warning = False
110
115
        self.headers = {}       # Header names : values
111
116
 
112
117
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
113
 
                script_path, req):
 
118
                script_path, req, gentle):
114
119
    """
115
120
    trampoline: Full path on the local system to the CGI wrapper program
116
121
        being executed.
165
170
    # process_cgi_line: Reads a single line of CGI output and processes it.
166
171
    # Prints to req, and also does fancy HTML warnings if Content-Type
167
172
    # omitted.
168
 
    cgiflags = CGIFlags()
 
173
    cgiflags = CGIFlags(gentle)
169
174
 
170
175
    # Read from the process's stdout into req
171
176
    data = pid.stdout.read(CGI_BLOCK_SIZE)
227
232
            if len(split) == 1:
228
233
                split = headers.split('\n', 1)
229
234
 
 
235
        # Is this an internal IVLE error condition?
 
236
        hs = cgiflags.headers
 
237
        if 'X-IVLE-Error-Type' in hs:
 
238
            t = hs['X-IVLE-Error-Type']
 
239
            if t == IVLEError.__name__:
 
240
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
 
241
                                hs['X-IVLE-Error-Message'])
 
242
            else:
 
243
                try:
 
244
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
 
245
                                        hs['X-IVLE-Error-Message'],
 
246
                                        hs['X-IVLE-Error-Info'])
 
247
                except KeyError:
 
248
                    raise IVLEError(500, 'bad error headers written by CGI')
 
249
 
230
250
        # Check to make sure the required headers were written
231
 
        if cgiflags.wrote_html_warning:
 
251
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
232
252
            # We already reported an error, that's enough
233
253
            pass
234
254
        elif "Content-Type" in cgiflags.headers:
261
281
    try:
262
282
        name, value = line.split(':', 1)
263
283
    except ValueError:
 
284
        # If we are being gentle, we want to help the user understand what
 
285
        # went wrong. Otherwise, we bail out.
 
286
        if not cgiflags.gentle:
 
287
            raise
264
288
        # No colon. The user did not write valid headers.
265
289
        if len(cgiflags.headers) == 0:
266
290
            # First line was not a header line. We can assume this is not
292
316
        try:
293
317
            req.status = int(value.split(' ', 1)[0])
294
318
        except ValueError:
 
319
            if not cgiflags.gentle:
 
320
                # This isn't user code, so it should be good.
 
321
                # Get us out of here!
 
322
                raise
295
323
            message = """The "Status" CGI header was invalid. You need to
296
324
print a number followed by a message, such as "302 Found"."""
297
325
            write_html_warning(req, message)