CGI-Easy

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        my $r    = CGI::Easy::Request->new();
        my $h    = CGI::Easy::Headers->new();
        my $sess = CGI::Easy::Session->new($r, $h);
    
        # -- access basic GET request details
        my $url = "$r->{scheme}://$r->{host}:$r->{port}$r->{path}";
        my $param_name  = $r->{GET}{name};
        my @param_color = @{ $r->{GET}{'color[]'} };
        my $cookie_some = $r->{cookie}{some};
    
        # -- file upload
        my $avatar_image    = $r->{POST}{avatar};
        my $avatar_filename = $r->{filename}{avatar};
        my $avatar_mimetype = $r->{mimetype}{avatar};
    
        # -- easy way to identify visitors and get data stored in cookies
        my $session_id  = $sess->{id};
        my $tempcookie_x= $sess->{temp}{x};
        my $permcookie_y= $sess->{perm}{y};
    
        # -- set custom HTTP headers and cookies

lib/CGI/Easy.pm  view on Meta::CPAN

    my $r    = CGI::Easy::Request->new();
    my $h    = CGI::Easy::Headers->new();
    my $sess = CGI::Easy::Session->new($r, $h);

    # -- access basic GET request details
    my $url = "$r->{scheme}://$r->{host}:$r->{port}$r->{path}";
    my $param_name  = $r->{GET}{name};
    my @param_color = @{ $r->{GET}{'color[]'} };
    my $cookie_some = $r->{cookie}{some};

    # -- file upload
    my $avatar_image    = $r->{POST}{avatar};
    my $avatar_filename = $r->{filename}{avatar};
    my $avatar_mimetype = $r->{mimetype}{avatar};

    # -- easy way to identify visitors and get data stored in cookies
    my $session_id  = $sess->{id};
    my $tempcookie_x= $sess->{temp}{x};
    my $permcookie_y= $sess->{perm}{y};

    # -- set custom HTTP headers and cookies

lib/CGI/Easy/Request.pm  view on Meta::CPAN

    }

    my @all_param_names = $r->param();
    my $myparam = $r->param('myparam'); # first 'myparam' value
    my @myparam = $r->param('myparam'); # all 'myparam' values

    print $r->{GET}{'myarray[]'}[0];
    print $r->{GET}{myparam};       # without keep_all_values=>1
    print $r->{GET}{myparam}[0];    # with keep_all_values=>1

    $uploaded_file      = $r->{POST}{myfile};
    $uploaded_filename  = $r->{filename}{myfile};

    print $r->{cookie}{mycookie};

    print $r->{ENV}{HTTP_USER_AGENT};


=head1 DESCRIPTION

Parse CGI params (from %ENV and STDIN) and provide user with ease to use
hash (object) with all interesting data.

lib/CGI/Easy/Request.pm  view on Meta::CPAN

=item * DoS protection

Maximum size of content in STDIN is B<always> limited.

=item * HTTP Basic authorization support

Provide CGI with remote user name and password.

=item * UTF8 support

Decode path, GET/POST/cookie names and values (except uploaded files content)
and uploaded file names from UTF8 to Unicode.

=item * Frontend web server support

Can take REMOTE_ADDR/REMOTE_PORT and "https" scheme from non-standard HTTP
headers (like X-Real-REMOTE-ADDR) which is usually set by nginx/lighttpd
frontends.

=item * HEAD/GET/POST/PUT/DELETE/… support

Params sent with POST or PUT method will be placed in C<< {POST} >>,

lib/CGI/Easy/Request.pm  view on Meta::CPAN

By default only parameters which names ending with '[]' are allowed to have
more than one value. These parameters stored in fields
C<< {GET}, {POST}, {filename} and {mimetype} >> as ARRAYREF, while all other
parameters stored as SCALAR (only first value for these parameters is stored).
If you want to allow more than one value in all parameters you should set
C<< keep_all_values => 1 >> option, and all parameters will be stored as ARRAYREF.

=item {raw}

By default we suppose request send either in UTF8 (or ASCII) encoding.
Request path, GET/POST/cookie names and values (except uploaded files content)
and uploaded file names will be decoded from UTF8 to Unicode.

If you need to handle requests in other encodings, you should disable
automatic decoding from UTF8 using C<< raw => 1 >> option and decode
all these things manually.

=back

Created object will contain these fields:

=over

lib/CGI/Easy/Request.pm  view on Meta::CPAN

    },
    POST => {}

Parameter names and values (except file content) be decoded from UTF8 to
Unicode unless new() called with option C<< raw=>1 >>.

=item {filename}

=item {mimetype}

When C<< <INPUT TYPE="FILE"> >> used to upload files, browser will send
uploaded file name and MIME type in addition to file contents.
These values will be available in fields C<< {filename} >> and C<< {mimetype} >>,
which have same format as C<< {POST} >> field.

Example: submitted form contain parameter "a" with value "5" and parameter
"image" with value of file "C:\Images\some.gif" will be parsed to:

    GET => {},
    POST => {
        a       => 5,
        image   => '...binary image data...',

lib/CGI/Easy/Util.pm  view on Meta::CPAN

or STDIN content for non-multipart forms sent using POST method).

Return HASHREF with params, each param's value will be ARRAYREF
(because there can be more than one value for any parameter in source string).

=head2 burst_multipart

    ($params, $filenames, $mimetypes) = burst_multipart( $buffer, $boundary );

Unpack buffer with name/value pairs in multipart/form-data format.
This format usually used to upload files from forms, and each name/value
pair may additionally contain 'file name' and 'mime type' properties.

Return three HASHREF (with param's values, with param's file names, and
with param's mime types), all values in all three HASHREF are ARRAYREF
(because there can be more than one value for any parameter in source string).
For non-file-upload parameters corresponding values in last two hashes
(with file names and mime types) will be undef().


=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/perl-CGI-Easy/issues>.
You will be notified automatically of any progress on your issue.

t/examples.t  view on Meta::CPAN

my $param_name  = $r->{GET}{name};
my @param_color = @{ $r->{GET}{'color[]'} };
my $cookie_some = $r->{cookie}{some};
is $url, 'https://example.com:80/index.php',    'scheme/host/port/path';
is $param_name, 'powerman',                     'GET scalar';
is_deeply \@param_color, ['red', 'green'],      'GET array';
is $cookie_some, '123',                         'cookie';

###############################
setup_request('http', <<'EOF');
POST /upload/ HTTP/1.0
Host: example.com
Content-Type: multipart/form-data; boundary=----------2A33hj1wqbMp0fkWlQoYU5
Content-Length: AUTO

------------2A33hj1wqbMp0fkWlQoYU5
Content-Disposition: form-data; name="name"

John Smith
------------2A33hj1wqbMp0fkWlQoYU5
Content-Disposition: form-data; name="age"

t/examples.t  view on Meta::CPAN

------------2A33hj1wqbMp0fkWlQoYU5
Content-Disposition: form-data; name="avatar"; filename="C:\images\avatar.png"
Content-Type: image/png

PNG
IMAGE
HERE
------------2A33hj1wqbMp0fkWlQoYU5--
EOF

ok 1, '--- file upload';
my $avatar_image    = $r->{POST}{avatar};
my $avatar_filename = $r->{filename}{avatar};
my $avatar_mimetype = $r->{mimetype}{avatar};
is $avatar_image, "PNG\r\nIMAGE\r\nHERE",       'POST file content';
is $avatar_filename, 'C:\\images\\avatar.png',  'POST file filename';
is $avatar_mimetype, 'image/png',               'POST file mimetype';

###############################
setup_request('http', <<"EOF");
GET / HTTP/1.0

t/request.t  view on Meta::CPAN

greet=${\uri_escape($hi_bin)}\&${\uri_escape($hi_bin)}=greet
EOF
setup_request({}, 'http', $REQUEST);
is $r->{POST}{greet}, $hi_str,                          'POST Unicode value';
is $r->{POST}{$hi_str}, 'greet',                        'POST Unicode name';
setup_request({raw=>1}, 'http', $REQUEST);
is $r->{POST}{greet}, $hi_bin,                          'POST raw value';
is $r->{POST}{$hi_bin}, 'greet',                        'POST raw name';

$REQUEST = <<"EOF";
POST /upload/ HTTP/1.0
Host: example.com
Content-Type: multipart/form-data; boundary=----------2A33hj1wqbMp0fkWlQoYU5
Content-Length: AUTO

------------2A33hj1wqbMp0fkWlQoYU5
Content-Disposition: form-data; name="greet"

$hi_bin
------------2A33hj1wqbMp0fkWlQoYU5
Content-Disposition: form-data; name="$hi_bin"



( run in 1.248 second using v1.01-cache-2.11-cpan-b16cb0d3907 )