| 1158 | |
| 1159 | |
| 1160 | class HttpClientThread(Thread): |
| 1161 | def __init__(self, connection: socket.socket, cmd: str, resultPath: str, latestResults: list) -> None: |
| 1162 | Thread.__init__(self) |
| 1163 | self.connection = connection |
| 1164 | self.cmd = cmd |
| 1165 | self.resultPath = resultPath |
| 1166 | self.infoPath = os.path.join(self.resultPath, 'info_output') |
| 1167 | self.latestResults = latestResults |
| 1168 | |
| 1169 | # TODO: use a proper parser |
| 1170 | @staticmethod |
| 1171 | def parse_req(cmd): |
| 1172 | req_parts = cmd.split(' ') |
| 1173 | if len(req_parts) != 3 or req_parts[0] != 'GET' or not req_parts[2].startswith('HTTP'): |
| 1174 | return None, None |
| 1175 | url_obj = urlparse(req_parts[1]) |
| 1176 | return url_obj.path, dict(urllib.parse.parse_qsl(url_obj.query)) |
| 1177 | |
| 1178 | def run(self): |
| 1179 | try: |
| 1180 | cmd = self.cmd |
| 1181 | url, queryParams = self.parse_req(cmd) |
| 1182 | if url is None: |
| 1183 | print_ts('invalid request: {}'.format(cmd)) |
| 1184 | self.connection.close() |
| 1185 | return |
| 1186 | t_start = time.perf_counter() |
| 1187 | if url == '/': |
| 1188 | html = overviewReport() |
| 1189 | httpGetResponse(self.connection, html, 'text/html') |
| 1190 | elif url == '/latest.html': |
| 1191 | html = latestReport(self.latestResults) |
| 1192 | httpGetResponse(self.connection, html, 'text/html') |
| 1193 | elif url == '/crash.html': |
| 1194 | text, mime = crashReport(self.resultPath, queryParams) |
| 1195 | httpGetResponse(self.connection, text, mime) |
| 1196 | elif url == '/timeout.html': |
| 1197 | html = timeoutReport(self.resultPath) |
| 1198 | httpGetResponse(self.connection, html, 'text/html') |
| 1199 | elif url == '/stale.html': |
| 1200 | html = staleReport(self.resultPath, queryParams) |
| 1201 | httpGetResponse(self.connection, html, 'text/html') |
| 1202 | elif url == '/diff.html': |
| 1203 | html = diffReport(self.resultPath) |
| 1204 | httpGetResponse(self.connection, html, 'text/html') |
| 1205 | elif url.startswith('/difftoday-'): |
| 1206 | messageId = url[len('/difftoday-'):] |
| 1207 | text = diffMessageIdTodayReport(self.resultPath, messageId) |
| 1208 | httpGetResponse(self.connection, text, 'text/plain') |
| 1209 | elif url.startswith('/diff-'): |
| 1210 | messageId = url[len('/diff-'):] |
| 1211 | text = diffMessageIdReport(self.resultPath, messageId) |
| 1212 | httpGetResponse(self.connection, text, 'text/plain') |
| 1213 | elif url == '/head.html': |
| 1214 | html = headReport(self.resultPath) |
| 1215 | httpGetResponse(self.connection, html, 'text/html') |
| 1216 | elif url == '/headinfo.html': |
| 1217 | html = infoReport(self.infoPath) |