forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_headers.py
52 lines (40 loc) · 1.24 KB
/
check_headers.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
#!/usr/bin/env python2
import sys
from os import unlink
from os.path import exists
HEADERS = ('Content-Disposition', 'Content-Length', 'Content-Type',
'ETag', 'Last-Modified')
def is_sig_header(header):
header = header.lower()
for s in HEADERS:
if header.startswith(s.lower()):
return True
def do():
headers_fn = sys.argv[1]
signature_fn = sys.argv[2]
# first, get all the headers from the latest request
with open(headers_fn) as fd:
headers = [line.strip() for line in fd.readlines()]
last_index = 0
for index, header in enumerate(headers):
if header.startswith('HTTP/1.'):
last_index = index
headers = headers[last_index:]
# select few headers for the signature
headers = [header for header in headers if is_sig_header(header)]
signature = '\n'.join(headers)
# read the original signature
if exists(signature_fn):
with open(signature_fn) as fd:
original_signature = fd.read()
if original_signature == signature:
return 0
unlink(signature_fn)
if signature:
with open(signature_fn, 'w') as fd:
fd.write(signature)
try:
ret = do()
except:
ret = 1
sys.exit(ret)