| 1 | package Nginx::HTTP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings 'uninitialized'; | ||
| 6 | use bytes; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT = qw(ngx_http_client ngx_http); | ||
| 11 | our $VERSION = '0.04'; | ||
| 12 | |||
| 13 | use Nginx; | ||
| 14 | # use Nginx::Verbose; | ||
| 15 | use HTTP::Parser2::XS; | ||
| 16 | |||
| 17 | sub CRLF { "\x0d\x0a" } | ||
| 18 | sub ngx_http_client ($$$$); | ||
| 19 | |||
| 20 | our $TIMEOUT = 5; | ||
| 21 | my %CLIENTS; | ||
| 22 | |||
| 23 | |||
| 24 | sub ngx_http_client ($$$$) { | ||
| 25 | my ($ip, $port, $timeout, $ssl) = @_; | ||
| 26 | |||
| 27 | my ($error, $connect, $readwrite, $push, $enqueue, | ||
| 28 | $read, $read_header, $read_identity, $read_chunked, $read_tileof, | ||
| 29 | $c, @queue, $active, %headers, $keepalive, $min, $max); | ||
| 30 | |||
| 31 | my $buf = ''; | ||
| 32 | my $decoded_buf = ''; | ||
| 33 | |||
| 34 | |||
| 35 | $push = sub { | ||
| 36 | |||
| 37 | return 0 | ||
| 38 | if @queue == 0 || | ||
| 39 | $active ; | ||
| 40 | |||
| 41 | $active = shift @queue; | ||
| 42 | |||
| 43 | $buf = ref $active->[0] eq 'SCALAR' | ||
| 44 | ? ${$active->[0]} | ||
| 45 | : $active->[0]; | ||
| 46 | |||
| 47 | delete $active->[0]; # saving memory | ||
| 48 | |||
| 49 | &$connect (), | ||
| 50 | return 0 | ||
| 51 | unless $c; | ||
| 52 | |||
| 53 | return 1; | ||
| 54 | }; | ||
| 55 | |||
| 56 | |||
| 57 | $enqueue = sub { | ||
| 58 | |||
| 59 | push @queue, \@_; | ||
| 60 | |||
| 61 | &$push () && | ||
| 62 | ngx_write $c; | ||
| 63 | }; | ||
| 64 | |||
| 65 | |||
| 66 | $error = sub { | ||
| 67 | |||
| 68 | undef $c; | ||
| 69 | |||
| 70 | if ($active) { | ||
| 71 | my $cb = $active->[1]; | ||
| 72 | |||
| 73 | &$cb (undef); | ||
| 74 | |||
| 75 | undef $active; | ||
| 76 | } | ||
| 77 | |||
| 78 | $buf = ''; | ||
| 79 | $decoded_buf = ''; | ||
| 80 | |||
| 81 | &$push () && | ||
| 82 | ngx_write $c; | ||
| 83 | }; | ||
| 84 | |||
| 85 | |||
| 86 | $connect = sub { | ||
| 87 | |||
| 88 | ngx_connector $ip, $port, $timeout, sub { | ||
| 89 | |||
| 90 | &$error (), | ||
| 91 | return NGX_CLOSE | ||
| 92 | if $!; | ||
| 93 | |||
| 94 | $c = shift; | ||
| 95 | |||
| 96 | if ($ssl) { | ||
| 97 | |||
| 98 | ngx_ssl_handshaker $c, $timeout, sub { | ||
| 99 | |||
| 100 | &$error (), | ||
| 101 | return NGX_CLOSE | ||
| 102 | if $!; | ||
| 103 | |||
| 104 | &$readwrite (); | ||
| 105 | |||
| 106 | return NGX_WRITE; | ||
| 107 | }; | ||
| 108 | |||
| 109 | return NGX_SSL_HANDSHAKE; | ||
| 110 | |||
| 111 | } else { | ||
| 112 | |||
| 113 | &$readwrite (); | ||
| 114 | |||
| 115 | return NGX_WRITE; | ||
| 116 | } | ||
| 117 | }; | ||
| 118 | }; | ||
| 119 | |||
| 120 | |||
| 121 | $readwrite = sub { | ||
| 122 | |||
| 123 | ngx_writer $c, $buf, $timeout, sub { | ||
| 124 | |||
| 125 | &$error (), | ||
| 126 | return NGX_CLOSE | ||
| 127 | if $!; | ||
| 128 | |||
| 129 | $buf = ''; | ||
| 130 | $decoded_buf = ''; | ||
| 131 | |||
| 132 | $read = $read_header; | ||
| 133 | |||
| 134 | $min = | ||
| 135 | $max = 0; | ||
| 136 | |||
| 137 | return NGX_READ; | ||
| 138 | }; | ||
| 139 | |||
| 140 | ngx_reader $c, $buf, $min, $max, $timeout, sub { | ||
| 141 | |||
| 142 | return &$read; | ||
| 143 | }; | ||
| 144 | }; | ||
| 145 | |||
| 146 | |||
| 147 | $read_header = sub { | ||
| 148 | |||
| 149 | &$error (), | ||
| 150 | return NGX_CLOSE | ||
| 151 | if $!; | ||
| 152 | |||
| 153 | %headers = (); | ||
| 154 | my $len = parse_http_response $buf, \%headers; | ||
| 155 | |||
| 156 | &$error (), # error | ||
| 157 | return NGX_CLOSE # and closing connection | ||
| 158 | if !defined $len || # if parser fails | ||
| 159 | $len == -1 || | ||
| 160 | ( $len == -2 && | ||
| 161 | length($buf) > 4096 ) ; | ||
| 162 | |||
| 163 | return NGX_READ | ||
| 164 | if $len == -2; | ||
| 165 | |||
| 166 | $buf = substr $buf, $len; | ||
| 167 | |||
| 168 | $keepalive = $headers{'_keepalive'} && | ||
| 169 | ( !exists $headers{'connection'} || | ||
| 170 | $headers{'connection'}->[0] !~ /^close$/i ); | ||
| 171 | |||
| 172 | if ($headers{'_status'} == 304) { | ||
| 173 | |||
| 174 | $read = $read_identity; | ||
| 175 | return &$read (); | ||
| 176 | } elsif ($headers{'_content_length'}) { | ||
| 177 | |||
| 178 | if (length($buf) < $headers{'_content_length'}) { | ||
| 179 | $min = $max = $headers{'_content_length'}; | ||
| 180 | $read = $read_identity; | ||
| 181 | return NGX_READ; | ||
| 182 | } else { | ||
| 183 | $read = $read_identity; | ||
| 184 | return &$read (); | ||
| 185 | } | ||
| 186 | } else { | ||
| 187 | if ($headers{'transfer-encoding'} && | ||
| 188 | $headers{'transfer-encoding'}->[0] =~ /^chunked$/i) | ||
| 189 | { | ||
| 190 | $read = $read_chunked; | ||
| 191 | |||
| 192 | return &$read () | ||
| 193 | if length($buf) > 0; | ||
| 194 | |||
| 195 | return NGX_READ; | ||
| 196 | |||
| 197 | } else { | ||
| 198 | |||
| 199 | $read = $read_tileof; | ||
| 200 | $keepalive = 0; | ||
| 201 | |||
| 202 | return &$read () | ||
| 203 | if length($buf) > 0; | ||
| 204 | |||
| 205 | return NGX_READ; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | }; | ||
| 209 | |||
| 210 | |||
| 211 | $read_identity = sub { | ||
| 212 | |||
| 213 | &$error (), | ||
| 214 | return NGX_CLOSE | ||
| 215 | if $!; | ||
| 216 | |||
| 217 | |||
| 218 | my $cb = $active->[1]; | ||
| 219 | |||
| 220 | &$cb (\%headers, \$buf); | ||
| 221 | |||
| 222 | undef $active; | ||
| 223 | %headers = (); | ||
| 224 | $buf = ''; | ||
| 225 | |||
| 226 | |||
| 227 | &$error (), | ||
| 228 | return NGX_CLOSE | ||
| 229 | unless $keepalive; | ||
| 230 | |||
| 231 | &$push () && | ||
| 232 | return NGX_WRITE; | ||
| 233 | |||
| 234 | $read = $read_header, | ||
| 235 | return NGX_READ; | ||
| 236 | }; | ||
| 237 | |||
| 238 | |||
| 239 | $read_tileof = sub { | ||
| 240 | |||
| 241 | &$error (), | ||
| 242 | return NGX_CLOSE | ||
| 243 | if $! && $! != NGX_EOF; | ||
| 244 | |||
| 245 | return NGX_READ | ||
| 246 | unless $! == NGX_EOF; | ||
| 247 | |||
| 248 | |||
| 249 | my $cb = $active->[1]; | ||
| 250 | |||
| 251 | &$cb (\%headers, \$buf); | ||
| 252 | |||
| 253 | undef $active; | ||
| 254 | %headers = (); | ||
| 255 | $buf = ''; | ||
| 256 | |||
| 257 | |||
| 258 | &$error (), | ||
| 259 | return NGX_CLOSE; | ||
| 260 | }; | ||
| 261 | |||
| 262 | |||
| 263 | $read_chunked = sub { | ||
| 264 | |||
| 265 | &$error (), | ||
| 266 | return NGX_CLOSE | ||
| 267 | if $!; | ||
| 268 | |||
| 269 | my $done; | ||
| 270 | |||
| 271 | while ( $buf =~ / ^ ( [0-9a-fA-F]{1,8} ) \x0d\x0a /sx ) { | ||
| 272 | |||
| 273 | my $len = hex $1; | ||
| 274 | |||
| 275 | if ($len && length($buf) < length($&) + $len + 2 + 5) { | ||
| 276 | |||
| 277 | $min = length($&) + $len + 2 + 5; | ||
| 278 | $max = 0; | ||
| 279 | return NGX_READ; | ||
| 280 | |||
| 281 | } elsif ($len) { | ||
| 282 | |||
| 283 | $decoded_buf .= substr $buf, length($&), $len; | ||
| 284 | $buf = substr $buf, length($&) + $len + 2; | ||
| 285 | |||
| 286 | } else { | ||
| 287 | |||
| 288 | if (length($buf) < length($&) + 2) { | ||
| 289 | $min = | ||
| 290 | $max = length($&) + 2; | ||
| 291 | return NGX_READ; | ||
| 292 | } | ||
| 293 | |||
| 294 | $buf = substr $buf, length($&) + 2; | ||
| 295 | $done = 1; | ||
| 296 | last; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | if (!$done && $buf && length($buf) < 12) { | ||
| 301 | |||
| 302 | $min = | ||
| 303 | $max = 0; | ||
| 304 | return NGX_READ; | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | my $cb = $active->[1]; | ||
| 309 | |||
| 310 | &$cb (\%headers, \$decoded_buf); | ||
| 311 | |||
| 312 | undef $active; | ||
| 313 | %headers = (); | ||
| 314 | $buf = ''; | ||
| 315 | $decoded_buf = ''; | ||
| 316 | |||
| 317 | |||
| 318 | &$error (), | ||
| 319 | return NGX_CLOSE | ||
| 320 | if !$done || !$keepalive; | ||
| 321 | |||
| 322 | &$push () && | ||
| 323 | return NGX_WRITE; | ||
| 324 | |||
| 325 | $read = $read_header, | ||
| 326 | return NGX_READ; | ||
| 327 | }; | ||
| 328 | |||
| 329 | |||
| 330 | return $enqueue; | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | sub ngx_http { | ||
| 335 | my $dest = shift; | ||
| 336 | my $enqueue = $CLIENTS{$dest}; | ||
| 337 | |||
| 338 | &$enqueue, | ||
| 339 | return | ||
| 340 | if defined $enqueue; | ||
| 341 | |||
| 342 | my ($ip, $port, $rest) = split ':', $dest; | ||
| 343 | |||
| 344 | unless ($ip =~ /^ \d{1,3} (?: \. \d{1,3} ){3} $/x) { | ||
| 345 | my $cb = pop; | ||
| 346 | $! = NGX_EINVAL; | ||
| 347 | &$cb (); | ||
| 348 | return; | ||
| 349 | } | ||
| 350 | |||
| 351 | my %args = map { split /\s*=\s*/, $_, 2 } split /\s*[:;]\s*/, $rest; | ||
| 352 | |||
| 353 | $args{'ssl'} = 0 unless $args{'ssl'}; | ||
| 354 | $args{'timeout'} = $TIMEOUT unless $args{'timeout'}; | ||
| 355 | |||
| 356 | $enqueue = ngx_http_client $ip, $port, $args{'timeout'}, $args{'ssl'}; | ||
| 357 | |||
| 358 | $CLIENTS{$dest} = $enqueue; | ||
| 359 | |||
| 360 | &$enqueue, | ||
| 361 | return; | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | 1; | ||
| 366 | __END__ |