@@ -124,22 +124,17 @@ def do_REQUEST(self):
124124 self .url , self .params = path , params
125125
126126 if self .url == '/' :
127- self .send_response (OK )
128127
129128 if not any (_ in self .params for _ in ("id" , "query" )):
129+ self .send_response (OK )
130130 self .send_header ("Content-type" , "text/html" )
131131 self .send_header ("Connection" , "close" )
132132 self .end_headers ()
133133 self .wfile .write (b"<html><p><h3>GET:</h3><a href='/?id=1'>link</a></p><hr><p><h3>POST:</h3><form method='post'>ID: <input type='text' name='id'><input type='submit' value='Submit'></form></p></html>" )
134134 else :
135- self .send_header ("Content-type" , "text/html" )
136- self .send_header ("Connection" , "close" )
137-
138- if not self .raw_requestline .startswith (b"HEAD" ):
139- self .end_headers ()
135+ code , output = OK , ""
140136
141137 try :
142- output = ""
143138
144139 if self .params .get ("echo" , "" ):
145140 output += "%s<br>" % self .params ["echo" ]
@@ -163,12 +158,19 @@ def do_REQUEST(self):
163158 output += "</table>\n "
164159 output += "</body></html>"
165160 except Exception as ex :
161+ code = INTERNAL_SERVER_ERROR
166162 output = "%s: %s" % (re .search (r"'([^']+)'" , str (type (ex ))).group (1 ), ex )
167163
164+ self .send_response (code )
165+
166+ self .send_header ("Content-type" , "text/html" )
167+ self .send_header ("Connection" , "close" )
168+
168169 if self .raw_requestline .startswith (b"HEAD" ):
169170 self .send_header ("Content-Length" , str (len (output )))
170171 self .end_headers ()
171172 else :
173+ self .end_headers ()
172174 self .wfile .write (output .encode ("utf8" ))
173175 else :
174176 self .send_response (NOT_FOUND )
0 commit comments