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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
#include "tango_cache_pending.h"
#include <tfe_utils.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
time_t get_time_value(const char* field_value, const char* field_type)
{
time_t time;
char* time_value = NULL;
field_value += strlen(field_type);
field_value++;
int len = strlen(field_value);
time_value = ALLOC(char, len+1);
int index = 0;
while (field_value[index] != ',' && field_value[index] != '\r' && index < len)
{
time_value[index] = field_value[index];
index++;
}
time_value[index] = '\0';
time = (time_t)atol(time_value);
free(time_value);
return time;
}
void get_request_freshness(const char *value, struct request_freshness* restrict)
{
const char* field_value = NULL;
field_value = strstr(value, "min-fresh");
if (field_value != NULL)
{
restrict->min_fresh = get_time_value(field_value, "min-fresh");;
}
field_value = strstr(value, "max-age");
if (field_value != NULL)
{
restrict->max_age = get_time_value(field_value, "max-age");;
}
}
enum cache_pending_action request_cache_control(const char* value, struct request_freshness* restrict)
{
if (strstr(value, "no-cache") != NULL)
{
return REVALIDATE;
}
if (strstr(value, "no-store") != NULL)
{
return FORBIDDEN;
}
get_request_freshness(value, restrict);
return ALLOWED;
}
bool cache_verify(const struct tfe_http_half *request)
{
if( !tfe_http_std_field_read(request,TFE_HTTP_IF_MATCH) ||
!tfe_http_std_field_read(request,TFE_HTTP_IF_NONE_MATCH) ||
!tfe_http_std_field_read(request,TFE_HTTP_IF_MODIFIED_SINCE) ||
!tfe_http_std_field_read(request,TFE_HTTP_IF_UNMODIFIED_SINCE)
)
{
return true;
}
return false;
}
const char* get_head_value(const struct tfe_http_field *http_fields, size_t n_fields, enum tfe_http_std_field head_key)
{
size_t i = 0;
for (i = 0; i < n_fields; i++)
{
if (http_fields[i].http_field == head_key)
{
return http_fields[i].value;
}
}
return NULL;
}
enum cache_pending_action get_pragma_action(const char * value)
{
const char *pragma_value = "no-cache";
if (strcasecmp(value, pragma_value) == 0)
{
return REVALIDATE;
}
return UNDEFINED;
}
enum cache_pending_action tfe_cache_get_pending(const struct tfe_http_half *request, struct request_freshness* restrict)
{
enum cache_pending_action res = UNDEFINED;
const char *value = NULL;
memset(restrict,0,sizeof(struct request_freshness));
if(request->req_spec.method!=TFE_HTTP_METHOD_GET)
{
return FORBIDDEN;
}
if(NULL!=tfe_http_std_field_read(request, TFE_HTTP_CONT_RANGE) ||
NULL!=tfe_http_std_field_read(request, TFE_HTTP_AUTHORIZATION)||
NULL!=tfe_http_nonstd_field_read(request, "WWW-Authenticate"))
{
return FORBIDDEN;
}
value = tfe_http_std_field_read(request, TFE_HTTP_PRAGMA);
if (value != NULL)
{
res = get_pragma_action(value);
}
else
{
value = tfe_http_std_field_read(request, TFE_HTTP_CACHE_CONTROL);
if (value != NULL)
{
res = request_cache_control(value, restrict);
}
else
{
if (cache_verify(request))
{
res = REVALIDATE;
}
}
}
return res;
}
time_t read_GMT_time(const char* gmt_string)
{
time_t expire_rel_time;
struct tm expire_gmt_time;
memset(&expire_gmt_time, 0, sizeof(expire_gmt_time));
strptime(gmt_string, "%a, %d %b %Y %H:%M:%S GMT", &expire_gmt_time);
expire_rel_time = mktime(&expire_gmt_time);
return expire_rel_time;
}
bool is_standard_gmt_format(const char* value)
{
int str_len = strlen(value);
if(0==strcasecmp(value+str_len-3,"GMT"))
{
return true;
}
else
{
return false;
}
}
time_t get_response_s_maxage(const char* cache_ctl)
{
const char* s_maxage = NULL;
s_maxage = strstr(cache_ctl, "s-maxage");
if (s_maxage != NULL)
{
return get_time_value(s_maxage, "s-maxage");
}
else
{
return 0;
}
}
time_t get_response_maxage(const char* cache_ctl)
{
const char* max_age = NULL;
max_age = strstr(cache_ctl, "max-age");
if (max_age != NULL)
{
return get_time_value(max_age, "max-age");
}
else
{
return 0;
}
}
void get_response_freshness(const struct tfe_http_half *response, struct response_freshness* freshness)
{
time_t expire_rel_time = 0;
time_t cur_rel_time = 0;
struct tm cur_gmt_time;
const char* field_value = NULL;
field_value = tfe_http_std_field_read(response, TFE_HTTP_CACHE_CONTROL);
if (field_value != NULL)
{
freshness->timeout = get_response_s_maxage(field_value);
if (freshness->timeout == 0)
{
freshness->timeout = get_response_maxage(field_value);
}
}
else
{
field_value = tfe_http_std_field_read(response, TFE_HTTP_EXPIRES);
if (field_value != NULL && is_standard_gmt_format(field_value))
{
expire_rel_time = read_GMT_time(field_value);
const time_t cur_ct_time = time(NULL);
if (gmtime_r(&cur_ct_time, &cur_gmt_time) == NULL)
{
assert(0);
}
cur_rel_time = mktime(&cur_gmt_time);
freshness->timeout = expire_rel_time - cur_rel_time;
}
}
field_value = tfe_http_std_field_read(response, TFE_HTTP_DATE);
if (field_value != NULL)
{
if(is_standard_gmt_format(field_value))
{
freshness->date = read_GMT_time(field_value);;
}
}
field_value = tfe_http_std_field_read(response, TFE_HTTP_LAST_MODIFIED);
if (field_value != NULL && is_standard_gmt_format(field_value))
{
freshness->last_modified = read_GMT_time(field_value);;
}
}
enum cache_pending_action response_cache_control(const char* value)
{
const char *forbidden_vaule[] = {"no-store", "private"};
const char *verify_vaule[] = { "no-cache", "must-revalidate","proxy-revalidate" };
int i = 0;
for (i = 0; i < 2; i++)
{
if (strstr(value, forbidden_vaule[i]) != NULL)
{
return FORBIDDEN;
}
}
for (i = 0; i < 3; i++)
{
if (strstr(value, verify_vaule[i]) != NULL)
{
return REVALIDATE;
}
}
return ALLOWED;
}
enum cache_pending_action tfe_cache_put_pending(const struct tfe_http_half *response, struct response_freshness* freshness)
{
enum cache_pending_action res = UNDEFINED;
const char *value = NULL;
memset(freshness,0,sizeof(struct response_freshness));
if(response->resp_spec.resp_code!=TFE_HTTP_STATUS_OK
|| NULL!=tfe_http_std_field_read(response, TFE_HTTP_CONT_RANGE) //NOT upload response with content-range
|| NULL==response->resp_spec.content_length
|| NULL!=tfe_http_std_field_read(response, TFE_HTTP_AUTHORIZATION)
|| NULL!=tfe_http_nonstd_field_read(response, "WWW-Authenticate")
|| NULL!=tfe_http_std_field_read(response, TFE_HTTP_SET_COOKIE))
{
return FORBIDDEN;
}
value = tfe_http_std_field_read(response, TFE_HTTP_PRAGMA);
if (value != NULL)
{
res = get_pragma_action(value);
}
else
{
value = tfe_http_std_field_read(response, TFE_HTTP_CACHE_CONTROL);
if (value != NULL)
{
res = response_cache_control(value);
}
else
{
value = tfe_http_std_field_read(response, TFE_HTTP_EXPIRES);
if (value != NULL && 0!= read_GMT_time(value))
{
res = ALLOWED;
}
}
}
if (res == ALLOWED)
{
get_response_freshness(response, freshness);
}
return res;
}
|