Mercurial > hg > nginx
comparison src/http/ngx_http_header_filter.c @ 343:6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 28 May 2004 15:49:23 +0000 |
parents | 5cfd65b8b0a7 |
children | 2e3cbc1bbe3c |
comparison
equal
deleted
inserted
replaced
342:0ee0642af5f1 | 343:6bdf858bff8c |
---|---|
93 static int ngx_http_header_filter(ngx_http_request_t *r) | 93 static int ngx_http_header_filter(ngx_http_request_t *r) |
94 { | 94 { |
95 u_char *p; | 95 u_char *p; |
96 size_t len; | 96 size_t len; |
97 ngx_uint_t status, i; | 97 ngx_uint_t status, i; |
98 ngx_hunk_t *h; | 98 ngx_buf_t *b; |
99 ngx_chain_t *ln; | 99 ngx_chain_t *ln; |
100 ngx_table_elt_t *header; | 100 ngx_table_elt_t *header; |
101 | 101 |
102 if (r->http_version < NGX_HTTP_VERSION_10) { | 102 if (r->http_version < NGX_HTTP_VERSION_10) { |
103 return NGX_OK; | 103 return NGX_OK; |
213 | 213 |
214 /* 2 is for ": " and 2 is for "\r\n" */ | 214 /* 2 is for ": " and 2 is for "\r\n" */ |
215 len += header[i].key.len + 2 + header[i].value.len + 2; | 215 len += header[i].key.len + 2 + header[i].value.len + 2; |
216 } | 216 } |
217 | 217 |
218 if (!(h = ngx_create_temp_hunk(r->pool, len))) { | 218 if (!(b = ngx_create_temp_buf(r->pool, len))) { |
219 return NGX_ERROR; | 219 return NGX_ERROR; |
220 } | 220 } |
221 | 221 |
222 /* "HTTP/1.x " */ | 222 /* "HTTP/1.x " */ |
223 h->last = ngx_cpymem(h->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1); | 223 b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1); |
224 | 224 |
225 /* status line */ | 225 /* status line */ |
226 if (r->headers_out.status_line.len) { | 226 if (r->headers_out.status_line.len) { |
227 h->last = ngx_cpymem(h->last, r->headers_out.status_line.data, | 227 b->last = ngx_cpymem(b->last, r->headers_out.status_line.data, |
228 r->headers_out.status_line.len); | 228 r->headers_out.status_line.len); |
229 | 229 |
230 } else { | 230 } else { |
231 h->last = ngx_cpymem(h->last, http_codes[status].data, | 231 b->last = ngx_cpymem(b->last, http_codes[status].data, |
232 http_codes[status].len); | 232 http_codes[status].len); |
233 } | 233 } |
234 *(h->last++) = CR; *(h->last++) = LF; | 234 *(b->last++) = CR; *(b->last++) = LF; |
235 | 235 |
236 if (!(r->headers_out.server && r->headers_out.server->key.len)) { | 236 if (!(r->headers_out.server && r->headers_out.server->key.len)) { |
237 h->last = ngx_cpymem(h->last, server_string, sizeof(server_string) - 1); | 237 b->last = ngx_cpymem(b->last, server_string, sizeof(server_string) - 1); |
238 } | 238 } |
239 | 239 |
240 if (!(r->headers_out.date && r->headers_out.date->key.len)) { | 240 if (!(r->headers_out.date && r->headers_out.date->key.len)) { |
241 h->last = ngx_cpymem(h->last, "Date: ", sizeof("Date: ") - 1); | 241 b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1); |
242 h->last = ngx_cpymem(h->last, ngx_cached_http_time.data, | 242 b->last = ngx_cpymem(b->last, ngx_cached_http_time.data, |
243 ngx_cached_http_time.len); | 243 ngx_cached_http_time.len); |
244 | 244 |
245 *(h->last++) = CR; *(h->last++) = LF; | 245 *(b->last++) = CR; *(b->last++) = LF; |
246 } | 246 } |
247 | 247 |
248 if (r->headers_out.content_length == NULL) { | 248 if (r->headers_out.content_length == NULL) { |
249 if (r->headers_out.content_length_n >= 0) { | 249 if (r->headers_out.content_length_n >= 0) { |
250 h->last += ngx_snprintf((char *) h->last, | 250 b->last += ngx_snprintf((char *) b->last, |
251 sizeof("Content-Length: ") + NGX_OFF_T_LEN + 2, | 251 sizeof("Content-Length: ") + NGX_OFF_T_LEN + 2, |
252 "Content-Length: " OFF_T_FMT CRLF, | 252 "Content-Length: " OFF_T_FMT CRLF, |
253 r->headers_out.content_length_n); | 253 r->headers_out.content_length_n); |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 if (r->headers_out.content_type && r->headers_out.content_type->value.len) { | 257 if (r->headers_out.content_type && r->headers_out.content_type->value.len) { |
258 h->last = ngx_cpymem(h->last, "Content-Type: ", | 258 b->last = ngx_cpymem(b->last, "Content-Type: ", |
259 sizeof("Content-Type: ") - 1); | 259 sizeof("Content-Type: ") - 1); |
260 p = h->last; | 260 p = b->last; |
261 h->last = ngx_cpymem(h->last, r->headers_out.content_type->value.data, | 261 b->last = ngx_cpymem(b->last, r->headers_out.content_type->value.data, |
262 r->headers_out.content_type->value.len); | 262 r->headers_out.content_type->value.len); |
263 | 263 |
264 if (r->headers_out.charset.len) { | 264 if (r->headers_out.charset.len) { |
265 h->last = ngx_cpymem(h->last, "; charset=", | 265 b->last = ngx_cpymem(b->last, "; charset=", |
266 sizeof("; charset=") - 1); | 266 sizeof("; charset=") - 1); |
267 h->last = ngx_cpymem(h->last, r->headers_out.charset.data, | 267 b->last = ngx_cpymem(b->last, r->headers_out.charset.data, |
268 r->headers_out.charset.len); | 268 r->headers_out.charset.len); |
269 | 269 |
270 r->headers_out.content_type->value.len = h->last - p; | 270 r->headers_out.content_type->value.len = b->last - p; |
271 r->headers_out.content_type->value.data = p; | 271 r->headers_out.content_type->value.data = p; |
272 } | 272 } |
273 | 273 |
274 *(h->last++) = CR; *(h->last++) = LF; | 274 *(b->last++) = CR; *(b->last++) = LF; |
275 } | 275 } |
276 | 276 |
277 if (r->headers_out.location | 277 if (r->headers_out.location |
278 && r->headers_out.location->value.len | 278 && r->headers_out.location->value.len |
279 && r->headers_out.location->value.data[0] == '/') | 279 && r->headers_out.location->value.data[0] == '/') |
280 { | 280 { |
281 p = h->last + sizeof("Location: ") - 1; | 281 p = b->last + sizeof("Location: ") - 1; |
282 h->last = ngx_cpymem(h->last, "Location: http://", | 282 b->last = ngx_cpymem(b->last, "Location: http://", |
283 sizeof("Location: http://") - 1); | 283 sizeof("Location: http://") - 1); |
284 h->last = ngx_cpymem(h->last, r->server_name->data, | 284 b->last = ngx_cpymem(b->last, r->server_name->data, |
285 r->server_name->len); | 285 r->server_name->len); |
286 if (r->port != 80) { | 286 if (r->port != 80) { |
287 h->last = ngx_cpymem(h->last, r->port_name->data, | 287 b->last = ngx_cpymem(b->last, r->port_name->data, |
288 r->port_name->len); | 288 r->port_name->len); |
289 } | 289 } |
290 | 290 |
291 h->last = ngx_cpymem(h->last, r->headers_out.location->value.data, | 291 b->last = ngx_cpymem(b->last, r->headers_out.location->value.data, |
292 r->headers_out.location->value.len); | 292 r->headers_out.location->value.len); |
293 | 293 |
294 r->headers_out.location->value.len = h->last - p; | 294 r->headers_out.location->value.len = b->last - p; |
295 r->headers_out.location->value.data = p; | 295 r->headers_out.location->value.data = p; |
296 | 296 |
297 *(h->last++) = CR; *(h->last++) = LF; | 297 *(b->last++) = CR; *(b->last++) = LF; |
298 } | 298 } |
299 | 299 |
300 if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len) | 300 if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len) |
301 && r->headers_out.last_modified_time != -1) | 301 && r->headers_out.last_modified_time != -1) |
302 { | 302 { |
303 h->last = ngx_cpymem(h->last, "Last-Modified: ", | 303 b->last = ngx_cpymem(b->last, "Last-Modified: ", |
304 sizeof("Last-Modified: ") - 1); | 304 sizeof("Last-Modified: ") - 1); |
305 h->last += ngx_http_time(h->last, r->headers_out.last_modified_time); | 305 b->last += ngx_http_time(b->last, r->headers_out.last_modified_time); |
306 | 306 |
307 *(h->last++) = CR; *(h->last++) = LF; | 307 *(b->last++) = CR; *(b->last++) = LF; |
308 } | 308 } |
309 | 309 |
310 if (r->chunked) { | 310 if (r->chunked) { |
311 h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF, | 311 b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF, |
312 sizeof("Transfer-Encoding: chunked" CRLF) - 1); | 312 sizeof("Transfer-Encoding: chunked" CRLF) - 1); |
313 } | 313 } |
314 | 314 |
315 if (r->keepalive) { | 315 if (r->keepalive) { |
316 h->last = ngx_cpymem(h->last, "Connection: keep-alive" CRLF, | 316 b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF, |
317 sizeof("Connection: keep-alive" CRLF) - 1); | 317 sizeof("Connection: keep-alive" CRLF) - 1); |
318 | 318 |
319 } else { | 319 } else { |
320 h->last = ngx_cpymem(h->last, "Connection: close" CRLF, | 320 b->last = ngx_cpymem(b->last, "Connection: close" CRLF, |
321 sizeof("Connection: close" CRLF) - 1); | 321 sizeof("Connection: close" CRLF) - 1); |
322 } | 322 } |
323 | 323 |
324 for (i = 0; i < r->headers_out.headers.nelts; i++) { | 324 for (i = 0; i < r->headers_out.headers.nelts; i++) { |
325 if (header[i].key.len == 0) { | 325 if (header[i].key.len == 0) { |
326 continue; | 326 continue; |
327 } | 327 } |
328 | 328 |
329 h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len); | 329 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len); |
330 *(h->last++) = ':' ; *(h->last++) = ' ' ; | 330 *(b->last++) = ':' ; *(b->last++) = ' ' ; |
331 | 331 |
332 h->last = ngx_cpymem(h->last, header[i].value.data, | 332 b->last = ngx_cpymem(b->last, header[i].value.data, |
333 header[i].value.len); | 333 header[i].value.len); |
334 *(h->last++) = CR; *(h->last++) = LF; | 334 *(b->last++) = CR; *(b->last++) = LF; |
335 } | 335 } |
336 | 336 |
337 #if (NGX_DEBUG) | 337 #if (NGX_DEBUG) |
338 *(h->last) = '\0'; | 338 *(b->last) = '\0'; |
339 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "%s\n", h->pos); | 339 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "%s\n", b->pos); |
340 #endif | 340 #endif |
341 | 341 |
342 /* the end of HTTP header */ | 342 /* the end of HTTP header */ |
343 *(h->last++) = CR; *(h->last++) = LF; | 343 *(b->last++) = CR; *(b->last++) = LF; |
344 | 344 |
345 r->header_size = h->last - h->pos; | 345 r->header_size = b->last - b->pos; |
346 | 346 |
347 if (r->header_only) { | 347 if (r->header_only) { |
348 h->type |= NGX_HUNK_LAST; | 348 b->last_buf = 1; |
349 } | 349 } |
350 | 350 |
351 if (!(ln = ngx_alloc_chain_link(r->pool))) { | 351 if (!(ln = ngx_alloc_chain_link(r->pool))) { |
352 return NGX_ERROR; | 352 return NGX_ERROR; |
353 } | 353 } |
354 | 354 |
355 ln->hunk = h; | 355 ln->buf = b; |
356 ln->next = NULL; | 356 ln->next = NULL; |
357 | 357 |
358 return ngx_http_write_filter(r, ln); | 358 return ngx_http_write_filter(r, ln); |
359 } | 359 } |
360 | 360 |