| 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::HTTP::Request::PSGI; | ||
| 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::HTTP::Request::PSGI - The HTTP request manager. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # get app context | ||
| 24 | $app = $self->app; | ||
| 25 | |||
| 26 | # get request instance which extends CGI::Simple | ||
| 27 | $request = $app->request; | ||
| 28 | |||
| 29 | $email = $request->param("email"); | ||
| 30 | |||
| 31 | $value = $request->cookie("username"); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Nile::HTTP::Request::PSGI - The HTTP request manager. | ||
| 36 | |||
| 37 | The http request is available as a shared object extending the L<CGI::Simple> module. This means that all methods supported | ||
| 38 | by L<CGI::Simple> is available with the additions to these few methods: | ||
| 39 | |||
| 40 | base_url | ||
| 41 | abs_url | ||
| 42 | url_path | ||
| 43 | is_ajax | ||
| 44 | is_post | ||
| 45 | is_get | ||
| 46 | is_head | ||
| 47 | is_put | ||
| 48 | is_delete | ||
| 49 | is_patch | ||
| 50 | |||
| 51 | You access the request object by $self->app->request. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | use Nile::Base; | ||
| 56 | use MooseX::NonMoose; | ||
| 57 | extends 'Nile::HTTP::PSGI'; | ||
| 58 | |||
| 59 | #Methods: HEAD, POST, GET, PUT, DELETE, PATCH | ||
| 60 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 61 | sub is_ajax { | ||
| 62 | my $self = shift; | ||
| 63 | if (exists $ENV{HTTP_X_REQUESTED_WITH} && lc($ENV{HTTP_X_REQUESTED_WITH}) eq 'xmlhttprequest') { | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | elsif (exists $self->env->{HTTP_X_REQUESTED_WITH} && lc($self->env->{HTTP_X_REQUESTED_WITH}) eq 'xmlhttprequest') { | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 72 | sub is_post {lc(shift->request_method) eq "post";} | ||
| 73 | sub is_get {lc(shift->request_method) eq "get";} | ||
| 74 | sub is_head {lc(shift->request_method) eq "head";} | ||
| 75 | sub is_put {lc(shift->request_method) eq "put";} | ||
| 76 | sub is_delete {lc(shift->request_method) eq "delete";} | ||
| 77 | sub is_patch {lc(shift->request_method) eq "patch";} | ||
| 78 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 79 | sub base_url { | ||
| 80 | my $self = shift; | ||
| 81 | my $url = $self->url(); | ||
| 82 | my $script = $self->url(-relative=>1); | ||
| 83 | $url =~ s/\Q$script\E//; | ||
| 84 | $url = "$url/" if $url !~ m{/$}; | ||
| 85 | return $url; | ||
| 86 | } | ||
| 87 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 88 | sub abs_url { | ||
| 89 | my $self = shift; | ||
| 90 | my $url = $self->url(-absolute=>1); | ||
| 91 | my $script = $self->url(-relative=>1); | ||
| 92 | $url =~ s/\Q$script\E//; | ||
| 93 | $url = "$url/" if $url !~ m|/$|; | ||
| 94 | return $url; | ||
| 95 | } | ||
| 96 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 97 | sub url_path { | ||
| 98 | my $self = shift; | ||
| 99 | my $route = ""; | ||
| 100 | my ($path, $script_name) = $self->script_name =~ m#(.*)/(.*)$#; | ||
| 101 | $path ||= ""; | ||
| 102 | $script_name ||= ""; | ||
| 103 | my ($request_uri, $params) = split(/\?/, ($ENV{REQUEST_URI} || $self->app->env->{REQUEST_URI} || '')); | ||
| 104 | if ($request_uri) { | ||
| 105 | $route = $request_uri; | ||
| 106 | |||
| 107 | # remove path part from the route | ||
| 108 | $route =~ s/^$path//; | ||
| 109 | |||
| 110 | #remove script name from route | ||
| 111 | $route =~ s/$script_name\/?$//; | ||
| 112 | } | ||
| 113 | $route = "$route/" if $route !~ m|/$|; | ||
| 114 | return $route; | ||
| 115 | } | ||
| 116 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 117 | sub request_base { | ||
| 118 | my $self = shift; | ||
| 119 | return $self->env->{REQUEST_BASE} || $self->env->{HTTP_REQUEST_BASE} || ""; | ||
| 120 | } | ||
| 121 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 122 | |||
| 123 | =pod | ||
| 124 | |||
| 125 | =head1 Bugs | ||
| 126 | |||
| 127 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 128 | |||
| 129 | =head1 HOMEPAGE | ||
| 130 | |||
| 131 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 132 | |||
| 133 | =head1 SOURCE | ||
| 134 | |||
| 135 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 136 | |||
| 137 | =head1 SEE ALSO | ||
| 138 | |||
| 139 | See L<Nile> for details about the complete framework. | ||
| 140 | |||
| 141 | =head1 AUTHOR | ||
| 142 | |||
| 143 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 144 | Website: http://www.mewsoft.com | ||
| 145 | |||
| 146 | =head1 COPYRIGHT AND LICENSE | ||
| 147 | |||
| 148 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 149 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 150 | |||
| 151 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | 1; |