(self)
| 33 | |
| 34 | class WSGIHandler(SimpleHTTPRequestHandler): |
| 35 | def run_wsgi_app(self): |
| 36 | protocol, host, path, parameters, query, fragment = urlparse( |
| 37 | "http://dummyhost%s" % self.path |
| 38 | ) |
| 39 | |
| 40 | # we only use path, query |
| 41 | env = { |
| 42 | "wsgi.version": (1, 0), |
| 43 | "wsgi.url_scheme": "http", |
| 44 | "wsgi.input": self.rfile, |
| 45 | "wsgi.errors": sys.stderr, |
| 46 | "wsgi.multithread": 1, |
| 47 | "wsgi.multiprocess": 0, |
| 48 | "wsgi.run_once": 0, |
| 49 | "REQUEST_METHOD": self.command, |
| 50 | "REQUEST_URI": self.path, |
| 51 | "PATH_INFO": path, |
| 52 | "QUERY_STRING": query, |
| 53 | "CONTENT_TYPE": self.headers.get("Content-Type", ""), |
| 54 | "CONTENT_LENGTH": self.headers.get("Content-Length", ""), |
| 55 | "REMOTE_ADDR": self.client_address[0], |
| 56 | "SERVER_NAME": self.server.server_address[0], |
| 57 | "SERVER_PORT": str(self.server.server_address[1]), |
| 58 | "SERVER_PROTOCOL": self.request_version, |
| 59 | } |
| 60 | |
| 61 | for http_header, http_value in self.headers.items(): |
| 62 | env["HTTP_%s" % http_header.replace("-", "_").upper()] = http_value |
| 63 | |
| 64 | # Setup the state |
| 65 | self.wsgi_sent_headers = 0 |
| 66 | self.wsgi_headers = [] |
| 67 | |
| 68 | try: |
| 69 | # We have there environment, now invoke the application |
| 70 | result = self.server.app(env, self.wsgi_start_response) |
| 71 | try: |
| 72 | try: |
| 73 | for data in result: |
| 74 | if data: |
| 75 | self.wsgi_write_data(data) |
| 76 | finally: |
| 77 | if hasattr(result, "close"): |
| 78 | result.close() |
| 79 | except OSError as socket_err: |
| 80 | # Catch common network errors and suppress them |
| 81 | if socket_err.args[0] in (errno.ECONNABORTED, errno.EPIPE): |
| 82 | return |
| 83 | except TimeoutError: |
| 84 | return |
| 85 | except: |
| 86 | print(traceback.format_exc(), file=web.debug) |
| 87 | |
| 88 | if not self.wsgi_sent_headers: |
| 89 | # We must write out something! |
| 90 | self.wsgi_write_data(" ") |
| 91 | return |
| 92 |
no test coverage detected