comparison src/http/ngx_http_header_filter.c @ 26:53cb81681040

nginx-0.0.1-2002-12-15-09:25:09 import
author Igor Sysoev <igor@sysoev.ru>
date Sun, 15 Dec 2002 06:25:09 +0000
parents 77c7629a2627
children 59e7c7f30d49
comparison
equal deleted inserted replaced
25:a8b156554dfe 26:53cb81681040
7 #include <ngx_table.h> 7 #include <ngx_table.h>
8 #include <ngx_hunk.h> 8 #include <ngx_hunk.h>
9 #include <ngx_http.h> 9 #include <ngx_http.h>
10 10
11 11
12 #if 0 12 static int ngx_http_header_filter(ngx_http_request_t *r);
13 13
14 ngx_http_module_t ngx_http_header_filter_module = { 14 ngx_http_module_t ngx_http_header_filter_module = {
15 NGX_HTTP_MODULE, 15 NGX_HTTP_MODULE,
16 16
17 NULL, /* create server config */ 17 NULL, /* create server config */
19 NULL, /* module directives */ 19 NULL, /* module directives */
20 20
21 NULL, /* init module */ 21 NULL, /* init module */
22 NULL, /* translate handler */ 22 NULL, /* translate handler */
23 23
24 ngx_http_header_filter_init /* init output header filter */ 24 ngx_http_header_filter, /* output header filter */
25 NULL /* init output body filter */ 25 NULL, /* next output header filter */
26 NULL, /* output body filter */
27 NULL /* next output body filter */
26 }; 28 };
27 29
28 #endif
29
30 30
31 static char server_string[] = "Server: " NGINX_VER CRLF; 31 static char server_string[] = "Server: " NGINX_VER CRLF;
32 32
33 33
34 static ngx_str_t http_codes[] = { 34 static ngx_str_t http_codes[] = {
35
35 { 6, "200 OK" }, 36 { 6, "200 OK" },
36 37
37 { 21, "301 Moved Permanently" }, 38 { 21, "301 Moved Permanently" },
39 { 21, "302 Moved Temporarily" },
40 { 0, NULL },
41 { 16, "304 Not Modified" },
38 42
39 { 15, "400 Bad Request" }, 43 { 15, "400 Bad Request" },
40 { 0, NULL }, 44 { 0, NULL },
41 { 0, NULL }, 45 { 0, NULL },
42 { 13, "403 Forbidden" }, 46 { 13, "403 Forbidden" },
43 { 13, "404 Not Found" } 47 { 13, "404 Not Found" },
48
49 { 25, "500 Internal Server Error" }
44 }; 50 };
45 51
46 52
47 53
48 int ngx_http_header_filter(ngx_http_request_t *r) 54 static int ngx_http_header_filter(ngx_http_request_t *r)
49 { 55 {
50 int len, status, i; 56 int len, status, i;
57 time_t ims;
51 ngx_hunk_t *h; 58 ngx_hunk_t *h;
52 ngx_chain_t *ch; 59 ngx_chain_t *ch;
53 ngx_table_elt_t *header; 60 ngx_table_elt_t *header;
54 61
55 if (r->http_version < NGX_HTTP_VERSION_10) 62 if (r->http_version < NGX_HTTP_VERSION_10)
56 return NGX_OK; 63 return NGX_OK;
57 64
58 /* 9 is for "HTTP/1.1 ", 2 is for trailing "\r\n" 65 /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n"
59 and 2 is for end of header */ 66 and 2 is for end of header */
60 len = 9 + 2 + 2; 67 len = 9 + 2 + 2;
68
69 if (r->headers_in.if_modified_since && r->headers_out.status == NGX_HTTP_OK)
70 {
71 /* TODO: check LM header */
72 if (r->headers_out.last_modified_time) {
73 ims = ngx_http_parse_time(
74 r->headers_in.if_modified_since->value.data,
75 r->headers_in.if_modified_since->value.len);
76
77 ngx_log_debug(r->connection->log, "%d %d" _
78 ims _ r->headers_out.last_modified_time);
79
80 if (ims != NGX_ERROR && ims >= r->headers_out.last_modified_time) {
81 r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
82 r->headers_out.content_length = -1;
83 r->headers_out.content_type->key.len = 0;
84 r->header_only = 1;
85 }
86 }
87 }
61 88
62 /* status line */ 89 /* status line */
63 if (r->headers_out.status_line.len) { 90 if (r->headers_out.status_line.len) {
64 len += r->headers_out.status_line.len; 91 len += r->headers_out.status_line.len;
65 } else { 92 } else {
67 status = r->headers_out.status - NGX_HTTP_OK; 94 status = r->headers_out.status - NGX_HTTP_OK;
68 95
69 else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) 96 else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST)
70 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1; 97 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1;
71 98
99 else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR)
100 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4;
101
72 else 102 else
73 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 1; 103 status = r->headers_out.status
104 - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5;
74 105
75 len += http_codes[status].len; 106 len += http_codes[status].len;
76 } 107 }
77 108
78 if (r->headers_out.server && r->headers_out.server->key.len) { 109 if (r->headers_out.server && r->headers_out.server->key.len) {
97 #if 0 128 #if 0
98 if (r->headers_out.content_type.len) 129 if (r->headers_out.content_type.len)
99 len += r->headers_out.content_type.len + 16; 130 len += r->headers_out.content_type.len + 16;
100 #endif 131 #endif
101 132
133 if (r->headers_out.last_modified && r->headers_out.last_modified->key.len) {
134 len += r->headers_out.last_modified->key.len
135 + r->headers_out.last_modified->value.len + 2;
136 } else if (r->headers_out.last_modified_time != -1) {
137 /* "Last-Modified: ... \r\n"; */
138 len += 46;
139 }
140
102 if (r->keepalive) 141 if (r->keepalive)
103 len += 24; 142 len += 24;
104 else 143 else
105 len += 19; 144 len += 19;
106 145
112 len += header[i].key.len + 2 + header[i].value.len + 2; 151 len += header[i].key.len + 2 + header[i].value.len + 2;
113 } 152 }
114 153
115 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR); 154 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR);
116 155
117 /* "HTTP/1.1 " */ 156 /* "HTTP/1.x " */
118 ngx_memcpy(h->last.mem, "HTTP/1.1 ", 9); 157 ngx_memcpy(h->last.mem, "HTTP/1.1 ", 9);
119 h->last.mem += 9; 158 h->last.mem += 9;
120 159
121 /* status line */ 160 /* status line */
122 if (r->headers_out.status_line.len) { 161 if (r->headers_out.status_line.len) {
157 h->last.mem += r->headers_out.content_type.len; 196 h->last.mem += r->headers_out.content_type.len;
158 *(h->last.mem++) = CR; *(h->last.mem++) = LF; 197 *(h->last.mem++) = CR; *(h->last.mem++) = LF;
159 } 198 }
160 #endif 199 #endif
161 200
201 if (!(r->headers_out.last_modified
202 && r->headers_out.last_modified->key.len)
203 && r->headers_out.last_modified_time != -1)
204 {
205 ngx_memcpy(h->last.mem, "Last-Modified: ", 15);
206 h->last.mem += 15;
207 h->last.mem += ngx_http_get_time(h->last.mem,
208 r->headers_out.last_modified_time);
209 *(h->last.mem++) = CR; *(h->last.mem++) = LF;
210 }
211
162 if (r->keepalive) { 212 if (r->keepalive) {
163 ngx_memcpy(h->last.mem, "Connection: keep-alive" CRLF, 24); 213 ngx_memcpy(h->last.mem, "Connection: keep-alive" CRLF, 24);
164 h->last.mem += 24; 214 h->last.mem += 24;
165 215
166 } else { 216 } else {
179 ngx_memcpy(h->last.mem, header[i].value.data, header[i].value.len); 229 ngx_memcpy(h->last.mem, header[i].value.data, header[i].value.len);
180 h->last.mem += header[i].value.len; 230 h->last.mem += header[i].value.len;
181 *(h->last.mem++) = CR; *(h->last.mem++) = LF; 231 *(h->last.mem++) = CR; *(h->last.mem++) = LF;
182 } 232 }
183 233
234 /* STUB */
235 *(h->last.mem) = '\0';
236 ngx_log_debug(r->connection->log, "%s\n" _ h->pos.mem);
237 /**/
238
184 /* end of HTTP header */ 239 /* end of HTTP header */
185 *(h->last.mem++) = CR; *(h->last.mem++) = LF; 240 *(h->last.mem++) = CR; *(h->last.mem++) = LF;
186 241
242 if (r->header_only)
243 h->type |= NGX_HUNK_LAST;
244
187 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); 245 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR);
188 246
189 ch->hunk = h; 247 ch->hunk = h;
190 ch->next = NULL; 248 ch->next = NULL;
191 249