comparison roundup/cgi/client.py @ 6509:1fc765ef6379

Fix 204 responses, hangs and crashes with REST. Remove Content-Type and make sure no content is returned by OPTIONS request in REST interface. In write_html set the Content-Length when response is not encoded/compressed (fixes hang due to missing content-length with unencoded data). In REST interface do not raise UsageError for invalid api version. Return json error with proper message. Fixes crash.
author John Rouillard <rouilj@ieee.org>
date Sat, 16 Oct 2021 13:34:04 -0400
parents e162845193c4
children f8df7fed18f6
comparison
equal deleted inserted replaced
6507:bc95f7431efb 6509:1fc765ef6379
643 output = handler.dispatch(self.env['REQUEST_METHOD'], 643 output = handler.dispatch(self.env['REQUEST_METHOD'],
644 self.path, self.form) 644 self.path, self.form)
645 645
646 # type header set by rest handler 646 # type header set by rest handler
647 # self.setHeader("Content-Type", "text/xml") 647 # self.setHeader("Content-Type", "text/xml")
648 self.setHeader("Content-Length", str(len(output))) 648 if self.response_code == 204: # no body with 204
649 self.write(output) 649 self.write("")
650 else:
651 self.setHeader("Content-Length", str(len(output)))
652 self.write(output)
650 653
651 def add_ok_message(self, msg, escape=True): 654 def add_ok_message(self, msg, escape=True):
652 add_message(self._ok_message, msg, escape) 655 add_message(self._ok_message, msg, escape)
653 656
654 def add_error_message(self, msg, escape=True): 657 def add_error_message(self, msg, escape=True):
846 self.send_error_to_admin(e.subject, e.html, e.txt) 849 self.send_error_to_admin(e.subject, e.html, e.txt)
847 self.write_html(e.html) 850 self.write_html(e.html)
848 else: 851 else:
849 # in debug mode, only write error to screen. 852 # in debug mode, only write error to screen.
850 self.write_html(e.html) 853 self.write_html(e.html)
851 except: 854 except Exception as e:
852 # Something has gone badly wrong. Therefore, we should 855 # Something has gone badly wrong. Therefore, we should
853 # make sure that the response code indicates failure. 856 # make sure that the response code indicates failure.
854 if self.response_code == http_.client.OK: 857 if self.response_code == http_.client.OK:
855 self.response_code = http_.client.INTERNAL_SERVER_ERROR 858 self.response_code = http_.client.INTERNAL_SERVER_ERROR
856 # Help the administrator work out what went wrong. 859 # Help the administrator work out what went wrong.
2194 if not self.headers_done: 2197 if not self.headers_done:
2195 # at this point, we are sure about Content-Type 2198 # at this point, we are sure about Content-Type
2196 if 'Content-Type' not in self.additional_headers: 2199 if 'Content-Type' not in self.additional_headers:
2197 self.additional_headers['Content-Type'] = \ 2200 self.additional_headers['Content-Type'] = \
2198 'text/html; charset=%s' % self.charset 2201 'text/html; charset=%s' % self.charset
2202 if 'Content-Length' not in self.additional_headers:
2203 self.additional_headers['Content-Length'] = len(content)
2199 self.header() 2204 self.header()
2200 2205
2201 if self.env['REQUEST_METHOD'] == 'HEAD': 2206 if self.env['REQUEST_METHOD'] == 'HEAD':
2202 # client doesn't care about content 2207 # client doesn't care about content
2203 return 2208 return
2495 headers.update(self.additional_headers) 2500 headers.update(self.additional_headers)
2496 2501
2497 if headers.get('Content-Type', 'text/html') == 'text/html': 2502 if headers.get('Content-Type', 'text/html') == 'text/html':
2498 headers['Content-Type'] = 'text/html; charset=utf-8' 2503 headers['Content-Type'] = 'text/html; charset=utf-8'
2499 2504
2505 if response == 204: # has no body so no content-type
2506 del(headers['Content-Type'])
2507
2500 headers = list(headers.items()) 2508 headers = list(headers.items())
2501 2509
2502 for ((path, name), (value, expire)) in self._cookies.items(): 2510 for ((path, name), (value, expire)) in self._cookies.items():
2503 cookie = "%s=%s; Path=%s;"%(name, value, path) 2511 cookie = "%s=%s; Path=%s;"%(name, value, path)
2504 if expire is not None: 2512 if expire is not None:

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