Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first commit

  • Loading branch information...
commit ccf13e63ada6605a6b250dc616405ee917d86665 0 parents
Kazuhiro Osawa authored September 13, 2009
14  Example.pm
... ...
@@ -0,0 +1,14 @@
  1
+package Example;
  2
+use strict;
  3
+use warnings;
  4
+use Data::Dumper;
  5
+
  6
+sub handler {
  7
+    my $env = shift;
  8
+
  9
+    my $body = Dumper($env);
  10
+    [200, ['Content-Type', 'text/plain', 'Content-Length', length($body), 'X-Yappo', 'Hokke'], [$body]];
  11
+}
  12
+
  13
+1;
  14
+
314  nginx.patch
... ...
@@ -0,0 +1,314 @@
  1
+Only in nginx-0.7.61: Makefile
  2
+Only in nginx-0.7.61: objs
  3
+diff -ur nginx-0.7.61-original/src/http/modules/perl/nginx.pm nginx-0.7.61/src/http/modules/perl/nginx.pm
  4
+--- nginx-0.7.61-original/src/http/modules/perl/nginx.pm	2009-09-13 12:55:21.000000000 +0900
  5
++++ nginx-0.7.61/src/http/modules/perl/nginx.pm	2009-09-13 12:46:26.000000000 +0900
  6
+@@ -99,6 +99,71 @@
  7
+ }
  8
+ 
  9
+ 
  10
++sub psgi_handler {
  11
++    my($r, $handler, ) = @_;
  12
++
  13
++    my %env;
  14
++    $env{'psig.version'} = [1,0];
  15
++    $env{'psig.inputs'}   = undef; # XXX tied?
  16
++    $env{'psig.errors'}   = undef; # XXX
  17
++    $env{REQUEST_METHOD} = $r->request_method;
  18
++    $env{SCRIPT_NAME}    = $r->uri;
  19
++    $env{QUERY_STRING}   = $r->args;
  20
++    $env{SERVER_NAME}    = 'nginx';
  21
++
  22
++    $env{REMOTE_ADDR}    = $r->remote_addr;
  23
++
  24
++    # fetch headers;
  25
++    my $max = $r->headers();
  26
++    my @headers;
  27
++    for my $c (0..($max)) {
  28
++        push @headers, $r->header_by_n($c);
  29
++    }
  30
++
  31
++    for my $name (@headers) {
  32
++        next unless $name;
  33
++        my $v = $r->header_in($name);
  34
++        $name =~ s/-/_/g;
  35
++        $name = uc $name;
  36
++        $name = "HTTP_$name" unless $name eq 'CONTENT_LENGTH' || $name eq 'CONTENT_TYPE';
  37
++
  38
++        $env{$name} = $v;
  39
++    }
  40
++
  41
++    my $res;
  42
++    {
  43
++        no strict 'refs';
  44
++        my $code = \&{$handler};
  45
++        $res = $code->(\%env);
  46
++    };
  47
++    my $content_type = 'text/html';
  48
++    while (my($k, $v) = splice @{ $res->[1] }, 0, 2) {
  49
++        if (uc $k eq 'CONTENT-TYPE') {
  50
++            $content_type = $v;
  51
++        } else {
  52
++            $r->header_out($k, $v);
  53
++        }
  54
++    }
  55
++    $r->send_http_header($content_type);
  56
++
  57
++    return $res->[0] if $r->header_only;
  58
++
  59
++    my $body = $res->[2];
  60
++    if (ref $body eq 'ARRAY') {
  61
++        for my $line (@$body) {
  62
++            $r->print($line);
  63
++        }
  64
++    } else {
  65
++        while (defined(my $line = $body->getline)) {
  66
++            $r->print($line);
  67
++        }
  68
++        $body->close;
  69
++    }
  70
++    $r->rflush;
  71
++
  72
++    return $res->[0];
  73
++}
  74
++
  75
+ 1;
  76
+ __END__
  77
+ 
  78
+diff -ur nginx-0.7.61-original/src/http/modules/perl/nginx.xs nginx-0.7.61/src/http/modules/perl/nginx.xs
  79
+--- nginx-0.7.61-original/src/http/modules/perl/nginx.xs	2009-09-13 12:55:21.000000000 +0900
  80
++++ nginx-0.7.61/src/http/modules/perl/nginx.xs	2009-09-12 20:50:10.000000000 +0900
  81
+@@ -213,6 +213,93 @@
  82
+ 
  83
+     ST(0) = TARG;
  84
+ 
  85
++void
  86
++headers(r)
  87
++    CODE:
  88
++
  89
++    dXSTARG;
  90
++    ngx_http_request_t     *r;
  91
++    ngx_str_t               value;
  92
++    ngx_uint_t              i, c;
  93
++    ngx_table_elt_t        *h;
  94
++    ngx_list_part_t        *part;
  95
++
  96
++    ngx_http_perl_set_request(r);
  97
++
  98
++    part = &r->headers_in.headers.part;
  99
++    h = part->elts;
  100
++    c = 0;
  101
++    for (i = 0; /* void */ ; i++) {
  102
++        if (i >= part->nelts) {
  103
++            if (part->next == NULL) {
  104
++                break;
  105
++            }
  106
++
  107
++            part = part->next;
  108
++            h = part->elts;
  109
++            i = 0;
  110
++        }
  111
++	c++;
  112
++    }
  113
++
  114
++    value.data = ngx_pcalloc(r->pool, 10);
  115
++    value.len  = ngx_sprintf(value.data, "%d", c) - value.data;
  116
++    ngx_http_perl_set_targ(value.data, value.len);
  117
++
  118
++    ST(0) = TARG;
  119
++
  120
++void
  121
++header_by_n(r, key)
  122
++    CODE:
  123
++
  124
++    dXSTARG;
  125
++    ngx_http_request_t     *r;
  126
++    SV                      *key;
  127
++    ngx_str_t               value;
  128
++    ngx_uint_t              i, c;
  129
++    u_char                 *p;
  130
++    STRLEN                  len;
  131
++    ngx_table_elt_t        *h;
  132
++    ngx_list_part_t        *part;
  133
++
  134
++    ngx_http_perl_set_request(r);
  135
++
  136
++    key = ST(1);
  137
++
  138
++    if (SvROK(key) && SvTYPE(SvRV(key)) == SVt_PV) {
  139
++        key = SvRV(key);
  140
++    }
  141
++
  142
++    p = (u_char *) SvPV(key, len);
  143
++
  144
++    part = &r->headers_in.headers.part;
  145
++    h = part->elts;
  146
++    c = 0;
  147
++    value.data = ngx_pcalloc(r->pool, 10);
  148
++    for (i = 0; /* void */ ; i++) {
  149
++        if (i >= part->nelts) {
  150
++            if (part->next == NULL) {
  151
++                break;
  152
++            }
  153
++
  154
++            part = part->next;
  155
++            h = part->elts;
  156
++            i = 0;
  157
++        }
  158
++
  159
++	c++;
  160
++        value.len  = ngx_sprintf(value.data, "%d", c) - value.data;
  161
++
  162
++        if (len != value.len
  163
++            || ngx_strcasecmp(p, value.data) != 0)
  164
++        {
  165
++            continue;
  166
++        }
  167
++
  168
++        ngx_http_perl_set_targ(h[i].key.data, h[i].key.len);
  169
++    }
  170
++
  171
++    ST(0) = TARG;
  172
+ 
  173
+ void
  174
+ header_in(r, key)
  175
+diff -ur nginx-0.7.61-original/src/http/modules/perl/ngx_http_perl_module.c nginx-0.7.61/src/http/modules/perl/ngx_http_perl_module.c
  176
+--- nginx-0.7.61-original/src/http/modules/perl/ngx_http_perl_module.c	2009-09-13 12:55:21.000000000 +0900
  177
++++ nginx-0.7.61/src/http/modules/perl/ngx_http_perl_module.c	2009-09-13 12:22:10.000000000 +0900
  178
+@@ -21,6 +21,7 @@
  179
+ typedef struct {
  180
+     SV                *sub;
  181
+     ngx_str_t          handler;
  182
++    ngx_str_t          psgi_handler;
  183
+ } ngx_http_perl_loc_conf_t;
  184
+ 
  185
+ 
  186
+@@ -60,6 +61,7 @@
  187
+ static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd,
  188
+     void *conf);
  189
+ static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  190
++static char *ngx_http_perl_psgi(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  191
+ static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  192
+ 
  193
+ #if (NGX_HAVE_PERL_MULTIPLICITY)
  194
+@@ -93,6 +95,13 @@
  195
+       0,
  196
+       NULL },
  197
+ 
  198
++    { ngx_string("psgi"),
  199
++      NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
  200
++      ngx_http_perl_psgi,
  201
++      NGX_HTTP_LOC_CONF_OFFSET,
  202
++      0,
  203
++      NULL },
  204
++
  205
+     { ngx_string("perl_set"),
  206
+       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
  207
+       ngx_http_perl_set,
  208
+@@ -188,7 +197,7 @@
  209
+ void
  210
+ ngx_http_perl_handle_request(ngx_http_request_t *r)
  211
+ {
  212
+-    SV                         *sub;
  213
++    SV                         *sub, **asv;
  214
+     ngx_int_t                   rc;
  215
+     ngx_str_t                   uri, args, *handler;
  216
+     ngx_http_perl_ctx_t        *ctx;
  217
+@@ -216,6 +225,7 @@
  218
+     dTHXa(pmcf->perl);
  219
+     PERL_SET_CONTEXT(pmcf->perl);
  220
+ 
  221
++    plcf = NULL;
  222
+     if (ctx->next == NULL) {
  223
+         plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module);
  224
+         sub = plcf->sub;
  225
+@@ -227,7 +237,15 @@
  226
+         ctx->next = NULL;
  227
+     }
  228
+ 
  229
+-    rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sub, NULL, handler,
  230
++    if (plcf != NULL && plcf->psgi_handler.data != NULL) {
  231
++        asv = ngx_pcalloc(r->pool, sizeof(SV *) * 2);
  232
++	asv[0] = (SV *) 1;
  233
++        asv[1] = newSVpvn((char *) plcf->psgi_handler.data, plcf->psgi_handler.len);
  234
++    } else {
  235
++        asv = NULL;
  236
++    }
  237
++
  238
++    rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sub, asv, handler,
  239
+                                     NULL);
  240
+ 
  241
+     }
  242
+@@ -944,7 +962,9 @@
  243
+         }
  244
+     }
  245
+ 
  246
+-    plcf->handler = value[1];
  247
++    plcf->handler           = value[1];
  248
++    plcf->psgi_handler.data = NULL;
  249
++    plcf->psgi_handler.len  = 0;
  250
+ 
  251
+     {
  252
+ 
  253
+@@ -973,6 +993,61 @@
  254
+ 
  255
+ 
  256
+ static char *
  257
++ngx_http_perl_psgi(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  258
++{
  259
++    ngx_http_perl_loc_conf_t *plcf = conf;
  260
++
  261
++    ngx_str_t                  *value;
  262
++    ngx_http_core_loc_conf_t   *clcf;
  263
++    ngx_http_perl_main_conf_t  *pmcf;
  264
++    ngx_str_t                   psgi_handler = ngx_string("nginx::psgi_handler");
  265
++
  266
++    value = cf->args->elts;
  267
++
  268
++    if (plcf->handler.data) {
  269
++        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  270
++                           "duplicate perl handler \"%V\"", &value[1]);
  271
++        return NGX_CONF_ERROR;
  272
++    }
  273
++
  274
++    pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
  275
++
  276
++    if (pmcf->perl == NULL) {
  277
++        if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
  278
++            return NGX_CONF_ERROR;
  279
++        }
  280
++    }
  281
++
  282
++    plcf->handler      = psgi_handler;
  283
++    plcf->psgi_handler = value[1];
  284
++
  285
++    {
  286
++
  287
++    dTHXa(pmcf->perl);
  288
++    PERL_SET_CONTEXT(pmcf->perl);
  289
++
  290
++    ngx_http_perl_eval_anon_sub(aTHX_ &plcf->handler, &plcf->sub);
  291
++
  292
++    if (plcf->sub == &PL_sv_undef) {
  293
++        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
  294
++                           "eval_pv(\"%V\") failed", &value[1]);
  295
++        return NGX_CONF_ERROR;
  296
++    }
  297
++
  298
++    if (plcf->sub == NULL) {
  299
++        plcf->sub = newSVpvn((char *) psgi_handler.data, psgi_handler.len);
  300
++    }
  301
++
  302
++    }
  303
++
  304
++    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  305
++    clcf->handler = ngx_http_perl_handler;
  306
++
  307
++    return NGX_CONF_OK;
  308
++}
  309
++
  310
++
  311
++static char *
  312
+ ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  313
+ {
  314
+     ngx_int_t                   index;
11  tinyconfig.conf
... ...
@@ -0,0 +1,11 @@
  1
+http {
  2
+    perl_modules your/app/lib/dir
  3
+    perl_require  Example.pm;
  4
+    server {
  5
+        listen       8011;
  6
+        server_name  localhost;
  7
+        location / {
  8
+            psgi Example::handler;
  9
+        }
  10
+    }
  11
+}

0 notes on commit ccf13e6

Please sign in to comment.
Something went wrong with that request. Please try again.