Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / http / modules / perl / nginx.xs
blobbbfef079c230555b3f6da75881985b3ca3bf384a
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
8 #define PERL_NO_GET_CONTEXT
10 #include <ngx_config.h>
11 #include <ngx_core.h>
12 #include <ngx_http.h>
13 #include <ngx_http_perl_module.h>
15 #include "XSUB.h"
18 #define ngx_http_perl_set_request(r)                                          \
19     r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0))))
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)
29 static ngx_int_t
30 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv)
32     u_char  *p;
33     STRLEN   len;
35     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
36         sv = SvRV(sv);
37     }
39     p = (u_char *) SvPV(sv, len);
41     s->len = len;
43     if (SvREADONLY(sv) && SvPOK(sv)) {
44         s->data = p;
46         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
47                        "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
49         return NGX_OK;
50     }
52     s->data = ngx_pnalloc(r->pool, len);
53     if (s->data == NULL) {
54         return NGX_ERROR;
55     }
57     ngx_memcpy(s->data, p, len);
59     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
60                    "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
62     return NGX_OK;
66 static ngx_int_t
67 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b)
69     ngx_chain_t           out;
70 #if (NGX_HTTP_SSI)
71     ngx_chain_t          *cl;
72     ngx_http_perl_ctx_t  *ctx;
74     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
76     if (ctx->ssi) {
77         cl = ngx_alloc_chain_link(r->pool);
78         if (cl == NULL) {
79             return NGX_ERROR;
80         }
82         cl->buf = b;
83         cl->next = NULL;
84         *ctx->ssi->last_out = cl;
85         ctx->ssi->last_out = &cl->next;
87         return NGX_OK;
88     }
89 #endif
91     out.buf = b;
92     out.next = NULL;
94     return ngx_http_output_filter(r, &out);
98 MODULE = nginx    PACKAGE = nginx
101 void
102 status(r, code)
103     CODE:
105     ngx_http_request_t  *r;
107     ngx_http_perl_set_request(r);
109     r->headers_out.status = SvIV(ST(1));
111     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
112                    "perl status: %d", r->headers_out.status);
114     XSRETURN_UNDEF;
117 void
118 send_http_header(r, ...)
119     CODE:
121     ngx_http_request_t  *r;
122     SV                  *sv;
124     ngx_http_perl_set_request(r);
126     if (r->headers_out.status == 0) {
127         r->headers_out.status = NGX_HTTP_OK;
128     }
130     if (items != 1) {
131         sv = ST(1);
133         if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv)
134             != NGX_OK)
135         {
136             XSRETURN_EMPTY;
137         }
139         r->headers_out.content_type_len = r->headers_out.content_type.len;
141     } else {
142         if (ngx_http_set_content_type(r) != NGX_OK) {
143             XSRETURN_EMPTY;
144         }
145     }
147     (void) ngx_http_send_header(r);
150 void
151 header_only(r)
152     CODE:
154     dXSTARG;
155     ngx_http_request_t  *r;
157     ngx_http_perl_set_request(r);
159     sv_upgrade(TARG, SVt_IV);
160     sv_setiv(TARG, r->header_only);
162     ST(0) = TARG;
165 void
166 uri(r)
167     CODE:
169     dXSTARG;
170     ngx_http_request_t  *r;
172     ngx_http_perl_set_request(r);
173     ngx_http_perl_set_targ(r->uri.data, r->uri.len);
175     ST(0) = TARG;
178 void
179 args(r)
180     CODE:
182     dXSTARG;
183     ngx_http_request_t  *r;
185     ngx_http_perl_set_request(r);
186     ngx_http_perl_set_targ(r->args.data, r->args.len);
188     ST(0) = TARG;
191 void
192 request_method(r)
193     CODE:
195     dXSTARG;
196     ngx_http_request_t  *r;
198     ngx_http_perl_set_request(r);
199     ngx_http_perl_set_targ(r->method_name.data, r->method_name.len);
201     ST(0) = TARG;
204 void
205 remote_addr(r)
206     CODE:
208     dXSTARG;
209     ngx_http_request_t  *r;
211     ngx_http_perl_set_request(r);
212     ngx_http_perl_set_targ(r->connection->addr_text.data,
213                            r->connection->addr_text.len);
215     ST(0) = TARG;
218 void
219 header_in(r, key)
220     CODE:
222     dXSTARG;
223     ngx_http_request_t         *r;
224     SV                         *key;
225     u_char                     *p, *lowcase_key, *cookie;
226     STRLEN                      len;
227     ssize_t                     size;
228     ngx_uint_t                  i, n, hash;
229     ngx_list_part_t            *part;
230     ngx_table_elt_t            *h, **ph;
231     ngx_http_header_t          *hh;
232     ngx_http_core_main_conf_t  *cmcf;
234     ngx_http_perl_set_request(r);
236     key = ST(1);
238     if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
239         key = SvRV(key);
240     }
242     p = (u_char *) SvPV(key, len);
244     /* look up hashed headers */
246     lowcase_key = ngx_pnalloc(r->pool, len);
247     if (lowcase_key == NULL) {
248         XSRETURN_UNDEF;
249     }
251     hash = ngx_hash_strlow(lowcase_key, p, len);
253     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
255     hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len);
257     if (hh) {
258         if (hh->offset) {
260             ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset);
262             if (*ph) {
263                 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
265                 goto done;
266             }
268             XSRETURN_UNDEF;
269         }
271         /* Cookie */
273         n = r->headers_in.cookies.nelts;
275         if (n == 0) {
276             XSRETURN_UNDEF;
277         }
279         ph = r->headers_in.cookies.elts;
281         if (n == 1) {
282             ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
284             goto done;
285         }
287         size = - (ssize_t) (sizeof("; ") - 1);
289         for (i = 0; i < n; i++) {
290             size += ph[i]->value.len + sizeof("; ") - 1;
291         }
293         cookie = ngx_pnalloc(r->pool, size);
294         if (cookie == NULL) {
295             XSRETURN_UNDEF;
296         }
298         p = cookie;
300         for (i = 0; /* void */ ; i++) {
301             p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len);
303             if (i == n - 1) {
304                 break;
305             }
307             *p++ = ';'; *p++ = ' ';
308         }
310         ngx_http_perl_set_targ(cookie, size);
312         goto done;
313     }
315     /* iterate over all headers */
317     part = &r->headers_in.headers.part;
318     h = part->elts;
320     for (i = 0; /* void */ ; i++) {
322         if (i >= part->nelts) {
323             if (part->next == NULL) {
324                 break;
325             }
327             part = part->next;
328             h = part->elts;
329             i = 0;
330         }
332         if (len != h[i].key.len
333             || ngx_strcasecmp(p, h[i].key.data) != 0)
334         {
335             continue;
336         }
338         ngx_http_perl_set_targ(h[i].value.data, h[i].value.len);
340         goto done;
341     }
343     XSRETURN_UNDEF;
345     done:
347     ST(0) = TARG;
350 void
351 has_request_body(r, next)
352     CODE:
354     dXSTARG;
355     ngx_http_request_t   *r;
356     ngx_http_perl_ctx_t  *ctx;
358     ngx_http_perl_set_request(r);
360     if (r->headers_in.content_length_n <= 0 && !r->headers_in.chunked) {
361         XSRETURN_UNDEF;
362     }
364     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
365     ctx->next = SvRV(ST(1));
367     r->request_body_in_single_buf = 1;
368     r->request_body_in_persistent_file = 1;
369     r->request_body_in_clean_file = 1;
371     if (r->request_body_in_file_only) {
372         r->request_body_file_log_level = 0;
373     }
375     ngx_http_read_client_request_body(r, ngx_http_perl_handle_request);
377     sv_upgrade(TARG, SVt_IV);
378     sv_setiv(TARG, 1);
380     ST(0) = TARG;
383 void
384 request_body(r)
385     CODE:
387     dXSTARG;
388     ngx_http_request_t  *r;
389     u_char              *p, *data;
390     size_t               len;
391     ngx_buf_t           *buf;
392     ngx_chain_t         *cl;
394     ngx_http_perl_set_request(r);
396     if (r->request_body == NULL
397         || r->request_body->temp_file
398         || r->request_body->bufs == NULL)
399     {
400         XSRETURN_UNDEF;
401     }
403     cl = r->request_body->bufs;
404     buf = cl->buf;
406     if (cl->next == NULL) {
407         len = buf->last - buf->pos;
408         data = buf->pos;
409         goto done;
410     }
412     len = buf->last - buf->pos;
413     cl = cl->next;
415     for ( /* void */ ; cl; cl = cl->next) {
416         buf = cl->buf;
417         len += buf->last - buf->pos;
418     }
420     p = ngx_pnalloc(r->pool, len);
421     if (p == NULL) {
422         return XSRETURN_UNDEF;
423     }
425     data = p;
426     cl = r->request_body->bufs;
428     for ( /* void */ ; cl; cl = cl->next) {
429         buf = cl->buf;
430         p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
431     }
433     done:
435     if (len == 0) {
436         XSRETURN_UNDEF;
437     }
439     ngx_http_perl_set_targ(data, len);
441     ST(0) = TARG;
444 void
445 request_body_file(r)
446     CODE:
448     dXSTARG;
449     ngx_http_request_t  *r;
451     ngx_http_perl_set_request(r);
453     if (r->request_body == NULL || r->request_body->temp_file == NULL) {
454         XSRETURN_UNDEF;
455     }
457     ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data,
458                            r->request_body->temp_file->file.name.len);
460     ST(0) = TARG;
463 void
464 discard_request_body(r)
465     CODE:
467     ngx_http_request_t  *r;
469     ngx_http_perl_set_request(r);
471     ngx_http_discard_request_body(r);
474 void
475 header_out(r, key, value)
476     CODE:
478     ngx_http_request_t  *r;
479     SV                  *key;
480     SV                  *value;
481     ngx_table_elt_t     *header;
483     ngx_http_perl_set_request(r);
485     key = ST(1);
486     value = ST(2);
488     header = ngx_list_push(&r->headers_out.headers);
489     if (header == NULL) {
490         XSRETURN_EMPTY;
491     }
493     header->hash = 1;
495     if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) {
496         XSRETURN_EMPTY;
497     }
499     if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) {
500         XSRETURN_EMPTY;
501     }
503     if (header->key.len == sizeof("Content-Length") - 1
504         && ngx_strncasecmp(header->key.data, (u_char *) "Content-Length",
505                            sizeof("Content-Length") - 1) == 0)
506     {
507         r->headers_out.content_length_n = (off_t) SvIV(value);
508         r->headers_out.content_length = header;
509     }
511     if (header->key.len == sizeof("Content-Encoding") - 1
512         && ngx_strncasecmp(header->key.data, (u_char *) "Content-Encoding",
513                            sizeof("Content-Encoding") - 1) == 0)
514     {
515         r->headers_out.content_encoding = header;
516     }
519 void
520 filename(r)
521     CODE:
523     dXSTARG;
524     size_t                root;
525     ngx_http_request_t   *r;
526     ngx_http_perl_ctx_t  *ctx;
528     ngx_http_perl_set_request(r);
530     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
531     if (ctx->filename.data) {
532         goto done;
533     }
535     if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) {
536         XSRETURN_UNDEF;
537     }
539     ctx->filename.len--;
540     sv_setpv(PL_statname, (char *) ctx->filename.data);
542     done:
544     ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len);
546     ST(0) = TARG;
549 void
550 print(r, ...)
551     CODE:
553     ngx_http_request_t  *r;
554     SV                  *sv;
555     int                  i;
556     u_char              *p;
557     size_t               size;
558     STRLEN               len;
559     ngx_buf_t           *b;
561     ngx_http_perl_set_request(r);
563     if (items == 2) {
565         /*
566          * do zero copy for prolate single read-only SV:
567          *     $r->print("some text\n");
568          */
570         sv = ST(1);
572         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
573             sv = SvRV(sv);
574         }
576         if (SvREADONLY(sv) && SvPOK(sv)) {
578             p = (u_char *) SvPV(sv, len);
580             if (len == 0) {
581                 XSRETURN_EMPTY;
582             }
584             b = ngx_calloc_buf(r->pool);
585             if (b == NULL) {
586                 XSRETURN_EMPTY;
587             }
589             b->memory = 1;
590             b->pos = p;
591             b->last = p + len;
592             b->start = p;
593             b->end = b->last;
595             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
596                            "$r->print: read-only SV: %z", len);
598             goto out;
599         }
600     }
602     size = 0;
604     for (i = 1; i < items; i++) {
606         sv = ST(i);
608         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
609             sv = SvRV(sv);
610         }
612         (void) SvPV(sv, len);
614         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
615                        "$r->print: copy SV: %z", len);
617         size += len;
618     }
620     if (size == 0) {
621         XSRETURN_EMPTY;
622     }
624     b = ngx_create_temp_buf(r->pool, size);
625     if (b == NULL) {
626         XSRETURN_EMPTY;
627     }
629     for (i = 1; i < items; i++) {
630         sv = ST(i);
632         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
633             sv = SvRV(sv);
634         }
636         p = (u_char *) SvPV(sv, len);
637         b->last = ngx_cpymem(b->last, p, len);
638     }
640     out:
642     (void) ngx_http_perl_output(r, b);
645 void
646 sendfile(r, filename, offset = -1, bytes = 0)
647     CODE:
649     ngx_http_request_t        *r;
650     char                      *filename;
651     off_t                      offset;
652     size_t                     bytes;
653     ngx_str_t                  path;
654     ngx_buf_t                 *b;
655     ngx_open_file_info_t       of;
656     ngx_http_core_loc_conf_t  *clcf;
658     ngx_http_perl_set_request(r);
660     filename = SvPV_nolen(ST(1));
662     if (filename == NULL) {
663         croak("sendfile(): NULL filename");
664     }
666     offset = items < 3 ? -1 : SvIV(ST(2));
667     bytes = items < 4 ? 0 : SvIV(ST(3));
669     b = ngx_calloc_buf(r->pool);
670     if (b == NULL) {
671         XSRETURN_EMPTY;
672     }
674     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
675     if (b->file == NULL) {
676         XSRETURN_EMPTY;
677     }
679     path.len = ngx_strlen(filename);
681     path.data = ngx_pnalloc(r->pool, path.len + 1);
682     if (path.data == NULL) {
683         XSRETURN_EMPTY;
684     }
686     (void) ngx_cpystrn(path.data, (u_char *) filename, path.len + 1);
688     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
690     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
692     of.read_ahead = clcf->read_ahead;
693     of.directio = clcf->directio;
694     of.valid = clcf->open_file_cache_valid;
695     of.min_uses = clcf->open_file_cache_min_uses;
696     of.errors = clcf->open_file_cache_errors;
697     of.events = clcf->open_file_cache_events;
699     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
700         XSRETURN_EMPTY;
701     }
703     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
704         != NGX_OK)
705     {
706         if (of.err == 0) {
707             XSRETURN_EMPTY;
708         }
710         ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
711                       "%s \"%s\" failed", of.failed, filename);
712         XSRETURN_EMPTY;
713     }
715     if (offset == -1) {
716         offset = 0;
717     }
719     if (bytes == 0) {
720         bytes = of.size - offset;
721     }
723     b->in_file = 1;
725     b->file_pos = offset;
726     b->file_last = offset + bytes;
728     b->file->fd = of.fd;
729     b->file->log = r->connection->log;
730     b->file->directio = of.is_directio;
732     (void) ngx_http_perl_output(r, b);
735 void
736 flush(r)
737     CODE:
739     ngx_http_request_t  *r;
740     ngx_buf_t           *b;
742     ngx_http_perl_set_request(r);
744     b = ngx_calloc_buf(r->pool);
745     if (b == NULL) {
746         XSRETURN_EMPTY;
747     }
749     b->flush = 1;
751     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
753     (void) ngx_http_perl_output(r, b);
755     XSRETURN_EMPTY;
758 void
759 internal_redirect(r, uri)
760     CODE:
762     ngx_http_request_t   *r;
763     SV                   *uri;
764     ngx_uint_t            i;
765     ngx_http_perl_ctx_t  *ctx;
767     ngx_http_perl_set_request(r);
769     uri = ST(1);
771     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
773     if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
774         XSRETURN_EMPTY;
775     }
777     for (i = 0; i < ctx->redirect_uri.len; i++) {
778         if (ctx->redirect_uri.data[i] == '?') {
780             ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1);
781             ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1];
782             ctx->redirect_uri.len = i;
784             XSRETURN_EMPTY;
785         }
786     }
789 void
790 allow_ranges(r)
791     CODE:
793     ngx_http_request_t  *r;
795     ngx_http_perl_set_request(r);
797     r->allow_ranges = 1;
800 void
801 unescape(r, text, type = 0)
802     CODE:
804     dXSTARG;
805     ngx_http_request_t  *r;
806     SV                  *text;
807     int                  type;
808     u_char              *p, *dst, *src;
809     STRLEN               len;
811     ngx_http_perl_set_request(r);
813     text = ST(1);
815     src = (u_char *) SvPV(text, len);
817     p = ngx_pnalloc(r->pool, len + 1);
818     if (p == NULL) {
819         XSRETURN_UNDEF;
820     }
822     dst = p;
824     type = items < 3 ? 0 : SvIV(ST(2));
826     ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
827     *dst = '\0';
829     ngx_http_perl_set_targ(p, dst - p);
831     ST(0) = TARG;
834 void
835 variable(r, name, value = NULL)
836     CODE:
838     dXSTARG;
839     ngx_http_request_t         *r;
840     SV                         *name, *value;
841     u_char                     *p, *lowcase;
842     STRLEN                      len;
843     ngx_str_t                   var, val;
844     ngx_uint_t                  i, hash;
845     ngx_http_perl_var_t        *v;
846     ngx_http_perl_ctx_t        *ctx;
847     ngx_http_variable_value_t  *vv;
849     ngx_http_perl_set_request(r);
851     name = ST(1);
853     if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
854         name = SvRV(name);
855     }
857     if (items == 2) {
858         value = NULL;
860     } else {
861         value = ST(2);
863         if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
864             value = SvRV(value);
865         }
867         if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
868             XSRETURN_UNDEF;
869         }
870     }
872     p = (u_char *) SvPV(name, len);
874     lowcase = ngx_pnalloc(r->pool, len);
875     if (lowcase == NULL) {
876         XSRETURN_UNDEF;
877     }
879     hash = ngx_hash_strlow(lowcase, p, len);
881     var.len = len;
882     var.data = lowcase;
884     #if (NGX_DEBUG)
886     if (value) {
887         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
888                        "perl variable: \"%V\"=\"%V\"", &var, &val);
889     } else {
890         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
891                        "perl variable: \"%V\"", &var);
892     }
894     #endif
896     vv = ngx_http_get_variable(r, &var, hash);
897     if (vv == NULL) {
898         XSRETURN_UNDEF;
899     }
901     if (vv->not_found) {
903         ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
905         if (ctx->variables) {
907             v = ctx->variables->elts;
908             for (i = 0; i < ctx->variables->nelts; i++) {
910                 if (hash != v[i].hash
911                     || len != v[i].name.len
912                     || ngx_strncmp(lowcase, v[i].name.data, len) != 0)
913                 {
914                     continue;
915                 }
917                 if (value) {
918                     v[i].value = val;
919                     XSRETURN_UNDEF;
920                 }
922                 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
924                 goto done;
925             }
926         }
928         if (value) {
929             if (ctx->variables == NULL) {
930                 ctx->variables = ngx_array_create(r->pool, 1,
931                                                   sizeof(ngx_http_perl_var_t));
932                 if (ctx->variables == NULL) {
933                     XSRETURN_UNDEF;
934                 }
935             }
937             v = ngx_array_push(ctx->variables);
938             if (v == NULL) {
939                 XSRETURN_UNDEF;
940             }
942             v->hash = hash;
943             v->name.len = len;
944             v->name.data = lowcase;
945             v->value = val;
947             XSRETURN_UNDEF;
948         }
950         XSRETURN_UNDEF;
951     }
953     if (value) {
954         vv->len = val.len;
955         vv->valid = 1;
956         vv->no_cacheable = 0;
957         vv->not_found = 0;
958         vv->data = val.data;
960         XSRETURN_UNDEF;
961     }
963     ngx_http_perl_set_targ(vv->data, vv->len);
965     done:
967     ST(0) = TARG;
970 void
971 sleep(r, sleep, next)
972     CODE:
974     ngx_http_request_t   *r;
975     ngx_msec_t            sleep;
976     ngx_http_perl_ctx_t  *ctx;
978     ngx_http_perl_set_request(r);
980     sleep = (ngx_msec_t) SvIV(ST(1));
982     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
983                    "perl sleep: %M", sleep);
985     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
987     ctx->next = SvRV(ST(2));
989     ngx_add_timer(r->connection->write, sleep);
991     r->write_event_handler = ngx_http_perl_sleep_handler;
992     r->main->count++;
995 void
996 log_error(r, err, msg)
997     CODE:
999     ngx_http_request_t  *r;
1000     SV                  *err, *msg;
1001     u_char              *p;
1002     STRLEN               len;
1003     ngx_err_t            e;
1005     ngx_http_perl_set_request(r);
1007     err = ST(1);
1009     if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) {
1010         err = SvRV(err);
1011     }
1013     e = SvIV(err);
1015     msg = ST(2);
1017     if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) {
1018         msg = SvRV(msg);
1019     }
1021     p = (u_char *) SvPV(msg, len);
1023     ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p);