92
96
pid = subprocess.Popen([filename, studentprog],
93
97
stdin=f, stdout=subprocess.PIPE, cwd=progdir)
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
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
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:"):
120
cgiflags.got_cgi_header = True
121
elif line.startswith("Status:"):
123
cgiflags.got_cgi_header = True
124
elif cgiflags.got_cgi_header:
127
req.write("Invalid header")
130
# Assume the user is not printing headers and give a warning
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">
139
<meta http-equiv="Content-Type"
140
content="text/html; charset=utf-8" />
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>
149
<div style="margin: 8px;">
152
cgiflags.got_cgi_header = True
153
cgiflags.wrote_html_warning = True
154
cgiflags.started_cgi_body = True
95
157
# Read from the process's stdout into req
97
# TODO: Read CGI response headers
98
response = pid.stdout.read()
158
for line in pid.stdout:
159
process_cgi_line(line)
161
# If we wrote an HTML warning header, write the footer
162
if cgiflags.wrote_html_warning:
101
168
# Mapping of mime types to executables