支持缓存校验。

This commit is contained in:
zhengchao
2018-10-25 18:45:33 +08:00
parent 990e247155
commit 27d4581d9b
7 changed files with 523 additions and 211 deletions

View File

@@ -53,7 +53,7 @@ enum cache_pending_action request_cache_control(const char* value, struct reques
int i = 0;
if (strstr(value, "no-cache") != NULL)
{
return VERIFY;
return REVALIDATE;
}
if (strstr(value, "no-store") != NULL)
{
@@ -64,7 +64,7 @@ enum cache_pending_action request_cache_control(const char* value, struct reques
}
bool cache_vertify(const struct tfe_http_half *request)
bool cache_verify(const struct tfe_http_half *request)
{
int i = 0;
if( !tfe_http_std_field_read(request,TFE_HTTP_IF_MATCH) ||
@@ -98,7 +98,7 @@ enum cache_pending_action get_pragma_action(const char * value)
const char *pragma_value = "no-cache";
if (strcasecmp(value, pragma_value) == 0)
{
return VERIFY;
return REVALIDATE;
}
return UNDEFINED;
}
@@ -106,45 +106,49 @@ enum cache_pending_action get_pragma_action(const char * value)
enum cache_pending_action tfe_cache_get_pending(const struct tfe_http_half *request, struct request_freshness* restrict)
{
enum cache_pending_action res = UNDEFINED;
int i = 0;
int index = 0;
const char *value = NULL;
memset(restrict,0,sizeof(struct request_freshness));
enum cache_pending_action res = UNDEFINED;
int i = 0;
int index = 0;
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))
{
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_vertify(request))
{
res = VERIFY;
}
}
}
return res;
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 absolute_to_relative_time(const char* gmt_time)
time_t read_GMT_time(const char* gmt_string)
{
time_t expire_rel_time;
struct tm expire_gmt_time;
strptime(gmt_time, "%a, %d %b %Y %H:%M:%S GMT", &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;
}
@@ -222,7 +226,7 @@ void get_response_freshness(const struct tfe_http_half *response, struct respons
field_value = tfe_http_std_field_read(response, TFE_HTTP_EXPIRES);
if (field_value != NULL && is_standard_gmt_format(field_value))
{
expire_rel_time = absolute_to_relative_time(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)
{
@@ -236,12 +240,12 @@ void get_response_freshness(const struct tfe_http_half *response, struct respons
if (field_value != NULL)
{
assert(is_standard_gmt_format(field_value));
freshness->date = absolute_to_relative_time(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 = absolute_to_relative_time(field_value);;
freshness->last_modified = read_GMT_time(field_value);;
}
}
@@ -262,7 +266,7 @@ enum cache_pending_action response_cache_control(const char* value)
{
if (strstr(value, verify_vaule[i]) != NULL)
{
return VERIFY;
return REVALIDATE;
}
}
return ALLOWED;
@@ -277,7 +281,8 @@ enum cache_pending_action tfe_cache_put_pending(const struct tfe_http_half *resp
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!=tfe_http_std_field_read(response, TFE_HTTP_CONT_RANGE) //NOT upload response with content-range
|| NULL==response->resp_spec.content_length)
{
return FORBIDDEN;
}