修复cachekey生成过程中解析cookie的bug。

This commit is contained in:
zhengchao
2018-11-11 17:14:45 +08:00
parent 3e01a3474d
commit c92c41523c
2 changed files with 52 additions and 32 deletions

View File

@@ -342,33 +342,50 @@ char is_dynamic_url(const char* url)
}
return 0;
}
char * cookie_scanvalue(const char * key, const char * qs, char * val, size_t val_len)
char * cookie_scanvalue(const char * key, const char * cookies, char * val, size_t val_len)
{
int i=0, key_len=0;
int i=0, j=0, k=0, key_len=0;
int found=1;
char* key_dup=ALLOC(char, strlen(key)+2);
char* cookie_dup=ALLOC(char, strlen(cookies)+1);
strcat(key_dup, key);
if(key_dup[strlen(key)]!='=')
{
strcat(key_dup, "=");
}
for(i=0; i<strlen(cookies); i++)
{
if(cookies[i]==' '||cookies[i]=='\t'||cookies[i]=='\r'||cookies[i]=='\n')
{
continue;
}
cookie_dup[j]=cookies[i];
j++;
}
char *token=NULL,*sub_token=NULL,*saveptr;
key_len = strlen(key);
while(qs[0] != '\0')
{
if ( strncmp(key, qs, key_len) == 0 )
break;
qs += strcspn(qs, ";") + 1;
}
if ( qs[0] == '\0' ) return NULL;
qs += strcspn(qs, "=;");
if ( qs[0] == '=' )
{
qs++;
i = strcspn(qs, "=;");
strncpy(val, qs, (val_len-1)<(i+1) ? (val_len-1) : (i+1));
}
else
{
if ( val_len > 0 )
val[0] = '\0';
}
for (token = cookie_dup; ; token= NULL)
{
sub_token= strtok_r(token,";", &saveptr);
if (sub_token == NULL)
break;
if(0==strncasecmp(sub_token, key_dup, strlen(key_dup)))
{
found=1;
sub_token+=strlen(key_dup);
k = strcspn(sub_token, ";");
strncpy(val, sub_token, MIN(val_len-1, k+1));
break;
}
}
if(!found &&val_len > 0)
{
val[0] = '\0';
}
FREE(&key_dup);
FREE(&cookie_dup);
return val;
}
@@ -479,7 +496,7 @@ void cache_param_new(int idx, const struct Maat_rule_t* rule, const char* srv_de
}
}
item=cJSON_GetObjectItem(key_desc,"cookie");
if(item && item->type==cJSON_String) param->key_descr.include_cookie=tfe_strdup(param->key_descr.include_cookie);
if(item && item->type==cJSON_String) param->key_descr.include_cookie=tfe_strdup(item->valuestring);
}
@@ -758,6 +775,7 @@ static void cache_query_obj_on_succ(future_result_t * result, void * user)
{
struct promise * p = (struct promise *) user;
struct cache_query_context* ctx=(struct cache_query_context*)promise_get_ctx(p);
int last_call=0;
ctx->ref_tango_cache_result=tango_cache_read_result(result);
switch(ctx->ref_tango_cache_result->type)
{
@@ -778,7 +796,7 @@ static void cache_query_obj_on_succ(future_result_t * result, void * user)
//last call.
ATOMIC_DEC(&(ctx->ref_handle->stat_val[STAT_CACHE_QUERYING]));
promise_dettach_ctx(p);
cache_query_ctx_free_cb(ctx);
last_call=1;
break;
case RESULT_TYPE_BODY:
ATOMIC_ADD(&(ctx->ref_handle->stat_val[STAT_CACHE_QUERY_BYTES]), ctx->ref_tango_cache_result->size);
@@ -787,6 +805,7 @@ static void cache_query_obj_on_succ(future_result_t * result, void * user)
break;
}
promise_success(p, ctx);
if(last_call) cache_query_ctx_free_cb(ctx);
return;
}
@@ -956,6 +975,8 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
if(ret>0)
{
TFE_LOG_DEBUG(handle->logger, "cache policy %d matched: %s", cache_policy.config_id, request->req_spec.url);
ex_data=Maat_rule_get_ex_data(handle->ref_feather, &cache_policy, handle->cache_param_idx);
if(ex_data!=NULL)
{
@@ -963,15 +984,15 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
_mid->is_using_exception_param=1;
_mid->param=param;
}
if(cache_policy.action==CACHE_ACTION_BYPASS)
if((unsigned char)cache_policy.action==CACHE_ACTION_BYPASS)
{
_mid->shall_bypass=1;
}
_mid->cfg_id=cache_policy.config_id;
}
if(_mid->shall_bypass ||
(!param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) ||
(param->cache_cookied_cont && _mid->has_cookie) )
if(_mid->shall_bypass ||
(!param->force_caching && !param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) ||
(!param->force_caching && param->cache_cookied_cont && _mid->has_cookie))
{
_mid->result=PENDING_RESULT_FOBIDDEN;
return _mid->result;
@@ -1169,7 +1190,6 @@ struct cache_update_context* web_cache_update_start(struct cache_handle* handle,
|| (!param->cache_cookied_cont && _mid->has_cookie) )
{
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_UPLOAD_BYPASS]));
TFE_LOG_DEBUG(handle->logger, "cache update bypass: %d : %s", _mid->cfg_id, session->req->req_spec.url);
return NULL;
}
break;