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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2007-12-14 06:17:31 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:64
apps/server: execute_cgi now processess CGI headers (incomplete and messy),
including a nice HTML warning message if no content type is provided.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
        req.content_type = type
60
60
        req.sendfile(path)
61
61
 
 
62
# Used to store mutable data
 
63
class Dummy:
 
64
    pass
 
65
 
62
66
def execute_cgi(filename, studentprog, req):
63
67
    """
64
68
    filename: Full path on the local system to the CGI wrapper program
92
96
    pid = subprocess.Popen([filename, studentprog],
93
97
        stdin=f, stdout=subprocess.PIPE, cwd=progdir)
94
98
 
 
99
    # process_cgi_line: Reads a single line of CGI output and processes it.
 
100
    # Prints to req, and also does fancy HTML warnings if Content-Type
 
101
    # omitted.
 
102
    cgiflags = Dummy()
 
103
    cgiflags.got_cgi_header = False
 
104
    cgiflags.started_cgi_body = False
 
105
    cgiflags.wrote_html_warning = False
 
106
    def process_cgi_line(line):
 
107
        # FIXME? Issue with binary files (processing per-line?)
 
108
        if cgiflags.started_cgi_body:
 
109
            # FIXME: HTML escape text if wrote_html_warning
 
110
            req.write(line)
 
111
        else:
 
112
            # Read CGI headers
 
113
            if line.strip() == "" and cgiflags.got_cgi_header:
 
114
                cgiflags.started_cgi_body = True
 
115
            elif line.startswith("Content-Type:"):
 
116
                req.content_type = line[13:].strip()
 
117
                cgiflags.got_cgi_header = True
 
118
            elif line.startswith("Location:"):
 
119
                # TODO
 
120
                cgiflags.got_cgi_header = True
 
121
            elif line.startswith("Status:"):
 
122
                # TODO
 
123
                cgiflags.got_cgi_header = True
 
124
            elif cgiflags.got_cgi_header:
 
125
                # Invalid header
 
126
                # TODO
 
127
                req.write("Invalid header")
 
128
                pass
 
129
            else:
 
130
                # Assume the user is not printing headers and give a warning
 
131
                # about that.
 
132
                # User program did not print header.
 
133
                # Make a fancy HTML warning for them.
 
134
                req.content_type = "text/html"
 
135
                req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
136
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
137
<html xmlns="http://www.w3.org/1999/xhtml">
 
138
<head>
 
139
  <meta http-equiv="Content-Type"
 
140
    content="text/html; charset=utf-8" />
 
141
</head>
 
142
<body style="margin: 0; padding: 0; font-family: sans;">
 
143
  <div style="background-color: #faa; border-bottom: 1px solid black;
 
144
    border-top: 1px solid #faa; padding: 8px;">
 
145
    <p><strong>Warning</strong>: You did not print a "Content-Type" header.
 
146
    CGI requires you to print some content type. You may wish to try:</p>
 
147
    <pre style="margin-left: 1em">Content-Type: text/html</pre>
 
148
  </div>
 
149
  <div style="margin: 8px;">
 
150
    <pre>
 
151
""")
 
152
                cgiflags.got_cgi_header = True
 
153
                cgiflags.wrote_html_warning = True
 
154
                cgiflags.started_cgi_body = True
 
155
                req.write(line)
 
156
 
95
157
    # Read from the process's stdout into req
96
 
    # FIXME: Efficiency
97
 
    # TODO: Read CGI response headers
98
 
    response = pid.stdout.read()
99
 
    req.write(response)
 
158
    for line in pid.stdout:
 
159
        process_cgi_line(line)
 
160
 
 
161
    # If we wrote an HTML warning header, write the footer
 
162
    if cgiflags.wrote_html_warning:
 
163
        req.write("""</pre>
 
164
  </div>
 
165
</body>
 
166
</html>""")
100
167
 
101
168
# Mapping of mime types to executables
102
169