diff --git a/plugin/business/pangu-http/pangu_http.cpp b/plugin/business/pangu-http/pangu_http.cpp index abf65fd..9fcf796 100644 --- a/plugin/business/pangu-http/pangu_http.cpp +++ b/plugin/business/pangu-http/pangu_http.cpp @@ -446,27 +446,38 @@ static void cache_query_on_succ(future_result_t * result, void * user) { struct pangu_http_ctx * ctx = (struct pangu_http_ctx *)user; struct cached_meta* meta=NULL; - if(result==NULL) + enum cache_query_result_type type=cache_query_result_get_type(result); + switch(type) { - assert(ctx->cached_body!=NULL); - ctx->cache_query_status=WEB_CACHE_HIT; - future_destroy(ctx->f_cache_query); - ctx->f_cache_query=NULL; - tfe_http_session_resume(ctx->ref_session); - ctx->resume_from_cache_query=1; - printf("cache query success: %s\n", ctx->ref_session->req->req_spec.url); - return; - } - if(cache_query_result_is_header(result)) - { - ctx->cached_header=cache_query_result_get_header(result); - ctx->cached_body=evbuffer_new(); - } - if(cache_query_result_is_body(result)) - { - cache_query_result_append_data(ctx->cached_body, result); + case CACHE_QUERY_RESULT_MISS: + ctx->cache_query_status=WEB_CACHE_NOT_HIT; + printf("cache query miss: %s\n", ctx->ref_session->req->req_spec.url); + goto __cleanup; + break; + case CACHE_QUERY_RESULT_END: + assert(ctx->cached_body!=NULL); + ctx->cache_query_status=WEB_CACHE_HIT; + printf("cache query hit: %s\n", ctx->ref_session->req->req_spec.url); + goto __cleanup; + break; + case CACHE_QUERY_RESULT_META: + ctx->cached_header=cache_query_result_get_header(result); + ctx->cached_body=evbuffer_new(); + break; + case CACHE_QUERY_RESULT_DATA: + cache_query_result_append_data(ctx->cached_body, result);; + break; + default: + assert(0); } + return; +__cleanup: + future_destroy(ctx->f_cache_query); + ctx->f_cache_query=NULL; + tfe_http_session_resume(ctx->ref_session); + ctx->resume_from_cache_query=1; + return; } static void cache_query_on_fail(enum e_future_error err, const char * what, void * user) { @@ -476,7 +487,7 @@ static void cache_query_on_fail(enum e_future_error err, const char * what, void tfe_http_session_resume(ctx->ref_session); ctx->cache_query_status=WEB_CACHE_NOT_HIT; ctx->resume_from_cache_query=1; - printf("cache query failed: %s\n", ctx->ref_session->req->req_spec.url); + printf("cache query failed: %s %s\n", ctx->ref_session->req->req_spec.url, what); } void http_replace(const struct tfe_stream * stream, const struct tfe_http_session * session, enum tfe_http_event events, const unsigned char * body_frag, size_t frag_size, struct pangu_http_ctx * ctx) @@ -961,7 +972,8 @@ void pangu_on_http_data(const struct tfe_stream * stream, const struct tfe_http_ { //resume from cache query. assert(ctx->action==PG_ACTION_NONE); - cache_make_response(session, ctx); + cache_make_response(session, ctx); + tfe_http_session_detach(session); return; } if(!ctx->resume_from_cache_query) diff --git a/plugin/business/pangu-http/pangu_web_cache.cpp b/plugin/business/pangu-http/pangu_web_cache.cpp index ab55095..8164e87 100644 --- a/plugin/business/pangu-http/pangu_web_cache.cpp +++ b/plugin/business/pangu-http/pangu_web_cache.cpp @@ -66,31 +66,20 @@ void cache_query_free_meta(struct cached_meta* meta) FREE(&meta); return; } -int cache_query_result_is_header(future_result_t * result) + +enum cache_query_result_type cache_query_result_get_type(future_result_t * result) { struct tango_cache_result* cache_result=tango_cache_read_result(result); - if(cache_result->type==RESULT_TYPE_HEADER) - { - return 1; - } - else - { - return 0; - } -} -int cache_query_result_is_body(future_result_t * result) -{ - struct tango_cache_result* cache_result=tango_cache_read_result(result); - if(cache_result->type==RESULT_TYPE_BODY) - { - return 1; - } - else - { - return 0; - } + enum cache_query_result_type map[__CACHE_QUERY_RESULT_MAX]; + map[RESULT_TYPE_BODY]=CACHE_QUERY_RESULT_DATA; + map[RESULT_TYPE_HEADER]=CACHE_QUERY_RESULT_META; + map[RESULT_TYPE_USERTAG]=CACHE_QUERY_RESULT_IRRELEVANT; + map[RESULT_TYPE_END]=CACHE_QUERY_RESULT_END; + map[RESULT_TYPE_MISS]=CACHE_QUERY_RESULT_MISS; + return map[cache_result->type]; } + struct cached_meta* cache_query_result_get_header(future_result_t * result) { struct tango_cache_result* cache_result=tango_cache_read_result(result); @@ -132,10 +121,10 @@ enum cache_query_status async_web_cache_query(struct cache_handle* handle, unsig return WEB_CACHE_NOT_APPLICABLE; } - struct tango_cache_meta meta; + struct tango_cache_meta_get meta; memset(&meta, 0, sizeof(meta)); meta.url=request->req_spec.url; - memcpy(&(meta.put), &req_fresshness, sizeof(meta.put)); + memcpy(&(meta.get), &req_fresshness, sizeof(meta.get)); ret=tango_cache_fetch_object(handle->clients[thread_id], f, &meta); assert(ret==0); return WEB_CACHE_QUERING; @@ -157,7 +146,7 @@ struct cache_update_context* web_cache_update_start(struct cache_handle* handle, return NULL; } - struct tango_cache_meta meta; + struct tango_cache_meta_put meta; memset(&meta, 0, sizeof(meta)); meta.url=session->req->req_spec.url; i=0; diff --git a/plugin/business/pangu-http/pangu_web_cache.h b/plugin/business/pangu-http/pangu_web_cache.h index 1f13f15..350f52b 100644 --- a/plugin/business/pangu-http/pangu_web_cache.h +++ b/plugin/business/pangu-http/pangu_web_cache.h @@ -24,9 +24,17 @@ void cache_query_result_append_data(struct evbuffer* buf, future_result_t * resu enum cache_query_status async_web_cache_query(struct cache_handle* handle, unsigned int thread_id, const struct tfe_http_half * request, struct future* f); +enum cache_query_result_type +{ + CACHE_QUERY_RESULT_MISS, + CACHE_QUERY_RESULT_META, + CACHE_QUERY_RESULT_DATA, + CACHE_QUERY_RESULT_END, + CACHE_QUERY_RESULT_IRRELEVANT, + __CACHE_QUERY_RESULT_MAX +}; +enum cache_query_result_type cache_query_result_get_type(future_result_t * result); -int cache_query_result_is_header(future_result_t * result); -int cache_query_result_is_body(future_result_t * result); struct cache_update_context;