Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
272 lines (249 loc) · 9.49 KB
/
Copy pathapi.py
File metadata and controls
272 lines (249 loc) · 9.49 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#
# cache test by moriya@runserver.jp
#
from pathlib import Path
import datetime
import responder
wd = Path(__file__).parent
api = responder.API(templates_dir=wd/'templates')
myamya_jpg = Path('static/img/myamya.jpg')
def file_timestamp(p):
return datetime.datetime.fromtimestamp(p.stat().st_mtime, tz=datetime.timezone.utc)
# ヘッダに何も指定しない
#
# ie11 ではキャッシュされない
#
# chrome(92.0.4515.131) ではキャッシュされない
#
# => 特に指定がない場合の動作
#
@api.route("/cache_nospec/")
async def normal(req, resp):
now = datetime.datetime.now()
resp.html = api.template('cache_nospec.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
#
# /cache_nospec/ と同様
# ただし、javascript / css / 画像へのリンクを含んでいる
#
@api.route("/cache_nospec2/")
async def cache_nospec2(req, resp):
now = datetime.datetime.now()
resp.html = api.template('cache_nospec2.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
@api.route("/script_test")
async def script_test(req, resp):
resp.headers['Content-Type'] = 'text/javascript'
resp.text = "console.log('javascript')"
@api.route("/script_test_maxage")
async def script_test_maxage(req, resp):
resp.headers['Content-Type'] = 'text/javascript'
resp.headers['Cache-Control'] = 'max-age=60'
resp.text = "console.log('javascript')"
@api.route("/script_test2")
async def script_test2(req, resp):
resp.headers['Content-Type'] = 'text/plain'
resp.text = "console.log('plain')"
@api.route("/css_test")
async def css_test(req, resp):
resp.headers['Content-Type'] = 'text/css'
resp.text = '// test css'
@api.route("/css_test_maxage")
async def css_test_maxage(req, resp):
resp.headers['Content-Type'] = 'text/css'
resp.headers['Cache-Control'] = 'max-age=60'
resp.text = '// test css'
@api.route("/css_test2")
async def css_test2(req, resp):
resp.headers['Content-Type'] = 'text/plain'
resp.text = '// test css'
@api.route("/myamya")
async def myamya(req, resp):
resp.headers['Content-Type'] = 'image/jpeg'
resp.content = open(myamya_jpg, 'rb').read()
@api.route("/myamya_maxage")
async def myamya_maxage(req, resp):
resp.headers['Content-Type'] = 'image/jpeg'
resp.headers['Cache-Control'] = 'max-age=60'
resp.content = open(myamya_jpg, 'rb').read()
# Cache-Control ヘッダ: max-age=60
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => max-age を指定した場合の動作
# ie11 はアドレスバーで URL を入力した場合は、リンククリックと同様
#
# chrome はアドレスバーで URL を入力した場合は、リロードと同様
# https://stackoverflow.com/questions/11245767/is-chrome-ignoring-cache-control-max-age
# https://blog.chromium.org/2017/01/reload-reloaded-faster-and-leaner-page_26.html
# などか
#
@api.route("/cache_control1/")
async def cache_control1(req, resp):
now = datetime.datetime.now()
resp.headers['Cache-Control'] = 'max-age=60'
resp.html = api.template('cache_control1.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# Cache-Control ヘッダ max-age=60
# ドキュメントの http-equiv Cache-Control の値 no-store
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => Cache-Control は http-equiv では指定できない?
#
@api.route("/cache_control1a/")
async def cache_control1a(req, resp):
now = datetime.datetime.now()
resp.headers['Cache-Control'] = 'max-age=60'
# <meta http-equiv="Cache-Control" content="no-store">
resp.html = api.template('cache_control1a.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# Cache-Control ヘッダ: max-age=60
# ドキュメントの http-equiv Expires の値: Thu, 01 Dec 1994 16:00:00 GMT
#
# ie11
# リンククリック/アドレスバー: 再リクエストされる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => ie11 では、http-equiv expires は有効
# chrome は http-equiv expires を無視?
#
@api.route("/cache_control2/")
async def cache_control2(req, resp):
now = datetime.datetime.now()
resp.headers['Cache-Control'] = 'max-age=60'
# <meta http-equiv="Expires" content="Thu, 01 Dec 1994 16:00:00 GMT">
resp.html = api.template('cache_control2.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# Cache-Control ヘッダ: max-age=60
# ドキュメントの http-equiv Cache-Control 値: max-age=0
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => ie11/chrome とも ドキュメントの http-equiv cache-control は無視?
#
@api.route("/cache_control2a/")
async def cache_control2a(req, resp):
now = datetime.datetime.now()
resp.headers['Cache-Control'] = 'max-age=60'
# <meta http-equiv="Cache-Control" content="max-age=0">
resp.html = api.template('cache_control2a.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# レスポンスヘッダなし
# ドキュメントの http-equiv Cache-Control 値: max-age=60
#
# ie11
# リンククリック/アドレスバー: 再リクエストされる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: 再リクエストされる
# アドレスバー/リロード: 再リクエストされる
#
# => ie11/chrome とも ドキュメントの http-equiv cache-control は無視?
#
@api.route("/cache_control2b/")
async def cache_control2b(req, resp):
now = datetime.datetime.now()
# <meta http-equiv="Cache-Control" content="max-age=60">
resp.html = api.template('cache_control2b.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# レスポンスヘッダなし
# ドキュメントの http-equiv Expires 値: 60秒後
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: 再リクエストされる
# アドレスバー/リロード: 再リクエストされる
#
# => ie11 はドキュメントの http-equiv が使われる
# chrome は http-equiv expires を無視?
#
@api.route("/cache_control2c/")
async def cache_control2c(req, resp):
now = datetime.datetime.now()
utcnow = datetime.datetime.now(datetime.timezone.utc)
expires = utcnow + datetime.timedelta(seconds=60)
# <meta http-equiv="Expires" content="現在時刻+60秒">
resp.html = api.template('cache_control2c.html', time=now.strftime('%Y/%m/%d %H:%M:%S'), expires=expires.strftime('%a, %d %b %Y %H:%M:%S GMT'))
# Expires ヘッダ: Thu, 01 Dec 1994 16:00:00 GMT
# ドキュメントの http-equiv Expires 値: 60秒後
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: 再リクエストされる
# アドレスバー/リロード: 再リクエストされる
#
# => ie11 はドキュメントの http-equiv が優先
# chrome は http-equiv expires を無視?
#
@api.route("/cache_control2d/")
async def cache_control2d(req, resp):
now = datetime.datetime.now()
utcnow = datetime.datetime.now(datetime.timezone.utc)
expires = utcnow + datetime.timedelta(seconds=60)
resp.headers['Expires'] = 'Thu, 01 Dec 1994 16:00:00 GMT'
# <meta http-equiv="Expires" content="現在時刻+60秒">
resp.html = api.template('cache_control2c.html', time=now.strftime('%Y/%m/%d %H:%M:%S'), expires=expires.strftime('%a, %d %b %Y %H:%M:%S GMT'))
# Expires ヘッダ: 60秒後
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => Expires ヘッダは有効(念のため確認)
#
@api.route("/cache_control2e/")
async def cache_control2e(req, resp):
now = datetime.datetime.now()
utcnow = datetime.datetime.now(datetime.timezone.utc)
expires = utcnow + datetime.timedelta(seconds=60)
resp.headers['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')
resp.html = api.template('cache_control2e.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
# Cache-Control ヘッダ: max-age=60
# Expires ヘッダ: Thu, 01 Dec 1994 16:00:00 GMT
# MDN によると、Cache-Control に max-age 指定があると、Expires は無視される
#
# ie11
# リンククリック/アドレスバー: キャッシュが使われる
# リロード: 再リクエストされる
#
# chrome
# リンククリック: キャッシュが使われる
# アドレスバー/リロード: 再リクエストされる
#
# => MDN の記述どおり
#
@api.route("/cache_control3/")
async def cache_control3(req, resp):
now = datetime.datetime.now()
resp.headers['Cache-Control'] = 'max-age=60'
resp.headers['Expires'] = 'Thu, 01 Dec 1994 16:00:00 GMT'
resp.html = api.template('cache_control3.html', time=now.strftime('%Y/%m/%d %H:%M:%S'))
if __name__ == "__main__":
api.run(address='0.0.0.0')