Full-featured perl support for nginx.

Nginx-perl completes original embedded perl with asynchronous capabilities and a few useful functions.

Download

nginx-perl-1.8.1.10.tar.gz 807 K
nginx-perl-1.2.9.7.tar.gz 707 K

Github

zzzcpan/nginx-perl

Changes

CHANGES.perl (nginx-perl)
CHANGES (nginx)

Documentation

Features

  • - full official nginx perl API
  • - asynchronous connections (ngx_connector, ngx_reader, ngx_writer)
  • - timer (ngx_timer)
  • - SSL (ngx_ssl_handshaker)
  • - resolver (ngx_resolver)
  • - access handlers (perl_access)
  • - app handlers (perl_app)
  • - configuration level eval (perl_eval)
  • - perl_init_worker, perl_exit_worker handlers
  • - logging functions (ngx_log_*)
  • - client connection takeover for websockets, etc

Synopsis

    use Nginx;

    # nginx's asynchronous resolver
    #     "resolver 1.2.3.4;" in nginx-perl.conf

    ngx_resolver "www.google.com", 15, sub {
        my (@IPs) = @_;

        if ($!) {
            my ($errcode, $errstr) = @_;
            ngx_log_error $!, "Cannot resolve google's IP: $errstr";
        }
    };

    # timer

    ngx_timer 5, 0, sub {
        ngx_log_notice 0, "5 seconds gone";
    };

    # asynchronous connections
    # with explicit flow control

    ngx_connector "1.2.3.4", 80, 15, sub {
        if ($!) {
            ngx_log_error $!, "Connect error: $!";
            return NGX_CLOSE;
        }

        my $c = shift;  # connection
        my $wbuf = "GET /\x0d\x0a";
        my $rbuf;

        ngx_writer $c, $wbuf, 15, sub {
            if ($!) {
                ngx_log_error $!, "Write error: $!";
                return NGX_CLOSE;
            }

            return NGX_READ;
        };

        ngx_reader $c, $rbuf, 0, 0, 15, sub {
            if ($! && $! != NGX_EOF) { 
                ngx_log_error $!, "Read error: $!";
                return NGX_CLOSE;
            }

            if ($! == NGX_EOF) {
                ngx_log_info 0, "response length: " . length ($rbuf);
                return NGX_CLOSE;
            }

            return NGX_READ;  # no errors - read again
        };

        return NGX_WRITE;  # what to do on connect
    };

    # SSL handshake

    ngx_connector "1.2.3.4", 80, 15, sub {
        ...
        my $c = shift;

        ngx_ssl_handshaker $c, 15, sub {
            ...
            ngx_writer $c, $wbuf, 15, sub {
                ...
            };

            ngx_reader $c, $rbuf, 0, 0, 15, sub {
                ...
            };

            return NGX_WRITE;
        };

        return NGX_SSL_HANDSHAKE;
    };

    # asynchronous response
    # via HTTP API

    sub handler {
        my ($r) = shift;

        $r->main_count_inc;

        ngx_resolver "www.google.com", 15, sub {

            $r->send_http_header ('text/html');

            unless ($!) {
                lcoal $, = ', ';
                $r->print ("OK, @_\n");
            } else {
                $r->print ("FAILED, $_[1]\n");
            }

            $r->send_special (NGX_HTTP_LAST);
            $r->finalize_request (NGX_OK);
        };

        return NGX_DONE;
    }

    # and more... 

© 2012 Alexandr Gomoliako