GET method handler.
(self)
| 110 | self.end_headers() |
| 111 | |
| 112 | def do_GET(self): |
| 113 | """GET method handler.""" |
| 114 | try: |
| 115 | path = self.path[1:].split("?")[0] |
| 116 | |
| 117 | if path == "echo_headers": |
| 118 | self._send_response("text/plain") |
| 119 | header_lines = [f"{k}: {v}" for k, v in self.headers.items()] |
| 120 | self.wfile.write("\n".join(header_lines).encode("utf-8")) |
| 121 | return |
| 122 | |
| 123 | if path == "echo_json": |
| 124 | self.send_response(200) |
| 125 | self.send_header("Content-type", "application/json") |
| 126 | self.end_headers() |
| 127 | self.wfile.write(b'{"status": "ok"}') |
| 128 | return |
| 129 | |
| 130 | if path == "set_cookie": |
| 131 | qs = urllib.parse.urlparse(self.path).query |
| 132 | params = urllib.parse.parse_qs(qs) |
| 133 | name = params.get("name", ["test"])[0] |
| 134 | value = params.get("value", ["value"])[0] |
| 135 | self.send_response(200) |
| 136 | self.send_header("Content-type", "text/plain") |
| 137 | self.send_header("Set-Cookie", f"{name}={value}; Path=/") |
| 138 | self.end_headers() |
| 139 | self.wfile.write(b"cookie set") |
| 140 | return |
| 141 | |
| 142 | if path == "basic-auth": |
| 143 | if self._is_authenticated(): |
| 144 | self._send_response("application/json") |
| 145 | self.wfile.write(b'{"authenticated": true}') |
| 146 | else: |
| 147 | self.send_response(401) |
| 148 | self.send_header("WWW-Authenticate", 'Basic realm="Fake Realm"') |
| 149 | self.send_header("Content-type", "text/plain") |
| 150 | self.end_headers() |
| 151 | # Body must not contain the substring "authenticated" — tests |
| 152 | # assert its absence when an auth challenge is cancelled. |
| 153 | self.wfile.write(b"Access denied") |
| 154 | return |
| 155 | |
| 156 | file_path = os.path.join(HTML_ROOT, path) |
| 157 | if path.startswith("page/"): |
| 158 | html = self._serve_page(path[5:]) |
| 159 | self._send_response("text/html") |
| 160 | self.wfile.write(html) |
| 161 | elif os.path.isfile(file_path): |
| 162 | content, content_type = self._serve_file(file_path) |
| 163 | self._send_response(content_type) |
| 164 | self.wfile.write(content) |
| 165 | else: |
| 166 | self.send_error(404, f"File Not Found: {path}") |
| 167 | except OSError: |
| 168 | self.send_error(404, f"File Not Found: {path}") |
| 169 |
nothing calls this directly
no test coverage detected