NAME
Nginx::Util - utility functions
SYNOPSIS
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] ... ) {
...
}
};
DESCRIPTION
Just a few useful functions to quickly start using asynchronous model.
EXPORT
ngx_http_req
ngx_pure_http_req
FUNCTIONS
- ngx_http_req $ip, $port, $reqbuf, $respbuf, $timeout, sub { };
-
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:{ _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"; };
- ngx_pure_http_req $ip, $port, $reqbuf, $respbuf, $timeout, sub { };
-
Same as
ngx_http_req
but without any queueing. To use for healthcheck or things like that.
AUTHOR
Alexandr Gomoliako <zzz@zzz.org.ua>
LICENSE
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.