| 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::CGI; | ||
| 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::CGI - CGI Handler. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # run the app in CGI standalone mode | ||
| 24 | $app->object("Nile::Handler::CGI")->run(); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Nile::Handler::CGI - CGI Handler. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | use Nile::Base; | ||
| 33 | use Nile::App; | ||
| 34 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 35 | sub run { | ||
| 36 | |||
| 37 | my ($self) = shift; | ||
| 38 | |||
| 39 | my $app = Nile::App->new(app => $self->app()); | ||
| 40 | |||
| 41 | # direct CGI mode. | ||
| 42 | my $request = $app->new_request(); | ||
| 43 | |||
| 44 | $app->response($app->object("Nile::HTTP::Response")); | ||
| 45 | my $response = $app->response(); | ||
| 46 | |||
| 47 | #$app->log->debug("CGI/FCGI request start"); | ||
| 48 | |||
| 49 | $app->start(); | ||
| 50 | |||
| 51 | #$app->hook->on_request(); | ||
| 52 | $app->hook->off_request(); | ||
| 53 | #------------------------------------------------------ | ||
| 54 | # dispatch the action | ||
| 55 | my $content = $app->dispatcher->dispatch; | ||
| 56 | #------------------------------------------------------ | ||
| 57 | $app->hook->on_response; | ||
| 58 | |||
| 59 | # assume OK response if not set | ||
| 60 | $response->code(200) unless ($response->code); | ||
| 61 | |||
| 62 | if (ref($content) eq 'GLOB') { | ||
| 63 | # response is file handle | ||
| 64 | if (!defined $response->header('Content-Length')) { | ||
| 65 | my $size = (stat($content))[7]; | ||
| 66 | $response->header('Content-Length' => $size); | ||
| 67 | } | ||
| 68 | $response->content($content); | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | |||
| 72 | my $ctype = $response->header('Content-Type'); | ||
| 73 | if ($app->charset && $ctype && $app->content_type_text($ctype)) { | ||
| 74 | $response->header('Content-Type' => "$ctype; charset=" . $app->charset) if $ctype !~ /charset/i; | ||
| 75 | } | ||
| 76 | |||
| 77 | $response->content($content); | ||
| 78 | |||
| 79 | if (!$ctype) { | ||
| 80 | $response->content_type('text/html;charset=' . $app->charset); | ||
| 81 | } | ||
| 82 | |||
| 83 | if (!defined $response->header('Content-Length')) { | ||
| 84 | use bytes; # turn off character semantics | ||
| 85 | $response->header('Content-Length' => length($content)); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # run any plugin action or route | ||
| 90 | #$app->dispatcher->dispatch('/accounts/register/create'); | ||
| 91 | #$app->dispatcher->dispatch('/accounts/register/create', 'POST'); | ||
| 92 | |||
| 93 | #$response->cookies->{username} = {value => 'mewsoft', path => "/", domain => '.mewsoft.com', expires => time + 24 * 60 * 60,}; | ||
| 94 | #$response->content_type('text/html;charset=utf-8'); | ||
| 95 | #$response->content_encoding('utf-8'); | ||
| 96 | #$response->header('Content-Type' => 'text/html'); | ||
| 97 | #$response->header(Content_Base => 'http://www.mewsoft.com/'); | ||
| 98 | #$response->header(Accept => "text/html, text/plain, image/*"); | ||
| 99 | #$response->header(MIME_Version => '1.0', User_Agent => 'Nile Web Client/0.26'); | ||
| 100 | #$response->content("Hello world content."); | ||
| 101 | #my $res = $response->finalize; | ||
| 102 | #my $res = $response->headers_as_string; | ||
| 103 | |||
| 104 | $app->hook->off_response; | ||
| 105 | |||
| 106 | my $res = $response->as_string; | ||
| 107 | |||
| 108 | #print "Content-type: text/html;charset=utf-8\n\n"; | ||
| 109 | #print $res, "\n", $response->content; | ||
| 110 | #binmode STDOUT, ":UTF8"; | ||
| 111 | #binmode STDOUT, ':encoding(utf8)'; | ||
| 112 | |||
| 113 | #$app->log->debug("CGI/FCGI request end"); | ||
| 114 | #$app->stop_logger; | ||
| 115 | |||
| 116 | print $res; | ||
| 117 | } | ||
| 118 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head1 Bugs | ||
| 123 | |||
| 124 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 125 | |||
| 126 | =head1 HOMEPAGE | ||
| 127 | |||
| 128 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 129 | |||
| 130 | =head1 SOURCE | ||
| 131 | |||
| 132 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 133 | |||
| 134 | =head1 SEE ALSO | ||
| 135 | |||
| 136 | See L<Nile> for details about the complete framework. | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 141 | Website: http://www.mewsoft.com | ||
| 142 | |||
| 143 | =head1 COPYRIGHT AND LICENSE | ||
| 144 | |||
| 145 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 146 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 147 | |||
| 148 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | 1; |