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