Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #define PERL_NO_GET_CONTEXT
0009 
0010 #include <ngx_config.h>
0011 #include <ngx_core.h>
0012 #include <ngx_http.h>
0013 #include <ngx_http_perl_module.h>
0014 
0015 #include "XSUB.h"
0016 
0017 
0018 #define ngx_http_perl_set_request(r)                                          \
0019     r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0))))
0020 
0021 
0022 #define ngx_http_perl_set_targ(p, len)                                        \
0023                                                                               \
0024     SvUPGRADE(TARG, SVt_PV);                                                  \
0025     SvPOK_on(TARG);                                                           \
0026     sv_setpvn(TARG, (char *) p, len)
0027 
0028 
0029 static ngx_int_t
0030 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv)
0031 {
0032     u_char  *p;
0033     STRLEN   len;
0034 
0035     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
0036         sv = SvRV(sv);
0037     }
0038 
0039     p = (u_char *) SvPV(sv, len);
0040 
0041     s->len = len;
0042 
0043     if (SvREADONLY(sv) && SvPOK(sv)) {
0044         s->data = p;
0045 
0046         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0047                        "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
0048 
0049         return NGX_OK;
0050     }
0051 
0052     s->data = ngx_pnalloc(r->pool, len);
0053     if (s->data == NULL) {
0054         return NGX_ERROR;
0055     }
0056 
0057     ngx_memcpy(s->data, p, len);
0058 
0059     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0060                    "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
0061 
0062     return NGX_OK;
0063 }
0064 
0065 
0066 static ngx_int_t
0067 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b)
0068 {
0069     ngx_chain_t           out;
0070 #if (NGX_HTTP_SSI)
0071     ngx_chain_t          *cl;
0072     ngx_http_perl_ctx_t  *ctx;
0073 
0074     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
0075 
0076     if (ctx->ssi) {
0077         cl = ngx_alloc_chain_link(r->pool);
0078         if (cl == NULL) {
0079             return NGX_ERROR;
0080         }
0081 
0082         cl->buf = b;
0083         cl->next = NULL;
0084         *ctx->ssi->last_out = cl;
0085         ctx->ssi->last_out = &cl->next;
0086 
0087         return NGX_OK;
0088     }
0089 #endif
0090 
0091     out.buf = b;
0092     out.next = NULL;
0093 
0094     return ngx_http_output_filter(r, &out);
0095 }
0096 
0097 
0098 MODULE = nginx    PACKAGE = nginx
0099 
0100 
0101 PROTOTYPES: DISABLE
0102 
0103 
0104 void
0105 status(r, code)
0106     CODE:
0107 
0108     ngx_http_request_t  *r;
0109 
0110     ngx_http_perl_set_request(r);
0111 
0112     r->headers_out.status = SvIV(ST(1));
0113 
0114     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0115                    "perl status: %d", r->headers_out.status);
0116 
0117     XSRETURN_UNDEF;
0118 
0119 
0120 void
0121 send_http_header(r, ...)
0122     CODE:
0123 
0124     ngx_http_request_t  *r;
0125     SV                  *sv;
0126 
0127     ngx_http_perl_set_request(r);
0128 
0129     if (r->headers_out.status == 0) {
0130         r->headers_out.status = NGX_HTTP_OK;
0131     }
0132 
0133     if (items != 1) {
0134         sv = ST(1);
0135 
0136         if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv)
0137             != NGX_OK)
0138         {
0139             XSRETURN_EMPTY;
0140         }
0141 
0142         r->headers_out.content_type_len = r->headers_out.content_type.len;
0143 
0144     } else {
0145         if (ngx_http_set_content_type(r) != NGX_OK) {
0146             XSRETURN_EMPTY;
0147         }
0148     }
0149 
0150     (void) ngx_http_send_header(r);
0151 
0152 
0153 void
0154 header_only(r)
0155     CODE:
0156 
0157     dXSTARG;
0158     ngx_http_request_t  *r;
0159 
0160     ngx_http_perl_set_request(r);
0161 
0162     sv_upgrade(TARG, SVt_IV);
0163     sv_setiv(TARG, r->header_only);
0164 
0165     ST(0) = TARG;
0166 
0167 
0168 void
0169 uri(r)
0170     CODE:
0171 
0172     dXSTARG;
0173     ngx_http_request_t  *r;
0174 
0175     ngx_http_perl_set_request(r);
0176     ngx_http_perl_set_targ(r->uri.data, r->uri.len);
0177 
0178     ST(0) = TARG;
0179 
0180 
0181 void
0182 args(r)
0183     CODE:
0184 
0185     dXSTARG;
0186     ngx_http_request_t  *r;
0187 
0188     ngx_http_perl_set_request(r);
0189     ngx_http_perl_set_targ(r->args.data, r->args.len);
0190 
0191     ST(0) = TARG;
0192 
0193 
0194 void
0195 request_method(r)
0196     CODE:
0197 
0198     dXSTARG;
0199     ngx_http_request_t  *r;
0200 
0201     ngx_http_perl_set_request(r);
0202     ngx_http_perl_set_targ(r->method_name.data, r->method_name.len);
0203 
0204     ST(0) = TARG;
0205 
0206 
0207 void
0208 remote_addr(r)
0209     CODE:
0210 
0211     dXSTARG;
0212     ngx_http_request_t  *r;
0213 
0214     ngx_http_perl_set_request(r);
0215     ngx_http_perl_set_targ(r->connection->addr_text.data,
0216                            r->connection->addr_text.len);
0217 
0218     ST(0) = TARG;
0219 
0220 
0221 void
0222 header_in(r, key)
0223     CODE:
0224 
0225     dXSTARG;
0226     ngx_http_request_t         *r;
0227     SV                         *key;
0228     u_char                     *p, *lowcase_key, *value, sep;
0229     STRLEN                      len;
0230     ssize_t                     size;
0231     ngx_uint_t                  i, n, hash;
0232     ngx_array_t                *a;
0233     ngx_list_part_t            *part;
0234     ngx_table_elt_t            *h, **ph;
0235     ngx_http_header_t          *hh;
0236     ngx_http_core_main_conf_t  *cmcf;
0237 
0238     ngx_http_perl_set_request(r);
0239 
0240     key = ST(1);
0241 
0242     if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
0243         key = SvRV(key);
0244     }
0245 
0246     p = (u_char *) SvPV(key, len);
0247 
0248     /* look up hashed headers */
0249 
0250     lowcase_key = ngx_pnalloc(r->pool, len);
0251     if (lowcase_key == NULL) {
0252         XSRETURN_UNDEF;
0253     }
0254 
0255     hash = ngx_hash_strlow(lowcase_key, p, len);
0256 
0257     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
0258 
0259     hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len);
0260 
0261     if (hh) {
0262 
0263         if (hh->offset == offsetof(ngx_http_headers_in_t, cookies)) {
0264             sep = ';';
0265             goto multi;
0266         }
0267 #if (NGX_HTTP_X_FORWARDED_FOR)
0268         if (hh->offset == offsetof(ngx_http_headers_in_t, x_forwarded_for)) {
0269             sep = ',';
0270             goto multi;
0271         }
0272 #endif
0273 
0274         ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset);
0275 
0276         if (*ph) {
0277             ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
0278 
0279             goto done;
0280         }
0281 
0282         XSRETURN_UNDEF;
0283 
0284     multi:
0285 
0286         /* Cookie, X-Forwarded-For */
0287 
0288         a = (ngx_array_t *) ((char *) &r->headers_in + hh->offset);
0289 
0290         n = a->nelts;
0291 
0292         if (n == 0) {
0293             XSRETURN_UNDEF;
0294         }
0295 
0296         ph = a->elts;
0297 
0298         if (n == 1) {
0299             ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
0300 
0301             goto done;
0302         }
0303 
0304         size = - (ssize_t) (sizeof("; ") - 1);
0305 
0306         for (i = 0; i < n; i++) {
0307             size += ph[i]->value.len + sizeof("; ") - 1;
0308         }
0309 
0310         value = ngx_pnalloc(r->pool, size);
0311         if (value == NULL) {
0312             XSRETURN_UNDEF;
0313         }
0314 
0315         p = value;
0316 
0317         for (i = 0; /* void */ ; i++) {
0318             p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len);
0319 
0320             if (i == n - 1) {
0321                 break;
0322             }
0323 
0324             *p++ = sep; *p++ = ' ';
0325         }
0326 
0327         ngx_http_perl_set_targ(value, size);
0328 
0329         goto done;
0330     }
0331 
0332     /* iterate over all headers */
0333 
0334     part = &r->headers_in.headers.part;
0335     h = part->elts;
0336 
0337     for (i = 0; /* void */ ; i++) {
0338 
0339         if (i >= part->nelts) {
0340             if (part->next == NULL) {
0341                 break;
0342             }
0343 
0344             part = part->next;
0345             h = part->elts;
0346             i = 0;
0347         }
0348 
0349         if (len != h[i].key.len
0350             || ngx_strcasecmp(p, h[i].key.data) != 0)
0351         {
0352             continue;
0353         }
0354 
0355         ngx_http_perl_set_targ(h[i].value.data, h[i].value.len);
0356 
0357         goto done;
0358     }
0359 
0360     XSRETURN_UNDEF;
0361 
0362     done:
0363 
0364     ST(0) = TARG;
0365 
0366 
0367 void
0368 has_request_body(r, next)
0369     CODE:
0370 
0371     dXSTARG;
0372     ngx_http_request_t   *r;
0373     ngx_http_perl_ctx_t  *ctx;
0374 
0375     ngx_http_perl_set_request(r);
0376 
0377     if (r->headers_in.content_length_n <= 0 && !r->headers_in.chunked) {
0378         XSRETURN_UNDEF;
0379     }
0380 
0381     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
0382     ctx->next = SvRV(ST(1));
0383 
0384     r->request_body_in_single_buf = 1;
0385     r->request_body_in_persistent_file = 1;
0386     r->request_body_in_clean_file = 1;
0387 
0388     if (r->request_body_in_file_only) {
0389         r->request_body_file_log_level = 0;
0390     }
0391 
0392     ngx_http_read_client_request_body(r, ngx_http_perl_handle_request);
0393 
0394     sv_upgrade(TARG, SVt_IV);
0395     sv_setiv(TARG, 1);
0396 
0397     ST(0) = TARG;
0398 
0399 
0400 void
0401 request_body(r)
0402     CODE:
0403 
0404     dXSTARG;
0405     ngx_http_request_t  *r;
0406     u_char              *p, *data;
0407     size_t               len;
0408     ngx_buf_t           *buf;
0409     ngx_chain_t         *cl;
0410 
0411     ngx_http_perl_set_request(r);
0412 
0413     if (r->request_body == NULL
0414         || r->request_body->temp_file
0415         || r->request_body->bufs == NULL)
0416     {
0417         XSRETURN_UNDEF;
0418     }
0419 
0420     cl = r->request_body->bufs;
0421     buf = cl->buf;
0422 
0423     if (cl->next == NULL) {
0424         len = buf->last - buf->pos;
0425         data = buf->pos;
0426         goto done;
0427     }
0428 
0429     len = buf->last - buf->pos;
0430     cl = cl->next;
0431 
0432     for ( /* void */ ; cl; cl = cl->next) {
0433         buf = cl->buf;
0434         len += buf->last - buf->pos;
0435     }
0436 
0437     p = ngx_pnalloc(r->pool, len);
0438     if (p == NULL) {
0439         XSRETURN_UNDEF;
0440     }
0441 
0442     data = p;
0443     cl = r->request_body->bufs;
0444 
0445     for ( /* void */ ; cl; cl = cl->next) {
0446         buf = cl->buf;
0447         p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
0448     }
0449 
0450     done:
0451 
0452     if (len == 0) {
0453         XSRETURN_UNDEF;
0454     }
0455 
0456     ngx_http_perl_set_targ(data, len);
0457 
0458     ST(0) = TARG;
0459 
0460 
0461 void
0462 request_body_file(r)
0463     CODE:
0464 
0465     dXSTARG;
0466     ngx_http_request_t  *r;
0467 
0468     ngx_http_perl_set_request(r);
0469 
0470     if (r->request_body == NULL || r->request_body->temp_file == NULL) {
0471         XSRETURN_UNDEF;
0472     }
0473 
0474     ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data,
0475                            r->request_body->temp_file->file.name.len);
0476 
0477     ST(0) = TARG;
0478 
0479 
0480 void
0481 discard_request_body(r)
0482     CODE:
0483 
0484     ngx_http_request_t  *r;
0485 
0486     ngx_http_perl_set_request(r);
0487 
0488     ngx_http_discard_request_body(r);
0489 
0490 
0491 void
0492 header_out(r, key, value)
0493     CODE:
0494 
0495     ngx_http_request_t  *r;
0496     SV                  *key;
0497     SV                  *value;
0498     ngx_table_elt_t     *header;
0499 
0500     ngx_http_perl_set_request(r);
0501 
0502     key = ST(1);
0503     value = ST(2);
0504 
0505     header = ngx_list_push(&r->headers_out.headers);
0506     if (header == NULL) {
0507         XSRETURN_EMPTY;
0508     }
0509 
0510     header->hash = 1;
0511 
0512     if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) {
0513         header->hash = 0;
0514         XSRETURN_EMPTY;
0515     }
0516 
0517     if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) {
0518         header->hash = 0;
0519         XSRETURN_EMPTY;
0520     }
0521 
0522     if (header->key.len == sizeof("Content-Length") - 1
0523         && ngx_strncasecmp(header->key.data, (u_char *) "Content-Length",
0524                            sizeof("Content-Length") - 1) == 0)
0525     {
0526         r->headers_out.content_length_n = (off_t) SvIV(value);
0527         r->headers_out.content_length = header;
0528     }
0529 
0530     if (header->key.len == sizeof("Content-Encoding") - 1
0531         && ngx_strncasecmp(header->key.data, (u_char *) "Content-Encoding",
0532                            sizeof("Content-Encoding") - 1) == 0)
0533     {
0534         r->headers_out.content_encoding = header;
0535     }
0536 
0537 
0538 void
0539 filename(r)
0540     CODE:
0541 
0542     dXSTARG;
0543     size_t                root;
0544     ngx_http_request_t   *r;
0545     ngx_http_perl_ctx_t  *ctx;
0546 
0547     ngx_http_perl_set_request(r);
0548 
0549     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
0550     if (ctx->filename.data) {
0551         goto done;
0552     }
0553 
0554     if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) {
0555         XSRETURN_UNDEF;
0556     }
0557 
0558     ctx->filename.len--;
0559     sv_setpv(PL_statname, (char *) ctx->filename.data);
0560 
0561     done:
0562 
0563     ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len);
0564 
0565     ST(0) = TARG;
0566 
0567 
0568 void
0569 print(r, ...)
0570     CODE:
0571 
0572     ngx_http_request_t  *r;
0573     SV                  *sv;
0574     int                  i;
0575     u_char              *p;
0576     size_t               size;
0577     STRLEN               len;
0578     ngx_buf_t           *b;
0579 
0580     ngx_http_perl_set_request(r);
0581 
0582     if (items == 2) {
0583 
0584         /*
0585          * do zero copy for prolate single read-only SV:
0586          *     $r->print("some text\n");
0587          */
0588 
0589         sv = ST(1);
0590 
0591         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
0592             sv = SvRV(sv);
0593         }
0594 
0595         if (SvREADONLY(sv) && SvPOK(sv)) {
0596 
0597             p = (u_char *) SvPV(sv, len);
0598 
0599             if (len == 0) {
0600                 XSRETURN_EMPTY;
0601             }
0602 
0603             b = ngx_calloc_buf(r->pool);
0604             if (b == NULL) {
0605                 XSRETURN_EMPTY;
0606             }
0607 
0608             b->memory = 1;
0609             b->pos = p;
0610             b->last = p + len;
0611             b->start = p;
0612             b->end = b->last;
0613 
0614             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0615                            "$r->print: read-only SV: %z", len);
0616 
0617             goto out;
0618         }
0619     }
0620 
0621     size = 0;
0622 
0623     for (i = 1; i < items; i++) {
0624 
0625         sv = ST(i);
0626 
0627         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
0628             sv = SvRV(sv);
0629         }
0630 
0631         (void) SvPV(sv, len);
0632 
0633         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0634                        "$r->print: copy SV: %z", len);
0635 
0636         size += len;
0637     }
0638 
0639     if (size == 0) {
0640         XSRETURN_EMPTY;
0641     }
0642 
0643     b = ngx_create_temp_buf(r->pool, size);
0644     if (b == NULL) {
0645         XSRETURN_EMPTY;
0646     }
0647 
0648     for (i = 1; i < items; i++) {
0649         sv = ST(i);
0650 
0651         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
0652             sv = SvRV(sv);
0653         }
0654 
0655         p = (u_char *) SvPV(sv, len);
0656         b->last = ngx_cpymem(b->last, p, len);
0657     }
0658 
0659     out:
0660 
0661     (void) ngx_http_perl_output(r, b);
0662 
0663 
0664 void
0665 sendfile(r, filename, offset = -1, bytes = 0)
0666     CODE:
0667 
0668     ngx_http_request_t        *r;
0669     char                      *filename;
0670     off_t                      offset;
0671     size_t                     bytes;
0672     ngx_str_t                  path;
0673     ngx_buf_t                 *b;
0674     ngx_open_file_info_t       of;
0675     ngx_http_core_loc_conf_t  *clcf;
0676 
0677     ngx_http_perl_set_request(r);
0678 
0679     filename = SvPV_nolen(ST(1));
0680 
0681     if (filename == NULL) {
0682         croak("sendfile(): NULL filename");
0683     }
0684 
0685     offset = items < 3 ? -1 : SvIV(ST(2));
0686     bytes = items < 4 ? 0 : SvIV(ST(3));
0687 
0688     b = ngx_calloc_buf(r->pool);
0689     if (b == NULL) {
0690         XSRETURN_EMPTY;
0691     }
0692 
0693     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
0694     if (b->file == NULL) {
0695         XSRETURN_EMPTY;
0696     }
0697 
0698     path.len = ngx_strlen(filename);
0699 
0700     path.data = ngx_pnalloc(r->pool, path.len + 1);
0701     if (path.data == NULL) {
0702         XSRETURN_EMPTY;
0703     }
0704 
0705     (void) ngx_cpystrn(path.data, (u_char *) filename, path.len + 1);
0706 
0707     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0708 
0709     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
0710 
0711     of.read_ahead = clcf->read_ahead;
0712     of.directio = clcf->directio;
0713     of.valid = clcf->open_file_cache_valid;
0714     of.min_uses = clcf->open_file_cache_min_uses;
0715     of.errors = clcf->open_file_cache_errors;
0716     of.events = clcf->open_file_cache_events;
0717 
0718     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
0719         XSRETURN_EMPTY;
0720     }
0721 
0722     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
0723         != NGX_OK)
0724     {
0725         if (of.err == 0) {
0726             XSRETURN_EMPTY;
0727         }
0728 
0729         ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
0730                       "%s \"%s\" failed", of.failed, filename);
0731         XSRETURN_EMPTY;
0732     }
0733 
0734     if (offset == -1) {
0735         offset = 0;
0736     }
0737 
0738     if (bytes == 0) {
0739         bytes = of.size - offset;
0740     }
0741 
0742     b->in_file = 1;
0743 
0744     b->file_pos = offset;
0745     b->file_last = offset + bytes;
0746 
0747     b->file->fd = of.fd;
0748     b->file->log = r->connection->log;
0749     b->file->directio = of.is_directio;
0750 
0751     (void) ngx_http_perl_output(r, b);
0752 
0753 
0754 void
0755 flush(r)
0756     CODE:
0757 
0758     ngx_http_request_t  *r;
0759     ngx_buf_t           *b;
0760 
0761     ngx_http_perl_set_request(r);
0762 
0763     b = ngx_calloc_buf(r->pool);
0764     if (b == NULL) {
0765         XSRETURN_EMPTY;
0766     }
0767 
0768     b->flush = 1;
0769 
0770     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
0771 
0772     (void) ngx_http_perl_output(r, b);
0773 
0774     XSRETURN_EMPTY;
0775 
0776 
0777 void
0778 internal_redirect(r, uri)
0779     CODE:
0780 
0781     ngx_http_request_t   *r;
0782     SV                   *uri;
0783     ngx_uint_t            i;
0784     ngx_http_perl_ctx_t  *ctx;
0785 
0786     ngx_http_perl_set_request(r);
0787 
0788     uri = ST(1);
0789 
0790     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
0791 
0792     if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
0793         XSRETURN_EMPTY;
0794     }
0795 
0796     for (i = 0; i < ctx->redirect_uri.len; i++) {
0797         if (ctx->redirect_uri.data[i] == '?') {
0798 
0799             ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1);
0800             ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1];
0801             ctx->redirect_uri.len = i;
0802 
0803             XSRETURN_EMPTY;
0804         }
0805     }
0806 
0807 
0808 void
0809 allow_ranges(r)
0810     CODE:
0811 
0812     ngx_http_request_t  *r;
0813 
0814     ngx_http_perl_set_request(r);
0815 
0816     r->allow_ranges = 1;
0817 
0818 
0819 void
0820 unescape(r, text, type = 0)
0821     CODE:
0822 
0823     dXSTARG;
0824     ngx_http_request_t  *r;
0825     SV                  *text;
0826     int                  type;
0827     u_char              *p, *dst, *src;
0828     STRLEN               len;
0829 
0830     ngx_http_perl_set_request(r);
0831 
0832     text = ST(1);
0833 
0834     src = (u_char *) SvPV(text, len);
0835 
0836     p = ngx_pnalloc(r->pool, len + 1);
0837     if (p == NULL) {
0838         XSRETURN_UNDEF;
0839     }
0840 
0841     dst = p;
0842 
0843     type = items < 3 ? 0 : SvIV(ST(2));
0844 
0845     ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
0846     *dst = '\0';
0847 
0848     ngx_http_perl_set_targ(p, dst - p);
0849 
0850     ST(0) = TARG;
0851 
0852 
0853 void
0854 variable(r, name, value = NULL)
0855     CODE:
0856 
0857     dXSTARG;
0858     ngx_http_request_t         *r;
0859     SV                         *name, *value;
0860     u_char                     *p, *lowcase;
0861     STRLEN                      len;
0862     ngx_str_t                   var, val;
0863     ngx_uint_t                  i, hash;
0864     ngx_http_perl_var_t        *v;
0865     ngx_http_perl_ctx_t        *ctx;
0866     ngx_http_variable_value_t  *vv;
0867 
0868     ngx_http_perl_set_request(r);
0869 
0870     name = ST(1);
0871 
0872     if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
0873         name = SvRV(name);
0874     }
0875 
0876     if (items == 2) {
0877         value = NULL;
0878 
0879     } else {
0880         value = ST(2);
0881 
0882         if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
0883             value = SvRV(value);
0884         }
0885 
0886         if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
0887             XSRETURN_UNDEF;
0888         }
0889     }
0890 
0891     p = (u_char *) SvPV(name, len);
0892 
0893     lowcase = ngx_pnalloc(r->pool, len);
0894     if (lowcase == NULL) {
0895         XSRETURN_UNDEF;
0896     }
0897 
0898     hash = ngx_hash_strlow(lowcase, p, len);
0899 
0900     var.len = len;
0901     var.data = lowcase;
0902 #if (NGX_DEBUG)
0903 
0904     if (value) {
0905         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0906                        "perl variable: \"%V\"=\"%V\"", &var, &val);
0907     } else {
0908         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0909                        "perl variable: \"%V\"", &var);
0910     }
0911 #endif
0912 
0913     vv = ngx_http_get_variable(r, &var, hash);
0914     if (vv == NULL) {
0915         XSRETURN_UNDEF;
0916     }
0917 
0918     if (vv->not_found) {
0919 
0920         ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
0921 
0922         if (ctx->variables) {
0923 
0924             v = ctx->variables->elts;
0925             for (i = 0; i < ctx->variables->nelts; i++) {
0926 
0927                 if (hash != v[i].hash
0928                     || len != v[i].name.len
0929                     || ngx_strncmp(lowcase, v[i].name.data, len) != 0)
0930                 {
0931                     continue;
0932                 }
0933 
0934                 if (value) {
0935                     v[i].value = val;
0936                     XSRETURN_UNDEF;
0937                 }
0938 
0939                 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
0940 
0941                 goto done;
0942             }
0943         }
0944 
0945         if (value) {
0946             if (ctx->variables == NULL) {
0947                 ctx->variables = ngx_array_create(r->pool, 1,
0948                                                   sizeof(ngx_http_perl_var_t));
0949                 if (ctx->variables == NULL) {
0950                     XSRETURN_UNDEF;
0951                 }
0952             }
0953 
0954             v = ngx_array_push(ctx->variables);
0955             if (v == NULL) {
0956                 XSRETURN_UNDEF;
0957             }
0958 
0959             v->hash = hash;
0960             v->name.len = len;
0961             v->name.data = lowcase;
0962             v->value = val;
0963 
0964             XSRETURN_UNDEF;
0965         }
0966 
0967         XSRETURN_UNDEF;
0968     }
0969 
0970     if (value) {
0971         vv->len = val.len;
0972         vv->valid = 1;
0973         vv->no_cacheable = 0;
0974         vv->not_found = 0;
0975         vv->data = val.data;
0976 
0977         XSRETURN_UNDEF;
0978     }
0979 
0980     ngx_http_perl_set_targ(vv->data, vv->len);
0981 
0982     done:
0983 
0984     ST(0) = TARG;
0985 
0986 
0987 void
0988 sleep(r, sleep, next)
0989     CODE:
0990 
0991     ngx_http_request_t   *r;
0992     ngx_msec_t            sleep;
0993     ngx_http_perl_ctx_t  *ctx;
0994 
0995     ngx_http_perl_set_request(r);
0996 
0997     sleep = (ngx_msec_t) SvIV(ST(1));
0998 
0999     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);