Klaus Demo nginx / fd41918
handle big responses for "size" and "test" image_filters Igor Sysoev 13 years ago
4 changed file(s) with 60 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
66 #include <ngx_config.h>
77 #include <ngx_core.h>
88 #include <ngx_http.h>
9 #include "gd.h"
9
10 #include <gd.h>
1011
1112
1213 #define NGX_HTTP_IMAGE_OFF 0
1920 #define NGX_HTTP_IMAGE_START 0
2021 #define NGX_HTTP_IMAGE_READ 1
2122 #define NGX_HTTP_IMAGE_PROCESS 2
22 #define NGX_HTTP_IMAGE_DONE 3
23 #define NGX_HTTP_IMAGE_PASS 3
24 #define NGX_HTTP_IMAGE_DONE 4
2325
2426
2527 #define NGX_HTTP_IMAGE_NONE 0
5456 } ngx_http_image_filter_ctx_t;
5557
5658
59 static ngx_int_t ngx_http_image_send(ngx_http_request_t *r,
60 ngx_http_image_filter_ctx_t *ctx, ngx_chain_t *in);
5761 static ngx_uint_t ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in);
5862 static ngx_int_t ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in);
5963 static ngx_buf_t *ngx_http_image_process(ngx_http_request_t *r);
246250
247251 if (out.buf) {
248252 out.next = NULL;
249 in = &out;
250
251 break;
253 ctx->phase = NGX_HTTP_IMAGE_DONE;
254
255 return ngx_http_image_send(r, ctx, &out);
252256 }
253257 }
254258
263267 r->headers_out.content_type = *ct;
264268
265269 if (conf->filter == NGX_HTTP_IMAGE_TEST) {
266 break;
270 ctx->phase = NGX_HTTP_IMAGE_PASS;
271
272 return ngx_http_image_send(r, ctx, in);
267273 }
268274
269275 ctx->phase = NGX_HTTP_IMAGE_READ;
295301 }
296302
297303 out.next = NULL;
298 in = &out;
299
300 break;
304 ctx->phase = NGX_HTTP_IMAGE_PASS;
305
306 return ngx_http_image_send(r, ctx, &out);
307
308 case NGX_HTTP_IMAGE_PASS:
309
310 return ngx_http_next_body_filter(r, in);
301311
302312 default: /* NGX_HTTP_IMAGE_DONE */
303313
304 return ngx_http_next_body_filter(r, in);
305 }
306
307 ctx->phase = NGX_HTTP_IMAGE_DONE;
314 rc = ngx_http_next_body_filter(r, NULL);
315
316 /* NGX_ERROR resets any pending data */
317 return (rc == NGX_OK) ? NGX_ERROR : rc;
318 }
319 }
320
321
322 static ngx_int_t
323 ngx_http_image_send(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx,
324 ngx_chain_t *in)
325 {
326 ngx_int_t rc;
308327
309328 rc = ngx_http_next_header_filter(r);
310329
311330 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
312 return rc;
313 }
314
315 return ngx_http_next_body_filter(r, in);
331 return NGX_ERROR;
332 }
333
334 rc = ngx_http_next_body_filter(r, in);
335
336 if (ctx->phase == NGX_HTTP_IMAGE_DONE) {
337 /* NGX_ERROR resets any pending data */
338 return (rc == NGX_OK) ? NGX_ERROR : rc;
339 }
340
341 return rc;
316342 }
317343
318344
18141814 "http finalize request: %d, \"%V?%V\" %d",
18151815 rc, &r->uri, &r->args, r == c->data);
18161816
1817 if (rc == NGX_OK && r->filter_finalize) {
1818 c->error = 1;
1819 return;
1820 }
1821
18171822 if (rc == NGX_DECLINED) {
18181823 r->content_handler = NULL;
18191824 r->write_event_handler = ngx_http_core_run_phases;
477477 unsigned discard_body:1;
478478 unsigned internal:1;
479479 unsigned error_page:1;
480 unsigned filter_finalize:1;
480481 unsigned post_action:1;
481482 unsigned request_complete:1;
482483 unsigned request_output:1;
454454 /* clear the modules contexts */
455455 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
456456
457 r->filter_finalize = 1;
458
457459 rc = ngx_http_special_response_handler(r, error);
458460
459461 /* NGX_ERROR resets any pending data */
460462
461 return (rc == NGX_OK) ? NGX_ERROR : rc;
463 switch (rc) {
464
465 case NGX_OK:
466 case NGX_DONE:
467 return NGX_ERROR;
468
469 default:
470 return rc;
471 }
462472 }
463473
464474