-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional.py
More file actions
105 lines (85 loc) · 2.06 KB
/
Copy pathconditional.py
File metadata and controls
105 lines (85 loc) · 2.06 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
from datetime import datetime
from wsgiref.handlers import format_date_time
from dateutil.parser import parse
import os
import e_tag, imp_func
def valid_date(date_text):
try:
datetime.strptime(date_text, "%a, %d %b %Y %H:%M:%S GMT")
c=True
except:
c=False
return c
def check_modified(method, if_dict, last_modified):
if method in {'GET','HEAD'}:
sc=200
print("modified")
c_date=if_dict["if-modified-since"]
if valid_date(c_date):
s_Date=parse(last_modified)
c_date=parse(c_date)
if s_Date > c_date:
sc=200
else:
sc=304
return sc
def check_unmodified(if_dict, last_modified, etag, method):
sc=200
print("unmodified")
c_date=if_dict["if-unmodified-since"]
if valid_date(c_date):
s_Date=parse(last_modified)
c_date=parse(c_date)
if s_Date > c_date:
sc=412
if sc=="200":
if "if-none-match" in if_dict:
sc=check_none_match(if_dict, etag, method)
return sc
def check_match(if_dict, etag, method):
print("match")
c_tags=if_dict["if-match"].split(',')
for tag in c_tags:
tag=tag.strip()
if etag==tag:
sc=200
if "if-none-match" in if_dict:
sc=check_none_match(if_dict, etag, method)
break
else:
sc=412
return sc
def check_none_match(if_dict, etag, method):
sc=200
print("none-match")
c_tags=if_dict["if-none-match"].split(',')
for tag in c_tags:
tag=tag.strip()
if etag==tag:
if method in {'GET','HEAD'}:
sc=304
else:
sc=412
break
return sc
def get_if_dict(req):
#print(req)
if_dict={}
for tup in req:
if tup[0].startswith("if"):
if_dict[tup[0]]=tup[1]
return if_dict
def check_conditional_requests(req, content):
method=req[0][0]
last_modified = str(format_date_time(os.stat(content).st_mtime))
etag = e_tag.gen_etag(content)
if_dict=get_if_dict(req)
sc=200
if "if-match" in if_dict:
sc=check_match(if_dict, etag, method)
elif "if-unmodified-since" in if_dict:
sc=check_unmodified(if_dict, last_modified, etag, method)
elif "if-none-match" not in if_dict:
if "if-modified-since" in if_dict:
sc=check_modified(method,if_dict,last_modified)
return sc