nginx 0.7.8
[nginx-catap.git] / src / http / modules / perl / nginx.xs
blobfbc7f59be48df2cc2eaced761328cd30afcf216f
2 /*
3  * Copyright (C) Igor Sysoev
4  */
7 #define PERL_NO_GET_CONTEXT
9 #include <ngx_config.h>
10 #include <ngx_core.h>
11 #include <ngx_http.h>
12 #include <ngx_http_perl_module.h>
14 #include "XSUB.h"
17 #define ngx_http_perl_set_request(r)                                          \
18     r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0))))
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)
28 static ngx_int_t
29 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv)
31     u_char  *p;
32     STRLEN   len;
34     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
35         sv = SvRV(sv);
36     }
38     p = (u_char *) SvPV(sv, len);
40     s->len = len;
42     if (SvREADONLY(sv) && SvPOK(sv)) {
43         s->data = p;
45         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
46                        "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
48         return NGX_OK;
49     }
51     s->data = ngx_pnalloc(r->pool, len);
52     if (s->data == NULL) {
53         return NGX_ERROR;
54     }
56     ngx_memcpy(s->data, p, len);
58     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
59                    "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
61     return NGX_OK;
65 static ngx_int_t
66 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b)
68     ngx_chain_t           out;
69 #if (NGX_HTTP_SSI)
70     ngx_chain_t          *cl;
71     ngx_http_perl_ctx_t  *ctx;
73     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
75     if (ctx->ssi) {
76         cl = ngx_alloc_chain_link(r->pool);
77         if (cl == NULL) {
78             return NGX_ERROR;
79         }
81         cl->buf = b;
82         cl->next = NULL;
83         *ctx->ssi->last_out = cl;
84         ctx->ssi->last_out = &cl->next;
86         return NGX_OK;
87     }
88 #endif
90     out.buf = b;
91     out.next = NULL;
93     return ngx_http_output_filter(r, &out);
97 MODULE = nginx    PACKAGE = nginx
100 void
101 status(r, code)
102     CODE:
104     ngx_http_request_t  *r;
106     ngx_http_perl_set_request(r);
108     r->headers_out.status = SvIV(ST(1));
110     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
111                    "perl status: %d", r->headers_out.status);
113     XSRETURN_UNDEF;
116 void
117 send_http_header(r, ...)
118     CODE:
120     ngx_http_request_t  *r;
121     SV                  *sv;
123     ngx_http_perl_set_request(r);
125     if (r->headers_out.status == 0) {
126         r->headers_out.status = NGX_HTTP_OK;
127     }
129     if (items != 1) {
130         sv = ST(1);
132         if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv)
133             != NGX_OK)
134         {
135             XSRETURN_EMPTY;
136         }
138         r->headers_out.content_type_len = r->headers_out.content_type.len;
140     } else {
141         if (ngx_http_set_content_type(r) != NGX_OK) {
142             XSRETURN_EMPTY;
143         }
144     }
146     (void) ngx_http_send_header(r);
149 void
150 header_only(r)
151     CODE:
153     dXSTARG;
154     ngx_http_request_t  *r;
156     ngx_http_perl_set_request(r);
158     sv_upgrade(TARG, SVt_IV);
159     sv_setiv(TARG, r->header_only);
161     ST(0) = TARG;
164 void
165 uri(r)
166     CODE:
168     dXSTARG;
169     ngx_http_request_t  *r;
171     ngx_http_perl_set_request(r);
172     ngx_http_perl_set_targ(r->uri.data, r->uri.len);
174     ST(0) = TARG;
177 void
178 args(r)
179     CODE:
181     dXSTARG;
182     ngx_http_request_t  *r;
184     ngx_http_perl_set_request(r);
185     ngx_http_perl_set_targ(r->args.data, r->args.len);
187     ST(0) = TARG;
190 void
191 request_method(r)
192     CODE:
194     dXSTARG;
195     ngx_http_request_t  *r;
197     ngx_http_perl_set_request(r);
198     ngx_http_perl_set_targ(r->method_name.data, r->method_name.len);
200     ST(0) = TARG;
203 void
204 remote_addr(r)
205     CODE:
207     dXSTARG;
208     ngx_http_request_t  *r;
210     ngx_http_perl_set_request(r);
211     ngx_http_perl_set_targ(r->connection->addr_text.data,
212                            r->connection->addr_text.len);
214     ST(0) = TARG;
217 void
218 header_in(r, key)
219     CODE:
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;
233     ngx_http_perl_set_request(r);
235     key = ST(1);
237     if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
238         key = SvRV(key);
239     }
241     p = (u_char *) SvPV(key, len);
243     /* look up hashed headers */
245     lowcase_key = ngx_pnalloc(r->pool, len);
246     if (lowcase_key == NULL) {
247         XSRETURN_UNDEF;
248     }
250     hash = ngx_hash_strlow(lowcase_key, p, len);
252     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
254     hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len);
256     if (hh) {
257         if (hh->offset) {
259             ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset);
261             if (*ph) {
262                 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
264                 goto done;
265             }
267             XSRETURN_UNDEF;
268         }
270         /* Cookie */
272         n = r->headers_in.cookies.nelts;
274         if (n == 0) {
275             XSRETURN_UNDEF;
276         }
278         ph = r->headers_in.cookies.elts;
280         if (n == 1) {
281             ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
283             goto done;
284         }
286         size = - (ssize_t) (sizeof("; ") - 1);
288         for (i = 0; i < n; i++) {
289             size += ph[i]->value.len + sizeof("; ") - 1;
290         }
292         cookie = ngx_pnalloc(r->pool, size);
293         if (cookie == NULL) {
294             XSRETURN_UNDEF;
295         }
297         p = cookie;
299         for (i = 0; /* void */ ; i++) {
300             p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len);
302             if (i == n - 1) {
303                 break;
304             }
306             *p++ = ';'; *p++ = ' ';
307         }
309         ngx_http_perl_set_targ(cookie, size);
311         goto done;
312     }
314     /* iterate over all headers */
316     part = &r->headers_in.headers.part;
317     h = part->elts;
319     for (i = 0; /* void */ ; i++) {
321         if (i >= part->nelts) {
322             if (part->next == NULL) {
323                 break;
324             }
326             part = part->next;
327             h = part->elts;
328             i = 0;
329         }
331         if (len != h[i].key.len
332             || ngx_strcasecmp(p, h[i].key.data) != 0)
333         {
334             continue;
335         }
337         ngx_http_perl_set_targ(h[i].value.data, h[i].value.len);
339         goto done;
340     }
342     XSRETURN_UNDEF;
344     done:
346     ST(0) = TARG;
349 void
350 has_request_body(r, next)
351     CODE:
353     dXSTARG;
354     ngx_http_request_t   *r;
355     ngx_http_perl_ctx_t  *ctx;
357     ngx_http_perl_set_request(r);
359     if (r->headers_in.content_length_n <= 0) {
360         XSRETURN_UNDEF;
361     }
363     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
364     ctx->next = SvRV(ST(1));
366     r->request_body_in_single_buf = 1;
367     r->request_body_in_persistent_file = 1;
368     r->request_body_in_clean_file = 1;
370     if (r->request_body_in_file_only) {
371         r->request_body_file_log_level = 0;
372     }
374     ngx_http_read_client_request_body(r, ngx_http_perl_handle_request);
376     sv_upgrade(TARG, SVt_IV);
377     sv_setiv(TARG, 1);
379     ST(0) = TARG;
382 void
383 request_body(r)
384     CODE:
386     dXSTARG;
387     ngx_http_request_t  *r;
388     size_t               len;
390     ngx_http_perl_set_request(r);
392     if (r->request_body == NULL
393         || r->request_body->temp_file
394         || r->request_body->bufs == NULL)
395     {
396         XSRETURN_UNDEF;
397     }
399     len = r->request_body->bufs->buf->last - r->request_body->bufs->buf->pos;
401     if (len == 0) {
402         XSRETURN_UNDEF;
403     }
405     ngx_http_perl_set_targ(r->request_body->bufs->buf->pos, len);
407     ST(0) = TARG;
410 void
411 request_body_file(r)
412     CODE:
414     dXSTARG;
415     ngx_http_request_t  *r;
417     ngx_http_perl_set_request(r);
419     if (r->request_body == NULL || r->request_body->temp_file == NULL) {
420         XSRETURN_UNDEF;
421     }
423     ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data,
424                            r->request_body->temp_file->file.name.len);
426     ST(0) = TARG;
429 void
430 discard_request_body(r)
431     CODE:
433     ngx_http_request_t  *r;
435     ngx_http_perl_set_request(r);
437     ngx_http_discard_request_body(r);
440 void
441 header_out(r, key, value)
442     CODE:
444     ngx_http_request_t  *r;
445     SV                  *key;
446     SV                  *value;
447     ngx_table_elt_t     *header;
449     ngx_http_perl_set_request(r);
451     key = ST(1);
452     value = ST(2);
454     header = ngx_list_push(&r->headers_out.headers);
455     if (header == NULL) {
456         XSRETURN_EMPTY;
457     }
459     header->hash = 1;
461     if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) {
462         XSRETURN_EMPTY;
463     }
465     if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) {
466         XSRETURN_EMPTY;
467     }
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     }
478 void
479 filename(r)
480     CODE:
482     dXSTARG;
483     size_t                root;
484     ngx_http_request_t   *r;
485     ngx_http_perl_ctx_t  *ctx;
487     ngx_http_perl_set_request(r);
489     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
490     if (ctx->filename.data) {
491         goto done;
492     }
494     if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) {
495         XSRETURN_UNDEF;
496     }
498     ctx->filename.len--;
499     sv_setpv(PL_statname, (char *) ctx->filename.data);
501     done:
503     ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len);
505     ST(0) = TARG;
508 void
509 print(r, ...)
510     CODE:
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;
520     ngx_http_perl_set_request(r);
522     if (items == 2) {
524         /*
525          * do zero copy for prolate single read-only SV:
526          *     $r->print("some text\n");
527          */
529         sv = ST(1);
531         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
532             sv = SvRV(sv);
533         }
535         if (SvREADONLY(sv) && SvPOK(sv)) {
537             p = (u_char *) SvPV(sv, len);
539             if (len == 0) {
540                 XSRETURN_EMPTY;
541             }
543             b = ngx_calloc_buf(r->pool);
544             if (b == NULL) {
545                 XSRETURN_EMPTY;
546             }
548             b->memory = 1;
549             b->pos = p;
550             b->last = p + len;
551             b->start = p;
552             b->end = b->last;
554             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
555                            "$r->print: read-only SV: %z", len);
557             goto out;
558         }
559     }
561     size = 0;
563     for (i = 1; i < items; i++) {
565         sv = ST(i);
567         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
568             sv = SvRV(sv);
569         }
571         (void) SvPV(sv, len);
573         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
574                        "$r->print: copy SV: %z", len);
576         size += len;
577     }
579     if (size == 0) {
580         XSRETURN_EMPTY;
581     }
583     b = ngx_create_temp_buf(r->pool, size);
584     if (b == NULL) {
585         XSRETURN_EMPTY;
586     }
588     for (i = 1; i < items; i++) {
589         sv = ST(i);
591         if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
592             sv = SvRV(sv);
593         }
595         p = (u_char *) SvPV(sv, len);
596         b->last = ngx_cpymem(b->last, p, len);
597     }
599     out:
601     (void) ngx_http_perl_output(r, b);
604 void
605 sendfile(r, filename, offset = -1, bytes = 0)
606     CODE:
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;
617     ngx_http_perl_set_request(r);
619     filename = SvPV_nolen(ST(1));
621     if (filename == NULL) {
622         croak("sendfile(): NULL filename");
623     }
625     offset = items < 3 ? -1 : SvIV(ST(2));
626     bytes = items < 4 ? 0 : SvIV(ST(3));
628     b = ngx_calloc_buf(r->pool);
629     if (b == NULL) {
630         XSRETURN_EMPTY;
631     }
633     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
634     if (b->file == NULL) {
635         XSRETURN_EMPTY;
636     }
638     path.len = ngx_strlen(filename);
640     path.data = ngx_pnalloc(r->pool, path.len + 1);
641     if (path.data == NULL) {
642         XSRETURN_EMPTY;
643     }
645     (void) ngx_cpystrn(path.data, filename, path.len + 1);
647     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
649     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
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;
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         }
664         ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
665                       ngx_open_file_n " \"%s\" failed", filename);
666         XSRETURN_EMPTY;
667     }
669     if (offset == -1) {
670         offset = 0;
671     }
673     if (bytes == 0) {
674         bytes = of.size - offset;
675     }
677     b->in_file = 1;
679     b->file_pos = offset;
680     b->file_last = offset + bytes;
682     b->file->fd = of.fd;
683     b->file->log = r->connection->log;
685     (void) ngx_http_perl_output(r, b);
688 void
689 flush(r)
690     CODE:
692     ngx_http_request_t  *r;
693     ngx_buf_t           *b;
695     ngx_http_perl_set_request(r);
697     b = ngx_calloc_buf(r->pool);
698     if (b == NULL) {
699         XSRETURN_EMPTY;
700     }
702     b->flush = 1;
704     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
706     (void) ngx_http_perl_output(r, b);
708     XSRETURN_EMPTY;
711 void
712 internal_redirect(r, uri)
713     CODE:
715     ngx_http_request_t   *r;
716     SV                   *uri;
717     ngx_uint_t            i;
718     ngx_http_perl_ctx_t  *ctx;
720     ngx_http_perl_set_request(r);
722     uri = ST(1);
724     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
726     if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
727         XSRETURN_EMPTY;
728     }
730     for (i = 0; i < ctx->redirect_uri.len; i++) {
731         if (ctx->redirect_uri.data[i] == '?') {
733             ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1);
734             ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1];
735             ctx->redirect_uri.len = i;
737             XSRETURN_EMPTY;
738         }
739     }
742 void
743 allow_ranges(r)
744     CODE:
746     ngx_http_request_t  *r;
748     ngx_http_perl_set_request(r);
750     r->allow_ranges = 1;
753 void
754 unescape(r, text, type = 0)
755     CODE:
757     dXSTARG;
758     ngx_http_request_t  *r;
759     SV                  *text;
760     int                  type;
761     u_char              *p, *dst, *src;
762     STRLEN               len;
764     ngx_http_perl_set_request(r);
766     text = ST(1);
768     src = (u_char *) SvPV(text, len);
770     p = ngx_pnalloc(r->pool, len + 1);
771     if (p == NULL) {
772         XSRETURN_UNDEF;
773     }
775     dst = p;
777     type = items < 3 ? 0 : SvIV(ST(2));
779     ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
780     *dst = '\0';
782     ngx_http_perl_set_targ(p, dst - p);
784     ST(0) = TARG;
787 void
788 variable(r, name, value = NULL)
789     CODE:
791     dXSTARG;
792     ngx_http_request_t         *r;
793     SV                         *name, *value;
794     u_char                     *p, *lowcase;
795     STRLEN                      len;
796     ngx_str_t                   var, val;
797     ngx_uint_t                  i, hash;
798     ngx_http_perl_var_t        *v;
799     ngx_http_perl_ctx_t        *ctx;
800     ngx_http_variable_value_t  *vv;
802     ngx_http_perl_set_request(r);
804     name = ST(1);
806     if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
807         name = SvRV(name);
808     }
810     if (items == 2) {
811         value = NULL;
813     } else {
814         value = ST(2);
816         if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
817             value = SvRV(value);
818         }
820         if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
821             XSRETURN_UNDEF;
822         }
823     }
825     p = (u_char *) SvPV(name, len);
827     lowcase = ngx_pnalloc(r->pool, len);
828     if (lowcase == NULL) {
829         XSRETURN_UNDEF;
830     }
832     hash = ngx_hash_strlow(lowcase, p, len);
834     var.len = len;
835     var.data = lowcase;
837     #if (NGX_LOG_DEBUG)
839     if (value) {
840         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
841                        "perl variable: \"%V\"=\"%V\"", &var, &val);
842     } else {
843         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
844                        "perl variable: \"%V\"", &var);
845     }
847     #endif
849     vv = ngx_http_get_variable(r, &var, hash, 1);
850     if (vv == NULL) {
851         XSRETURN_UNDEF;
852     }
854     if (vv->not_found) {
856         ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
858         if (ctx->variables) {
860             v = ctx->variables->elts;
861             for (i = 0; i < ctx->variables->nelts; i++) {
863                 if (hash != v[i].hash
864                     || len != v[i].name.len
865                     || ngx_strncmp(lowcase, v[i].name.data, len) != 0)
866                 {
867                     continue;
868                 }
870                 if (value) {
871                     v[i].value = val;
872                     XSRETURN_UNDEF;
873                 }
875                 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
877                 goto done;
878             }
879         }
881         if (value) {
882             if (ctx->variables == NULL) {
883                 ctx->variables = ngx_array_create(r->pool, 1,
884                                                   sizeof(ngx_http_perl_var_t));
885                 if (ctx->variables == NULL) {
886                     XSRETURN_UNDEF;
887                 }
888             }
890             v = ngx_array_push(ctx->variables);
891             if (v == NULL) {
892                 XSRETURN_UNDEF;
893             }
895             v->hash = hash;
896             v->name.len = len;
897             v->name.data = lowcase;
898             v->value = val;
900             XSRETURN_UNDEF;
901         }
903         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
904                       "variable \"%V\" not found", &var);
906         XSRETURN_UNDEF;
907     }
909     if (value) {
910         vv->len = val.len;
911         vv->valid = 1;
912         vv->no_cacheable = 0;
913         vv->not_found = 0;
914         vv->data = val.data;
916         XSRETURN_UNDEF;
917     }
919     ngx_http_perl_set_targ(vv->data, vv->len);
921     done:
923     ST(0) = TARG;
926 void
927 sleep(r, sleep, next)
928     CODE:
930     ngx_http_request_t   *r;
931     ngx_msec_t            sleep;
932     ngx_http_perl_ctx_t  *ctx;
934     ngx_http_perl_set_request(r);
936     sleep = (ngx_msec_t) SvIV(ST(1));
938     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
939                    "perl sleep: %M", sleep);
941     ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
943     ctx->next = SvRV(ST(2));
945     ngx_add_timer(r->connection->write, sleep);
947     r->write_event_handler = ngx_http_perl_sleep_handler;
950 void
951 log_error(r, err, msg)
952     CODE:
954     ngx_http_request_t  *r;
955     SV                  *err, *msg;
956     u_char              *p;
957     STRLEN               len;
958     ngx_err_t            e;
960     ngx_http_perl_set_request(r);
962     err = ST(1);
964     if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) {
965         err = SvRV(err);
966     }
968     e = SvIV(err);
970     msg = ST(2);
972     if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) {
973         msg = SvRV(msg);
974     }
976     p = (u_char *) SvPV(msg, len);
978     ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p);