-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.py
82 lines (74 loc) · 2.81 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2022 Xiaomi Corp. (authors: Fangjun Kuang)
#
# See ../../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple
# Please sort it alphabetically
_static_files = (
("/css/bootstrap.min.css", "text/css"),
("/css/bootstrap.min.css.map", "text/css"),
("/index.html", "text/html"),
("/js/bootstrap.min.js", "application/javascript"),
("/js/bootstrap.min.js.map", "application/javascript"),
("/js/jquery-3.6.0.min.js", "application/javascript"),
("/js/offline_record.js", "application/javascript"),
("/js/offline_record.js", "application/javascript"),
("/js/popper.min.js", "application/javascript"),
("/js/popper.min.js.map", "application/javascript"),
("/js/streaming_record.js", "application/javascript"),
("/js/upload.js", "application/javascript"),
("/k2-logo.png", "image/png"),
("/nav-partial.html", "text/html"),
("/offline_record.html", "text/html"),
("/streaming_record.html", "text/html"),
("/upload.html", "text/html"),
)
_404_page = r"""
<!doctype html><html><head>
<title>Speech recognition with next-gen Kaldi</title><body>
<h1>404 ERROR! Please re-check your URL</h1>
</body></head></html>
"""
def read_file(root: str, name: str) -> str:
try:
with open(f"{root}/{name}") as f:
return f.read()
except: # noqa
with open(f"{root}/{name}", "rb") as f:
return f.read()
class HttpServer:
"""
A simple HTTP server that hosts only static files
"""
def __init__(self, doc_root: str):
content = dict()
for f, mime_type in _static_files:
content[f] = (read_file(doc_root, f), mime_type)
self.content = content
def process_request(self, f: str) -> Tuple[str, str, str]:
"""
Args:
f:
The filename to read.
Returns:
Return a tuple:
- a bool, True if the given file is found. False otherwise.
- a str, the content of the file if found. Otherwise, it
contains the content for the 404 page
- a str, the MIME type of the returned content
"""
if f in self.content:
return True, self.content[f][0], self.content[f][1]
else:
return False, _404_page, "text/html"