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: d0e2ae7a0a3f943304eb436b1aee92128bfc2ad1 (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

#include <nginx.h>

#include <ngx_config.h>

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

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


static void ngx_http_header_filter_init(ngx_pool_t *pool,
                                        ngx_http_conf_filter_t *cf);
static int ngx_http_header_filter(ngx_http_request_t *r);


ngx_http_module_t  ngx_http_header_filter_module_ctx = {
    NULL,                                  /* create server config */
    NULL,                                  /* init server config */

    NULL,                                  /* create location config */
    NULL,                                  /* merge location config */

    ngx_http_header_filter_init            /* init filters */
};


ngx_module_t  ngx_http_header_filter_module = {
    0,                                     /* module index */
    &ngx_http_header_filter_module_ctx,    /* module context */
    NULL,                                  /* module directives */
    NGX_HTTP_MODULE_TYPE,                  /* module type */
    NULL                                   /* init module */
};


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


static ngx_str_t http_codes[] = {

    ngx_string("200 OK"),

    ngx_string("301 Moved Permanently"),
    ngx_string("302 Moved Temporarily"),
    ngx_null_string,  /* 303 */
    ngx_string("304 Not Modified"),

    ngx_string("400 Bad Request"),
    ngx_null_string,  /* 401 */
    ngx_null_string,  /* 402 */
    ngx_string("403 Forbidden"),
    ngx_string("404 Not Found"),
    ngx_null_string,  /* 405 */
    ngx_null_string,  /* 406 */
    ngx_null_string,  /* 407 */
    ngx_string("408 Request Time-out"),
    ngx_null_string,  /* 409 */
    ngx_null_string,  /* 410 */
    ngx_string("411 Length Required"),
    ngx_null_string,  /* 412 */
    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 HTTP/0.9 requests
                         and send only the body without the header */
    ngx_null_string,  /* 415 */
    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;
    time_t            ims;
    ngx_hunk_t       *h;
    ngx_chain_t      *ch;
    ngx_table_elt_t  *header;

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

    /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n"
       and 2 is for end of header */
    len = 9 + 2 + 2;

    if (r->headers_in.if_modified_since && r->headers_out.status == NGX_HTTP_OK)
    {
        /* TODO: check LM header */
        if (r->headers_out.last_modified_time) {
            ims = ngx_http_parse_time(
                                  r->headers_in.if_modified_since->value.data,
                                  r->headers_in.if_modified_since->value.len);

            ngx_log_debug(r->connection->log, "%d %d" _
                          ims _ r->headers_out.last_modified_time);

            /* I think that the date equality is correcter */
            if (ims != NGX_ERROR && ims == r->headers_out.last_modified_time) {
                r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
                r->headers_out.content_length = -1;
                r->headers_out.content_type->key.len = 0;
            }
        }
    }

    /* 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) {
            status = r->headers_out.status - NGX_HTTP_OK;

        } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
            status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1;
            r->header_only = 1;

        } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
            status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4;

        } else {
            status = r->headers_out.status
                                 - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 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 {
        /* "Date: ... \r\n"; */
        len += 37;
    }

    /* 2^64 is 20 characters */
    if (r->headers_out.content_length >= 0) {
        len += 48;
    }

#if 0
    if (r->headers_out.content_type.len)
        len += r->headers_out.content_type.len + 16;
#endif

    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) {
        /* "Last-Modified: ... \r\n"; */
        len += 46;
    }

    if (r->keepalive == 0) {
        len += 19;
    } else {
        len += 24;
    }

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

        len += header[i].key.len + 2 + header[i].value.len + 2;
    }

    ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR);

    /* "HTTP/1.x " */
    ngx_memcpy(h->last, "HTTP/1.1 ", 9);
    h->last += 9;

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

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

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

    if (!(r->headers_out.date && r->headers_out.date->key.len)) {
        ngx_memcpy(h->last, "Date: ", 6);
        h->last += 6;
        h->last += ngx_http_get_time(h->last, time(NULL));
        *(h->last++) = CR; *(h->last++) = LF;
    }

    /* 2^64 is 20 characters  */
    if (r->headers_out.content_length >= 0) {
        h->last += ngx_snprintf(h->last, 49,
                                    "Content-Length: " OFF_FMT CRLF,
                                    r->headers_out.content_length);
    }

#if 0
    if (r->headers_out.content_type.len) {
        ngx_memcpy(h->last, "Content-Type: ", 14);
        h->last += 14;
        ngx_memcpy(h->last, r->headers_out.content_type.data,
                   r->headers_out.content_type.len);
        h->last += r->headers_out.content_type.len;
        *(h->last++) = CR; *(h->last++) = LF;
    }
#endif

    if (!(r->headers_out.last_modified
          && r->headers_out.last_modified->key.len)
        && r->headers_out.last_modified_time != -1)
    {
        ngx_memcpy(h->last, "Last-Modified: ", 15);
        h->last += 15;
        h->last += ngx_http_get_time(h->last,
                                         r->headers_out.last_modified_time);
        *(h->last++) = CR; *(h->last++) = LF;
    }

    if (r->keepalive == 0) {
        ngx_memcpy(h->last, "Connection: close" CRLF, 19);
        h->last += 19;

    } else {
        ngx_memcpy(h->last, "Connection: keep-alive" CRLF, 24);
        h->last += 24;
    }

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

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

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

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

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

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

    ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR);

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

    return ngx_http_write_filter(r, ch);
}


static void ngx_http_header_filter_init(ngx_pool_t *pool,
                                        ngx_http_conf_filter_t *cf)
{
    cf->output_header_filter = ngx_http_header_filter;
}