Mercurial > hg > nginx
annotate src/http/modules/perl/nginx.xs @ 2109:3db8a8263055 stable-0.5
r2015 merge:
avoid recursive loop
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 07 Jul 2008 11:50:21 +0000 |
parents | 6b81bbc36eaf |
children |
rev | line source |
---|---|
599 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
882
26c3e48b9996
the PERL_NO_GET_CONTEXT is actually required, see perlguts
Igor Sysoev <igor@sysoev.ru>
parents:
869
diff
changeset
|
7 #define PERL_NO_GET_CONTEXT |
26c3e48b9996
the PERL_NO_GET_CONTEXT is actually required, see perlguts
Igor Sysoev <igor@sysoev.ru>
parents:
869
diff
changeset
|
8 |
599 | 9 #include <ngx_config.h> |
10 #include <ngx_core.h> | |
11 #include <ngx_http.h> | |
12 #include <ngx_http_perl_module.h> | |
13 | |
603 | 14 #include "XSUB.h" |
15 | |
633 | 16 #define ngx_http_perl_set_request(r) \ |
17 r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0)))) | |
18 | |
19 | |
20 #define ngx_http_perl_set_targ(p, len, z) \ | |
21 \ | |
22 sv_upgrade(TARG, SVt_PV); \ | |
23 SvPOK_on(TARG); \ | |
24 SvPV_set(TARG, (char *) p); \ | |
25 SvLEN_set(TARG, len + z); \ | |
26 SvCUR_set(TARG, len); \ | |
27 SvFAKE_on(TARG); \ | |
28 SvREADONLY_on(TARG); \ | |
29 | |
599 | 30 |
31 static ngx_int_t | |
32 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv) | |
33 { | |
34 u_char *p; | |
35 STRLEN len; | |
36 | |
37 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
38 sv = SvRV(sv); | |
39 } | |
40 | |
41 p = (u_char *) SvPV(sv, len); | |
42 | |
43 s->len = len; | |
44 | |
45 if (SvREADONLY(sv)) { | |
46 s->data = p; | |
47 return NGX_OK; | |
48 } | |
49 | |
50 s->data = ngx_palloc(r->pool, len); | |
51 if (s->data == NULL) { | |
52 return NGX_ERROR; | |
53 } | |
54 | |
55 ngx_memcpy(s->data, p, len); | |
56 | |
57 return NGX_OK; | |
58 } | |
59 | |
60 | |
61 static ngx_int_t | |
62 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b) | |
63 { | |
617 | 64 ngx_chain_t out; |
65 #if (NGX_HTTP_SSI) | |
66 ngx_chain_t *cl; | |
599 | 67 ngx_http_perl_ctx_t *ctx; |
68 | |
69 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
70 | |
71 if (ctx->ssi) { | |
72 cl = ngx_alloc_chain_link(r->pool); | |
73 if (cl == NULL) { | |
74 return NGX_ERROR; | |
75 } | |
76 | |
77 cl->buf = b; | |
78 cl->next = NULL; | |
79 *ctx->ssi->last_out = cl; | |
80 ctx->ssi->last_out = &cl->next; | |
81 | |
82 return NGX_OK; | |
83 } | |
617 | 84 #endif |
599 | 85 |
86 out.buf = b; | |
87 out.next = NULL; | |
88 | |
89 return ngx_http_output_filter(r, &out); | |
90 } | |
91 | |
92 | |
93 MODULE = nginx PACKAGE = nginx | |
94 | |
95 | |
633 | 96 void |
915 | 97 status(r, code) |
98 CODE: | |
99 | |
100 ngx_http_request_t *r; | |
101 | |
102 ngx_http_perl_set_request(r); | |
103 | |
104 r->headers_out.status = SvIV(ST(1)); | |
105 | |
106 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
107 "perl status: %d", r->headers_out.status); | |
108 | |
109 XSRETURN_UNDEF; | |
110 | |
111 | |
112 void | |
599 | 113 send_http_header(r, ...) |
633 | 114 CODE: |
599 | 115 |
633 | 116 ngx_http_request_t *r; |
117 SV *sv; | |
599 | 118 |
633 | 119 ngx_http_perl_set_request(r); |
599 | 120 |
121 if (r->headers_out.status == 0) { | |
122 r->headers_out.status = NGX_HTTP_OK; | |
123 } | |
124 | |
125 if (items != 1) { | |
126 sv = ST(1); | |
127 | |
128 if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv) | |
129 != NGX_OK) | |
130 { | |
633 | 131 XSRETURN_EMPTY; |
599 | 132 } |
133 | |
1531 | 134 r->headers_out.content_type_len = r->headers_out.content_type.len; |
135 | |
599 | 136 } else { |
673 | 137 if (ngx_http_set_content_type(r) != NGX_OK) { |
138 XSRETURN_EMPTY; | |
599 | 139 } |
140 } | |
141 | |
633 | 142 (void) ngx_http_send_header(r); |
599 | 143 |
144 | |
633 | 145 void |
146 header_only(r) | |
599 | 147 CODE: |
148 | |
633 | 149 dXSTARG; |
150 ngx_http_request_t *r; | |
151 | |
152 ngx_http_perl_set_request(r); | |
599 | 153 |
633 | 154 sv_upgrade(TARG, SVt_IV); |
155 sv_setiv(TARG, r->header_only); | |
599 | 156 |
633 | 157 ST(0) = TARG; |
599 | 158 |
159 | |
633 | 160 void |
161 uri(r) | |
162 CODE: | |
163 | |
164 dXSTARG; | |
165 ngx_http_request_t *r; | |
599 | 166 |
633 | 167 ngx_http_perl_set_request(r); |
168 ngx_http_perl_set_targ(r->uri.data, r->uri.len, 0); | |
169 | |
170 ST(0) = TARG; | |
171 | |
172 | |
173 void | |
174 args(r) | |
599 | 175 CODE: |
176 | |
633 | 177 dXSTARG; |
178 ngx_http_request_t *r; | |
599 | 179 |
633 | 180 ngx_http_perl_set_request(r); |
181 ngx_http_perl_set_targ(r->args.data, r->args.len, 0); | |
599 | 182 |
633 | 183 ST(0) = TARG; |
599 | 184 |
185 | |
633 | 186 void |
629 | 187 request_method(r) |
633 | 188 CODE: |
189 | |
190 dXSTARG; | |
191 ngx_http_request_t *r; | |
629 | 192 |
633 | 193 ngx_http_perl_set_request(r); |
194 ngx_http_perl_set_targ(r->method_name.data, r->method_name.len, 0); | |
195 | |
196 ST(0) = TARG; | |
197 | |
198 | |
199 void | |
200 remote_addr(r) | |
629 | 201 CODE: |
202 | |
633 | 203 dXSTARG; |
204 ngx_http_request_t *r; | |
629 | 205 |
633 | 206 ngx_http_perl_set_request(r); |
207 ngx_http_perl_set_targ(r->connection->addr_text.data, | |
208 r->connection->addr_text.len, 1); | |
629 | 209 |
633 | 210 ST(0) = TARG; |
629 | 211 |
212 | |
633 | 213 void |
214 header_in(r, key) | |
629 | 215 CODE: |
216 | |
633 | 217 dXSTARG; |
667 | 218 ngx_http_request_t *r; |
219 SV *key; | |
220 u_char *p, *lowcase_key, *cookie; | |
221 STRLEN len; | |
222 ssize_t size; | |
223 ngx_uint_t i, n, hash; | |
224 ngx_list_part_t *part; | |
225 ngx_table_elt_t *h, **ph; | |
226 ngx_http_header_t *hh; | |
227 ngx_http_core_main_conf_t *cmcf; | |
629 | 228 |
633 | 229 ngx_http_perl_set_request(r); |
599 | 230 |
633 | 231 key = ST(1); |
599 | 232 |
233 if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) { | |
234 key = SvRV(key); | |
235 } | |
236 | |
237 p = (u_char *) SvPV(key, len); | |
238 | |
667 | 239 /* look up hashed headers */ |
240 | |
241 lowcase_key = ngx_palloc(r->pool, len); | |
242 if (lowcase_key == NULL) { | |
243 XSRETURN_UNDEF; | |
244 } | |
245 | |
246 hash = 0; | |
247 for (i = 0; i < len; i++) { | |
248 lowcase_key[i] = ngx_tolower(p[i]); | |
249 hash = ngx_hash(hash, lowcase_key[i]); | |
250 } | |
251 | |
252 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
253 | |
254 hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len); | |
255 | |
256 if (hh) { | |
257 if (hh->offset) { | |
258 | |
259 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset); | |
260 | |
261 if (*ph) { | |
262 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len, 0); | |
263 | |
264 goto done; | |
265 } | |
266 | |
267 XSRETURN_UNDEF; | |
268 } | |
269 | |
270 /* Cookie */ | |
271 | |
272 n = r->headers_in.cookies.nelts; | |
273 | |
274 if (n == 0) { | |
275 XSRETURN_UNDEF; | |
276 } | |
277 | |
278 ph = r->headers_in.cookies.elts; | |
279 | |
280 if (n == 1) { | |
281 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len, 0); | |
282 | |
283 goto done; | |
284 } | |
285 | |
286 size = - (ssize_t) (sizeof("; ") - 1); | |
287 | |
288 for (i = 0; i < n; i++) { | |
289 size += ph[i]->value.len + sizeof("; ") - 1; | |
290 } | |
291 | |
292 cookie = ngx_palloc(r->pool, size); | |
293 if (cookie == NULL) { | |
294 XSRETURN_UNDEF; | |
295 } | |
296 | |
297 p = cookie; | |
298 | |
299 for (i = 0; /* void */ ; i++) { | |
300 p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len); | |
301 | |
302 if (i == n - 1) { | |
303 break; | |
304 } | |
305 | |
306 *p++ = ';'; *p++ = ' '; | |
307 } | |
308 | |
309 ngx_http_perl_set_targ(cookie, size, 0); | |
310 | |
311 goto done; | |
312 } | |
313 | |
314 /* iterate over all headers */ | |
315 | |
599 | 316 part = &r->headers_in.headers.part; |
667 | 317 h = part->elts; |
599 | 318 |
319 for (i = 0; /* void */ ; i++) { | |
320 | |
321 if (i >= part->nelts) { | |
322 if (part->next == NULL) { | |
323 break; | |
324 } | |
325 | |
326 part = part->next; | |
667 | 327 h = part->elts; |
599 | 328 i = 0; |
329 } | |
330 | |
667 | 331 if (len != h[i].key.len |
332 || ngx_strcasecmp(p, h[i].key.data) != 0) | |
599 | 333 { |
334 continue; | |
335 } | |
336 | |
667 | 337 ngx_http_perl_set_targ(h[i].value.data, h[i].value.len, 0); |
599 | 338 |
339 goto done; | |
340 } | |
341 | |
342 XSRETURN_UNDEF; | |
343 | |
344 done: | |
345 | |
633 | 346 ST(0) = TARG; |
599 | 347 |
348 | |
633 | 349 void |
681 | 350 has_request_body(r, next) |
351 CODE: | |
352 | |
353 dXSTARG; | |
354 ngx_http_request_t *r; | |
355 ngx_http_perl_ctx_t *ctx; | |
356 | |
357 ngx_http_perl_set_request(r); | |
358 | |
359 if (r->headers_in.content_length_n <= 0) { | |
360 XSRETURN_UNDEF; | |
361 } | |
362 | |
363 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
909 | 364 ctx->next = SvRV(ST(1)); |
681 | 365 |
366 r->request_body_in_single_buf = 1; | |
367 r->request_body_in_persistent_file = 1; | |
1075
4d203f76b757
undo "client_body_in_file_only any"
Igor Sysoev <igor@sysoev.ru>
parents:
1056
diff
changeset
|
368 r->request_body_in_clean_file = 1; |
681 | 369 |
370 if (r->request_body_in_file_only) { | |
371 r->request_body_file_log_level = 0; | |
372 } | |
373 | |
374 ngx_http_read_client_request_body(r, ngx_http_perl_handle_request); | |
375 | |
376 sv_upgrade(TARG, SVt_IV); | |
377 sv_setiv(TARG, 1); | |
378 | |
379 ST(0) = TARG; | |
380 | |
381 | |
382 void | |
631 | 383 request_body(r) |
384 CODE: | |
385 | |
633 | 386 dXSTARG; |
387 ngx_http_request_t *r; | |
388 size_t len; | |
389 | |
390 ngx_http_perl_set_request(r); | |
631 | 391 |
941 | 392 if (r->request_body == NULL |
393 || r->request_body->temp_file | |
394 || r->request_body->bufs == NULL) | |
395 { | |
633 | 396 XSRETURN_UNDEF; |
397 } | |
631 | 398 |
633 | 399 len = r->request_body->bufs->buf->last - r->request_body->bufs->buf->pos; |
631 | 400 |
401 if (len == 0) { | |
402 XSRETURN_UNDEF; | |
403 } | |
404 | |
633 | 405 ngx_http_perl_set_targ(r->request_body->bufs->buf->pos, len, 0); |
631 | 406 |
633 | 407 ST(0) = TARG; |
631 | 408 |
409 | |
633 | 410 void |
411 request_body_file(r) | |
412 CODE: | |
413 | |
414 dXSTARG; | |
415 ngx_http_request_t *r; | |
416 | |
417 ngx_http_perl_set_request(r); | |
418 | |
941 | 419 if (r->request_body == NULL || r->request_body->temp_file == NULL) { |
633 | 420 XSRETURN_UNDEF; |
421 } | |
422 | |
423 ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data, | |
424 r->request_body->temp_file->file.name.len, 1); | |
599 | 425 |
633 | 426 ST(0) = TARG; |
427 | |
428 | |
429 void | |
430 header_out(r, key, value) | |
431 CODE: | |
599 | 432 |
633 | 433 ngx_http_request_t *r; |
434 SV *key; | |
435 SV *value; | |
436 ngx_table_elt_t *header; | |
599 | 437 |
633 | 438 ngx_http_perl_set_request(r); |
439 | |
440 key = ST(1); | |
441 value = ST(2); | |
599 | 442 |
443 header = ngx_list_push(&r->headers_out.headers); | |
444 if (header == NULL) { | |
633 | 445 XSRETURN_EMPTY; |
599 | 446 } |
447 | |
448 header->hash = 1; | |
449 | |
450 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) { | |
633 | 451 XSRETURN_EMPTY; |
599 | 452 } |
453 | |
454 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) { | |
633 | 455 XSRETURN_EMPTY; |
599 | 456 } |
457 | |
458 if (header->key.len == sizeof("Content-Length") - 1 | |
459 && ngx_strncasecmp(header->key.data, "Content-Length", | |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
460 sizeof("Content-Length") - 1) == 0) |
599 | 461 { |
741
63a08390a8a2
$r->headers_out("Content-Length", "NNN") did not work
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
462 r->headers_out.content_length_n = (off_t) SvIV(value); |
599 | 463 r->headers_out.content_length = header; |
464 } | |
465 | |
633 | 466 XSRETURN_EMPTY; |
599 | 467 |
468 | |
633 | 469 void |
599 | 470 filename(r) |
633 | 471 CODE: |
599 | 472 |
633 | 473 dXSTARG; |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
474 size_t root; |
633 | 475 ngx_http_request_t *r; |
599 | 476 ngx_http_perl_ctx_t *ctx; |
477 | |
633 | 478 ngx_http_perl_set_request(r); |
599 | 479 |
480 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
633 | 481 if (ctx->filename.data) { |
599 | 482 goto done; |
483 } | |
484 | |
774
589841f06b87
previous commit broke two modules
Igor Sysoev <igor@sysoev.ru>
parents:
741
diff
changeset
|
485 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) { |
599 | 486 XSRETURN_UNDEF; |
487 } | |
488 | |
633 | 489 ctx->filename.len--; |
490 sv_setpv(PL_statname, (char *) ctx->filename.data); | |
599 | 491 |
492 done: | |
493 | |
633 | 494 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len, 1); |
599 | 495 |
633 | 496 ST(0) = TARG; |
599 | 497 |
498 | |
633 | 499 void |
599 | 500 print(r, ...) |
501 CODE: | |
502 | |
633 | 503 ngx_http_request_t *r; |
504 SV *sv; | |
505 int i; | |
506 u_char *p; | |
507 size_t size; | |
508 STRLEN len; | |
509 ngx_buf_t *b; | |
510 | |
511 ngx_http_perl_set_request(r); | |
599 | 512 |
513 if (items == 2) { | |
514 | |
515 /* | |
516 * do zero copy for prolate single read-only SV: | |
517 * $r->print("some text\n"); | |
518 */ | |
519 | |
520 sv = ST(1); | |
521 | |
522 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
523 sv = SvRV(sv); | |
524 } | |
525 | |
526 if (SvREADONLY(sv)) { | |
527 | |
528 p = (u_char *) SvPV(sv, len); | |
529 | |
530 if (len == 0) { | |
633 | 531 XSRETURN_EMPTY; |
599 | 532 } |
533 | |
534 b = ngx_calloc_buf(r->pool); | |
535 if (b == NULL) { | |
633 | 536 XSRETURN_EMPTY; |
599 | 537 } |
538 | |
539 b->memory = 1; | |
540 b->pos = p; | |
541 b->last = p + len; | |
542 b->start = p; | |
543 b->end = b->last; | |
544 | |
601 | 545 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
546 "$r->print: read-only SV: %z", len); | |
547 | |
599 | 548 goto out; |
549 } | |
550 } | |
551 | |
552 size = 0; | |
553 | |
554 for (i = 1; i < items; i++) { | |
555 | |
556 sv = ST(i); | |
557 | |
558 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
559 sv = SvRV(sv); | |
560 } | |
561 | |
601 | 562 (void) SvPV(sv, len); |
599 | 563 |
601 | 564 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
565 "$r->print: copy SV: %z", len); | |
599 | 566 |
567 size += len; | |
568 } | |
569 | |
570 if (size == 0) { | |
633 | 571 XSRETURN_EMPTY; |
599 | 572 } |
573 | |
574 b = ngx_create_temp_buf(r->pool, size); | |
575 if (b == NULL) { | |
633 | 576 XSRETURN_EMPTY; |
599 | 577 } |
578 | |
579 for (i = 1; i < items; i++) { | |
580 sv = ST(i); | |
581 | |
582 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { | |
583 sv = SvRV(sv); | |
584 } | |
585 | |
586 p = (u_char *) SvPV(sv, len); | |
587 b->last = ngx_cpymem(b->last, p, len); | |
588 } | |
589 | |
590 out: | |
591 | |
633 | 592 (void) ngx_http_perl_output(r, b); |
599 | 593 |
633 | 594 XSRETURN_EMPTY; |
599 | 595 |
596 | |
633 | 597 void |
613 | 598 sendfile(r, filename, offset = -1, bytes = 0) |
633 | 599 CODE: |
600 | |
601 ngx_http_request_t *r; | |
602 char *filename; | |
613 | 603 int offset; |
604 size_t bytes; | |
599 | 605 ngx_fd_t fd; |
606 ngx_buf_t *b; | |
607 ngx_file_info_t fi; | |
608 ngx_pool_cleanup_t *cln; | |
609 ngx_pool_cleanup_file_t *clnf; | |
610 | |
633 | 611 ngx_http_perl_set_request(r); |
612 | |
613 filename = SvPV_nolen(ST(1)); | |
599 | 614 |
615 if (filename == NULL) { | |
616 croak("sendfile(): NULL filename"); | |
617 } | |
618 | |
633 | 619 offset = items < 3 ? -1 : SvIV(ST(2)); |
620 bytes = items < 4 ? 0 : SvIV(ST(3)); | |
621 | |
599 | 622 b = ngx_calloc_buf(r->pool); |
623 if (b == NULL) { | |
633 | 624 XSRETURN_EMPTY; |
599 | 625 } |
626 | |
627 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
628 if (b->file == NULL) { | |
633 | 629 XSRETURN_EMPTY; |
599 | 630 } |
631 | |
632 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); | |
633 if (cln == NULL) { | |
633 | 634 XSRETURN_EMPTY; |
599 | 635 } |
636 | |
1056
82b9e57993ec
update nginx.xs to ngx_open_file(name, mode, create, access)
Igor Sysoev <igor@sysoev.ru>
parents:
941
diff
changeset
|
637 fd = ngx_open_file((u_char *) filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
599 | 638 |
639 if (fd == NGX_INVALID_FILE) { | |
640 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
641 ngx_open_file_n " \"%s\" failed", filename); | |
633 | 642 XSRETURN_EMPTY; |
599 | 643 } |
644 | |
613 | 645 if (offset == -1) { |
646 offset = 0; | |
647 } | |
648 | |
649 if (bytes == 0) { | |
650 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
651 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
652 ngx_fd_info_n " \"%s\" failed", filename); | |
599 | 653 |
613 | 654 if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
655 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
656 ngx_close_file_n " \"%s\" failed", filename); | |
657 } | |
658 | |
633 | 659 XSRETURN_EMPTY; |
599 | 660 } |
661 | |
613 | 662 bytes = ngx_file_size(&fi) - offset; |
599 | 663 } |
664 | |
665 cln->handler = ngx_pool_cleanup_file; | |
666 clnf = cln->data; | |
667 | |
668 clnf->fd = fd; | |
669 clnf->name = (u_char *) ""; | |
670 clnf->log = r->pool->log; | |
671 | |
672 b->in_file = 1; | |
613 | 673 |
674 b->file_pos = offset; | |
675 b->file_last = offset + bytes; | |
599 | 676 |
677 b->file->fd = fd; | |
678 b->file->log = r->connection->log; | |
679 | |
633 | 680 (void) ngx_http_perl_output(r, b); |
599 | 681 |
633 | 682 XSRETURN_EMPTY; |
599 | 683 |
684 | |
633 | 685 void |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
686 flush(r) |
633 | 687 CODE: |
599 | 688 |
633 | 689 ngx_http_request_t *r; |
690 ngx_buf_t *b; | |
599 | 691 |
633 | 692 ngx_http_perl_set_request(r); |
599 | 693 |
694 b = ngx_calloc_buf(r->pool); | |
695 if (b == NULL) { | |
633 | 696 XSRETURN_EMPTY; |
599 | 697 } |
698 | |
699 b->flush = 1; | |
700 | |
1178
a77f6980de50
rename $r->rflush to $r->flush
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
701 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); |
601 | 702 |
633 | 703 (void) ngx_http_perl_output(r, b); |
599 | 704 |
633 | 705 XSRETURN_EMPTY; |
599 | 706 |
707 | |
708 void | |
709 internal_redirect(r, uri) | |
633 | 710 CODE: |
599 | 711 |
633 | 712 ngx_http_request_t *r; |
713 SV *uri; | |
599 | 714 ngx_uint_t i; |
715 ngx_http_perl_ctx_t *ctx; | |
716 | |
633 | 717 ngx_http_perl_set_request(r); |
718 | |
719 uri = ST(1); | |
599 | 720 |
721 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
722 | |
723 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { | |
724 XSRETURN_EMPTY; | |
725 } | |
726 | |
727 for (i = 0; i < ctx->redirect_uri.len; i++) { | |
728 if (ctx->redirect_uri.data[i] == '?') { | |
729 | |
730 ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); | |
731 ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; | |
732 ctx->redirect_uri.len = i; | |
733 | |
734 XSRETURN_EMPTY; | |
735 } | |
736 } | |
601 | 737 |
738 | |
633 | 739 void |
811 | 740 allow_ranges(r) |
741 CODE: | |
742 | |
743 ngx_http_request_t *r; | |
744 | |
745 ngx_http_perl_set_request(r); | |
746 | |
747 r->allow_ranges = 1; | |
748 | |
749 XSRETURN_EMPTY; | |
750 | |
751 | |
752 void | |
601 | 753 unescape(r, text, type = 0) |
754 CODE: | |
755 | |
633 | 756 dXSTARG; |
757 ngx_http_request_t *r; | |
758 SV *text; | |
759 int type; | |
760 u_char *p, *dst, *src; | |
761 STRLEN len; | |
601 | 762 |
633 | 763 ngx_http_perl_set_request(r); |
764 | |
765 text = ST(1); | |
766 | |
767 src = (u_char *) SvPV(text, len); | |
768 | |
769 p = ngx_palloc(r->pool, len + 1); | |
601 | 770 if (p == NULL) { |
771 XSRETURN_UNDEF; | |
772 } | |
773 | |
774 dst = p; | |
775 | |
633 | 776 type = items < 3 ? 0 : SvIV(ST(2)); |
777 | |
778 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); | |
601 | 779 *dst = '\0'; |
780 | |
633 | 781 ngx_http_perl_set_targ(p, dst - p, 1); |
601 | 782 |
633 | 783 ST(0) = TARG; |
833 | 784 |
785 | |
786 void | |
787 variable(r, name, value = NULL) | |
788 CODE: | |
789 | |
790 dXSTARG; | |
791 ngx_http_request_t *r; | |
792 SV *name, *value; | |
793 u_char *p, *lowcase; | |
794 STRLEN len; | |
795 ngx_str_t var, val; | |
796 ngx_uint_t i, hash; | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
797 ngx_http_perl_var_t *v; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
798 ngx_http_perl_ctx_t *ctx; |
833 | 799 ngx_http_variable_value_t *vv; |
800 | |
801 ngx_http_perl_set_request(r); | |
802 | |
803 name = ST(1); | |
804 | |
805 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { | |
806 name = SvRV(name); | |
807 } | |
808 | |
809 if (items == 2) { | |
810 value = NULL; | |
811 | |
812 } else { | |
813 value = ST(2); | |
814 | |
815 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { | |
816 value = SvRV(value); | |
817 } | |
818 | |
819 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { | |
820 XSRETURN_UNDEF; | |
821 } | |
822 } | |
823 | |
824 p = (u_char *) SvPV(name, len); | |
825 | |
826 lowcase = ngx_palloc(r->pool, len); | |
827 if (lowcase == NULL) { | |
828 XSRETURN_UNDEF; | |
829 } | |
830 | |
831 hash = 0; | |
832 for (i = 0; i < len; i++) { | |
833 lowcase[i] = ngx_tolower(p[i]); | |
834 hash = ngx_hash(hash, lowcase[i]); | |
835 } | |
836 | |
837 var.len = len; | |
838 var.data = lowcase; | |
839 | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
840 #if (NGX_LOG_DEBUG) |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
841 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
842 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
843 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
844 "perl variable: \"%V\"=\"%V\"", &var, &val); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
845 } else { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
846 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
847 "perl variable: \"%V\"", &var); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
848 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
849 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
850 #endif |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
851 |
833 | 852 vv = ngx_http_get_variable(r, &var, hash, 1); |
853 if (vv == NULL) { | |
854 XSRETURN_UNDEF; | |
855 } | |
856 | |
857 if (vv->not_found) { | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
858 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
859 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
860 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
861 if (ctx->variables) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
862 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
863 v = ctx->variables->elts; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
864 for (i = 0; i < ctx->variables->nelts; i++) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
865 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
866 if (hash != v[i].hash |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
867 || len != v[i].name.len |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
868 || ngx_strncmp(lowcase, v[i].name.data, len) != 0) |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
869 { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
870 continue; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
871 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
872 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
873 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
874 v[i].value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
875 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
876 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
877 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
878 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len, 0); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
879 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
880 goto done; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
881 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
882 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
883 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
884 if (value) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
885 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
886 ctx->variables = ngx_array_create(r->pool, 1, |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
887 sizeof(ngx_http_perl_var_t)); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
888 if (ctx->variables == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
889 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
890 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
891 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
892 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
893 v = ngx_array_push(ctx->variables); |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
894 if (v == NULL) { |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
895 XSRETURN_UNDEF; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
896 } |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
897 |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
898 v->hash = hash; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
899 v->name.len = len; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
900 v->name.data = lowcase; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
901 v->value = val; |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
902 |
833 | 903 XSRETURN_UNDEF; |
904 } | |
905 | |
906 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
907 "variable \"%V\" not found", &var); | |
908 | |
909 XSRETURN_UNDEF; | |
910 } | |
911 | |
912 if (value) { | |
913 vv->len = val.len; | |
914 vv->valid = 1; | |
1714 | 915 vv->no_cacheable = 0; |
833 | 916 vv->not_found = 0; |
917 vv->data = val.data; | |
918 | |
919 XSRETURN_UNDEF; | |
920 } | |
921 | |
922 ngx_http_perl_set_targ(vv->data, vv->len, 0); | |
923 | |
912
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
924 done: |
7fa926a7926d
$r->variable() supports perl only variables
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
925 |
833 | 926 ST(0) = TARG; |
907 | 927 |
928 | |
929 void | |
911 | 930 sleep(r, sleep, next) |
931 CODE: | |
932 | |
933 dXSTARG; | |
934 ngx_http_request_t *r; | |
935 ngx_http_perl_ctx_t *ctx; | |
936 | |
937 ngx_http_perl_set_request(r); | |
938 | |
939 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
940 | |
941 ctx->sleep = SvIV(ST(1)); | |
942 ctx->next = SvRV(ST(2)); | |
943 | |
944 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
945 "perl sleep: %d", ctx->sleep); | |
946 | |
947 XSRETURN_EMPTY; | |
948 | |
949 | |
950 void | |
907 | 951 log_error(r, err, msg) |
952 CODE: | |
953 | |
954 ngx_http_request_t *r; | |
955 SV *err, *msg; | |
956 u_char *p; | |
957 STRLEN len; | |
958 ngx_err_t e; | |
959 | |
960 ngx_http_perl_set_request(r); | |
961 | |
962 err = ST(1); | |
963 | |
964 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { | |
965 err = SvRV(err); | |
966 } | |
967 | |
968 e = SvIV(err); | |
969 | |
970 msg = ST(2); | |
971 | |
972 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { | |
973 msg = SvRV(msg); | |
974 } | |
975 | |
976 p = (u_char *) SvPV(msg, len); | |
977 | |
910 | 978 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); |
907 | 979 |
980 XSRETURN_EMPTY; |