Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ngx_http_output_filter.c « http « src - github.com/nginx/nginx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 345a4f161e465854725ad5932c52d8dbbab5a667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

#include <ngx_config.h>

#include <ngx_core.h>
#include <ngx_files.h>
#include <ngx_string.h>
#include <ngx_hunk.h>
#include <ngx_conf_file.h>

#include <ngx_http.h>
#include <ngx_http_config.h>
#include <ngx_http_output_filter.h>


static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src);
static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool);


static ngx_command_t  ngx_http_output_filter_commands[] = {

    {ngx_string("output_buffer"),
     NGX_CONF_TAKE1,
     ngx_conf_set_size_slot,
     NGX_HTTP_LOC_CONF,
     offsetof(ngx_http_output_filter_conf_t, hunk_size)},

    {ngx_string(""), 0, NULL, 0, 0}
};


static ngx_http_module_t  ngx_http_output_filter_module_ctx = {
    NGX_HTTP_MODULE,

    NULL,                                  /* create server config */
    NULL,                                  /* init server config */
    ngx_http_output_filter_create_conf,    /* create location config */
    NULL,                                  /* merge location config */

    NULL,                                  /* translate handler */

    NULL,                                  /* output header filter */
    NULL,                                  /* next output header filter */
    (int (*)(ngx_http_request_t *, ngx_chain_t *))
        ngx_http_output_filter,            /* output body filter */
    NULL                                   /* next output body filter */
};


ngx_module_t  ngx_http_output_filter_module = {
    &ngx_http_output_filter_module_ctx,    /* module context */
    ngx_http_output_filter_commands,       /* module directives */
    NGX_HTTP_MODULE_TYPE,                  /* module type */
    NULL                                   /* init module */
};


int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
{
    int      rc, once;
    size_t   size;
    ssize_t  n;
    ngx_chain_t  *ce;
    ngx_http_output_filter_ctx_t  *ctx;
    ngx_http_output_filter_conf_t *conf;

    ctx = (ngx_http_output_filter_ctx_t *)
                    ngx_http_get_module_ctx(r->main ? r->main : r,
                                            ngx_http_output_filter_module_ctx);

    if (ctx == NULL) {
        ngx_http_create_ctx(r, ctx,
                            ngx_http_output_filter_module_ctx,
                            sizeof(ngx_http_output_filter_ctx_t));
    }

    if (hunk && (hunk->type & NGX_HUNK_LAST)) {
        ctx->last = 1;
    }

    for (once = 1; once || ctx->in; once = 0) {

         /* input chain is not empty */
        if (ctx->in) {

            /* add hunk to input chain */
            if (once && hunk) {
                for (ce = ctx->in; ce->next; ce = ce->next) {
                    /* void */ ;
                }

                ngx_add_hunk_to_chain(ce->next, hunk, r->pool, NGX_ERROR);
            }

            /* our hunk is still busy */
            if (ctx->hunk->pos.mem < ctx->hunk->last.mem) {
                rc = ngx_http_output_filter_module_ctx.
                                              next_output_body_filter(r, NULL);

            /* our hunk is free */
            } else {
                ctx->out.hunk = ctx->hunk;

                rc = ngx_http_output_filter_copy_hunk(ctx->hunk, ctx->in->hunk);
#if (NGX_FILE_AIO_READ)
                if (rc == NGX_AGAIN) {
                    return rc;
                }
#endif
                if (rc == NGX_ERROR) {
                    return rc;
                }

                /* whole hunk is copied so we send to next filter chain part
                   up to next hunk that need to be copied */
                if (ctx->in->hunk->pos.mem == ctx->in->hunk->last.mem) {
                    ctx->out.next = ctx->in->next;

                    for (ce = ctx->in->next; ce; ce = ce->next) {
                        if (ce->hunk->type & NGX_HUNK_FILE) {
                            break;
                        }

                        if ((ce->hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP))
                            && (r->filter & NGX_HTTP_FILTER_NEED_TEMP))
                        {
                            break;
                        }
                    }

                    ctx->out.next = ce;

                } else {
                    ctx->out.next = NULL;
                }

                rc = ngx_http_output_filter_module_ctx.
                                         next_output_body_filter(r, &ctx->out);
            }

            /* delete completed hunks from input chain */
            for (ce = ctx->in; ce; ce = ce->next) {
                 if (ce->hunk->pos.file == ce->hunk->last.file) {
                     ctx->in = ce->next;
                 }
            }

            if (rc == NGX_OK && ctx->hunk) {
                ctx->hunk->pos.mem = ctx->hunk->last.mem = ctx->hunk->start;
            } else {
                return rc;
            }

        /* input chain is empty */
        } else {

            if (hunk == NULL) {
                rc = ngx_http_output_filter_module_ctx.
                                              next_output_body_filter(r, NULL);

            } else {

                /* we need to copy hunk to our hunk */
                if (((r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY)
                        && (hunk->type & NGX_HUNK_FILE))
                    || ((r->filter & NGX_HTTP_FILTER_NEED_TEMP)
                        && (hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP)))
                   ) {

                    /* out hunk is still busy */
                    if (ctx->hunk && ctx->hunk->pos.mem < ctx->hunk->last.mem) {
                        ngx_add_hunk_to_chain(ctx->in, hunk, r->pool,
                                              NGX_ERROR);

                        rc = ngx_http_output_filter_module_ctx.
                                              next_output_body_filter(r, NULL);

                    } else {
                        if (ctx->hunk == NULL) {

                            if (hunk->type & NGX_HUNK_LAST) {

                                conf = (ngx_http_output_filter_conf_t *)
                                        ngx_http_get_module_loc_conf(
                                            r->main ? r->main : r,
                                            ngx_http_output_filter_module_ctx);

                                size = hunk->last.mem - hunk->pos.mem;
                                if (size > conf->hunk_size) {
                                    size = conf->hunk_size;
                                }

                            } else {
                                size = conf->hunk_size;
                            }

                            ngx_test_null(ctx->hunk,
                                          ngx_create_temp_hunk(r->pool, size,
                                                               50, 50),
                                          NGX_ERROR);
                            ctx->hunk->type |= NGX_HUNK_RECYCLED;

                            rc = ngx_http_output_filter_copy_hunk(ctx->hunk,
                                                                  hunk);
#if (NGX_FILE_AIO_READ)
                            if (rc == NGX_AGAIN) {
                                /* add hunk to input chain */
                                ngx_add_hunk_to_chain(ctx->in, hunk, r->pool,
                                                      NGX_ERROR);

                                return rc;
                            }
#endif
                            if (rc == NGX_ERROR) {
                                return rc;
                            }

                            if (hunk->pos.mem < hunk->last.mem) {
                                ngx_add_hunk_to_chain(ctx->in, hunk, r->pool,
                                                      NGX_ERROR);
                            }

                            ctx->out.hunk = ctx->hunk;
                            ctx->out.next = NULL;

                            rc = ngx_http_output_filter_module_ctx.
                                         next_output_body_filter(r, &ctx->out);
                        }
                    }

                } else {
                    ctx->out.hunk = hunk;
                    ctx->out.next = NULL;

                    rc = ngx_http_output_filter_module_ctx.
                                         next_output_body_filter(r, &ctx->out);
                }
            }
        }

        if (rc == NGX_OK && ctx->hunk)
            ctx->hunk->pos.mem = ctx->hunk->last.mem = ctx->hunk->start;
    }

    if (rc == NGX_OK && ctx->last) {
        return NGX_OK;
    }

    if (rc == NGX_OK) {
        if (ctx->hunk) {
            ctx->hunk->pos.mem = ctx->hunk->last.mem = ctx->hunk->start;
        }
#if (!NGX_ONESHOT_EVENT)
        ngx_del_event(r->connection->write, NGX_WRITE_EVENT);
#endif
    }

    return rc;
}


static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src)
{
    size_t   size;
    ssize_t  n;

    size = src->last.mem - src->pos.mem;
    if (size > dst->end - dst->pos.mem) {
        size = dst->end - dst->pos.mem;
    }

    if (src->type & NGX_HUNK_FILE) {
        n = ngx_read_file(src->file, dst->pos.mem, size, src->pos.file);

        if (n == NGX_ERROR) {
            return n;

#if (NGX_FILE_AIO_READ)
        } else if (n == NGX_AGAIN) {
            return n;
#endif

        } else {
            ngx_assert((n == size), /* void */ ; , src->file->log,
                       ngx_read_file_n " reads only %d of %d" _
                       n _ size);
        }

        src->pos.mem += n;
        dst->last.mem += n;

    } else {
        ngx_memcpy(src->pos.mem, dst->pos.mem, size);

        src->pos.mem += size;
        dst->last.mem += size;
    }

    if (src->type & NGX_HUNK_LAST) {
        dst->type |= NGX_HUNK_LAST;
    }

    return NGX_OK;
}


static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool)
{
    ngx_http_output_filter_conf_t *conf;

    ngx_test_null(conf,
                  ngx_pcalloc(pool, sizeof(ngx_http_output_filter_conf_t)),
                  NULL);

    conf->hunk_size = NGX_CONF_UNSET;

    return conf;
}