3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_http_postpone_filter_add(ngx_http_request_t *r,
14 static ngx_int_t ngx_http_postpone_filter_init(ngx_conf_t *cf);
17 static ngx_http_module_t ngx_http_postpone_filter_module_ctx = {
18 NULL, /* preconfiguration */
19 ngx_http_postpone_filter_init, /* postconfiguration */
21 NULL, /* create main configuration */
22 NULL, /* init main configuration */
24 NULL, /* create server configuration */
25 NULL, /* merge server configuration */
27 NULL, /* create location configuration */
28 NULL /* merge location configuration */
32 ngx_module_t ngx_http_postpone_filter_module = {
34 &ngx_http_postpone_filter_module_ctx, /* module context */
35 NULL, /* module directives */
36 NGX_HTTP_MODULE, /* module type */
37 NULL, /* init master */
38 NULL, /* init module */
39 NULL, /* init process */
40 NULL, /* init thread */
41 NULL, /* exit thread */
42 NULL, /* exit process */
43 NULL, /* exit master */
48 static ngx_http_output_body_filter_pt ngx_http_next_filter;
52 ngx_http_postpone_filter(ngx_http_request_t *r, ngx_chain_t *in)
55 ngx_http_postponed_request_t *pr;
59 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
60 "http postpone filter \"%V?%V\" %p", &r->uri, &r->args, in);
65 ngx_http_postpone_filter_add(r, in);
70 /* TODO: SSI may pass NULL */
71 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
72 "http postpone filter NULL inactive request",
79 if (r->postponed == NULL) {
81 if (in || c->buffered) {
82 return ngx_http_next_filter(r->main, in);
89 ngx_http_postpone_filter_add(r, in);
97 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
98 "http postpone filter wake \"%V?%V\"",
99 &pr->request->uri, &pr->request->args);
101 r->postponed = pr->next;
103 c->data = pr->request;
105 return ngx_http_post_request(pr->request);
108 if (pr->out == NULL) {
109 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
110 "http postpone filter NULL output",
114 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
115 "http postpone filter output \"%V?%V\"",
118 if (ngx_http_next_filter(r->main, pr->out) == NGX_ERROR) {
123 r->postponed = pr->next;
125 } while (r->postponed);
132 ngx_http_postpone_filter_add(ngx_http_request_t *r, ngx_chain_t *in)
134 ngx_http_postponed_request_t *pr, **ppr;
137 for (pr = r->postponed; pr->next; pr = pr->next) { /* void */ }
139 if (pr->request == NULL) {
149 pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
162 if (ngx_chain_add_copy(r->pool, &pr->out, in) == NGX_OK) {
171 ngx_http_postpone_filter_init(ngx_conf_t *cf)
173 ngx_http_next_filter = ngx_http_top_body_filter;
174 ngx_http_top_body_filter = ngx_http_postpone_filter;