📄 cache_util.c
字号:
else { maxstale = 0; } /* extract min-fresh */ if (!conf->ignorecachecontrol && cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val) && val != NULL) { minfresh = apr_atoi64(val); } else { minfresh = 0; } /* override maxstale if must-revalidate or proxy-revalidate */ if (maxstale && ((cc_cresp && ap_cache_liststr(NULL, cc_cresp, "must-revalidate", NULL)) || (cc_cresp && ap_cache_liststr(NULL, cc_cresp, "proxy-revalidate", NULL)))) { maxstale = 0; } /* handle expiration */ if (((smaxage != -1) && (age < (smaxage - minfresh))) || ((maxage != -1) && (age < (maxage + maxstale - minfresh))) || ((smaxage == -1) && (maxage == -1) && (info->expire != APR_DATE_BAD) && (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) { const char *warn_head; warn_head = apr_table_get(h->resp_hdrs, "Warning"); /* it's fresh darlings... */ /* set age header on response */ apr_table_set(h->resp_hdrs, "Age", apr_psprintf(r->pool, "%lu", (unsigned long)age)); /* add warning if maxstale overrode freshness calculation */ if (!(((smaxage != -1) && age < smaxage) || ((maxage != -1) && age < maxage) || (info->expire != APR_DATE_BAD && (apr_time_sec(info->expire - info->date)) > age))) { /* make sure we don't stomp on a previous warning */ if ((warn_head == NULL) || ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) { apr_table_merge(h->resp_hdrs, "Warning", "110 Response is stale"); } } /* * If none of Expires, Cache-Control: max-age, or Cache-Control: * s-maxage appears in the response, and the respose header age * calculated is more than 24 hours add the warning 113 */ if ((maxage_cresp == -1) && (smaxage == -1) && (expstr == NULL) && (age > 86400)) { /* Make sure we don't stomp on a previous warning, and don't dup * a 113 marning that is already present. Also, make sure to add * the new warning to the correct *headers_out location. */ if ((warn_head == NULL) || ((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) { apr_table_merge(h->resp_hdrs, "Warning", "113 Heuristic expiration"); } } return 1; /* Cache object is fresh (enough) */ } return 0; /* Cache object is stale */}/* * list is a comma-separated list of case-insensitive tokens, with * optional whitespace around the tokens. * The return returns 1 if the token val is found in the list, or 0 * otherwise. */CACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list, const char *key, char **val){ apr_size_t key_len; const char *next; if (!list) { return 0; } key_len = strlen(key); next = list; for (;;) { /* skip whitespace and commas to find the start of the next key */ while (*next && (apr_isspace(*next) || (*next == ','))) { next++; } if (!*next) { return 0; } if (!strncasecmp(next, key, key_len)) { /* this field matches the key (though it might just be * a prefix match, so make sure the match is followed * by either a space or an equals sign) */ next += key_len; if (!*next || (*next == '=') || apr_isspace(*next) || (*next == ',')) { /* valid match */ if (val) { while (*next && (*next != '=') && (*next != ',')) { next++; } if (*next == '=') { next++; while (*next && apr_isspace(*next )) { next++; } if (!*next) { *val = NULL; } else { const char *val_start = next; while (*next && !apr_isspace(*next) && (*next != ',')) { next++; } *val = apr_pstrmemdup(p, val_start, next - val_start); } } else { *val = NULL; } } return 1; } } /* skip to the next field */ do { next++; if (!*next) { return 0; } } while (*next != ','); }}/* return each comma separated token, one at a time */CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list, const char **str){ apr_size_t i; const char *s; s = ap_strchr_c(list, ','); if (s != NULL) { i = s - list; do s++; while (apr_isspace(*s)) ; /* noop */ } else i = strlen(list); while (i > 0 && apr_isspace(list[i - 1])) i--; *str = s; if (i) return apr_pstrndup(p, list, i); else return NULL;}/* * Converts apr_time_t expressed as hex digits to * a true apr_time_t. */CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x){ int i, ch; apr_time_t j; for (i = 0, j = 0; i < sizeof(j) * 2; i++) { ch = x[i]; j <<= 4; if (apr_isdigit(ch)) j |= ch - '0'; else if (apr_isupper(ch)) j |= ch - ('A' - 10); else j |= ch - ('a' - 10); } return j;}/* * Converts apr_time_t to apr_time_t expressed as hex digits. */CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y){ int i, ch; for (i = (sizeof(j) * 2)-1; i >= 0; i--) { ch = (int)(j & 0xF); j >>= 4; if (ch >= 10) y[i] = ch + ('A' - 10); else y[i] = ch + '0'; } y[sizeof(j) * 2] = '\0';}static void cache_hash(const char *it, char *val, int ndepth, int nlength){ apr_md5_ctx_t context; unsigned char digest[16]; char tmp[22]; int i, k, d; unsigned int x; static const char enc_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@"; apr_md5_init(&context); apr_md5_update(&context, (const unsigned char *) it, strlen(it)); apr_md5_final(digest, &context); /* encode 128 bits as 22 characters, using a modified uuencoding * the encoding is 3 bytes -> 4 characters* i.e. 128 bits is * 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters */ for (i = 0, k = 0; i < 15; i += 3) { x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2]; tmp[k++] = enc_table[x >> 18]; tmp[k++] = enc_table[(x >> 12) & 0x3f]; tmp[k++] = enc_table[(x >> 6) & 0x3f]; tmp[k++] = enc_table[x & 0x3f]; } /* one byte left */ x = digest[15]; tmp[k++] = enc_table[x >> 2]; /* use up 6 bits */ tmp[k++] = enc_table[(x << 4) & 0x3f]; /* now split into directory levels */ for (i = k = d = 0; d < ndepth; ++d) { memcpy(&val[i], &tmp[k], nlength); k += nlength; val[i + nlength] = '/'; i += nlength + 1; } memcpy(&val[i], &tmp[k], 22 - k); val[i + 22 - k] = '\0';}CACHE_DECLARE(char *)ap_cache_generate_name(apr_pool_t *p, int dirlevels, int dirlength, const char *name){ char hashfile[66]; cache_hash(name, hashfile, dirlevels, dirlength); return apr_pstrdup(p, hashfile);}/* Create a new table consisting of those elements from an input * headers table that are allowed to be stored in a cache. */CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *pool, apr_table_t *t, server_rec *s){ cache_server_conf *conf; char **header; int i; /* Make a copy of the headers, and remove from * the copy any hop-by-hop headers, as defined in Section * 13.5.1 of RFC 2616 */ apr_table_t *headers_out; headers_out = apr_table_copy(pool, t); apr_table_unset(headers_out, "Connection"); apr_table_unset(headers_out, "Keep-Alive"); apr_table_unset(headers_out, "Proxy-Authenticate"); apr_table_unset(headers_out, "Proxy-Authorization"); apr_table_unset(headers_out, "TE"); apr_table_unset(headers_out, "Trailers"); apr_table_unset(headers_out, "Transfer-Encoding"); apr_table_unset(headers_out, "Upgrade"); conf = (cache_server_conf *)ap_get_module_config(s->module_config, &cache_module); /* Remove the user defined headers set with CacheIgnoreHeaders. * This may break RFC 2616 compliance on behalf of the administrator. */ header = (char **)conf->ignore_headers->elts; for (i = 0; i < conf->ignore_headers->nelts; i++) { apr_table_unset(headers_out, header[i]); } return headers_out;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -