3 * Copyright (C) Igor Sysoev
7 #define PERL_NO_GET_CONTEXT
9 #include <ngx_config.h>
12 #include <ngx_http_perl_module.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) \
23 SvUPGRADE(TARG, SVt_PV); \
25 sv_setpvn(TARG, (char *) p, len)
29 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv)
34 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
38 p = (u_char *) SvPV(sv, len);
42 if (SvREADONLY(sv) && SvPOK(sv)) {
45 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
46 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
51 s->data = ngx_pnalloc(r->pool, len);
52 if (s->data == NULL) {
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);
66 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b)
71 ngx_http_perl_ctx_t *ctx;
73 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
76 cl = ngx_alloc_chain_link(r->pool);
83 *ctx->ssi->last_out = cl;
84 ctx->ssi->last_out = &cl->next;
93 return ngx_http_output_filter(r, &out);
97 MODULE = nginx PACKAGE = nginx
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);
117 send_http_header(r, ...)
120 ngx_http_request_t *r;
123 ngx_http_perl_set_request(r);
125 if (r->headers_out.status == 0) {
126 r->headers_out.status = NGX_HTTP_OK;
132 if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv)
138 r->headers_out.content_type_len = r->headers_out.content_type.len;
141 if (ngx_http_set_content_type(r) != NGX_OK) {
146 (void) ngx_http_send_header(r);
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);
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);
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);
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);
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);
222 ngx_http_request_t *r;
224 u_char *p, *lowcase_key, *cookie;
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);
237 if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
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) {
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);
259 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset);
262 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
272 n = r->headers_in.cookies.nelts;
278 ph = r->headers_in.cookies.elts;
281 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
286 size = - (ssize_t) (sizeof("; ") - 1);
288 for (i = 0; i < n; i++) {
289 size += ph[i]->value.len + sizeof("; ") - 1;
292 cookie = ngx_pnalloc(r->pool, size);
293 if (cookie == NULL) {
299 for (i = 0; /* void */ ; i++) {
300 p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len);
306 *p++ = ';'; *p++ = ' ';
309 ngx_http_perl_set_targ(cookie, size);
314 /* iterate over all headers */
316 part = &r->headers_in.headers.part;
319 for (i = 0; /* void */ ; i++) {
321 if (i >= part->nelts) {
322 if (part->next == NULL) {
331 if (len != h[i].key.len
332 || ngx_strcasecmp(p, h[i].key.data) != 0)
337 ngx_http_perl_set_targ(h[i].value.data, h[i].value.len);
350 has_request_body(r, next)
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) {
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;
374 ngx_http_read_client_request_body(r, ngx_http_perl_handle_request);
376 sv_upgrade(TARG, SVt_IV);
387 ngx_http_request_t *r;
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)
399 len = r->request_body->bufs->buf->last - r->request_body->bufs->buf->pos;
405 ngx_http_perl_set_targ(r->request_body->bufs->buf->pos, len);
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) {
423 ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data,
424 r->request_body->temp_file->file.name.len);
430 discard_request_body(r)
433 ngx_http_request_t *r;
435 ngx_http_perl_set_request(r);
437 ngx_http_discard_request_body(r);
441 header_out(r, key, value)
444 ngx_http_request_t *r;
447 ngx_table_elt_t *header;
449 ngx_http_perl_set_request(r);
454 header = ngx_list_push(&r->headers_out.headers);
455 if (header == NULL) {
461 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) {
465 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) {
469 if (header->key.len == sizeof("Content-Length") - 1
470 && ngx_strncasecmp(header->key.data, "Content-Length",
471 sizeof("Content-Length") - 1) == 0)
473 r->headers_out.content_length_n = (off_t) SvIV(value);
474 r->headers_out.content_length = header;
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) {
494 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) {
499 sv_setpv(PL_statname, (char *) ctx->filename.data);
503 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len);
512 ngx_http_request_t *r;
520 ngx_http_perl_set_request(r);
525 * do zero copy for prolate single read-only SV:
526 * $r->print("some text\n");
531 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
535 if (SvREADONLY(sv) && SvPOK(sv)) {
537 p = (u_char *) SvPV(sv, len);
543 b = ngx_calloc_buf(r->pool);
554 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
555 "$r->print: read-only SV: %z", len);
563 for (i = 1; i < items; i++) {
567 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
571 (void) SvPV(sv, len);
573 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
574 "$r->print: copy SV: %z", len);
583 b = ngx_create_temp_buf(r->pool, size);
588 for (i = 1; i < items; i++) {
591 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
595 p = (u_char *) SvPV(sv, len);
596 b->last = ngx_cpymem(b->last, p, len);
601 (void) ngx_http_perl_output(r, b);
605 sendfile(r, filename, offset = -1, bytes = 0)
608 ngx_http_request_t *r;
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");
625 offset = items < 3 ? -1 : SvIV(ST(2));
626 bytes = items < 4 ? 0 : SvIV(ST(3));
628 b = ngx_calloc_buf(r->pool);
633 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
634 if (b->file == NULL) {
638 path.len = ngx_strlen(filename);
640 path.data = ngx_pnalloc(r->pool, path.len + 1);
641 if (path.data == NULL) {
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)
664 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
665 ngx_open_file_n " \"%s\" failed", filename);
674 bytes = of.size - offset;
679 b->file_pos = offset;
680 b->file_last = offset + bytes;
683 b->file->log = r->connection->log;
684 b->file->directio = of.is_directio;
686 (void) ngx_http_perl_output(r, b);
693 ngx_http_request_t *r;
696 ngx_http_perl_set_request(r);
698 b = ngx_calloc_buf(r->pool);
705 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
707 (void) ngx_http_perl_output(r, b);
713 internal_redirect(r, uri)
716 ngx_http_request_t *r;
719 ngx_http_perl_ctx_t *ctx;
721 ngx_http_perl_set_request(r);
725 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
727 if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
731 for (i = 0; i < ctx->redirect_uri.len; i++) {
732 if (ctx->redirect_uri.data[i] == '?') {
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;
747 ngx_http_request_t *r;
749 ngx_http_perl_set_request(r);
755 unescape(r, text, type = 0)
759 ngx_http_request_t *r;
762 u_char *p, *dst, *src;
765 ngx_http_perl_set_request(r);
769 src = (u_char *) SvPV(text, len);
771 p = ngx_pnalloc(r->pool, len + 1);
778 type = items < 3 ? 0 : SvIV(ST(2));
780 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
783 ngx_http_perl_set_targ(p, dst - p);
789 variable(r, name, value = NULL)
793 ngx_http_request_t *r;
799 ngx_http_perl_var_t *v;
800 ngx_http_perl_ctx_t *ctx;
801 ngx_http_variable_value_t *vv;
803 ngx_http_perl_set_request(r);
807 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
817 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
821 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
826 p = (u_char *) SvPV(name, len);
828 lowcase = ngx_pnalloc(r->pool, len);
829 if (lowcase == NULL) {
833 hash = ngx_hash_strlow(lowcase, p, len);
841 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
842 "perl variable: \"%V\"=\"%V\"", &var, &val);
844 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
845 "perl variable: \"%V\"", &var);
850 vv = ngx_http_get_variable(r, &var, hash, 1);
857 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
859 if (ctx->variables) {
861 v = ctx->variables->elts;
862 for (i = 0; i < ctx->variables->nelts; i++) {
864 if (hash != v[i].hash
865 || len != v[i].name.len
866 || ngx_strncmp(lowcase, v[i].name.data, len) != 0)
876 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
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) {
891 v = ngx_array_push(ctx->variables);
898 v->name.data = lowcase;
904 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
905 "variable \"%V\" not found", &var);
913 vv->no_cacheable = 0;
920 ngx_http_perl_set_targ(vv->data, vv->len);
928 sleep(r, sleep, next)
931 ngx_http_request_t *r;
933 ngx_http_perl_ctx_t *ctx;
935 ngx_http_perl_set_request(r);
937 sleep = (ngx_msec_t) SvIV(ST(1));
939 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
940 "perl sleep: %M", sleep);
942 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
944 ctx->next = SvRV(ST(2));
946 ngx_add_timer(r->connection->write, sleep);
948 r->write_event_handler = ngx_http_perl_sleep_handler;
952 log_error(r, err, msg)
955 ngx_http_request_t *r;
961 ngx_http_perl_set_request(r);
965 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) {
973 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) {
977 p = (u_char *) SvPV(msg, len);
979 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p);