support Cache-Control no-cache and max-age in cache
Igor Sysoev
13 years ago
69 | 69 | static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r, |
70 | 70 | ngx_table_elt_t *h, ngx_uint_t offset); |
71 | 71 | static ngx_int_t |
72 | ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
72 | ngx_http_upstream_process_cache_control(ngx_http_request_t *r, | |
73 | 73 | ngx_table_elt_t *h, ngx_uint_t offset); |
74 | 74 | static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, |
75 | 75 | ngx_table_elt_t *h, ngx_uint_t offset); |
187 | 187 | ngx_http_upstream_copy_header_line, 0, 1 }, |
188 | 188 | |
189 | 189 | { ngx_string("Cache-Control"), |
190 | ngx_http_upstream_process_multi_header_lines, | |
191 | offsetof(ngx_http_upstream_headers_in_t, cache_control), | |
190 | ngx_http_upstream_process_cache_control, 0, | |
192 | 191 | ngx_http_upstream_copy_multi_header_lines, |
193 | 192 | offsetof(ngx_http_headers_out_t, cache_control), 1 }, |
194 | 193 | |
2804 | 2803 | |
2805 | 2804 | |
2806 | 2805 | static ngx_int_t |
2807 | ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
2806 | ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2807 | ngx_uint_t offset) | |
2808 | { | |
2809 | return NGX_OK; | |
2810 | } | |
2811 | ||
2812 | ||
2813 | static ngx_int_t | |
2814 | ngx_http_upstream_process_cache_control(ngx_http_request_t *r, | |
2808 | 2815 | ngx_table_elt_t *h, ngx_uint_t offset) |
2809 | 2816 | { |
2817 | u_char *p, *last; | |
2818 | ngx_int_t n; | |
2810 | 2819 | ngx_array_t *pa; |
2811 | 2820 | ngx_table_elt_t **ph; |
2812 | 2821 | |
2813 | pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); | |
2822 | pa = &r->upstream->headers_in.cache_control; | |
2814 | 2823 | |
2815 | 2824 | if (pa->elts == NULL) { |
2816 | 2825 | if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) |
2826 | 2835 | |
2827 | 2836 | *ph = h; |
2828 | 2837 | |
2829 | return NGX_OK; | |
2830 | } | |
2831 | ||
2832 | ||
2833 | static ngx_int_t | |
2834 | ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2835 | ngx_uint_t offset) | |
2836 | { | |
2838 | if (r->cache == NULL) { | |
2839 | return NGX_OK; | |
2840 | } | |
2841 | ||
2842 | if (r->cache->valid_sec != 0) { | |
2843 | return NGX_OK; | |
2844 | } | |
2845 | ||
2846 | last = h->value.data + h->value.len; | |
2847 | ||
2848 | if (ngx_strlcasestrn(h->value.data, last, (u_char *) "no-cache", 8 - 1) | |
2849 | != NULL) | |
2850 | { | |
2851 | r->upstream->cacheable = 0; | |
2852 | return NGX_OK; | |
2853 | } | |
2854 | ||
2855 | p = ngx_strlcasestrn(h->value.data, last, (u_char *) "max-age=", 8 - 1); | |
2856 | ||
2857 | if (p == NULL) { | |
2858 | return NGX_OK; | |
2859 | } | |
2860 | ||
2861 | n = 0; | |
2862 | ||
2863 | for (p += 8; p < last; p++) { | |
2864 | if (*p == ';' || *p == ' ') { | |
2865 | break; | |
2866 | } | |
2867 | ||
2868 | if (*p >= '0' && *p <= '9') { | |
2869 | n = n * 10 + *p - '0'; | |
2870 | continue; | |
2871 | } | |
2872 | ||
2873 | r->upstream->cacheable = 0; | |
2874 | return NGX_OK; | |
2875 | } | |
2876 | ||
2877 | if (n == 0) { | |
2878 | r->upstream->cacheable = 0; | |
2879 | return NGX_OK; | |
2880 | } | |
2881 | ||
2882 | r->cache->valid_sec = ngx_time() + n; | |
2837 | 2883 | return NGX_OK; |
2838 | 2884 | } |
2839 | 2885 |