Mercurial > hg > nginx-quic
comparison src/http/ngx_http_header_filter.c @ 176:c0552e5ab567
nginx-0.0.1-2003-11-09-23:03:38 import; separate building
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sun, 09 Nov 2003 20:03:38 +0000 |
parents | 389d7ee9fa60 |
children | 4db54fdbcbe7 |
comparison
equal
deleted
inserted
replaced
175:e92c2c647c57 | 176:c0552e5ab567 |
---|---|
88 | 88 |
89 | 89 |
90 static int ngx_http_header_filter(ngx_http_request_t *r) | 90 static int ngx_http_header_filter(ngx_http_request_t *r) |
91 { | 91 { |
92 int len, status, i; | 92 int len, status, i; |
93 char *p; | |
93 ngx_hunk_t *h; | 94 ngx_hunk_t *h; |
94 ngx_chain_t *ch; | 95 ngx_chain_t *ln; |
95 ngx_table_elt_t *header; | 96 ngx_table_elt_t *header; |
96 | 97 |
97 if (r->http_version < NGX_HTTP_VERSION_10) { | 98 if (r->http_version < NGX_HTTP_VERSION_10) { |
98 return NGX_OK; | 99 return NGX_OK; |
99 } | 100 } |
147 | 148 |
148 if (r->headers_out.date && r->headers_out.date->key.len) { | 149 if (r->headers_out.date && r->headers_out.date->key.len) { |
149 len += r->headers_out.date->key.len | 150 len += r->headers_out.date->key.len |
150 + r->headers_out.date->value.len + 2; | 151 + r->headers_out.date->value.len + 2; |
151 } else { | 152 } else { |
152 len += sizeof("Date: Mon, 28 Sep 1970 00:00:00 GMT" CRLF) - 1; | 153 len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1; |
153 } | 154 } |
154 | 155 |
155 if (r->headers_out.content_length == NULL) { | 156 if (r->headers_out.content_length == NULL) { |
156 if (r->headers_out.content_length_n >= 0) { | 157 if (r->headers_out.content_length_n >= 0) { |
157 /* 2^64 */ | 158 /* 2^64 */ |
172 if (r->headers_out.location | 173 if (r->headers_out.location |
173 && r->headers_out.location->value.len | 174 && r->headers_out.location->value.len |
174 && r->headers_out.location->value.data[0] == '/') | 175 && r->headers_out.location->value.data[0] == '/') |
175 { | 176 { |
176 r->headers_out.location->key.len = 0; | 177 r->headers_out.location->key.len = 0; |
177 len += sizeof("Location: http://") - 1, | 178 len += sizeof("Location: http://") - 1 |
178 + r->server_name->len + r->headers_out.location->value.len + 2; | 179 + r->server_name->len + r->headers_out.location->value.len + 2; |
179 | 180 |
180 if (r->port != 80) { | 181 if (r->port != 80) { |
181 len += r->port_name->len; | 182 len += r->port_name->len; |
182 } | 183 } |
185 if (r->headers_out.last_modified && r->headers_out.last_modified->key.len) { | 186 if (r->headers_out.last_modified && r->headers_out.last_modified->key.len) { |
186 len += r->headers_out.last_modified->key.len | 187 len += r->headers_out.last_modified->key.len |
187 + r->headers_out.last_modified->value.len + 2; | 188 + r->headers_out.last_modified->value.len + 2; |
188 | 189 |
189 } else if (r->headers_out.last_modified_time != -1) { | 190 } else if (r->headers_out.last_modified_time != -1) { |
190 len += sizeof("Last-Modified: Mon, 28 Sep 1970 00:00:00 GMT" CRLF) - 1; | 191 len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1; |
191 } | 192 } |
192 | 193 |
193 if (r->chunked) { | 194 if (r->chunked) { |
194 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1; | 195 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1; |
195 } | 196 } |
208 | 209 |
209 /* 2 is for ": " and 2 is for "\r\n" */ | 210 /* 2 is for ": " and 2 is for "\r\n" */ |
210 len += header[i].key.len + 2 + header[i].value.len + 2; | 211 len += header[i].key.len + 2 + header[i].value.len + 2; |
211 } | 212 } |
212 | 213 |
213 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len), NGX_ERROR); | 214 if (!(h = ngx_create_temp_hunk(r->pool, len))) { |
215 return NGX_ERROR; | |
216 } | |
214 | 217 |
215 /* "HTTP/1.x " */ | 218 /* "HTTP/1.x " */ |
216 h->last = ngx_cpymem(h->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1); | 219 h->last = ngx_cpymem(h->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1); |
217 | 220 |
218 /* status line */ | 221 /* status line */ |
230 h->last = ngx_cpymem(h->last, server_string, sizeof(server_string) - 1); | 233 h->last = ngx_cpymem(h->last, server_string, sizeof(server_string) - 1); |
231 } | 234 } |
232 | 235 |
233 if (!(r->headers_out.date && r->headers_out.date->key.len)) { | 236 if (!(r->headers_out.date && r->headers_out.date->key.len)) { |
234 h->last = ngx_cpymem(h->last, "Date: ", sizeof("Date: ") - 1); | 237 h->last = ngx_cpymem(h->last, "Date: ", sizeof("Date: ") - 1); |
235 h->last += ngx_http_get_time(h->last, time(NULL)); | 238 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) |
239 p = h->last; | |
240 #endif | |
241 h->last += ngx_http_get_time(h->last, ngx_time()); | |
242 | |
243 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
244 r->headers_out.date = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | |
245 if (r->headers_out.date == NULL) { | |
246 return NGX_ERROR; | |
247 } | |
248 | |
249 r->headers_out.date->key.len = 0; | |
250 r->headers_out.date->key.data = NULL; | |
251 r->headers_out.date->value.len = h->last - p; | |
252 r->headers_out.date->value.data = p; | |
253 #endif | |
254 | |
236 *(h->last++) = CR; *(h->last++) = LF; | 255 *(h->last++) = CR; *(h->last++) = LF; |
237 } | 256 } |
238 | 257 |
239 if (r->headers_out.content_length == NULL) { | 258 if (r->headers_out.content_length == NULL) { |
240 if (r->headers_out.content_length_n >= 0) { | 259 if (r->headers_out.content_length_n >= 0) { |
260 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
261 p = h->last + sizeof("Content-Length: ") - 1; | |
262 #endif | |
241 h->last += ngx_snprintf(h->last, /* 2^64 */ | 263 h->last += ngx_snprintf(h->last, /* 2^64 */ |
242 sizeof("Content-Length: 18446744073709551616" CRLF), | 264 sizeof("Content-Length: 18446744073709551616" CRLF), |
243 "Content-Length: " OFF_FMT CRLF, | 265 "Content-Length: " OFF_FMT CRLF, |
244 r->headers_out.content_length_n); | 266 r->headers_out.content_length_n); |
267 | |
268 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
269 r->headers_out.content_length = ngx_palloc(r->pool, | |
270 sizeof(ngx_table_elt_t)); | |
271 if (r->headers_out.content_length == NULL) { | |
272 return NGX_ERROR; | |
273 } | |
274 | |
275 r->headers_out.content_length->key.len = 0; | |
276 r->headers_out.content_length->key.data = NULL; | |
277 r->headers_out.content_length->value.len = h->last - p - 2; | |
278 r->headers_out.content_length->value.data = p; | |
279 #endif | |
245 } | 280 } |
246 } | 281 } |
247 | 282 |
248 if (r->headers_out.content_type && r->headers_out.content_type->value.len) { | 283 if (r->headers_out.content_type && r->headers_out.content_type->value.len) { |
249 h->last = ngx_cpymem(h->last, "Content-Type: ", | 284 h->last = ngx_cpymem(h->last, "Content-Type: ", |
250 sizeof("Content-Type: ") - 1); | 285 sizeof("Content-Type: ") - 1); |
286 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
287 p = h->last; | |
288 #endif | |
251 h->last = ngx_cpymem(h->last, r->headers_out.content_type->value.data, | 289 h->last = ngx_cpymem(h->last, r->headers_out.content_type->value.data, |
252 r->headers_out.content_type->value.len); | 290 r->headers_out.content_type->value.len); |
253 | 291 |
292 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
254 if (r->headers_out.charset.len) { | 293 if (r->headers_out.charset.len) { |
255 h->last = ngx_cpymem(h->last, "; charset=", | 294 h->last = ngx_cpymem(h->last, "; charset=", |
256 sizeof("; charset=") - 1); | 295 sizeof("; charset=") - 1); |
257 h->last = ngx_cpymem(h->last, r->headers_out.charset.data, | 296 h->last = ngx_cpymem(h->last, r->headers_out.charset.data, |
258 r->headers_out.charset.len); | 297 r->headers_out.charset.len); |
298 | |
299 r->headers_out.content_type->value.len = h->last - p; | |
300 r->headers_out.content_type->value.data = p; | |
301 #endif | |
259 } | 302 } |
260 | 303 |
261 *(h->last++) = CR; *(h->last++) = LF; | 304 *(h->last++) = CR; *(h->last++) = LF; |
262 } | 305 } |
263 | 306 |
264 if (r->headers_out.location | 307 if (r->headers_out.location |
265 && r->headers_out.location->value.len | 308 && r->headers_out.location->value.len |
266 && r->headers_out.location->value.data[0] == '/') | 309 && r->headers_out.location->value.data[0] == '/') |
267 { | 310 { |
311 p = h->last + sizeof("Location: ") - 1; | |
268 h->last = ngx_cpymem(h->last, "Location: http://", | 312 h->last = ngx_cpymem(h->last, "Location: http://", |
269 sizeof("Location: http://") - 1); | 313 sizeof("Location: http://") - 1); |
270 h->last = ngx_cpymem(h->last, r->server_name->data, | 314 h->last = ngx_cpymem(h->last, r->server_name->data, |
271 r->server_name->len); | 315 r->server_name->len); |
272 if (r->port != 80) { | 316 if (r->port != 80) { |
275 } | 319 } |
276 | 320 |
277 h->last = ngx_cpymem(h->last, r->headers_out.location->value.data, | 321 h->last = ngx_cpymem(h->last, r->headers_out.location->value.data, |
278 r->headers_out.location->value.len); | 322 r->headers_out.location->value.len); |
279 | 323 |
324 r->headers_out.location->value.len = h->last - p; | |
325 r->headers_out.location->value.data = p; | |
326 | |
280 *(h->last++) = CR; *(h->last++) = LF; | 327 *(h->last++) = CR; *(h->last++) = LF; |
281 } | 328 } |
282 | 329 |
283 if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len) | 330 if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len) |
284 && r->headers_out.last_modified_time != -1) | 331 && r->headers_out.last_modified_time != -1) |
285 { | 332 { |
286 h->last = ngx_cpymem(h->last, "Last-Modified: ", | 333 h->last = ngx_cpymem(h->last, "Last-Modified: ", |
287 sizeof("Last-Modified: ") - 1); | 334 sizeof("Last-Modified: ") - 1); |
335 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
336 p = h->last; | |
337 #endif | |
288 h->last += ngx_http_get_time(h->last, | 338 h->last += ngx_http_get_time(h->last, |
289 r->headers_out.last_modified_time); | 339 r->headers_out.last_modified_time); |
340 | |
341 #if (NGX_HTTP_LOG_ALL_HEADERS_OUT) | |
342 r->headers_out.last_modified = ngx_palloc(r->pool, | |
343 sizeof(ngx_table_elt_t)); | |
344 if (r->headers_out.last_modified == NULL) { | |
345 return NGX_ERROR; | |
346 } | |
347 | |
348 r->headers_out.last_modified->key.len = 0; | |
349 r->headers_out.last_modified->key.data = NULL; | |
350 r->headers_out.last_modified->value.len = h->last - p; | |
351 r->headers_out.last_modified->value.data = p; | |
352 #endif | |
353 | |
290 *(h->last++) = CR; *(h->last++) = LF; | 354 *(h->last++) = CR; *(h->last++) = LF; |
291 } | 355 } |
292 | 356 |
293 if (r->chunked) { | 357 if (r->chunked) { |
294 h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF, | 358 h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF, |
327 | 391 |
328 if (r->header_only) { | 392 if (r->header_only) { |
329 h->type |= NGX_HUNK_LAST; | 393 h->type |= NGX_HUNK_LAST; |
330 } | 394 } |
331 | 395 |
332 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); | 396 if (!(ln = ngx_alloc_chain_link(r->pool))) { |
333 | 397 return NGX_ERROR; |
334 ch->hunk = h; | 398 } |
335 ch->next = NULL; | 399 |
336 | 400 ln->hunk = h; |
337 return ngx_http_write_filter(r, ch); | 401 ln->next = NULL; |
402 | |
403 return ngx_http_write_filter(r, ln); | |
338 } | 404 } |
339 | 405 |
340 | 406 |
341 static int ngx_http_header_filter_init(ngx_cycle_t *cycle) | 407 static int ngx_http_header_filter_init(ngx_cycle_t *cycle) |
342 { | 408 { |