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

ngx_http_header_filter.c « http « src - github.com/nginx/nginx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e39f45b8ed9f2a1da86d2b3cfe4c4172d96e288 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>


static int ngx_http_header_filter_init(ngx_cycle_t *cycle);
static int ngx_http_header_filter(ngx_http_request_t *r);


static ngx_http_module_t  ngx_http_header_filter_module_ctx = {
    NULL,                                  /* pre conf */

    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */

    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */

    NULL,                                  /* create location configuration */
    NULL,                                  /* merge location configuration */
};


ngx_module_t  ngx_http_header_filter_module = {
    NGX_MODULE,
    &ngx_http_header_filter_module_ctx,    /* module context */
    NULL,                                  /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    ngx_http_header_filter_init,           /* init module */
    NULL                                   /* init child */
};


static char server_string[] = "Server: " NGINX_VER CRLF;


static ngx_str_t http_codes[] = {

    ngx_string("200 OK"),
    ngx_null_string,  /* "201 Created" */
    ngx_null_string,  /* "202 Accepted" */
    ngx_null_string,  /* "203 Non-Authoritative Information" */
    ngx_null_string,  /* "204 No Content" */
    ngx_null_string,  /* "205 Reset Content" */
    ngx_string("206 Partial Content"),
    ngx_null_string,  /* "207 Multi-Status" */

#if 0
    ngx_null_string,  /* "300 Multiple Choices" */
#endif

    ngx_string("301 Moved Permanently"),
#if 0
    ngx_string("302 Moved Temporarily"),
#else
    ngx_string("302 Found"),
#endif
    ngx_null_string,  /* "303 See Other" */
    ngx_string("304 Not Modified"),

    ngx_string("400 Bad Request"),
    ngx_string("401 Unauthorized"),
    ngx_null_string,  /* "402 Payment Required" */
    ngx_string("403 Forbidden"),
    ngx_string("404 Not Found"),
    ngx_string("405 Not Allowed"),
    ngx_null_string,  /* "406 Not Acceptable" */
    ngx_null_string,  /* "407 Proxy Authentication Required" */
    ngx_string("408 Request Time-out"),
    ngx_null_string,  /* "409 Conflict" */
    ngx_null_string,  /* "410 Gone" */
    ngx_string("411 Length Required"),
    ngx_null_string,  /* "412 Precondition Failed" */
    ngx_string("413 Request Entity Too Large"),
    ngx_null_string,  /* "414 Request-URI Too Large" but we never send it
                         because we treat such requests as the HTTP/0.9 requests
                         and send only the body without the header */
    ngx_null_string,  /* "415 Unsupported Media Type" */
    ngx_string("416 Requested Range Not Satisfiable"),

    ngx_string("500 Internal Server Error"),
    ngx_string("501 Method Not Implemented"),
    ngx_string("502 Bad Gateway"),
    ngx_string("503 Service Temporarily Unavailable"),
    ngx_string("504 Gateway Time-out")
};



static int ngx_http_header_filter(ngx_http_request_t *r)
{
    int                len, status, i;
    char              *p;
    ngx_hunk_t        *h;
    ngx_chain_t       *ln;
    ngx_table_elt_t   *header;

    if (r->http_version < NGX_HTTP_VERSION_10) {
        return NGX_OK;
    }

    if (r->method == NGX_HTTP_HEAD) {
        r->header_only = 1;
    }

    /* 2 is for trailing "\r\n" and 2 is for "\r\n" in the end of header */
    len = sizeof("HTTP/1.x ") - 1 + 2 + 2;

    /* status line */
    if (r->headers_out.status_line.len) {
        len += r->headers_out.status_line.len;
#if (NGX_SUPPRESS_WARN)
        status = NGX_INVALID_ARRAY_INDEX;
#endif

    } else {

        if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
            /* 2XX */
            status = r->headers_out.status - NGX_HTTP_OK;

        } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
            /* 3XX */
            status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 8;

            if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
                r->header_only = 1;
            }

        } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
            /* 4XX */
            status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 8 + 4;

        } else {
            /* 5XX */
            status = r->headers_out.status
                                 - NGX_HTTP_INTERNAL_SERVER_ERROR + 8 + 4 + 17;
        }

        len += http_codes[status].len;
    }

    if (r->headers_out.server && r->headers_out.server->key.len) {
        len += r->headers_out.server->key.len
               + r->headers_out.server->value.len + 2;
    } else {
        len += sizeof(server_string) - 1;
    }

    if (r->headers_out.date && r->headers_out.date->key.len) {
        len += r->headers_out.date->key.len
               + r->headers_out.date->value.len + 2;
    } else {
        len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
    }

    if (r->headers_out.content_length == NULL) {
        if (r->headers_out.content_length_n >= 0) {
                                           /* 2^64 */
            len += sizeof("Content-Length: 18446744073709551616" CRLF) - 1;
        }
    }

    if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
        r->headers_out.content_type->key.len = 0;
        len += sizeof("Content-Type: ") - 1
               + r->headers_out.content_type->value.len + 2;

        if (r->headers_out.charset.len) {
            len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
        }
    }

    if (r->headers_out.location
        && r->headers_out.location->value.len
        && r->headers_out.location->value.data[0] == '/')
    {
        r->headers_out.location->key.len = 0;
        len += sizeof("Location: http://") - 1
               + r->server_name->len + r->headers_out.location->value.len + 2;

        if (r->port != 80) {
            len += r->port_name->len;
        }
    }

    if (r->headers_out.last_modified && r->headers_out.last_modified->key.len) {
        len += r->headers_out.last_modified->key.len
               + r->headers_out.last_modified->value.len + 2;

    } else if (r->headers_out.last_modified_time != -1) {
        len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
    }

    if (r->chunked) {
        len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
    }

    if (r->keepalive) {
        len += sizeof("Connection: keep-alive" CRLF) - 1;
    } else {
        len += sizeof("Connection: closed" CRLF) - 1;
    }

    header = r->headers_out.headers->elts;
    for (i = 0; i < r->headers_out.headers->nelts; i++) {
        if (header[i].key.len == 0) {
            continue;
        }

        /* 2 is for ": " and 2 is for "\r\n" */
        len += header[i].key.len + 2 + header[i].value.len + 2;
    }

    if (!(h = ngx_create_temp_hunk(r->pool, len))) {
        return NGX_ERROR;
    }

    /* "HTTP/1.x " */
    h->last = ngx_cpymem(h->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);

    /* status line */
    if (r->headers_out.status_line.len) {
        h->last = ngx_cpymem(h->last, r->headers_out.status_line.data,
                             r->headers_out.status_line.len);

    } else {
        h->last = ngx_cpymem(h->last, http_codes[status].data,
                             http_codes[status].len);
    }
    *(h->last++) = CR; *(h->last++) = LF;

    if (!(r->headers_out.server && r->headers_out.server->key.len)) {
        h->last = ngx_cpymem(h->last, server_string, sizeof(server_string) - 1);
    }

    if (!(r->headers_out.date && r->headers_out.date->key.len)) {
        h->last = ngx_cpymem(h->last, "Date: ", sizeof("Date: ") - 1);
#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
        p = h->last;
#endif
        h->last = ngx_cpymem(h->last, ngx_cached_http_time.data,
                             ngx_cached_http_time.len);

#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
        r->headers_out.date = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
        if (r->headers_out.date == NULL) {
            return NGX_ERROR;
        }

        r->headers_out.date->key.len = 0;
        r->headers_out.date->key.data = NULL;
        r->headers_out.date->value.len = h->last - p;
        r->headers_out.date->value.data = p;
#endif

        *(h->last++) = CR; *(h->last++) = LF;
    }

    if (r->headers_out.content_length == NULL) {
        if (r->headers_out.content_length_n >= 0) {
#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
            p = h->last + sizeof("Content-Length: ") - 1;
#endif
            h->last += ngx_snprintf(h->last,        /* 2^64 */
                            sizeof("Content-Length: 18446744073709551616" CRLF),
                            "Content-Length: " OFF_T_FMT CRLF,
                            r->headers_out.content_length_n);

#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
            r->headers_out.content_length = ngx_palloc(r->pool,
                                                       sizeof(ngx_table_elt_t));
            if (r->headers_out.content_length == NULL) {
                return NGX_ERROR;
            }

            r->headers_out.content_length->key.len = 0;
            r->headers_out.content_length->key.data = NULL;
            r->headers_out.content_length->value.len = h->last - p - 2;
            r->headers_out.content_length->value.data = p;
#endif
        }
    }

    if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
        h->last = ngx_cpymem(h->last, "Content-Type: ",
                             sizeof("Content-Type: ") - 1);
        p = h->last;
        h->last = ngx_cpymem(h->last, r->headers_out.content_type->value.data,
                             r->headers_out.content_type->value.len);

        if (r->headers_out.charset.len) {
            h->last = ngx_cpymem(h->last, "; charset=",
                                 sizeof("; charset=") - 1);
            h->last = ngx_cpymem(h->last, r->headers_out.charset.data,
                                 r->headers_out.charset.len);

            r->headers_out.content_type->value.len = h->last - p;
            r->headers_out.content_type->value.data = p;
        }

        *(h->last++) = CR; *(h->last++) = LF;
    }

    if (r->headers_out.location
        && r->headers_out.location->value.len
        && r->headers_out.location->value.data[0] == '/')
    {
        p = h->last + sizeof("Location: ") - 1;
        h->last = ngx_cpymem(h->last, "Location: http://",
                             sizeof("Location: http://") - 1);
        h->last = ngx_cpymem(h->last, r->server_name->data,
                             r->server_name->len);
        if (r->port != 80) {
            h->last = ngx_cpymem(h->last, r->port_name->data,
                                 r->port_name->len);
        }

        h->last = ngx_cpymem(h->last, r->headers_out.location->value.data,
                             r->headers_out.location->value.len);

        r->headers_out.location->value.len = h->last - p;
        r->headers_out.location->value.data = p;

        *(h->last++) = CR; *(h->last++) = LF;
    }

    if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len)
        && r->headers_out.last_modified_time != -1)
    {
        h->last = ngx_cpymem(h->last, "Last-Modified: ",
                             sizeof("Last-Modified: ") - 1);
#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
        p = h->last;
#endif
        h->last += ngx_http_time(h->last, r->headers_out.last_modified_time);

#if (NGX_HTTP_LOG_ALL_HEADERS_OUT)
        r->headers_out.last_modified = ngx_palloc(r->pool,
                                                  sizeof(ngx_table_elt_t));
        if (r->headers_out.last_modified == NULL) {
            return NGX_ERROR;
        }

        r->headers_out.last_modified->key.len = 0;
        r->headers_out.last_modified->key.data = NULL;
        r->headers_out.last_modified->value.len = h->last - p;
        r->headers_out.last_modified->value.data = p;
#endif

        *(h->last++) = CR; *(h->last++) = LF;
    }

    if (r->chunked) {
        h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF,
                             sizeof("Transfer-Encoding: chunked" CRLF) - 1);
    }

    if (r->keepalive) {
        h->last = ngx_cpymem(h->last, "Connection: keep-alive" CRLF,
                             sizeof("Connection: keep-alive" CRLF) - 1);

    } else {
        h->last = ngx_cpymem(h->last, "Connection: close" CRLF,
                             sizeof("Connection: close" CRLF) - 1);
    }

    for (i = 0; i < r->headers_out.headers->nelts; i++) {
        if (header[i].key.len == 0) {
            continue;
        }

        h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len);
        *(h->last++) = ':' ; *(h->last++) = ' ' ;

        h->last = ngx_cpymem(h->last, header[i].value.data,
                             header[i].value.len);
        *(h->last++) = CR; *(h->last++) = LF;
    }

    /* STUB */
    *(h->last) = '\0';
    ngx_log_debug(r->connection->log, "%s\n" _ h->pos);
    /**/

    /* the end of HTTP header */
    *(h->last++) = CR; *(h->last++) = LF;

    if (r->header_only) {
        h->type |= NGX_HUNK_LAST;
    }

    if (!(ln = ngx_alloc_chain_link(r->pool))) {
        return NGX_ERROR;
    }

    ln->hunk = h;
    ln->next = NULL;

    return ngx_http_write_filter(r, ln);
}


static int ngx_http_header_filter_init(ngx_cycle_t *cycle)
{
    ngx_http_top_header_filter = ngx_http_header_filter;

    return NGX_OK;
}