Nginx::Util - utility functions
use Nginx::Util; my $wbuf = ''; my $rbuf = "GET /shouldnotbethere HTTP/1.0\x0d\x0a". "Host: www.google.com\x0d\x0a". "Connection: close\x0d\x0a". "\x0d\x0a"; ngx_http_req '209.85.148.104', 80, $rbuf, $wbuf, 5, sub { my $http_headers = shift; if ($! && $! != NGX_EOF) { ... } if ($http_headers->{'_status'} ... ) { ... } if ($http_headers->{'content-type'}->[0] ... ) { ... } };
Just a few useful functions to quickly start using asynchronous model.
ngx_http_req ngx_pure_http_req
Connects to $ip:$port, sends http request from $reqbuf, parses response header and reads an entire body into $respbuf. Calls back with parsed response in $_[0] in this form:
$ip:$port
$reqbuf
$respbuf
$_[0]
{ _status => 200, _message => 'OK', _version => 'HTTP/1.0', content-type => ['text/html'], content-length => [1234] }
On errors calls back without any arguments. You may also check $! when error occurs.
Example:
my $buf = "GET /shouldnotbethere HTTP/1.0\x0d\x0a". "Host: www.google.com\x0d\x0a". "Connection: close\x0d\x0a". "\x0d\x0a"; ngx_http_req '209.85.148.104', 80, $buf, $buf, 15, sub { my $h = shift; my $dump = Dumper($h); warn "$!\n$dump\n$buf\n\n\n"; };
Same as ngx_http_req but without any queueing. To use for healthcheck or things like that.
ngx_http_req
Alexandr Gomoliako <zzz@zzz.org.ua>
Copyright 2011 Alexandr Gomoliako. All rights reserved.
This module is free software. It may be used, redistributed and/or modified under the same terms as nginx itself.
To install Nginx::Perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Nginx::Perl
CPAN shell
perl -MCPAN -e shell install Nginx::Perl
For more information on module installation, please visit the detailed CPAN module installation guide.