📄 mod_cache.c
字号:
* In addition, we make HTTP/1.1 age calculations and write them away * too. */ /* Read the date. Generate one if one is not supplied */ dates = apr_table_get(r->err_headers_out, "Date"); if (dates == NULL) { dates = apr_table_get(r->headers_out, "Date"); } if (dates != NULL) { info->date = apr_date_parse_http(dates); } else { info->date = APR_DATE_BAD; } now = apr_time_now(); if (info->date == APR_DATE_BAD) { /* No, or bad date */ /* no date header (or bad header)! */ info->date = now; } date = info->date; /* set response_time for HTTP/1.1 age calculations */ info->response_time = now; /* get the request time */ info->request_time = r->request_time; /* check last-modified date */ if (lastmod != APR_DATE_BAD && lastmod > date) { /* if it's in the future, then replace by date */ lastmod = date; lastmods = dates; ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "cache: Last modified is in the future, " "replacing with now"); } /* if no expiry date then * if Cache-Control: max-age present * expiry date = date + max-age * else if lastmod * expiry date = date + min((date - lastmod) * factor, maxexpire) * else * expire date = date + defaultexpire */ if (exp == APR_DATE_BAD) { char *max_age_val; if (ap_cache_liststr(r->pool, cc_out, "max-age", &max_age_val) && max_age_val != NULL) { apr_int64_t x; errno = 0; x = apr_atoi64(max_age_val); if (errno) { x = conf->defex; } else { x = x * MSEC_ONE_SEC; } if (x > conf->maxex) { x = conf->maxex; } exp = date + x; } else if ((lastmod != APR_DATE_BAD) && (lastmod < date)) { /* if lastmod == date then you get 0*conf->factor which results in * an expiration time of now. This causes some problems with * freshness calculations, so we choose the else path... */ apr_time_t x = (apr_time_t) ((date - lastmod) * conf->factor); if (x > conf->maxex) { x = conf->maxex; } exp = date + x; } else { exp = date + conf->defex; } } info->expire = exp; /* We found a stale entry which wasn't really stale. */ if (cache->stale_handle) { /* Load in the saved status and clear the status line. */ r->status = info->status; r->status_line = NULL; /* RFC 2616 10.3.5 states that entity headers are not supposed * to be in the 304 response. Therefore, we need to combine the * response headers with the cached headers *before* we update * the cached headers. * * However, before doing that, we need to first merge in * err_headers_out and we also need to strip any hop-by-hop * headers that might have snuck in. */ r->headers_out = apr_table_overlay(r->pool, r->headers_out, r->err_headers_out); r->headers_out = ap_cache_cacheable_hdrs_out(r->pool, r->headers_out, r->server); apr_table_clear(r->err_headers_out); /* Merge in our cached headers. However, keep any updated values. */ ap_cache_accept_headers(cache->handle, r, 1); } /* Write away header information to cache. It is possible that we are * trying to update headers for an entity which has already been cached. * * This may fail, due to an unwritable cache area. E.g. filesystem full, * permissions problems or a read-only (re)mount. This must be handled * later. */ rv = cache->provider->store_headers(cache->handle, r, info); /* Did we just update the cached headers on a revalidated response? * * If so, we can now decide what to serve to the client. This is done in * the same way as with a regular response, but conditions are now checked * against the cached or merged response headers. */ if (cache->stale_handle) { apr_bucket_brigade *bb; apr_bucket *bkt; int status; bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); /* Restore the original request headers and see if we need to * return anything else than the cached response (ie. the original * request was conditional). */ r->headers_in = cache->stale_headers; status = ap_meets_conditions(r); if (status != OK) { r->status = status; bkt = apr_bucket_flush_create(bb->bucket_alloc); APR_BRIGADE_INSERT_TAIL(bb, bkt); } else { cache->provider->recall_body(cache->handle, r->pool, bb); } cache->block_response = 1; /* Before returning we need to handle the possible case of an * unwritable cache. Rather than leaving the entity in the cache * and having it constantly re-validated, now that we have recalled * the body it is safe to try and remove the url from the cache. */ if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server, "cache: updating headers with store_headers failed. " "Removing cached url."); rv = cache->provider->remove_url(cache->stale_handle, r->pool); if (rv != OK) { /* Probably a mod_disk_cache cache area has been (re)mounted * read-only, or that there is a permissions problem. */ ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server, "cache: attempt to remove url from cache unsuccessful."); } } return ap_pass_brigade(f->next, bb); } if(rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server, "cache: store_headers failed"); ap_remove_output_filter(f); return ap_pass_brigade(f->next, in); } rv = cache->provider->store_body(cache->handle, r, in); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server, "cache: store_body failed"); ap_remove_output_filter(f); } return ap_pass_brigade(f->next, in);}/* * CACHE_REMOVE_URL filter * --------------- * * This filter gets added in the quick handler every time the CACHE_SAVE filter * gets inserted. Its purpose is to remove a confirmed stale cache entry from * the cache. * * CACHE_REMOVE_URL has to be a protocol filter to ensure that is run even if * the response is a canned error message, which removes the content filters * and thus the CACHE_SAVE filter from the chain. * * CACHE_REMOVE_URL expects cache request rec within its context because the * request this filter runs on can be different from the one whose cache entry * should be removed, due to internal redirects. * * Note that CACHE_SAVE_URL (as a content-set filter, hence run before the * protocol filters) will remove this filter if it decides to cache the file. * Therefore, if this filter is left in, it must mean we need to toss any * existing files. */static int cache_remove_url_filter(ap_filter_t *f, apr_bucket_brigade *in){ request_rec *r = f->r; cache_request_rec *cache; /* Setup cache_request_rec */ cache = (cache_request_rec *) f->ctx; if (!cache) { /* user likely configured CACHE_REMOVE_URL manually; they should really * use mod_cache configuration to do that. So: * 1. Remove ourselves * 2. Do nothing and bail out */ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "cache: CACHE_REMOVE_URL enabled unexpectedly"); ap_remove_output_filter(f); return ap_pass_brigade(f->next, in); } /* Now remove this cache entry from the cache */ cache_remove_url(cache, r->pool); /* remove ourselves */ ap_remove_output_filter(f); return ap_pass_brigade(f->next, in);}/* -------------------------------------------------------------- *//* Setup configurable data */static void * create_cache_config(apr_pool_t *p, server_rec *s){ cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf)); /* array of URL prefixes for which caching is enabled */ ps->cacheenable = apr_array_make(p, 10, sizeof(struct cache_enable)); /* array of URL prefixes for which caching is disabled */ ps->cachedisable = apr_array_make(p, 10, sizeof(struct cache_disable)); /* maximum time to cache a document */ ps->maxex = DEFAULT_CACHE_MAXEXPIRE; ps->maxex_set = 0; /* default time to cache a document */ ps->defex = DEFAULT_CACHE_EXPIRE; ps->defex_set = 0; /* factor used to estimate Expires date from LastModified date */ ps->factor = DEFAULT_CACHE_LMFACTOR; ps->factor_set = 0; ps->no_last_mod_ignore_set = 0; ps->no_last_mod_ignore = 0; ps->ignorecachecontrol = 0; ps->ignorecachecontrol_set = 0; ps->store_private = 0; ps->store_private_set = 0; ps->store_nostore = 0; ps->store_nostore_set = 0; /* array of headers that should not be stored in cache */ ps->ignore_headers = apr_array_make(p, 10, sizeof(char *)); ps->ignore_headers_set = CACHE_IGNORE_HEADERS_UNSET; /* flag indicating that query-string should be ignored when caching */ ps->ignorequerystring = 0; ps->ignorequerystring_set = 0; return ps;}static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv){ cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf)); cache_server_conf *base = (cache_server_conf *) basev; cache_server_conf *overrides = (cache_server_conf *) overridesv; /* array of URL prefixes for which caching is disabled */ ps->cachedisable = apr_array_append(p, base->cachedisable, overrides->cachedisable); /* array of URL prefixes for which caching is enabled */ ps->cacheenable = apr_array_append(p, base->cacheenable, overrides->cacheenable); /* maximum time to cache a document */ ps->maxex = (overrides->maxex_set == 0) ? base->maxex : overrides->maxex; /* default time to cache a document */ ps->defex = (overrides->defex_set == 0) ? base->defex : overrides->defex; /* factor used to estimate Expires date from LastModified date */ ps->factor = (overrides->factor_set == 0) ? base->factor : overrides->factor; ps->no_last_mod_ignore = (overrides->no_last_mod_ignore_set == 0) ? base->no_last_mod_ignore : overrides->no_last_mod_ignore; ps->ignorecachecontrol = (overrides->ignorecachecontrol_set == 0) ? base->ignorecachecontrol : overrides->ignorecachecontrol; ps->store_private = (overrides->store_private_set == 0) ? base->store_private : overrides->store_private; ps->store_nostore = (overrides->store_nostore_set == 0) ? base->store_nostore : overrides->store_nostore; ps->ignore_headers = (overrides->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) ? base->ignore_headers : overrides->ignore_headers; ps->ignorequerystring = (overrides->ignorequerystring_set == 0) ? base->ignorequerystring : overrides->ignorequerystring; return ps;}static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy, int flag)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -