Skip to content

Commit 632a795

Browse files
mimetype fix in http.py
1 parent e30e892 commit 632a795

File tree

4 files changed

+54
-45
lines changed

4 files changed

+54
-45
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#app_menu {
2+
position: fixed;
3+
height: 100%;
4+
width: 400px;
5+
top: 0;
6+
background: white;
7+
z-index: 10;
8+
box-shadow: 3px 3px 3px #888888;
9+
transition: left 0.4s ease;
10+
}
11+
12+
.app_menu-normal {
13+
left: 0;
14+
}
15+
16+
.app_menu-hidden {
17+
left: -403px;
18+
}
19+
20+
#app_menu button {
21+
position: absolute;
22+
left: 400px;
23+
bottom: 0;
24+
height: 6em;
25+
width: 12em;
26+
}
27+
28+
#app_about {
29+
/* itself */
30+
position: absolute;
31+
bottom: 0;
32+
width: 100%;
33+
/* content */
34+
text-align: center;
35+
}

example/game/public/index.css

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ body {
22
text-align: center;
33
}
44

5-
#app_about {
6-
/* itself */
7-
position: absolute;
8-
bottom: 0;
9-
width: 100%;
10-
/* content */
11-
text-align: center;
12-
}
135

146
#game_canvas {
157
border:solid 1px black;
@@ -32,32 +24,7 @@ body {
3224
text-align: center;
3325
}
3426

35-
#app_menu {
36-
position: fixed;
37-
height: 100%;
38-
width: 400px;
39-
top: 0;
40-
background: white;
41-
z-index: 10;
42-
box-shadow: 3px 3px 3px #888888;
43-
transition: left 0.4s ease;
44-
}
4527

46-
.app_menu-normal {
47-
left: 0;
48-
}
49-
50-
.app_menu-hidden {
51-
left: -403px;
52-
}
53-
54-
#app_menu button {
55-
position: absolute;
56-
left: 400px;
57-
bottom: 0;
58-
height: 6em;
59-
width: 12em;
60-
}
6128

6229
@media screen and (max-width: 1081px) {
6330
#game_canvas {

example/game/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<link rel="icon" href="img/atom.ico">
2222
<link rel="manifest" href="manifest.json">
2323
<link rel="stylesheet" type="text/css" href="index.css">
24+
<link rel="stylesheet" type="text/css" href="app_menu/app_menu.css">
2425
<script src="service.js"></script>
2526
<script src="user.js"></script>
2627
<script src="game.js"></script>

hyper2web/http.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
I currently think that they should be synchronized since they should not do IO
55
Where as endpoint module is designed for IO
66
"""
7+
import mimetypes
78
from collections import OrderedDict
89

910
from curio import spawn, Event, aopen
@@ -169,40 +170,37 @@ async def send(self, stream_id: int, headers, data: bytes=None):
169170
:param data: HTTP response body. Has to be bytes(binary data).
170171
It's users' responsibility to encode any kinds of data to binary.
171172
"""
172-
# print('HTTP.send')
173+
173174
# not sure if check for None or Falsy(empty containers)
174175
if data is None:
175176
self.connection.send_headers(stream_id, headers, end_stream=True)
176177
await self.sock.sendall(self.connection.data_to_send())
177178

178179
else:
180+
# todo: change this to logger in the future.
179181
print('HTTP.send ', headers)
180182

181183
self.connection.send_headers(stream_id, headers, end_stream=False)
182-
# print('HTTP.send headers')
184+
183185
await self.sock.sendall(self.connection.data_to_send())
184-
# print('HTTP.send before body')
186+
185187
# body
186188
i = 0
187189
while True:
188-
# print('HTTP.send in loop')
190+
189191
while not self.connection.local_flow_control_window(stream_id):
190192
await self.wait_for_flow_control(stream_id)
191-
# print('HTTP.send 1')
193+
192194
chunk_size = min(self.connection.local_flow_control_window(stream_id), READ_CHUNK_SIZE)
193-
# print('HTTP.send 2')
194-
# this line is sync
195+
195196
data_to_send = data[i:i+chunk_size]
196197
end_stream = (len(data_to_send) != chunk_size)
197-
# print('HTTP.send 3')
198-
# print(stream_id, len(data_to_send), end_stream)
198+
199199
try:
200200
self.connection.send_data(stream_id, data_to_send, end_stream=end_stream)
201201
except BaseException as e:
202202
print(e)
203-
# print(i, len(data_to_send), chunk_size)
204-
# print(data_to_send)
205-
# print(self.connection.data_to_send())
203+
206204
await self.sock.sendall(self.connection.data_to_send())
207205

208206
if end_stream:
@@ -239,7 +237,15 @@ async def send_file(self, file_path):
239237
# 不知道这个 context manager 是否处理文件没找到
240238
async with aopen(file_path, mode='rb') as f:
241239
data = await f.read()
240+
242241
self.headers['content-length'] = str(len(data))
242+
243+
content_type, content_encoding = mimetypes.guess_type(file_path)
244+
if content_type:
245+
self.headers['content-type'] = content_type
246+
if content_encoding:
247+
self.headers['content-encoding'] = content_encoding
248+
243249
await self.send(data)
244250

245251
async def send_status_code(self, status_code):

0 commit comments

Comments
 (0)