CGI-Plus

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

     $cgi->csrf(1);
    
     # get a cookie and look at its values
     $cookie = $cgi->incoming_cookies->{'mycookie'};
     print $cookie->{'values'}->{'x'}, "\n";
     print $cookie->{'values'}->{'y'}, "\n";
    
     # more concise way to get an incoming cookie
     $cookie = $cgi->ic->{'mycookie'};
    
     # resend a cookie, but change one of its values
     $cookie = $cgi->resend_cookie('mycookie');
     $cookie->{'values'}->{'x'} = 2;
    
     # add an outgoing cookie, set some values
     $cookie = $cgi->new_send_cookie('newcookie');
     $cookie->{'values'}->{'val1'} = '1';
     $cookie->{'values'}->{'val2'} = '2';
    
     # output HTTP header with outgoing cookies, including CSRF
     # check cookie, automatically added
     print $cgi->header_plus;
     
     # output header again if it hasn't already been sent, but if it
     # has then output an empty string
     print $cgi->header_plus;
    
     # output the URL of the current page but set a new value
     # for the "t" param and remove the "j" param
     $url = $cgi->self_link(params=>{t=>2, j=>undef});
    
     # check if the submitted form includes the value of the CSRF
     # cookie that was sent
     if (! $cgi->csrf_check)
         { die 'security error' }
    
     # output the randomly generated value of the CSRF cookie,
     # output: KTFnGgpkZ4
     print $cgi->csrf_value, "\n";
    
     # output the hidden input form field that uses the same
     # value as the CSRF cookie
     # output: <input type="hidden" name="csrf" value="KTFnGgpkZ4">
     print $cgi->csrf_field, "\n";
    
     # get the CSRF check param for use in a URL
     # output: csrf=KTFnGgpkZ4
     print $cgi->csrf_param;
    
     # set a custom header
     $cgi->set_header('myheader', 'whatever');
    
     # change content type
     $cgi->set_content_type('text/json');
    
     # output HTTP headers, including added cookies, the CSRF cookie,
     # and the new header
     print $cgi->header_plus;
    
     # outputs something like this:
     # Set-Cookie: newcookie=val2&2&val1&1; path=/
     # Set-Cookie: mycookie=y&2&x&2; path=/
     # Set-Cookie: csrf=v&KTFnGgpkZ4; path=/
     # Date: Sun, 29 Jul 2012 04:08:06 GMT
     # Myheader: whatever
     # Content-Type: text/json; charset=ISO-8859-1

INSTALLATION

    CGI::Plus can be installed with the usual routine:

     perl Makefile.PL
     make
     make test
     make install

METHODS

 CGI::Plus->new()

    Creates and returns a CGI::Plus object. New calls the super-class'
    new() method, so all params sent to this method will be passed through
    to CGI and CGI::Safe.

 $cgi->ic, $cgi->oc

 $cgi->self_link(%options)

    Returns a url that is a relative link to the current page. The local
    path of the URL is sent, but not the protocol or host. So, for example,
    if the URL of the current page is

     http://www.example.com/cgi-plus/?y=1&x=2&t=2&y=2

    then $cgi->self_link() would return something like as follows. Note
    that the order of the URL params mght be changed.

     /cgi-plus/?y=1&y=2&x=2&t=2

    NOTE: If all you want is to do is get the URL of the current page, then
    $cgi->url()
    <http://perldoc.perl.org/CGI.html#OBTAINING-THE-SCRIPT%27S-URL> is a
    better choice because it preserves the order of URL params.

    option: params

    The params option allows you to change the values of some of the URL
    params while leaving others as-is. params is a hashref of URL params
    and their new values. For example, consider this URL:

     http://www.example.com/cgi-plus/?y=1&x=2&t=2&y=2

    Suppose you want to change just that t param from 2 to 3. You would do
    that like this:

     $cgi->self_link(params=>{t=>3})

    which gives us this relative URL with the x and y values as they were
    before, but with the new t value:

     /cgi-plus/?y=1&y=2&x=2&t=3

    If the value of the param is an array ref, then the param is output



( run in 1.890 second using v1.01-cache-2.11-cpan-39bf76dae61 )