changeset 7112:c0d030bd472e

Fix: Send Content-Length header to client from top Exception handler The top exception handler in run_cgi wasn't sending the Content-Length header for the error message. This resulted in a hung client. Probably wasn't an issue with http 1.0, but when using 1.1 it's required.
author John Rouillard <rouilj@ieee.org>
date Sun, 11 Dec 2022 18:47:24 -0500
parents a7853002495c
children 5c6dd791d638
files CHANGES.txt roundup/scripts/roundup_server.py
diffstat 2 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.txt	Thu Dec 08 10:54:24 2022 -0500
+++ b/CHANGES.txt	Sun Dec 11 18:47:24 2022 -0500
@@ -54,7 +54,9 @@
   roundup-admin. (John Rouillard)
 - Document better that files in the template or static_files
   directories accessed via @@file are available to any user with the
-  url.
+  url. (John Rouillard)
+- Fix final exception handler in roundup-server to send proper
+  Content-Length header to the client. (John Rouillard)
 
 Features:
 
--- a/roundup/scripts/roundup_server.py	Thu Dec 08 10:54:24 2022 -0500
+++ b/roundup/scripts/roundup_server.py	Sun Dec 11 18:47:24 2022 -0500
@@ -272,32 +272,34 @@
             if hasattr(socket, 'timeout') and isinstance(val, socket.timeout):
                 self.log_error('timeout')
             else:
-                # it'd be nice to be able to detect if these are going to have
-                # any effect...
                 self.send_response(400)
                 self.send_header('Content-Type', 'text/html')
-                self.end_headers()
                 if self.DEBUG_MODE:
                     try:
                         reload(cgitb)
-                        self.wfile.write(s2b(cgitb.breaker()))
-                        self.wfile.write(s2b(cgitb.html()))
+                        output = s2b(cgitb.breaker()) + s2b(cgitb.html())
                     except Exception:
                         s = StringIO()
                         traceback.print_exc(None, s)
-                        self.wfile.write(b"<pre>")
-                        self.wfile.write(s2b(html_escape(s.getvalue())))
-                        self.wfile.write(b"</pre>\n")
+                        output = b"<pre>%s</pre>" % s2b(
+                            html_escape(s.getvalue()))
                 else:
                     # user feedback
-                    self.wfile.write(s2b(cgitb.breaker()))
                     ts = time.ctime()
-                    self.wfile.write(s2b('''<p>%s: An error occurred. Please check
-                    the server log for more information.</p>''' % ts))
+                    output = (
+                        s2b('''<body><p>%s: An error occurred. Please check
+                        the server log for more information.</p></body>''' %
+                            ts)
+                    )
                     # out to the logfile
                     print('EXCEPTION AT', ts)
                     traceback.print_exc()
 
+                # complete output to user.
+                self.send_header('Content-Length', len(output))
+                self.end_headers()
+                self.wfile.write(output)
+
     do_GET = do_POST = do_HEAD = do_PUT = do_DELETE = \
         do_PATCH = do_OPTIONS = run_cgi
 

Roundup Issue Tracker: http://roundup-tracker.org/