From e18a033b921d0d79fa8278f853548e6125b93e0c Mon Sep 17 00:00:00 2001 From: Konstantin Ananyev Date: Tue, 31 Oct 2017 12:40:17 +0000 Subject: Integrate TLDK with NGINX Created a clone of nginx (from https://github.com/nginx/nginx) to demonstrate and benchmark TLDK library integrated with real world application. A new nginx module is created and and BSD socket-like API is implemented on top of native TLDK API. Note, that right now only minimalistic subset of socket-like API is provided: - accept - close - readv - recv - writev so only limited nginx functionality is available for a moment. Change-Id: Ie1efe9349a0538da4348a48fb8306cbf636b5a92 Signed-off-by: Mohammad Abdul Awal Signed-off-by: Reshma Pattan Signed-off-by: Remy Horton Signed-off-by: Konstantin Ananyev --- app/nginx/src/http/modules/perl/Makefile.PL | 35 + app/nginx/src/http/modules/perl/nginx.pm | 138 +++ app/nginx/src/http/modules/perl/nginx.xs | 1039 +++++++++++++++++++ .../src/http/modules/perl/ngx_http_perl_module.c | 1086 ++++++++++++++++++++ .../src/http/modules/perl/ngx_http_perl_module.h | 67 ++ app/nginx/src/http/modules/perl/typemap | 3 + 6 files changed, 2368 insertions(+) create mode 100644 app/nginx/src/http/modules/perl/Makefile.PL create mode 100644 app/nginx/src/http/modules/perl/nginx.pm create mode 100644 app/nginx/src/http/modules/perl/nginx.xs create mode 100644 app/nginx/src/http/modules/perl/ngx_http_perl_module.c create mode 100644 app/nginx/src/http/modules/perl/ngx_http_perl_module.h create mode 100644 app/nginx/src/http/modules/perl/typemap (limited to 'app/nginx/src/http/modules/perl') diff --git a/app/nginx/src/http/modules/perl/Makefile.PL b/app/nginx/src/http/modules/perl/Makefile.PL new file mode 100644 index 0000000..7edadcb --- /dev/null +++ b/app/nginx/src/http/modules/perl/Makefile.PL @@ -0,0 +1,35 @@ + +# Copyright (C) Igor Sysoev +# Copyright (C) Nginx, Inc. + +use 5.006001; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'nginx', + VERSION_FROM => 'nginx.pm', # finds $VERSION + PREREQ_PM => {}, # e.g., Module::Name => 1.1 + + ABSTRACT_FROM => 'nginx.pm', # retrieve abstract from module + AUTHOR => 'Igor Sysoev', + + CCFLAGS => "$ENV{NGX_PM_CFLAGS}", + OPTIMIZE => '-O', + + LDDLFLAGS => "$ENV{NGX_PM_LDFLAGS}", + + INC => join(" ", map { + m#^/# ? "-I $_" : "-I ../../../../../$_" + } (split /\s+/, $ENV{NGX_INCS})), + + depend => { + 'nginx.c' => join(" ", map { + m#^/# ? $_ : "../../../../../$_" + } (split(/\s+/, $ENV{NGX_DEPS}), + "src/http/modules/perl/ngx_http_perl_module.h")) + }, + + PM => { + 'nginx.pm' => '$(INST_LIBDIR)/nginx.pm' + } +); diff --git a/app/nginx/src/http/modules/perl/nginx.pm b/app/nginx/src/http/modules/perl/nginx.pm new file mode 100644 index 0000000..e3f7361 --- /dev/null +++ b/app/nginx/src/http/modules/perl/nginx.pm @@ -0,0 +1,138 @@ +package nginx; + +use 5.006001; +use strict; +use warnings; + +require Exporter; + +our @ISA = qw(Exporter); + +our @EXPORT = qw( + OK + DECLINED + + HTTP_OK + HTTP_CREATED + HTTP_ACCEPTED + HTTP_NO_CONTENT + HTTP_PARTIAL_CONTENT + + HTTP_MOVED_PERMANENTLY + HTTP_MOVED_TEMPORARILY + HTTP_REDIRECT + HTTP_SEE_OTHER + HTTP_NOT_MODIFIED + HTTP_TEMPORARY_REDIRECT + + HTTP_BAD_REQUEST + HTTP_UNAUTHORIZED + HTTP_PAYMENT_REQUIRED + HTTP_FORBIDDEN + HTTP_NOT_FOUND + HTTP_NOT_ALLOWED + HTTP_NOT_ACCEPTABLE + HTTP_REQUEST_TIME_OUT + HTTP_CONFLICT + HTTP_GONE + HTTP_LENGTH_REQUIRED + HTTP_REQUEST_ENTITY_TOO_LARGE + HTTP_REQUEST_URI_TOO_LARGE + HTTP_UNSUPPORTED_MEDIA_TYPE + HTTP_RANGE_NOT_SATISFIABLE + + HTTP_INTERNAL_SERVER_ERROR + HTTP_SERVER_ERROR + HTTP_NOT_IMPLEMENTED + HTTP_BAD_GATEWAY + HTTP_SERVICE_UNAVAILABLE + HTTP_GATEWAY_TIME_OUT + HTTP_INSUFFICIENT_STORAGE +); + +our $VERSION = '%%VERSION%%'; + +require XSLoader; +XSLoader::load('nginx', $VERSION); + +# Preloaded methods go here. + +use constant OK => 0; +use constant DECLINED => -5; + +use constant HTTP_OK => 200; +use constant HTTP_CREATED => 201; +use constant HTTP_ACCEPTED => 202; +use constant HTTP_NO_CONTENT => 204; +use constant HTTP_PARTIAL_CONTENT => 206; + +use constant HTTP_MOVED_PERMANENTLY => 301; +use constant HTTP_MOVED_TEMPORARILY => 302; +use constant HTTP_REDIRECT => 302; +use constant HTTP_SEE_OTHER => 303; +use constant HTTP_NOT_MODIFIED => 304; +use constant HTTP_TEMPORARY_REDIRECT => 307; + +use constant HTTP_BAD_REQUEST => 400; +use constant HTTP_UNAUTHORIZED => 401; +use constant HTTP_PAYMENT_REQUIRED => 402; +use constant HTTP_FORBIDDEN => 403; +use constant HTTP_NOT_FOUND => 404; +use constant HTTP_NOT_ALLOWED => 405; +use constant HTTP_NOT_ACCEPTABLE => 406; +use constant HTTP_REQUEST_TIME_OUT => 408; +use constant HTTP_CONFLICT => 409; +use constant HTTP_GONE => 410; +use constant HTTP_LENGTH_REQUIRED => 411; +use constant HTTP_REQUEST_ENTITY_TOO_LARGE => 413; +use constant HTTP_REQUEST_URI_TOO_LARGE => 414; +use constant HTTP_UNSUPPORTED_MEDIA_TYPE => 415; +use constant HTTP_RANGE_NOT_SATISFIABLE => 416; + +use constant HTTP_INTERNAL_SERVER_ERROR => 500; +use constant HTTP_SERVER_ERROR => 500; +use constant HTTP_NOT_IMPLEMENTED => 501; +use constant HTTP_BAD_GATEWAY => 502; +use constant HTTP_SERVICE_UNAVAILABLE => 503; +use constant HTTP_GATEWAY_TIME_OUT => 504; +use constant HTTP_INSUFFICIENT_STORAGE => 507; + + +sub rflush { + my $r = shift; + + $r->flush; +} + + +1; +__END__ + +=head1 NAME + +nginx - Perl interface to the nginx HTTP server API + +=head1 SYNOPSIS + + use nginx; + +=head1 DESCRIPTION + +This module provides a Perl interface to the nginx HTTP server API. + + +=head1 SEE ALSO + +http://nginx.org/en/docs/http/ngx_http_perl_module.html + +=head1 AUTHOR + +Igor Sysoev + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) Igor Sysoev +Copyright (C) Nginx, Inc. + + +=cut diff --git a/app/nginx/src/http/modules/perl/nginx.xs b/app/nginx/src/http/modules/perl/nginx.xs new file mode 100644 index 0000000..cca64da --- /dev/null +++ b/app/nginx/src/http/modules/perl/nginx.xs @@ -0,0 +1,1039 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) Nginx, Inc. + */ + + +#define PERL_NO_GET_CONTEXT + +#include +#include +#include +#include + +#include "XSUB.h" + + +#define ngx_http_perl_set_request(r) \ + r = INT2PTR(ngx_http_request_t *, SvIV((SV *) SvRV(ST(0)))) + + +#define ngx_http_perl_set_targ(p, len) \ + \ + SvUPGRADE(TARG, SVt_PV); \ + SvPOK_on(TARG); \ + sv_setpvn(TARG, (char *) p, len) + + +static ngx_int_t +ngx_http_perl_sv2str(pTHX_ ngx_http_request_t *r, ngx_str_t *s, SV *sv) +{ + u_char *p; + STRLEN len; + + if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { + sv = SvRV(sv); + } + + p = (u_char *) SvPV(sv, len); + + s->len = len; + + if (SvREADONLY(sv) && SvPOK(sv)) { + s->data = p; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); + + return NGX_OK; + } + + s->data = ngx_pnalloc(r->pool, len); + if (s->data == NULL) { + return NGX_ERROR; + } + + ngx_memcpy(s->data, p, len); + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl sv2str: %08XD \"%V\"", sv->sv_flags, s); + + return NGX_OK; +} + + +static ngx_int_t +ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b) +{ + ngx_chain_t out; +#if (NGX_HTTP_SSI) + ngx_chain_t *cl; + ngx_http_perl_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ctx->ssi) { + cl = ngx_alloc_chain_link(r->pool); + if (cl == NULL) { + return NGX_ERROR; + } + + cl->buf = b; + cl->next = NULL; + *ctx->ssi->last_out = cl; + ctx->ssi->last_out = &cl->next; + + return NGX_OK; + } +#endif + + out.buf = b; + out.next = NULL; + + return ngx_http_output_filter(r, &out); +} + + +MODULE = nginx PACKAGE = nginx + + +PROTOTYPES: DISABLE + + +void +status(r, code) + CODE: + + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + + r->headers_out.status = SvIV(ST(1)); + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl status: %d", r->headers_out.status); + + XSRETURN_UNDEF; + + +void +send_http_header(r, ...) + CODE: + + ngx_http_request_t *r; + SV *sv; + + ngx_http_perl_set_request(r); + + if (r->headers_out.status == 0) { + r->headers_out.status = NGX_HTTP_OK; + } + + if (items != 1) { + sv = ST(1); + + if (ngx_http_perl_sv2str(aTHX_ r, &r->headers_out.content_type, sv) + != NGX_OK) + { + XSRETURN_EMPTY; + } + + r->headers_out.content_type_len = r->headers_out.content_type.len; + + } else { + if (ngx_http_set_content_type(r) != NGX_OK) { + XSRETURN_EMPTY; + } + } + + (void) ngx_http_send_header(r); + + +void +header_only(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + + sv_upgrade(TARG, SVt_IV); + sv_setiv(TARG, r->header_only); + + ST(0) = TARG; + + +void +uri(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + ngx_http_perl_set_targ(r->uri.data, r->uri.len); + + ST(0) = TARG; + + +void +args(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + ngx_http_perl_set_targ(r->args.data, r->args.len); + + ST(0) = TARG; + + +void +request_method(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + ngx_http_perl_set_targ(r->method_name.data, r->method_name.len); + + ST(0) = TARG; + + +void +remote_addr(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + ngx_http_perl_set_targ(r->connection->addr_text.data, + r->connection->addr_text.len); + + ST(0) = TARG; + + +void +header_in(r, key) + CODE: + + dXSTARG; + ngx_http_request_t *r; + SV *key; + u_char *p, *lowcase_key, *value, sep; + STRLEN len; + ssize_t size; + ngx_uint_t i, n, hash; + ngx_array_t *a; + ngx_list_part_t *part; + ngx_table_elt_t *h, **ph; + ngx_http_header_t *hh; + ngx_http_core_main_conf_t *cmcf; + + ngx_http_perl_set_request(r); + + key = ST(1); + + if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) { + key = SvRV(key); + } + + p = (u_char *) SvPV(key, len); + + /* look up hashed headers */ + + lowcase_key = ngx_pnalloc(r->pool, len); + if (lowcase_key == NULL) { + XSRETURN_UNDEF; + } + + hash = ngx_hash_strlow(lowcase_key, p, len); + + cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); + + hh = ngx_hash_find(&cmcf->headers_in_hash, hash, lowcase_key, len); + + if (hh) { + + if (hh->offset == offsetof(ngx_http_headers_in_t, cookies)) { + sep = ';'; + goto multi; + } +#if (NGX_HTTP_X_FORWARDED_FOR) + if (hh->offset == offsetof(ngx_http_headers_in_t, x_forwarded_for)) { + sep = ','; + goto multi; + } +#endif + + ph = (ngx_table_elt_t **) ((char *) &r->headers_in + hh->offset); + + if (*ph) { + ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); + + goto done; + } + + XSRETURN_UNDEF; + + multi: + + /* Cookie, X-Forwarded-For */ + + a = (ngx_array_t *) ((char *) &r->headers_in + hh->offset); + + n = a->nelts; + + if (n == 0) { + XSRETURN_UNDEF; + } + + ph = a->elts; + + if (n == 1) { + ngx_http_perl_set_targ((*ph)->value.data, (*ph)->value.len); + + goto done; + } + + size = - (ssize_t) (sizeof("; ") - 1); + + for (i = 0; i < n; i++) { + size += ph[i]->value.len + sizeof("; ") - 1; + } + + value = ngx_pnalloc(r->pool, size); + if (value == NULL) { + XSRETURN_UNDEF; + } + + p = value; + + for (i = 0; /* void */ ; i++) { + p = ngx_copy(p, ph[i]->value.data, ph[i]->value.len); + + if (i == n - 1) { + break; + } + + *p++ = sep; *p++ = ' '; + } + + ngx_http_perl_set_targ(value, size); + + goto done; + } + + /* iterate over all headers */ + + part = &r->headers_in.headers.part; + h = part->elts; + + for (i = 0; /* void */ ; i++) { + + if (i >= part->nelts) { + if (part->next == NULL) { + break; + } + + part = part->next; + h = part->elts; + i = 0; + } + + if (len != h[i].key.len + || ngx_strcasecmp(p, h[i].key.data) != 0) + { + continue; + } + + ngx_http_perl_set_targ(h[i].value.data, h[i].value.len); + + goto done; + } + + XSRETURN_UNDEF; + + done: + + ST(0) = TARG; + + +void +has_request_body(r, next) + CODE: + + dXSTARG; + ngx_http_request_t *r; + ngx_http_perl_ctx_t *ctx; + + ngx_http_perl_set_request(r); + + if (r->headers_in.content_length_n <= 0 && !r->headers_in.chunked) { + XSRETURN_UNDEF; + } + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + ctx->next = SvRV(ST(1)); + + r->request_body_in_single_buf = 1; + r->request_body_in_persistent_file = 1; + r->request_body_in_clean_file = 1; + + if (r->request_body_in_file_only) { + r->request_body_file_log_level = 0; + } + + ngx_http_read_client_request_body(r, ngx_http_perl_handle_request); + + sv_upgrade(TARG, SVt_IV); + sv_setiv(TARG, 1); + + ST(0) = TARG; + + +void +request_body(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + u_char *p, *data; + size_t len; + ngx_buf_t *buf; + ngx_chain_t *cl; + + ngx_http_perl_set_request(r); + + if (r->request_body == NULL + || r->request_body->temp_file + || r->request_body->bufs == NULL) + { + XSRETURN_UNDEF; + } + + cl = r->request_body->bufs; + buf = cl->buf; + + if (cl->next == NULL) { + len = buf->last - buf->pos; + data = buf->pos; + goto done; + } + + len = buf->last - buf->pos; + cl = cl->next; + + for ( /* void */ ; cl; cl = cl->next) { + buf = cl->buf; + len += buf->last - buf->pos; + } + + p = ngx_pnalloc(r->pool, len); + if (p == NULL) { + XSRETURN_UNDEF; + } + + data = p; + cl = r->request_body->bufs; + + for ( /* void */ ; cl; cl = cl->next) { + buf = cl->buf; + p = ngx_cpymem(p, buf->pos, buf->last - buf->pos); + } + + done: + + if (len == 0) { + XSRETURN_UNDEF; + } + + ngx_http_perl_set_targ(data, len); + + ST(0) = TARG; + + +void +request_body_file(r) + CODE: + + dXSTARG; + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + + if (r->request_body == NULL || r->request_body->temp_file == NULL) { + XSRETURN_UNDEF; + } + + ngx_http_perl_set_targ(r->request_body->temp_file->file.name.data, + r->request_body->temp_file->file.name.len); + + ST(0) = TARG; + + +void +discard_request_body(r) + CODE: + + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + + ngx_http_discard_request_body(r); + + +void +header_out(r, key, value) + CODE: + + ngx_http_request_t *r; + SV *key; + SV *value; + ngx_table_elt_t *header; + + ngx_http_perl_set_request(r); + + key = ST(1); + value = ST(2); + + header = ngx_list_push(&r->headers_out.headers); + if (header == NULL) { + XSRETURN_EMPTY; + } + + header->hash = 1; + + if (ngx_http_perl_sv2str(aTHX_ r, &header->key, key) != NGX_OK) { + XSRETURN_EMPTY; + } + + if (ngx_http_perl_sv2str(aTHX_ r, &header->value, value) != NGX_OK) { + XSRETURN_EMPTY; + } + + if (header->key.len == sizeof("Content-Length") - 1 + && ngx_strncasecmp(header->key.data, (u_char *) "Content-Length", + sizeof("Content-Length") - 1) == 0) + { + r->headers_out.content_length_n = (off_t) SvIV(value); + r->headers_out.content_length = header; + } + + if (header->key.len == sizeof("Content-Encoding") - 1 + && ngx_strncasecmp(header->key.data, (u_char *) "Content-Encoding", + sizeof("Content-Encoding") - 1) == 0) + { + r->headers_out.content_encoding = header; + } + + +void +filename(r) + CODE: + + dXSTARG; + size_t root; + ngx_http_request_t *r; + ngx_http_perl_ctx_t *ctx; + + ngx_http_perl_set_request(r); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + if (ctx->filename.data) { + goto done; + } + + if (ngx_http_map_uri_to_path(r, &ctx->filename, &root, 0) == NULL) { + XSRETURN_UNDEF; + } + + ctx->filename.len--; + sv_setpv(PL_statname, (char *) ctx->filename.data); + + done: + + ngx_http_perl_set_targ(ctx->filename.data, ctx->filename.len); + + ST(0) = TARG; + + +void +print(r, ...) + CODE: + + ngx_http_request_t *r; + SV *sv; + int i; + u_char *p; + size_t size; + STRLEN len; + ngx_buf_t *b; + + ngx_http_perl_set_request(r); + + if (items == 2) { + + /* + * do zero copy for prolate single read-only SV: + * $r->print("some text\n"); + */ + + sv = ST(1); + + if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { + sv = SvRV(sv); + } + + if (SvREADONLY(sv) && SvPOK(sv)) { + + p = (u_char *) SvPV(sv, len); + + if (len == 0) { + XSRETURN_EMPTY; + } + + b = ngx_calloc_buf(r->pool); + if (b == NULL) { + XSRETURN_EMPTY; + } + + b->memory = 1; + b->pos = p; + b->last = p + len; + b->start = p; + b->end = b->last; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "$r->print: read-only SV: %z", len); + + goto out; + } + } + + size = 0; + + for (i = 1; i < items; i++) { + + sv = ST(i); + + if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { + sv = SvRV(sv); + } + + (void) SvPV(sv, len); + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "$r->print: copy SV: %z", len); + + size += len; + } + + if (size == 0) { + XSRETURN_EMPTY; + } + + b = ngx_create_temp_buf(r->pool, size); + if (b == NULL) { + XSRETURN_EMPTY; + } + + for (i = 1; i < items; i++) { + sv = ST(i); + + if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PV) { + sv = SvRV(sv); + } + + p = (u_char *) SvPV(sv, len); + b->last = ngx_cpymem(b->last, p, len); + } + + out: + + (void) ngx_http_perl_output(r, b); + + +void +sendfile(r, filename, offset = -1, bytes = 0) + CODE: + + ngx_http_request_t *r; + char *filename; + off_t offset; + size_t bytes; + ngx_str_t path; + ngx_buf_t *b; + ngx_open_file_info_t of; + ngx_http_core_loc_conf_t *clcf; + + ngx_http_perl_set_request(r); + + filename = SvPV_nolen(ST(1)); + + if (filename == NULL) { + croak("sendfile(): NULL filename"); + } + + offset = items < 3 ? -1 : SvIV(ST(2)); + bytes = items < 4 ? 0 : SvIV(ST(3)); + + b = ngx_calloc_buf(r->pool); + if (b == NULL) { + XSRETURN_EMPTY; + } + + b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); + if (b->file == NULL) { + XSRETURN_EMPTY; + } + + path.len = ngx_strlen(filename); + + path.data = ngx_pnalloc(r->pool, path.len + 1); + if (path.data == NULL) { + XSRETURN_EMPTY; + } + + (void) ngx_cpystrn(path.data, (u_char *) filename, path.len + 1); + + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); + + ngx_memzero(&of, sizeof(ngx_open_file_info_t)); + + of.read_ahead = clcf->read_ahead; + of.directio = clcf->directio; + of.valid = clcf->open_file_cache_valid; + of.min_uses = clcf->open_file_cache_min_uses; + of.errors = clcf->open_file_cache_errors; + of.events = clcf->open_file_cache_events; + + if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) { + XSRETURN_EMPTY; + } + + if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) + != NGX_OK) + { + if (of.err == 0) { + XSRETURN_EMPTY; + } + + ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, + "%s \"%s\" failed", of.failed, filename); + XSRETURN_EMPTY; + } + + if (offset == -1) { + offset = 0; + } + + if (bytes == 0) { + bytes = of.size - offset; + } + + b->in_file = 1; + + b->file_pos = offset; + b->file_last = offset + bytes; + + b->file->fd = of.fd; + b->file->log = r->connection->log; + b->file->directio = of.is_directio; + + (void) ngx_http_perl_output(r, b); + + +void +flush(r) + CODE: + + ngx_http_request_t *r; + ngx_buf_t *b; + + ngx_http_perl_set_request(r); + + b = ngx_calloc_buf(r->pool); + if (b == NULL) { + XSRETURN_EMPTY; + } + + b->flush = 1; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "$r->flush"); + + (void) ngx_http_perl_output(r, b); + + XSRETURN_EMPTY; + + +void +internal_redirect(r, uri) + CODE: + + ngx_http_request_t *r; + SV *uri; + ngx_uint_t i; + ngx_http_perl_ctx_t *ctx; + + ngx_http_perl_set_request(r); + + uri = ST(1); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) { + XSRETURN_EMPTY; + } + + for (i = 0; i < ctx->redirect_uri.len; i++) { + if (ctx->redirect_uri.data[i] == '?') { + + ctx->redirect_args.len = ctx->redirect_uri.len - (i + 1); + ctx->redirect_args.data = &ctx->redirect_uri.data[i + 1]; + ctx->redirect_uri.len = i; + + XSRETURN_EMPTY; + } + } + + +void +allow_ranges(r) + CODE: + + ngx_http_request_t *r; + + ngx_http_perl_set_request(r); + + r->allow_ranges = 1; + + +void +unescape(r, text, type = 0) + CODE: + + dXSTARG; + ngx_http_request_t *r; + SV *text; + int type; + u_char *p, *dst, *src; + STRLEN len; + + ngx_http_perl_set_request(r); + + text = ST(1); + + src = (u_char *) SvPV(text, len); + + p = ngx_pnalloc(r->pool, len + 1); + if (p == NULL) { + XSRETURN_UNDEF; + } + + dst = p; + + type = items < 3 ? 0 : SvIV(ST(2)); + + ngx_unescape_uri(&dst, &src, len, (ngx_uint_t) type); + *dst = '\0'; + + ngx_http_perl_set_targ(p, dst - p); + + ST(0) = TARG; + + +void +variable(r, name, value = NULL) + CODE: + + dXSTARG; + ngx_http_request_t *r; + SV *name, *value; + u_char *p, *lowcase; + STRLEN len; + ngx_str_t var, val; + ngx_uint_t i, hash; + ngx_http_perl_var_t *v; + ngx_http_perl_ctx_t *ctx; + ngx_http_variable_value_t *vv; + + ngx_http_perl_set_request(r); + + name = ST(1); + + if (SvROK(name) && SvTYPE(SvRV(name)) == SVt_PV) { + name = SvRV(name); + } + + if (items == 2) { + value = NULL; + + } else { + value = ST(2); + + if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PV) { + value = SvRV(value); + } + + if (ngx_http_perl_sv2str(aTHX_ r, &val, value) != NGX_OK) { + XSRETURN_UNDEF; + } + } + + p = (u_char *) SvPV(name, len); + + lowcase = ngx_pnalloc(r->pool, len); + if (lowcase == NULL) { + XSRETURN_UNDEF; + } + + hash = ngx_hash_strlow(lowcase, p, len); + + var.len = len; + var.data = lowcase; +#if (NGX_DEBUG) + + if (value) { + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl variable: \"%V\"=\"%V\"", &var, &val); + } else { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl variable: \"%V\"", &var); + } +#endif + + vv = ngx_http_get_variable(r, &var, hash); + if (vv == NULL) { + XSRETURN_UNDEF; + } + + if (vv->not_found) { + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ctx->variables) { + + v = ctx->variables->elts; + for (i = 0; i < ctx->variables->nelts; i++) { + + if (hash != v[i].hash + || len != v[i].name.len + || ngx_strncmp(lowcase, v[i].name.data, len) != 0) + { + continue; + } + + if (value) { + v[i].value = val; + XSRETURN_UNDEF; + } + + ngx_http_perl_set_targ(v[i].value.data, v[i].value.len); + + goto done; + } + } + + if (value) { + if (ctx->variables == NULL) { + ctx->variables = ngx_array_create(r->pool, 1, + sizeof(ngx_http_perl_var_t)); + if (ctx->variables == NULL) { + XSRETURN_UNDEF; + } + } + + v = ngx_array_push(ctx->variables); + if (v == NULL) { + XSRETURN_UNDEF; + } + + v->hash = hash; + v->name.len = len; + v->name.data = lowcase; + v->value = val; + + XSRETURN_UNDEF; + } + + XSRETURN_UNDEF; + } + + if (value) { + vv->len = val.len; + vv->valid = 1; + vv->no_cacheable = 0; + vv->not_found = 0; + vv->data = val.data; + + XSRETURN_UNDEF; + } + + ngx_http_perl_set_targ(vv->data, vv->len); + + done: + + ST(0) = TARG; + + +void +sleep(r, sleep, next) + CODE: + + ngx_http_request_t *r; + ngx_msec_t sleep; + ngx_http_perl_ctx_t *ctx; + + ngx_http_perl_set_request(r); + + sleep = (ngx_msec_t) SvIV(ST(1)); + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl sleep: %M", sleep); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + ctx->next = SvRV(ST(2)); + + r->connection->write->delayed = 1; + ngx_add_timer(r->connection->write, sleep); + + r->write_event_handler = ngx_http_perl_sleep_handler; + r->main->count++; + + +void +log_error(r, err, msg) + CODE: + + ngx_http_request_t *r; + SV *err, *msg; + u_char *p; + STRLEN len; + ngx_err_t e; + + ngx_http_perl_set_request(r); + + err = ST(1); + + if (SvROK(err) && SvTYPE(SvRV(err)) == SVt_PV) { + err = SvRV(err); + } + + e = SvIV(err); + + msg = ST(2); + + if (SvROK(msg) && SvTYPE(SvRV(msg)) == SVt_PV) { + msg = SvRV(msg); + } + + p = (u_char *) SvPV(msg, len); + + ngx_log_error(NGX_LOG_ERR, r->connection->log, e, "perl: %s", p); diff --git a/app/nginx/src/http/modules/perl/ngx_http_perl_module.c b/app/nginx/src/http/modules/perl/ngx_http_perl_module.c new file mode 100644 index 0000000..6d3be91 --- /dev/null +++ b/app/nginx/src/http/modules/perl/ngx_http_perl_module.c @@ -0,0 +1,1086 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) Nginx, Inc. + */ + + +#include +#include +#include +#include + + +typedef struct { + PerlInterpreter *perl; + HV *nginx; + ngx_array_t *modules; + ngx_array_t *requires; +} ngx_http_perl_main_conf_t; + + +typedef struct { + SV *sub; + ngx_str_t handler; +} ngx_http_perl_loc_conf_t; + + +typedef struct { + SV *sub; + ngx_str_t handler; +} ngx_http_perl_variable_t; + + +#if (NGX_HTTP_SSI) +static ngx_int_t ngx_http_perl_ssi(ngx_http_request_t *r, + ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params); +#endif + +static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf, + ngx_http_perl_main_conf_t *pmcf); +static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf, + ngx_http_perl_main_conf_t *pmcf); +static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, + ngx_log_t *log); +static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, + HV *nginx, SV *sub, SV **args, ngx_str_t *handler, ngx_str_t *rv); +static void ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv); + +static ngx_int_t ngx_http_perl_preconfiguration(ngx_conf_t *cf); +static void *ngx_http_perl_create_main_conf(ngx_conf_t *cf); +static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf); +static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf); +static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, + void *child); +static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); + +#if (NGX_HAVE_PERL_MULTIPLICITY) +static void ngx_http_perl_cleanup_perl(void *data); +#endif + +static ngx_int_t ngx_http_perl_init_worker(ngx_cycle_t *cycle); +static void ngx_http_perl_exit(ngx_cycle_t *cycle); + + +static ngx_command_t ngx_http_perl_commands[] = { + + { ngx_string("perl_modules"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_str_array_slot, + NGX_HTTP_MAIN_CONF_OFFSET, + offsetof(ngx_http_perl_main_conf_t, modules), + NULL }, + + { ngx_string("perl_require"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_str_array_slot, + NGX_HTTP_MAIN_CONF_OFFSET, + offsetof(ngx_http_perl_main_conf_t, requires), + NULL }, + + { ngx_string("perl"), + NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1, + ngx_http_perl, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + + { ngx_string("perl_set"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, + ngx_http_perl_set, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + + ngx_null_command +}; + + +static ngx_http_module_t ngx_http_perl_module_ctx = { + ngx_http_perl_preconfiguration, /* preconfiguration */ + NULL, /* postconfiguration */ + + ngx_http_perl_create_main_conf, /* create main configuration */ + ngx_http_perl_init_main_conf, /* init main configuration */ + + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + + ngx_http_perl_create_loc_conf, /* create location configuration */ + ngx_http_perl_merge_loc_conf /* merge location configuration */ +}; + + +ngx_module_t ngx_http_perl_module = { + NGX_MODULE_V1, + &ngx_http_perl_module_ctx, /* module context */ + ngx_http_perl_commands, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + ngx_http_perl_init_worker, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + ngx_http_perl_exit, /* exit master */ + NGX_MODULE_V1_PADDING +}; + + +#if (NGX_HTTP_SSI) + +#define NGX_HTTP_PERL_SSI_SUB 0 +#define NGX_HTTP_PERL_SSI_ARG 1 + + +static ngx_http_ssi_param_t ngx_http_perl_ssi_params[] = { + { ngx_string("sub"), NGX_HTTP_PERL_SSI_SUB, 1, 0 }, + { ngx_string("arg"), NGX_HTTP_PERL_SSI_ARG, 0, 1 }, + { ngx_null_string, 0, 0, 0 } +}; + +static ngx_http_ssi_command_t ngx_http_perl_ssi_command = { + ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 0, 1 +}; + +#endif + + +static ngx_str_t ngx_null_name = ngx_null_string; +static HV *nginx_stash; + +#if (NGX_HAVE_PERL_MULTIPLICITY) +static ngx_uint_t ngx_perl_term; +#else +static PerlInterpreter *perl; +#endif + + +static void +ngx_http_perl_xs_init(pTHX) +{ + newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); + + nginx_stash = gv_stashpv("nginx", TRUE); +} + + +static ngx_int_t +ngx_http_perl_handler(ngx_http_request_t *r) +{ + r->main->count++; + + ngx_http_perl_handle_request(r); + + return NGX_DONE; +} + + +void +ngx_http_perl_handle_request(ngx_http_request_t *r) +{ + SV *sub; + ngx_int_t rc; + ngx_str_t uri, args, *handler; + ngx_http_perl_ctx_t *ctx; + ngx_http_perl_loc_conf_t *plcf; + ngx_http_perl_main_conf_t *pmcf; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler"); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ctx == NULL) { + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); + if (ctx == NULL) { + ngx_http_finalize_request(r, NGX_ERROR); + return; + } + + ngx_http_set_ctx(r, ctx, ngx_http_perl_module); + } + + pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); + + { + + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + + if (ctx->next == NULL) { + plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module); + sub = plcf->sub; + handler = &plcf->handler; + + } else { + sub = ctx->next; + handler = &ngx_null_name; + ctx->next = NULL; + } + + rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sub, NULL, handler, + NULL); + + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl handler done: %i", rc); + + if (rc == NGX_DONE) { + ngx_http_finalize_request(r, rc); + return; + } + + if (rc > 600) { + rc = NGX_OK; + } + + if (ctx->redirect_uri.len) { + uri = ctx->redirect_uri; + args = ctx->redirect_args; + + } else { + uri.len = 0; + } + + ctx->filename.data = NULL; + ctx->redirect_uri.len = 0; + + if (ctx->done || ctx->next) { + ngx_http_finalize_request(r, NGX_DONE); + return; + } + + if (uri.len) { + ngx_http_internal_redirect(r, &uri, &args); + ngx_http_finalize_request(r, NGX_DONE); + return; + } + + if (rc == NGX_OK || rc == NGX_HTTP_OK) { + ngx_http_send_special(r, NGX_HTTP_LAST); + ctx->done = 1; + } + + ngx_http_finalize_request(r, rc); +} + + +void +ngx_http_perl_sleep_handler(ngx_http_request_t *r) +{ + ngx_event_t *wev; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl sleep handler"); + + wev = r->connection->write; + + if (wev->delayed) { + + if (ngx_handle_write_event(wev, 0) != NGX_OK) { + ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); + } + + return; + } + + ngx_http_perl_handle_request(r); +} + + +static ngx_int_t +ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, + uintptr_t data) +{ + ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data; + + ngx_int_t rc; + ngx_str_t value; + ngx_http_perl_ctx_t *ctx; + ngx_http_perl_main_conf_t *pmcf; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl variable handler"); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ctx == NULL) { + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); + if (ctx == NULL) { + return NGX_ERROR; + } + + ngx_http_set_ctx(r, ctx, ngx_http_perl_module); + } + + pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); + + value.data = NULL; + + { + + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + + rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, pv->sub, NULL, + &pv->handler, &value); + + } + + if (value.data) { + v->len = value.len; + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + v->data = value.data; + + } else { + v->not_found = 1; + } + + ctx->filename.data = NULL; + ctx->redirect_uri.len = 0; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl variable done"); + + return rc; +} + + +#if (NGX_HTTP_SSI) + +static ngx_int_t +ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx, + ngx_str_t **params) +{ + SV *sv, **asv; + ngx_int_t rc; + ngx_str_t *handler, **args; + ngx_uint_t i; + ngx_http_perl_ctx_t *ctx; + ngx_http_perl_main_conf_t *pmcf; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "perl ssi handler"); + + ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); + + if (ctx == NULL) { + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); + if (ctx == NULL) { + return NGX_ERROR; + } + + ngx_http_set_ctx(r, ctx, ngx_http_perl_module); + } + + pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); + + ctx->ssi = ssi_ctx; + + handler = params[NGX_HTTP_PERL_SSI_SUB]; + handler->data[handler->len] = '\0'; + + { + + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + +#if 0 + + /* the code is disabled to force the precompiled perl code using only */ + + ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv); + + if (sv == &PL_sv_undef) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "eval_pv(\"%V\") failed", handler); + return NGX_ERROR; + } + + if (sv == NULL) { + sv = newSVpvn((char *) handler->data, handler->len); + } + +#endif + + sv = newSVpvn((char *) handler->data, handler->len); + + args = ¶ms[NGX_HTTP_PERL_SSI_ARG]; + + if (args[0]) { + + for (i = 0; args[i]; i++) { /* void */ } + + asv = ngx_pcalloc(r->pool, (i + 1) * sizeof(SV *)); + + if (asv == NULL) { + SvREFCNT_dec(sv); + return NGX_ERROR; + } + + asv[0] = (SV *) (uintptr_t) i; + + for (i = 0; args[i]; i++) { + asv[i + 1] = newSVpvn((char *) args[i]->data, args[i]->len); + } + + } else { + asv = NULL; + } + + rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sv, asv, handler, + NULL); + + SvREFCNT_dec(sv); + + } + + ctx->filename.data = NULL; + ctx->redirect_uri.len = 0; + ctx->ssi = NULL; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl ssi done"); + + return rc; +} + +#endif + + +static char * +ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf) +{ + ngx_str_t *m; + ngx_uint_t i; +#if (NGX_HAVE_PERL_MULTIPLICITY) + ngx_pool_cleanup_t *cln; + + cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + return NGX_CONF_ERROR; + } + +#endif + +#ifdef NGX_PERL_MODULES + if (pmcf->modules == NGX_CONF_UNSET_PTR) { + + pmcf->modules = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t)); + if (pmcf->modules == NULL) { + return NGX_CONF_ERROR; + } + + m = ngx_array_push(pmcf->modules); + if (m == NULL) { + return NGX_CONF_ERROR; + } + + ngx_str_set(m, NGX_PERL_MODULES); + } +#endif + + if (pmcf->modules != NGX_CONF_UNSET_PTR) { + m = pmcf->modules->elts; + for (i = 0; i < pmcf->modules->nelts; i++) { + if (ngx_conf_full_name(cf->cycle, &m[i], 0) != NGX_OK) { + return NGX_CONF_ERROR; + } + } + } + +#if !(NGX_HAVE_PERL_MULTIPLICITY) + + if (perl) { + + if (ngx_set_environment(cf->cycle, NULL) == NULL) { + return NGX_CONF_ERROR; + } + + if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log) + != NGX_OK) + { + return NGX_CONF_ERROR; + } + + pmcf->perl = perl; + pmcf->nginx = nginx_stash; + + return NGX_CONF_OK; + } + +#endif + + if (nginx_stash == NULL) { + PERL_SYS_INIT(&ngx_argc, &ngx_argv); + } + + pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf); + + if (pmcf->perl == NULL) { + return NGX_CONF_ERROR; + } + + pmcf->nginx = nginx_stash; + +#if (NGX_HAVE_PERL_MULTIPLICITY) + + cln->handler = ngx_http_perl_cleanup_perl; + cln->data = pmcf->perl; + +#else + + perl = pmcf->perl; + +#endif + + return NGX_CONF_OK; +} + + +static PerlInterpreter * +ngx_http_perl_create_interpreter(ngx_conf_t *cf, + ngx_http_perl_main_conf_t *pmcf) +{ + int n; + STRLEN len; + SV *sv; + char *ver, **embedding; + ngx_str_t *m; + ngx_uint_t i; + PerlInterpreter *perl; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter"); + + if (ngx_set_environment(cf->cycle, NULL) == NULL) { + return NULL; + } + + perl = perl_alloc(); + if (perl == NULL) { + ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed"); + return NULL; + } + + { + + dTHXa(perl); + PERL_SET_CONTEXT(perl); + PERL_SET_INTERP(perl); + + perl_construct(perl); + +#ifdef PERL_EXIT_DESTRUCT_END + PL_exit_flags |= PERL_EXIT_DESTRUCT_END; +#endif + + n = (pmcf->modules != NGX_CONF_UNSET_PTR) ? pmcf->modules->nelts * 2 : 0; + + embedding = ngx_palloc(cf->pool, (5 + n) * sizeof(char *)); + if (embedding == NULL) { + goto fail; + } + + embedding[0] = ""; + + if (n++) { + m = pmcf->modules->elts; + for (i = 0; i < pmcf->modules->nelts; i++) { + embedding[2 * i + 1] = "-I"; + embedding[2 * i + 2] = (char *) m[i].data; + } + } + + embedding[n++] = "-Mnginx"; + embedding[n++] = "-e"; + embedding[n++] = "0"; + embedding[n] = NULL; + + n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL); + + if (n != 0) { + ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n); + goto fail; + } + + sv = get_sv("nginx::VERSION", FALSE); + ver = SvPV(sv, len); + + if (ngx_strcmp(ver, NGINX_VERSION) != 0) { + ngx_log_error(NGX_LOG_ALERT, cf->log, 0, + "version " NGINX_VERSION " of nginx.pm is required, " + "but %s was found", ver); + goto fail; + } + + if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log) != NGX_OK) { + goto fail; + } + + } + + return perl; + +fail: + + (void) perl_destruct(perl); + + perl_free(perl); + + return NULL; +} + + +static ngx_int_t +ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log) +{ + u_char *err; + STRLEN len; + ngx_str_t *script; + ngx_uint_t i; + + if (requires == NGX_CONF_UNSET_PTR) { + return NGX_OK; + } + + script = requires->elts; + for (i = 0; i < requires->nelts; i++) { + + require_pv((char *) script[i].data); + + if (SvTRUE(ERRSV)) { + + err = (u_char *) SvPV(ERRSV, len); + while (--len && (err[len] == CR || err[len] == LF)) { /* void */ } + + ngx_log_error(NGX_LOG_EMERG, log, 0, + "require_pv(\"%s\") failed: \"%*s\"", + script[i].data, len + 1, err); + + return NGX_ERROR; + } + } + + return NGX_OK; +} + + +static ngx_int_t +ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub, + SV **args, ngx_str_t *handler, ngx_str_t *rv) +{ + SV *sv; + int n, status; + char *line; + u_char *err; + STRLEN len, n_a; + ngx_uint_t i; + ngx_connection_t *c; + + dSP; + + status = 0; + + ENTER; + SAVETMPS; + + PUSHMARK(sp); + + sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx)); + XPUSHs(sv); + + if (args) { + EXTEND(sp, (intptr_t) args[0]); + + for (i = 1; i <= (uintptr_t) args[0]; i++) { + PUSHs(sv_2mortal(args[i])); + } + } + + PUTBACK; + + c = r->connection; + + n = call_sv(sub, G_EVAL); + + SPAGAIN; + + if (n) { + if (rv == NULL) { + status = POPi; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "call_sv: %d", status); + + } else { + line = SvPVx(POPs, n_a); + rv->len = n_a; + + rv->data = ngx_pnalloc(r->pool, n_a); + if (rv->data == NULL) { + return NGX_ERROR; + } + + ngx_memcpy(rv->data, line, n_a); + } + } + + PUTBACK; + + FREETMPS; + LEAVE; + + /* check $@ */ + + if (SvTRUE(ERRSV)) { + + err = (u_char *) SvPV(ERRSV, len); + while (--len && (err[len] == CR || err[len] == LF)) { /* void */ } + + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "call_sv(\"%V\") failed: \"%*s\"", handler, len + 1, err); + + if (rv) { + return NGX_ERROR; + } + + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } + + if (n != 1) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "call_sv(\"%V\") returned %d results", handler, n); + status = NGX_OK; + } + + if (rv) { + return NGX_OK; + } + + return (ngx_int_t) status; +} + + +static void +ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv) +{ + u_char *p; + + for (p = handler->data; *p; p++) { + if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) { + break; + } + } + + if (ngx_strncmp(p, "sub ", 4) == 0 + || ngx_strncmp(p, "sub{", 4) == 0 + || ngx_strncmp(p, "use ", 4) == 0) + { + *sv = eval_pv((char *) p, FALSE); + + /* eval_pv() does not set ERRSV on failure */ + + return; + } + + *sv = NULL; +} + + +static void * +ngx_http_perl_create_main_conf(ngx_conf_t *cf) +{ + ngx_http_perl_main_conf_t *pmcf; + + pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t)); + if (pmcf == NULL) { + return NULL; + } + + pmcf->modules = NGX_CONF_UNSET_PTR; + pmcf->requires = NGX_CONF_UNSET_PTR; + + return pmcf; +} + + +static char * +ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf) +{ + ngx_http_perl_main_conf_t *pmcf = conf; + + if (pmcf->perl == NULL) { + if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + } + + return NGX_CONF_OK; +} + + +#if (NGX_HAVE_PERL_MULTIPLICITY) + +static void +ngx_http_perl_cleanup_perl(void *data) +{ + PerlInterpreter *perl = data; + + PERL_SET_CONTEXT(perl); + PERL_SET_INTERP(perl); + + (void) perl_destruct(perl); + + perl_free(perl); + + if (ngx_perl_term) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, "perl term"); + + PERL_SYS_TERM(); + } +} + +#endif + + +static ngx_int_t +ngx_http_perl_preconfiguration(ngx_conf_t *cf) +{ +#if (NGX_HTTP_SSI) + ngx_int_t rc; + ngx_http_ssi_main_conf_t *smcf; + + smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module); + + rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name, + &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY); + + if (rc != NGX_OK) { + if (rc == NGX_BUSY) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "conflicting SSI command \"%V\"", + &ngx_http_perl_ssi_command.name); + } + + return NGX_ERROR; + } +#endif + + return NGX_OK; +} + + +static void * +ngx_http_perl_create_loc_conf(ngx_conf_t *cf) +{ + ngx_http_perl_loc_conf_t *plcf; + + plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t)); + if (plcf == NULL) { + return NULL; + } + + /* + * set by ngx_pcalloc(): + * + * plcf->handler = { 0, NULL }; + */ + + return plcf; +} + + +static char * +ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) +{ + ngx_http_perl_loc_conf_t *prev = parent; + ngx_http_perl_loc_conf_t *conf = child; + + if (conf->sub == NULL) { + conf->sub = prev->sub; + conf->handler = prev->handler; + } + + return NGX_CONF_OK; +} + + +static char * +ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_perl_loc_conf_t *plcf = conf; + + ngx_str_t *value; + ngx_http_core_loc_conf_t *clcf; + ngx_http_perl_main_conf_t *pmcf; + + value = cf->args->elts; + + if (plcf->handler.data) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "duplicate perl handler \"%V\"", &value[1]); + return NGX_CONF_ERROR; + } + + pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); + + if (pmcf->perl == NULL) { + if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + } + + plcf->handler = value[1]; + + { + + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + + ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub); + + if (plcf->sub == &PL_sv_undef) { + ngx_conf_log_error(NGX_LOG_ERR, cf, 0, + "eval_pv(\"%V\") failed", &value[1]); + return NGX_CONF_ERROR; + } + + if (plcf->sub == NULL) { + plcf->sub = newSVpvn((char *) value[1].data, value[1].len); + } + + } + + clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); + clcf->handler = ngx_http_perl_handler; + + return NGX_CONF_OK; +} + + +static char * +ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_int_t index; + ngx_str_t *value; + ngx_http_variable_t *v; + ngx_http_perl_variable_t *pv; + ngx_http_perl_main_conf_t *pmcf; + + value = cf->args->elts; + + if (value[1].data[0] != '$') { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "invalid variable name \"%V\"", &value[1]); + return NGX_CONF_ERROR; + } + + value[1].len--; + value[1].data++; + + v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); + if (v == NULL) { + return NGX_CONF_ERROR; + } + + pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t)); + if (pv == NULL) { + return NGX_CONF_ERROR; + } + + index = ngx_http_get_variable_index(cf, &value[1]); + if (index == NGX_ERROR) { + return NGX_CONF_ERROR; + } + + pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); + + if (pmcf->perl == NULL) { + if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + } + + pv->handler = value[2]; + + { + + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + + ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub); + + if (pv->sub == &PL_sv_undef) { + ngx_conf_log_error(NGX_LOG_ERR, cf, 0, + "eval_pv(\"%V\") failed", &value[2]); + return NGX_CONF_ERROR; + } + + if (pv->sub == NULL) { + pv->sub = newSVpvn((char *) value[2].data, value[2].len); + } + + } + + v->get_handler = ngx_http_perl_variable; + v->data = (uintptr_t) pv; + + return NGX_CONF_OK; +} + + +static ngx_int_t +ngx_http_perl_init_worker(ngx_cycle_t *cycle) +{ + ngx_http_perl_main_conf_t *pmcf; + + pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module); + + if (pmcf) { + dTHXa(pmcf->perl); + PERL_SET_CONTEXT(pmcf->perl); + PERL_SET_INTERP(pmcf->perl); + + /* set worker's $$ */ + + sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid); + } + + return NGX_OK; +} + + +static void +ngx_http_perl_exit(ngx_cycle_t *cycle) +{ +#if (NGX_HAVE_PERL_MULTIPLICITY) + + /* + * the master exit hook is run before global pool cleanup, + * therefore just set flag here + */ + + ngx_perl_term = 1; + +#else + + if (nginx_stash) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "perl term"); + + (void) perl_destruct(perl); + + perl_free(perl); + + PERL_SYS_TERM(); + } + +#endif +} diff --git a/app/nginx/src/http/modules/perl/ngx_http_perl_module.h b/app/nginx/src/http/modules/perl/ngx_http_perl_module.h new file mode 100644 index 0000000..5e60b03 --- /dev/null +++ b/app/nginx/src/http/modules/perl/ngx_http_perl_module.h @@ -0,0 +1,67 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) Nginx, Inc. + */ + + +#ifndef _NGX_HTTP_PERL_MODULE_H_INCLUDED_ +#define _NGX_HTTP_PERL_MODULE_H_INCLUDED_ + + +#include +#include +#include +#include + +#include +#include + + +typedef ngx_http_request_t *nginx; + +typedef struct { + ngx_str_t filename; + ngx_str_t redirect_uri; + ngx_str_t redirect_args; + + SV *next; + + ngx_uint_t done; /* unsigned done:1; */ + + ngx_array_t *variables; /* array of ngx_http_perl_var_t */ + +#if (NGX_HTTP_SSI) + ngx_http_ssi_ctx_t *ssi; +#endif +} ngx_http_perl_ctx_t; + + +typedef struct { + ngx_uint_t hash; + ngx_str_t name; + ngx_str_t value; +} ngx_http_perl_var_t; + + +extern ngx_module_t ngx_http_perl_module; + + +/* + * workaround for "unused variable `Perl___notused'" warning + * when building with perl 5.6.1 + */ +#ifndef PERL_IMPLICIT_CONTEXT +#undef dTHXa +#define dTHXa(a) +#endif + + +extern void boot_DynaLoader(pTHX_ CV* cv); + + +void ngx_http_perl_handle_request(ngx_http_request_t *r); +void ngx_http_perl_sleep_handler(ngx_http_request_t *r); + + +#endif /* _NGX_HTTP_PERL_MODULE_H_INCLUDED_ */ diff --git a/app/nginx/src/http/modules/perl/typemap b/app/nginx/src/http/modules/perl/typemap new file mode 100644 index 0000000..e2f1a4c --- /dev/null +++ b/app/nginx/src/http/modules/perl/typemap @@ -0,0 +1,3 @@ +TYPEMAP + +nginx T_PTROBJ -- cgit 1.2.3-korg