Mercurial > p > roundup > code
comparison roundup/cgi/wsgi_handler.py @ 6080:274c2c082f68
factor out get_tracker so we can potentially customise if we want to reuse
tracker instances or create a new instance for each request
| author | Christof Meerwald <cmeerw@cmeerw.org> |
|---|---|
| date | Wed, 05 Feb 2020 19:43:57 +0000 |
| parents | 82816000aef3 |
| children | f74d078cfd9a |
comparison
equal
deleted
inserted
replaced
| 6079:7022af20a15a | 6080:274c2c082f68 |
|---|---|
| 4 # and/or modify under the same terms as Python. | 4 # and/or modify under the same terms as Python. |
| 5 # | 5 # |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import weakref | 8 import weakref |
| 9 | |
| 10 from contextlib import contextmanager | |
| 9 | 11 |
| 10 from roundup.anypy.html import html_escape | 12 from roundup.anypy.html import html_escape |
| 11 | 13 |
| 12 import roundup.instance | 14 import roundup.instance |
| 13 from roundup.cgi import TranslationService | 15 from roundup.cgi import TranslationService |
| 87 request.wfile.write(s2b(DEFAULT_ERROR_MESSAGE % locals())) | 89 request.wfile.write(s2b(DEFAULT_ERROR_MESSAGE % locals())) |
| 88 return [] | 90 return [] |
| 89 | 91 |
| 90 tracker = roundup.instance.open(self.home, not self.debug) | 92 tracker = roundup.instance.open(self.home, not self.debug) |
| 91 | 93 |
| 92 # need to strip the leading '/' | 94 with self.get_tracker() as tracker: |
| 93 environ["PATH_INFO"] = environ["PATH_INFO"][1:] | 95 # need to strip the leading '/' |
| 94 if request.timing: | 96 environ["PATH_INFO"] = environ["PATH_INFO"][1:] |
| 95 environ["CGI_SHOW_TIMING"] = request.timing | 97 if request.timing: |
| 98 environ["CGI_SHOW_TIMING"] = request.timing | |
| 96 | 99 |
| 97 form = BinaryFieldStorage(fp=environ['wsgi.input'], environ=environ) | 100 form = BinaryFieldStorage(fp=environ['wsgi.input'], environ=environ) |
| 98 | 101 |
| 99 if environ['REQUEST_METHOD'] in ("OPTIONS", "DELETE"): | 102 if environ['REQUEST_METHOD'] in ("OPTIONS", "DELETE"): |
| 100 # these methods have no data. When we init tracker.Client | 103 # these methods have no data. When we init tracker.Client |
| 101 # set form to None and request.rfile to None to get a | 104 # set form to None and request.rfile to None to get a |
| 102 # properly initialized empty form. | 105 # properly initialized empty form. |
| 103 form = None | 106 form = None |
| 104 request.rfile = None | 107 request.rfile = None |
| 105 | 108 |
| 106 client = tracker.Client(tracker, request, environ, form, | 109 client = tracker.Client(tracker, request, environ, form, |
| 107 request.translator) | 110 request.translator) |
| 108 try: | 111 try: |
| 109 client.main() | 112 client.main() |
| 110 except roundup.cgi.client.NotFound: | 113 except roundup.cgi.client.NotFound: |
| 111 request.start_response([('Content-Type', 'text/html')], 404) | 114 request.start_response([('Content-Type', 'text/html')], 404) |
| 112 request.wfile.write(s2b('Not found: %s' % | 115 request.wfile.write(s2b('Not found: %s' % |
| 113 html_escape(client.path))) | 116 html_escape(client.path))) |
| 114 | 117 |
| 115 # all body data has been written using wfile | 118 # all body data has been written using wfile |
| 116 return [] | 119 return [] |
| 117 | 120 |
| 118 def start_response(self, headers, response_code): | 121 def start_response(self, headers, response_code): |
| 123 | 126 |
| 124 def get_wfile(self): | 127 def get_wfile(self): |
| 125 if self.__wfile is None: | 128 if self.__wfile is None: |
| 126 raise ValueError('start_response() not called') | 129 raise ValueError('start_response() not called') |
| 127 return self.__wfile | 130 return self.__wfile |
| 131 | |
| 132 @contextmanager | |
| 133 def get_tracker(self): | |
| 134 # get a new instance for each request | |
| 135 yield roundup.instance.open(self.home, not self.debug) |
