summaryrefslogtreecommitdiff
path: root/cache
diff options
context:
space:
mode:
authorzhengchao <[email protected]>2018-10-25 18:45:33 +0800
committerzhengchao <[email protected]>2018-10-25 18:58:26 +0800
commit27d4581d9bcdc41ef1297c3b93e1e84017542dc0 (patch)
treeccaf76641bb465f75e5c16c8c7ed285d02fe7e0d /cache
parent990e247155a767436522608d38446660ea0b6094 (diff)
支持缓存校验。
Diffstat (limited to 'cache')
-rw-r--r--cache/include/tango_cache_pending.h8
-rw-r--r--cache/src/tango_cache_pending.cpp75
-rw-r--r--cache/test/test_cache_pending.cpp20
3 files changed, 55 insertions, 48 deletions
diff --git a/cache/include/tango_cache_pending.h b/cache/include/tango_cache_pending.h
index f295d90..0c72635 100644
--- a/cache/include/tango_cache_pending.h
+++ b/cache/include/tango_cache_pending.h
@@ -7,7 +7,7 @@ enum cache_pending_action {
UNDEFINED = 0,
ALLOWED,
FORBIDDEN,
- VERIFY
+ REVALIDATE
};
@@ -30,6 +30,8 @@ struct response_freshness{
time_t timeout;
};
+
+time_t read_GMT_time(const char* gmt_string);
/*
函数功能:
根据请求头字段判断是否允许将缓存作为该请求的响应,并且将请求字段对缓存新鲜度的约束范围作为传出参数返回给调用者
@@ -41,7 +43,7 @@ restrict:如果该函数返回值为ALLOWED,则返回请求Cache-Control字段
UNDEFINED = 0,//请求字段中未定义缓存的行为
ALLOWED ,//允许使用缓存作为该请求的响应
FORBIDDEN,//禁止使用缓存作为该请求的响应,需要向源服务器请求
-VERIFY,//禁止使用未验证有效性的缓存作为该请求的响应
+REVALIDATE,//禁止使用未验证有效性的缓存作为该请求的响应
*/
enum cache_pending_action tfe_cache_get_pending(const struct tfe_http_half *request, struct request_freshness* restrict);
@@ -59,4 +61,4 @@ UNDEFINED = 0,//响应字段中未定义缓存的行为
ALLOWED ,//允许缓存该响应
FORBIDDEN,//禁止缓存该响应
*/
-enum cache_pending_action tfe_cache_put_pending(const struct tfe_http_half *response, struct response_freshness* freshness); \ No newline at end of file
+enum cache_pending_action tfe_cache_put_pending(const struct tfe_http_half *response, struct response_freshness* freshness);
diff --git a/cache/src/tango_cache_pending.cpp b/cache/src/tango_cache_pending.cpp
index da3e125..6a732f1 100644
--- a/cache/src/tango_cache_pending.cpp
+++ b/cache/src/tango_cache_pending.cpp
@@ -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;
}
diff --git a/cache/test/test_cache_pending.cpp b/cache/test/test_cache_pending.cpp
index 447937a..b21c335 100644
--- a/cache/test/test_cache_pending.cpp
+++ b/cache/test/test_cache_pending.cpp
@@ -18,10 +18,10 @@ TEST(CacheActionTest, PragmaField)
http_heads.http_field = TFE_HTTP_PRAGMA;
http_heads.value = "no-cache";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1,&restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1,&restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
- EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), VERIFY);
+ EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), REVALIDATE);
EXPECT_EQ(freshness.date, 0);
EXPECT_EQ(freshness.last_modified, 0);
EXPECT_EQ(freshness.timeout, 0);
@@ -35,10 +35,10 @@ TEST(CacheActionTest, CacheCtlNoCache)
struct response_freshness freshness;
http_heads.http_field = TFE_HTTP_CACHE_CONTROL;
http_heads.value = "no-cache";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
- EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), VERIFY);
+ EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), REVALIDATE);
EXPECT_EQ(freshness.date, 0);
EXPECT_EQ(freshness.last_modified, 0);
EXPECT_EQ(freshness.timeout, 0);
@@ -92,7 +92,7 @@ TEST(CacheActionTest, CacheCtlMustRevalidate)
struct response_freshness freshness;
http_heads.http_field = TFE_HTTP_CACHE_CONTROL;
http_heads.value = "must-revalidate";
- EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), VERIFY);
+ EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), REVALIDATE);
EXPECT_EQ(freshness.date, 0);
EXPECT_EQ(freshness.last_modified, 0);
EXPECT_EQ(freshness.timeout, 0);
@@ -103,7 +103,7 @@ TEST(CacheActionTest, CacheCtlProxyRevalidate)
struct response_freshness freshness;
http_heads.http_field = TFE_HTTP_CACHE_CONTROL;
http_heads.value = "proxy-revalidate";
- EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), VERIFY);
+ EXPECT_EQ(tfe_cache_put_pending(&http_heads, 1, &freshness), REVALIDATE);
EXPECT_EQ(freshness.date, 0);
EXPECT_EQ(freshness.last_modified, 0);
EXPECT_EQ(freshness.timeout, 0);
@@ -149,7 +149,7 @@ TEST(CacheActionTest, IfMatchRequest)
struct request_freshness restrict;
http_heads.http_field = TLF_HTTP_IF_MATCH;
http_heads.value = "50b1c1d4f775c61:df3";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
}
@@ -159,7 +159,7 @@ TEST(CacheActionTest, IfNoMatchRequest)
struct request_freshness restrict;
http_heads.http_field = TLF_HTTP_IF_NONE_MATCH;
http_heads.value = "50b1c1d4f775c61:df3";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
}
@@ -169,7 +169,7 @@ TEST(CacheActionTest, IfModifiedSinceRequest)
struct request_freshness restrict;
http_heads.http_field = TLF_HTTP_IF_MODIFIED_SINCE;
http_heads.value = "Sun, 01 Dec 2019 16:00:00 GMT";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
}
@@ -179,7 +179,7 @@ TEST(CacheActionTest, IfUnModifiedSinceRequest)
struct request_freshness restrict;
http_heads.http_field = TLF_HTTP_IF_UNMODIFIED_SINCE;
http_heads.value = "Sun, 01 Dec 2019 16:00:00 GMT";
- EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), VERIFY);
+ EXPECT_EQ(tfe_cache_get_pending(&http_heads, 1, &restrict), REVALIDATE);
EXPECT_EQ(restrict.min_fresh, 0);
EXPECT_EQ(restrict.max_age, 0);
}