Skip to content

Commit 1df1c56

Browse files
committed
feature: HTTP conditional GET requests are now supported (both If-Modified-Since and If-Unmodified-Since request headers are properly handled). thanks Nginx_User777.
1 parent e124488 commit 1df1c56

File tree

3 files changed

+408
-6
lines changed

3 files changed

+408
-6
lines changed

src/ngx_http_srcache_fetch.c

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
static ngx_int_t ngx_http_srcache_fetch_subrequest(ngx_http_request_t *r,
1212
ngx_http_srcache_loc_conf_t *conf, ngx_http_srcache_ctx_t *ctx);
1313

14+
static ngx_int_t ngx_http_srcache_fetch_header_filter(ngx_http_request_t *r);
15+
16+
static ngx_int_t ngx_http_srcache_test_not_modified(ngx_http_request_t *r);
17+
18+
static ngx_int_t ngx_http_srcache_test_precondition(ngx_http_request_t *r);
19+
1420

1521
ngx_int_t
1622
ngx_http_srcache_access_handler(ngx_http_request_t *r)
@@ -144,15 +150,20 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
144150

145151
r->headers_out.content_length_n = len;
146152

147-
rc = ngx_http_srcache_next_header_filter(r);
153+
rc = ngx_http_srcache_fetch_header_filter(r);
148154

149-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
155+
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
150156
return rc;
151157
}
152158

153159
rc = ngx_http_srcache_next_body_filter(r, ctx->body_from_cache);
154160

155-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
161+
if (rc == NGX_ERROR) {
162+
r->connection->error = 1;
163+
return NGX_ERROR;
164+
}
165+
166+
if (rc > NGX_OK) {
156167
return rc;
157168
}
158169

@@ -161,9 +172,9 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
161172
} else {
162173
r->headers_out.content_length_n = 0;
163174

164-
rc = ngx_http_srcache_next_header_filter(r);
175+
rc = ngx_http_srcache_fetch_header_filter(r);
165176

166-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
177+
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
167178
return rc;
168179
}
169180

@@ -178,7 +189,12 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
178189

179190
rc = ngx_http_srcache_next_body_filter(r, cl);
180191

181-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
192+
if (rc == NGX_ERROR) {
193+
r->connection->error = 1;
194+
return NGX_ERROR;
195+
}
196+
197+
if (rc > NGX_OK) {
182198
return rc;
183199
}
184200

@@ -393,5 +409,85 @@ ngx_http_srcache_fetch_subrequest(ngx_http_request_t *r,
393409
}
394410

395411

412+
static ngx_int_t
413+
ngx_http_srcache_fetch_header_filter(ngx_http_request_t *r)
414+
{
415+
if (r->headers_out.status != NGX_HTTP_OK
416+
|| r->headers_out.last_modified_time == -1)
417+
{
418+
return ngx_http_srcache_next_header_filter(r);
419+
}
420+
421+
if (r->headers_in.if_unmodified_since) {
422+
return ngx_http_srcache_test_precondition(r);
423+
}
424+
425+
if (r->headers_in.if_modified_since) {
426+
return ngx_http_srcache_test_not_modified(r);
427+
}
428+
429+
return ngx_http_srcache_next_header_filter(r);
430+
}
431+
432+
433+
static ngx_int_t
434+
ngx_http_srcache_test_not_modified(ngx_http_request_t *r)
435+
{
436+
time_t ims;
437+
ngx_http_core_loc_conf_t *clcf;
438+
439+
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
440+
441+
if (clcf->if_modified_since == NGX_HTTP_IMS_OFF) {
442+
return ngx_http_srcache_next_header_filter(r);
443+
}
444+
445+
ims = ngx_http_parse_time(r->headers_in.if_modified_since->value.data,
446+
r->headers_in.if_modified_since->value.len);
447+
448+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
449+
"http ims:%d lm:%d", ims, r->headers_out.last_modified_time);
450+
451+
if (ims != r->headers_out.last_modified_time) {
452+
453+
if (clcf->if_modified_since == NGX_HTTP_IMS_EXACT
454+
|| ims < r->headers_out.last_modified_time)
455+
{
456+
return ngx_http_srcache_next_header_filter(r);
457+
}
458+
}
459+
460+
r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
461+
r->headers_out.status_line.len = 0;
462+
r->headers_out.content_type.len = 0;
463+
ngx_http_clear_content_length(r);
464+
ngx_http_clear_accept_ranges(r);
465+
466+
if (r->headers_out.content_encoding) {
467+
r->headers_out.content_encoding->hash = 0;
468+
r->headers_out.content_encoding = NULL;
469+
}
470+
471+
return ngx_http_srcache_next_header_filter(r);
472+
}
473+
474+
475+
static ngx_int_t
476+
ngx_http_srcache_test_precondition(ngx_http_request_t *r)
477+
{
478+
time_t iums;
396479

480+
iums = ngx_http_parse_time(r->headers_in.if_unmodified_since->value.data,
481+
r->headers_in.if_unmodified_since->value.len);
482+
483+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
484+
"http iums:%d lm:%d", iums, r->headers_out.last_modified_time);
485+
486+
if (iums >= r->headers_out.last_modified_time) {
487+
return ngx_http_srcache_next_header_filter(r);
488+
}
489+
490+
return ngx_http_filter_finalize_request(r, NULL,
491+
NGX_HTTP_PRECONDITION_FAILED);
492+
}
397493

src/ngx_http_srcache_headers.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static ngx_int_t
194194
ngx_http_srcache_process_last_modified(ngx_http_request_t *r,
195195
ngx_table_elt_t *h, ngx_uint_t offset)
196196
{
197+
#if 1
197198
ngx_table_elt_t *ho;
198199

199200
ho = ngx_list_push(&r->headers_out.headers);
@@ -204,6 +205,13 @@ ngx_http_srcache_process_last_modified(ngx_http_request_t *r,
204205
*ho = *h;
205206

206207
r->headers_out.last_modified = ho;
208+
#endif
209+
210+
r->headers_out.last_modified_time = ngx_http_parse_time(h->value.data,
211+
h->value.len);
212+
213+
dd("setting last-modified-time: %d",
214+
(int) r->headers_out.last_modified_time);
207215

208216
return NGX_OK;
209217
}

0 commit comments

Comments
 (0)