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