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