Mercurial > hg > nginx
comparison src/event/ngx_event_proxy.c @ 76:6127d7075471
nginx-0.0.1-2003-04-15-19:06:52 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 15 Apr 2003 15:06:52 +0000 |
parents | 869b10be682f |
children | 57c2e18d3572 |
comparison
equal
deleted
inserted
replaced
75:869b10be682f | 76:6127d7075471 |
---|---|
1 | 1 |
2 #include <ngx_event_proxy.h> | 2 #include <ngx_event_proxy.h> |
3 | |
4 | |
5 #define NGX_EVENT_COPY_FILTER 0 | |
6 | |
7 #if (NGX_EVENT_COPY_FILTER) | |
8 static int ngx_event_proxy_copy_input_filter(ngx_event_proxy_t *p, | |
9 ngx_chain_t *chain); | |
10 #endif | |
11 | |
3 | 12 |
4 | 13 |
5 int ngx_event_proxy_read_upstream(ngx_event_proxy_t *p) | 14 int ngx_event_proxy_read_upstream(ngx_event_proxy_t *p) |
6 { | 15 { |
7 int n, rc, size; | 16 int n, rc, size; |
8 ngx_hunk_t *h, *nh; | 17 ngx_hunk_t *h, *nh; |
9 ngx_chain_t *chain, *temp, *entry, *next; | 18 ngx_chain_t *chain, *temp, *entry, *next; |
10 | 19 |
20 #if (NGX_EVENT_COPY_FILTER) | |
21 | |
22 if (p->input_filter == NULL) { | |
23 p->input_filter = ngx_event_proxy_copy_input_filter; | |
24 } | |
25 | |
26 #endif | |
27 | |
11 p->level++; | 28 p->level++; |
12 | 29 |
13 ngx_log_debug(p->log, "read upstream"); | 30 ngx_log_debug(p->log, "read upstream"); |
14 | 31 |
15 for ( ;; ) { | 32 for ( ;; ) { |
43 | 60 |
44 } else if (p->shadow_hunks) { | 61 } else if (p->shadow_hunks) { |
45 chain = p->shadow_hunks; | 62 chain = p->shadow_hunks; |
46 p->shadow_hunks = NULL; | 63 p->shadow_hunks = NULL; |
47 | 64 |
48 ngx_log_debug(p->log, "shadow hunk: %08X" _ chain->hunk); | 65 ngx_log_debug(p->log, "shadow hunk: %08X" _ chain->hunk _ |
66 chain->hunk->end - chain->hunk->last); | |
49 | 67 |
50 /* if it's allowed then save the incoming hunks to a temporary file, | 68 /* if it's allowed then save the incoming hunks to a temporary file, |
51 move the saved hunks to a shadow chain, | 69 move the saved hunks to a shadow chain, |
52 and add the file hunks to an outgoing chain */ | 70 and add the file hunks to an outgoing chain */ |
53 | 71 |
61 } | 79 } |
62 | 80 |
63 chain = p->shadow_hunks; | 81 chain = p->shadow_hunks; |
64 p->shadow_hunks = NULL; | 82 p->shadow_hunks = NULL; |
65 | 83 |
66 ngx_log_debug(p->log, "new shadow hunk: %08X" _ chain->hunk); | 84 ngx_log_debug(p->log, "new shadow hunk: %08X:%d" _ chain->hunk _ |
85 chain->hunk->end - chain->hunk->last); | |
67 | 86 |
68 /* if there're no hunks to read in then disable a level event */ | 87 /* if there're no hunks to read in then disable a level event */ |
69 | 88 |
70 } else { | 89 } else { |
71 if (ngx_event_flags & NGX_USE_LEVEL_EVENT) { | 90 if (ngx_event_flags & NGX_USE_LEVEL_EVENT) { |
95 | 114 |
96 return NGX_AGAIN; | 115 return NGX_AGAIN; |
97 } | 116 } |
98 | 117 |
99 if (n == 0) { | 118 if (n == 0) { |
100 p->free_hunks = chain; | 119 if (chain->hunk->shadow == NULL) { |
120 p->free_hunks = chain; | |
121 } | |
101 p->upstream_eof = 1; | 122 p->upstream_eof = 1; |
102 p->block_upstream = 0; | 123 p->block_upstream = 0; |
103 break; | 124 break; |
104 } | 125 } |
105 | 126 |
145 | 166 |
146 p->last_read_hunk = entry; | 167 p->last_read_hunk = entry; |
147 | 168 |
148 n -= size; | 169 n -= size; |
149 | 170 |
150 /* the copy input filter */ | 171 #if !(NGX_EVENT_COPY_FILTER) |
151 | 172 |
152 if (p->input_filter == NULL) { | 173 if (p->input_filter) { |
153 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR); | 174 continue; |
154 ngx_memcpy(h, entry->hunk, sizeof(ngx_hunk_t)); | 175 } |
155 h->shadow = entry->hunk; | 176 |
156 h->type |= NGX_HUNK_LAST_SHADOW; | 177 /* the inline copy input filter */ |
157 | 178 |
158 ngx_test_null(temp, ngx_alloc_chain_entry(p->pool), | 179 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR); |
159 NGX_ERROR); | 180 ngx_memcpy(h, entry->hunk, sizeof(ngx_hunk_t)); |
160 temp->hunk = h; | 181 h->shadow = entry->hunk; |
161 temp->next = NULL; | 182 h->type |= NGX_HUNK_LAST_SHADOW; |
162 | 183 entry->hunk->shadow = h; |
163 if (p->in_hunks) { | 184 |
164 p->last_in_hunk->next = temp; | 185 ngx_test_null(temp, ngx_alloc_chain_entry(p->pool), |
165 | 186 NGX_ERROR); |
166 } else { | 187 temp->hunk = h; |
167 p->in_hunks = temp; | 188 temp->next = NULL; |
168 } | 189 |
169 | 190 if (p->in_hunks) { |
170 p->last_in_hunk = temp; | 191 p->last_in_hunk->next = temp; |
171 } | 192 |
193 } else { | |
194 p->in_hunks = temp; | |
195 } | |
196 | |
197 p->last_in_hunk = temp; | |
198 #endif | |
172 | 199 |
173 } else { | 200 } else { |
174 entry->hunk->last += n; | 201 entry->hunk->last += n; |
175 p->free_hunks = entry; | 202 p->free_hunks = entry; |
176 | 203 |
177 n = 0; | 204 n = 0; |
178 } | 205 } |
179 } | 206 } |
180 | 207 |
208 if (chain == p->free_hunks) { | |
209 chain = NULL; | |
210 } | |
211 | |
212 /* the input filter i.e. that moves HTTP/1.1 chunks | |
213 from a read chain to an incoming chain */ | |
214 | |
215 if (p->input_filter) { | |
216 if (p->input_filter(p, chain) == NGX_ERROR) { | |
217 return NGX_ERROR; | |
218 } | |
219 } | |
220 | |
181 ngx_log_debug(p->log, "rest chain: %08X" _ entry); | 221 ngx_log_debug(p->log, "rest chain: %08X" _ entry); |
182 | 222 |
183 /* if the rest hunks are shadow then move them to a shadow chain | 223 /* if the rest hunks are shadow then move them to a shadow chain |
184 otherwise add them to a free chain */ | 224 otherwise add them to a free chain */ |
185 | 225 |
197 } | 237 } |
198 | 238 |
199 p->block_upstream = 0; | 239 p->block_upstream = 0; |
200 break; | 240 break; |
201 } | 241 } |
202 | |
203 /* the input filter i.e. that moves HTTP/1.1 chunks | |
204 from a read chain to an incoming chain */ | |
205 | |
206 if (p->input_filter) { | |
207 if (p->input_filter(p) == NGX_ERROR) { | |
208 return NGX_ERROR; | |
209 } | |
210 } | |
211 } | 242 } |
212 | 243 |
213 ngx_log_debug(p->log, "eof: %d block: %d" _ | 244 ngx_log_debug(p->log, "eof: %d block: %d" _ |
214 p->upstream_eof _ p->block_upstream); | 245 p->upstream_eof _ p->block_upstream); |
215 | 246 |
220 p->upstream->read->ready = 0; | 251 p->upstream->read->ready = 0; |
221 | 252 |
222 if (p->free_hunks | 253 if (p->free_hunks |
223 && p->free_hunks->hunk->pos < p->free_hunks->hunk->last) | 254 && p->free_hunks->hunk->pos < p->free_hunks->hunk->last) |
224 { | 255 { |
256 | |
257 #if (NGX_EVENT_COPY_FILTER) | |
258 | |
259 if (p->input_filter(p, NULL) == NGX_ERROR) { | |
260 return NGX_ERROR; | |
261 } | |
262 #else | |
225 if (p->input_filter) { | 263 if (p->input_filter) { |
226 if (p->input_filter(p) == NGX_ERROR) { | 264 if (p->input_filter(p, NULL) == NGX_ERROR) { |
227 return NGX_ERROR; | 265 return NGX_ERROR; |
228 } | 266 } |
229 | 267 |
230 } else { | 268 } else { |
231 entry = p->free_hunks; | 269 entry = p->free_hunks; |
240 p->last_in_hunk = entry; | 278 p->last_in_hunk = entry; |
241 } | 279 } |
242 | 280 |
243 p->free_hunks = entry->next; | 281 p->free_hunks = entry->next; |
244 entry->next = NULL; | 282 entry->next = NULL; |
283 #endif | |
245 } | 284 } |
246 | 285 |
247 #if 0 | 286 #if 0 |
248 /* free the unneeded hunks */ | 287 /* free the unneeded hunks */ |
249 | 288 |
312 if (h == NULL) { | 351 if (h == NULL) { |
313 if (p->out_hunks) { | 352 if (p->out_hunks) { |
314 entry = p->out_hunks; | 353 entry = p->out_hunks; |
315 p->out_hunks = entry->next; | 354 p->out_hunks = entry->next; |
316 h = entry->hunk; | 355 h = entry->hunk; |
356 entry->next = NULL; | |
317 | 357 |
318 if (p->shadow_hunks) { | 358 if (p->shadow_hunks) { |
319 if (p->shadow_hunks->hunk == h->shadow) { | 359 if (p->shadow_hunks->hunk == h->shadow) { |
320 p->shadow_hunks = p->shadow_hunks->next; | 360 p->shadow_hunks = p->shadow_hunks->next; |
321 } | 361 } |
322 } | 362 } |
323 | 363 |
324 entry->next = NULL; | |
325 | 364 |
326 } else if (p->cachable == 0 && p->in_hunks) { | 365 } else if (p->cachable == 0 && p->in_hunks) { |
327 entry = p->in_hunks; | 366 entry = p->in_hunks; |
328 p->in_hunks = entry->next; | 367 p->in_hunks = entry->next; |
329 h = entry->hunk; | 368 h = entry->hunk; |
330 entry->next = NULL; | 369 entry->next = NULL; |
331 } | 370 |
332 | 371 if (p->read_hunks) { |
333 ngx_log_debug(p->log, "event proxy write hunk: %08X:%08X" _ h _ h->pos); | 372 if (p->read_hunks->hunk == h->shadow) { |
373 p->read_hunks = p->read_hunks->next; | |
374 | |
375 } else { | |
376 ngx_log_error(NGX_LOG_CRIT, p->log, 0, "ERROR !!!"); | |
377 } | |
378 } | |
379 } | |
380 | |
381 ngx_log_debug(p->log, "event proxy write hunk: %08X" _ h); | |
334 | 382 |
335 if (h == NULL) { | 383 if (h == NULL) { |
336 if (p->upstream->read->ready) { | 384 if (p->upstream->read->ready) { |
337 rc = ngx_event_proxy_read_upstream(p); | 385 rc = ngx_event_proxy_read_upstream(p); |
338 } | 386 } |
339 | 387 |
340 return NGX_OK; | 388 return NGX_OK; |
341 } | 389 } |
342 } | 390 } |
343 | 391 |
392 #if 0 | |
344 ngx_log_debug(p->log, "event proxy write: %d" _ h->last - h->pos); | 393 ngx_log_debug(p->log, "event proxy write: %d" _ h->last - h->pos); |
394 #endif | |
345 | 395 |
346 rc = p->output_filter(p->output_data, h); | 396 rc = p->output_filter(p->output_data, h); |
347 | 397 |
348 ngx_log_debug(p->log, "event proxy: %d" _ rc); | 398 ngx_log_debug(p->log, "event proxy: %d" _ rc); |
349 | 399 |
483 h->file = p->temp_file; | 533 h->file = p->temp_file; |
484 h->file_pos = p->temp_offset; | 534 h->file_pos = p->temp_offset; |
485 p->temp_offset += h->last - h->pos; | 535 p->temp_offset += h->last - h->pos; |
486 h->file_last = p->temp_offset; | 536 h->file_last = p->temp_offset; |
487 | 537 |
488 ngx_log_debug(p->log, "event proxy file hunk: %08X:%08X" _ h _ h->pos); | 538 ngx_log_debug(p->log, "event proxy file hunk: %08X:%08X" _ h _ h->shadow); |
489 | 539 |
490 if (entry->hunk->type & NGX_HUNK_LAST_SHADOW) { | 540 if (h->type & NGX_HUNK_LAST_SHADOW) { |
491 entry->hunk->shadow->last = entry->hunk->shadow->pos; | 541 h->shadow->last = h->shadow->pos; |
492 } | 542 } |
493 | 543 |
494 if (p->out_hunks) { | 544 if (p->out_hunks) { |
495 p->last_out_hunk->next = entry; | 545 p->last_out_hunk->next = entry; |
496 | 546 |
506 p->read_hunks = saved_read; | 556 p->read_hunks = saved_read; |
507 p->in_hunks = saved_in; | 557 p->in_hunks = saved_in; |
508 | 558 |
509 return NGX_OK; | 559 return NGX_OK; |
510 } | 560 } |
561 | |
562 #if (NGX_EVENT_COPY_FILTER) | |
563 | |
564 /* the copy input filter */ | |
565 | |
566 static int ngx_event_proxy_copy_input_filter(ngx_event_proxy_t *p, | |
567 ngx_chain_t *chain) | |
568 { | |
569 ngx_hunk_t *h; | |
570 ngx_chain_t *entry, *temp; | |
571 | |
572 if (p->upstream_eof) { | |
573 entry = p->free_hunks; | |
574 | |
575 if (p->in_hunks) { | |
576 p->last_in_hunk->next = entry; | |
577 | |
578 } else { | |
579 p->in_hunks = entry; | |
580 } | |
581 | |
582 p->last_in_hunk = entry; | |
583 | |
584 p->free_hunks = entry->next; | |
585 entry->next = NULL; | |
586 | |
587 return NGX_OK; | |
588 } | |
589 | |
590 for (entry = chain; entry; entry = entry->next) { | |
591 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR); | |
592 ngx_memcpy(h, entry->hunk, sizeof(ngx_hunk_t)); | |
593 h->shadow = entry->hunk; | |
594 h->type |= NGX_HUNK_LAST_SHADOW; | |
595 entry->hunk->shadow = h; | |
596 | |
597 ngx_test_null(temp, ngx_alloc_chain_entry(p->pool), NGX_ERROR); | |
598 temp->hunk = h; | |
599 temp->next = NULL; | |
600 | |
601 if (p->in_hunks) { | |
602 p->last_in_hunk->next = temp; | |
603 | |
604 } else { | |
605 p->in_hunks = temp; | |
606 } | |
607 | |
608 p->last_in_hunk = temp; | |
609 } | |
610 | |
611 return NGX_OK; | |
612 } | |
613 | |
614 #endif |