3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #define PERL_NO_GET_CONTEXT
10 #include <ngx_config.h>
13 #include <ngx_http_perl_module.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) \
24 SvUPGRADE(TARG, SVt_PV); \
26 sv_setpvn(TARG, (char *) p, len)
30 ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv)
35 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
39 p = (u_char *) SvPV(sv, len);
43 if (SvREADONLY(sv) && SvPOK(sv)) {
46 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
47 "perl sv2str: %08XD \"%V\"", sv->sv_flags, s);
52 s->data = ngx_pnalloc(r->pool, len);
53 if (s->data == NULL) {
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);
67 ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b)
72 ngx_http_perl_ctx_t *ctx;
74 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
77 cl = ngx_alloc_chain_link(r->pool);
84 *ctx->ssi->last_out = cl;
85 ctx->ssi->last_out = &cl->next;
94 return ngx_http_output_filter(r, &out);
98 MODULE = nginx PACKAGE = nginx
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);
118 send_http_header(r, ...)
121 ngx_http_request_t *r;
124 ngx_http_perl_set_request(r);
126 if (r->headers_out.status == 0) {
127 r->headers_out.status = NGX_HTTP_OK;
133 if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv)
139 r->headers_out.content_type_len = r->headers_out.content_type.len;
142 if (ngx_http_set_content_type(r) != NGX_OK) {
147 (void) ngx_http_send_header(r);
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);
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);
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);
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);
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);
223 ngx_http_request_t *r;
225 u_char *p, *lowcase_key, *cookie;
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);
238 if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
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) {
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);
260 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset);
263 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
273 n = r->headers_in.cookies.nelts;
279 ph = r->headers_in.cookies.elts;
282 ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len);
287 size = - (ssize_t) (sizeof("; ") - 1);
289 for (i = 0; i < n; i++) {
290 size += ph[i]->value.len + sizeof("; ") - 1;
293 cookie = ngx_pnalloc(r->pool, size);
294 if (cookie == NULL) {
300 for (i = 0; /* void */ ; i++) {
301 p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len);
307 *p++ = ';'; *p++ = ' ';
310 ngx_http_perl_set_targ(cookie, size);
315 /* iterate over all headers */
317 part = &r->headers_in.headers.part;
320 for (i = 0; /* void */ ; i++) {
322 if (i >= part->nelts) {
323 if (part->next == NULL) {
332 if (len != h[i].key.len
333 || ngx_strcasecmp(p, h[i].key.data) != 0)
338 ngx_http_perl_set_targ(h[i].value.data, h[i].value.len);
351 has_request_body(r, next)
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) {
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;
375 ngx_http_read_client_request_body(r, ngx_http_perl_handle_request);
377 sv_upgrade(TARG, SVt_IV);
388 ngx_http_request_t *r;
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)
403 cl = r->request_body->bufs;
406 if (cl->next == NULL) {
407 len = buf->last - buf->pos;
412 len = buf->last - buf->pos;
415 for ( /* void */ ; cl; cl = cl->next) {
417 len += buf->last - buf->pos;
420 p = ngx_pnalloc(r->pool, len);
422 return XSRETURN_UNDEF;
426 cl = r->request_body->bufs;
428 for ( /* void */ ; cl; cl = cl->next) {
430 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
439 ngx_http_perl_set_targ(data, len);
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) {
457 ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data,
458 r->request_body->temp_file->file.name.len);
464 discard_request_body(r)
467 ngx_http_request_t *r;
469 ngx_http_perl_set_request(r);
471 ngx_http_discard_request_body(r);
475 header_out(r, key, value)
478 ngx_http_request_t *r;
481 ngx_table_elt_t *header;
483 ngx_http_perl_set_request(r);
488 header = ngx_list_push(&r->headers_out.headers);
489 if (header == NULL) {
495 if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) {
499 if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) {
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)
507 r->headers_out.content_length_n = (off_t) SvIV(value);
508 r->headers_out.content_length = header;
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)
515 r->headers_out.content_encoding = header;
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) {
535 if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) {
540 sv_setpv(PL_statname, (char *) ctx->filename.data);
544 ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len);
553 ngx_http_request_t *r;
561 ngx_http_perl_set_request(r);
566 * do zero copy for prolate single read-only SV:
567 * $r->print("some text\n");
572 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
576 if (SvREADONLY(sv) && SvPOK(sv)) {
578 p = (u_char *) SvPV(sv, len);
584 b = ngx_calloc_buf(r->pool);
595 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
596 "$r->print: read-only SV: %z", len);
604 for (i = 1; i < items; i++) {
608 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
612 (void) SvPV(sv, len);
614 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
615 "$r->print: copy SV: %z", len);
624 b = ngx_create_temp_buf(r->pool, size);
629 for (i = 1; i < items; i++) {
632 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) {
636 p = (u_char *) SvPV(sv, len);
637 b->last = ngx_cpymem(b->last, p, len);
642 (void) ngx_http_perl_output(r, b);
646 sendfile(r, filename, offset = -1, bytes = 0)
649 ngx_http_request_t *r;
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");
666 offset = items < 3 ? -1 : SvIV(ST(2));
667 bytes = items < 4 ? 0 : SvIV(ST(3));
669 b = ngx_calloc_buf(r->pool);
674 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
675 if (b->file == NULL) {
679 path.len = ngx_strlen(filename);
681 path.data = ngx_pnalloc(r->pool, path.len + 1);
682 if (path.data == NULL) {
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) {
703 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
710 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
711 "%s \"%s\" failed", of.failed, filename);
720 bytes = of.size - offset;
725 b->file_pos = offset;
726 b->file_last = offset + bytes;
729 b->file->log = r->connection->log;
730 b->file->directio = of.is_directio;
732 (void) ngx_http_perl_output(r, b);
739 ngx_http_request_t *r;
742 ngx_http_perl_set_request(r);
744 b = ngx_calloc_buf(r->pool);
751 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush");
753 (void) ngx_http_perl_output(r, b);
759 internal_redirect(r, uri)
762 ngx_http_request_t *r;
765 ngx_http_perl_ctx_t *ctx;
767 ngx_http_perl_set_request(r);
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) {
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;
793 ngx_http_request_t *r;
795 ngx_http_perl_set_request(r);
801 unescape(r, text, type = 0)
805 ngx_http_request_t *r;
808 u_char *p, *dst, *src;
811 ngx_http_perl_set_request(r);
815 src = (u_char *) SvPV(text, len);
817 p = ngx_pnalloc(r->pool, len + 1);
824 type = items < 3 ? 0 : SvIV(ST(2));
826 ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type);
829 ngx_http_perl_set_targ(p, dst - p);
835 variable(r, name, value = NULL)
839 ngx_http_request_t *r;
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);
853 if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) {
863 if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) {
867 if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) {
872 p = (u_char *) SvPV(name, len);
874 lowcase = ngx_pnalloc(r->pool, len);
875 if (lowcase == NULL) {
879 hash = ngx_hash_strlow(lowcase, p, len);
887 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
888 "perl variable: \"%V\"=\"%V\"", &var, &val);
890 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
891 "perl variable: \"%V\"", &var);
896 vv = ngx_http_get_variable(r, &var, hash);
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)
922 ngx_http_perl_set_targ(v[i].value.data, v[i].value.len);
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) {
937 v = ngx_array_push(ctx->variables);
944 v->name.data = lowcase;
956 vv->no_cacheable = 0;
963 ngx_http_perl_set_targ(vv->data, vv->len);
971 sleep(r, sleep, next)
974 ngx_http_request_t *r;
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;
996 log_error(r, err, msg)
999 ngx_http_request_t *r;
1005 ngx_http_perl_set_request(r);
1009 if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) {
1017 if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) {
1021 p = (u_char *) SvPV(msg, len);
1023 ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p);