| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Handler::FCGI; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Handler::FCGI - FCGI Handler. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # run the app in FCGI standalone mode | ||
| 24 | $app->object("Nile::Handler::FCGI")->run(); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Nile::Handler::FCGI - FCGI Handler. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | use Nile::Base; | ||
| 33 | use FCGI; | ||
| 34 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 35 | our $fcgi_request_count = 0; # the number of requests this fcgi process handled. | ||
| 36 | our $handling_request = 0; | ||
| 37 | our $exit_requested = 0; | ||
| 38 | our $app_quit_request = 0; # End the application but not the FCGI process | ||
| 39 | |||
| 40 | # workaround for known bug in libfcgi | ||
| 41 | while ((our $ignore) = each %ENV) { } | ||
| 42 | |||
| 43 | our $fcgi_request = FCGI::Request(); | ||
| 44 | #$fcgi_request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket); | ||
| 45 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 46 | sub run { | ||
| 47 | |||
| 48 | my ($self) = shift; | ||
| 49 | |||
| 50 | my $app = $self->app; | ||
| 51 | |||
| 52 | # The goal of fast cgi is to load the program once, and iterate in a loop for every request. | ||
| 53 | |||
| 54 | while ($handling_request = ($fcgi_request->Accept() >= 0)) { | ||
| 55 | |||
| 56 | #$app->log->debug("FCGI request start"); | ||
| 57 | |||
| 58 | $app->env(\%ENV); | ||
| 59 | |||
| 60 | # handle it as the normal CGI request | ||
| 61 | $app->object("Nile::Handler::CGI")->run(); | ||
| 62 | |||
| 63 | $handling_request = 0; | ||
| 64 | last if $exit_requested; | ||
| 65 | #exit if -M $ENV{SCRIPT_FILENAME} < 0; # Autorestart | ||
| 66 | |||
| 67 | #$app->log->debug("FCGI request end"); | ||
| 68 | #$app->stop_logger; | ||
| 69 | } | ||
| 70 | |||
| 71 | $fcgi_request->Finish(); | ||
| 72 | } | ||
| 73 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 74 | sub request { | ||
| 75 | $fcgi_request; | ||
| 76 | } | ||
| 77 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 78 | sub is_fcgi { | ||
| 79 | my ($self) = shift; | ||
| 80 | if (defined($fcgi_request) && ref($fcgi_request) && $fcgi_request->IsFastCGI()) { | ||
| 81 | return 1; | ||
| 82 | } else { | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 87 | sub accept { | ||
| 88 | my ($self) = shift; | ||
| 89 | $fcgi_request->Accept() >= 0; | ||
| 90 | } | ||
| 91 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 92 | sub finish { | ||
| 93 | my ($self) = shift; | ||
| 94 | $exit_requested = 1; | ||
| 95 | } | ||
| 96 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | =head1 Bugs | ||
| 101 | |||
| 102 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 103 | |||
| 104 | =head1 HOMEPAGE | ||
| 105 | |||
| 106 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 107 | |||
| 108 | =head1 SOURCE | ||
| 109 | |||
| 110 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 111 | |||
| 112 | =head1 SEE ALSO | ||
| 113 | |||
| 114 | See L<Nile> for details about the complete framework. | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 119 | Website: http://www.mewsoft.com | ||
| 120 | |||
| 121 | =head1 COPYRIGHT AND LICENSE | ||
| 122 | |||
| 123 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 124 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 125 | |||
| 126 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | 1; |