ASP4

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

t/htdocs/pageparser/master.asp
t/htdocs/register-cleanup.asp
t/htdocs/routing/category.asp
t/htdocs/routing/deeply-nested.asp
t/htdocs/routing/lang-locale-wiki.asp
t/htdocs/seo-page/index.asp
t/htdocs/static.txt
t/htdocs/useragent/hello-world.asp
t/htdocs/useragent/simple-args.asp
t/htdocs/useragent/simple-form.asp
t/htdocs/useragent/upload-form.asp
t/lib/My/Filter.pm
t/lib/My/SEOFilter.pm

README.markdown  view on Meta::CPAN


Examples:

### $Request->Cookies( $name )

    my $cookie = $Request->Cookies("some-cookie-name");

### $Request->FileUpload( $field_name )

    if( my $file = $Request->FileUpload('avatar_pic') ) {
      # Handle the uploaded file:
      $file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
    }

See also the [ASP4::FileUpload](http://search.cpan.org/perldoc?ASP4::FileUpload) documentation.

## $Response

An instance of [ASP4::Response](http://search.cpan.org/perldoc?ASP4::Response), the `$Response` object gives shortcuts for dealing
with the outgoing reply from the server back to the client.

lib/ASP4.pm  view on Meta::CPAN


Examples:

=head3 $Request->Cookies( $name )

  my $cookie = $Request->Cookies("some-cookie-name");

=head3 $Request->FileUpload( $field_name )

  if( my $file = $Request->FileUpload('avatar_pic') ) {
    # Handle the uploaded file:
    $file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
  }

See also the L<ASP4::FileUpload> documentation.

=head2 $Response

An instance of L<ASP4::Response>, the C<$Response> object gives shortcuts for dealing
with the outgoing reply from the server back to the client.

lib/ASP4/Config.pm  view on Meta::CPAN

  $Config->errors->mail_errors_to;
  $Config->errors->mail_errors_from;
  $Config->errors->smtp_server;
  
  # Web:
  $Config->web->application_name;
  $Config->web->application_root;
  $Config->web->project_root;
  $Config->web->www_root;
  $Config->web->handler_root;
  $Config->web->media_manager_upload_root;
  $Config->web->page_cache_root;
  
  # Data Connections:
  foreach my $conn ( map { $Config->data_connections->$_ } qw/ session application main / )
  {
    my $dbh = DBI->connect(
      $conn->dsn,
      $conn->username,
      $conn->password
    );

lib/ASP4/FileUpload.pm  view on Meta::CPAN


=head1 SYNOPSIS

  # In your handler:
  sub run {
    my ($s, $context) = @_;
    
    if( my $file = $Request->FileUpload('fieldname') ) {
    
      # Save the file:
      $file->SaveAs('/var/media/uploads/budget.csv');
      
      # Some info about it:
      warn $file->UploadedFileName; # C:\Users\billg\budget.csv
      warn $file->FileName;         # budget.csv
      warn $file->FileExtension;    # csv
      warn $file->FileSize;         # 273478 (Calculated via (stat(FH))[7] )
      warn $file->ContentType;      # text/csv
      warn $file->FileContents;     # (The contents of the file)
      my $ifh = $file->FileHandle;  # A normal, plain old filehandle
    }
  }

=head1 DESCRIPTION

This class provides a simple interface to uploaded files in ASP4.

=head1 PUBLIC PROPERTIES

=head2 UploadedFileName

The name of the file - as uploaded by the user.  For example, if the user was on 
Windows, it might look like C<C:\Users\billg\Desktop\file.txt>

=head2 Filename

The name of the file itself - eg: C<file.txt>

=head2 FileExtension

If the filename is C<file.txt>, C<FileExtension> would return C<txt>.

=head2 FileSize

The size of the uploaded file in bytes.

=head2 FileHandle

Returns a filehandle (open for reading) pointing to the uploaded file.

=head2 ContentType

The C<content-type> header supplied by the browser for the uploaded file.

=head2 FileContents

The contents of the uploaded file.

=head1 PUBLIC METHODS

=head2 SaveAs( $path )

Writes the contents of the uploaded file to C<$path>.  Will throw an exception if
something goes wrong.

=head1 BUGS

It's possible that some bugs have found their way into this release.

Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.

=head1 HOMEPAGE

lib/ASP4/Request.pm  view on Meta::CPAN

  my ($s, $name) = @_;
  $name ? $s->context->cgi->cookie( $name ) : $s->context->cgi->cookie;
}# end Cookies()

sub ServerVariables { $ENV{ $_[1] } }

sub FileUpload
{
  my ($s, $field) = @_;
  
  my $ifh = $s->context->cgi->upload($field)
    or return;
  my %info = ( );
  
  if( my $upInfo = eval { $s->context->cgi->uploadInfo( $ifh ) } )
  {
    no warnings 'uninitialized';
    %info = (
      ContentType         => $upInfo->{'Content-Type'},
      FileHandle          => $ifh,
      FileName            => $s->{form}->{ $field } . "",
      ContentDisposition  => $upInfo->{'Content-Disposition'},
    );
  }
  else
  {
    no warnings 'uninitialized';
    %info = (
      ContentType         => $s->context->cgi->{uploads}->{ $field }->{headers}->{'Content-Type'},
      FileHandle          => $ifh,
      FileName            => $s->context->cgi->{uploads}->{ $field }->{filename},
      ContentDisposition  => 'attachment',
    );
  }# end if()
  
  require ASP4::FileUpload;
  return ASP4::FileUpload->new( %info );
}# end FileUpload()


sub Reroute

lib/ASP4/Request.pm  view on Meta::CPAN


ASP4::Request - Interface to the incoming request

=head1 SYNOPSIS

  if( my $cookie = $Request->Cookies('cust-email') ) {
    # Greet our returning user:
  }
  
  if( my $file = $Request->FileUpload('avatar_pic') ) {
    # Handle the uploaded file:
    $file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
  }
  
  if( $Request->ServerVariables("HTTPS") ) {
    # We're under SSL:
  }

=head1 DESCRIPTION

The intrinsic C<$Request> object provides a few easy-to-use methods to simplify
the processing of incoming requests - specifically file uploads and cookies.

=head1 METHODS

=head2 Cookies( [$name] )

Returns a cookie by name, or all cookies if no name is provided.

=head2 ServerVariables( [$name] )

A wrapper around the global C<%ENV> variable.

lib/ASP4/Request.pm  view on Meta::CPAN

is the same as:

  $ENV{HTTP_HOST}

=head2 FileUpload( $fieldname )

Returns a L<ASP4::FileUpload> object that corresponds to the fieldname specified.

So...if your form has this:

  <input type="file" name="my_uploaded_file" />

Then you would get to it like this:

  my $upload = $Request->FileUpload('my_uploaded-file');

=head2 Header( $name )

Returns the value of an incoming http request header by the given name.

=head1 BUGS

It's possible that some bugs have found their way into this release.

Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.

lib/ASP4/SimpleCGI.pm  view on Meta::CPAN

use strict;
use warnings 'all';
use HTTP::Body;


sub new
{
  my ($s, %args) = @_;
  
  my %params = ();
  my %upload_data = ();
  no warnings 'uninitialized';
  if( length($args{querystring}) )
  {
    foreach my $part ( split /&/, $args{querystring} )
    {
      my ($k,$v) = map { $s->unescape($_) } split /\=/, $part;
      
      if( exists($params{$k}) )
      {
        if( ref($params{$k}) )

lib/ASP4/SimpleCGI.pm  view on Meta::CPAN

    # Parse form values:
    my $form_info = $body->param || { };
    if( keys(%$form_info) )
    {
      foreach( keys(%$form_info) )
      {
        $params{$_} = $form_info->{$_};
      }# end foreach()
    }# end if()
    
    # Parse uploaded data:
    if( my $uploads = $body->upload )
    {
      foreach my $name ( keys(%$uploads) )
      {
        open my $ifh, '<', $uploads->{$name}->{tempname}
          or die "Cannot open '$uploads->{$name}->{tempname}' for reading: $!";
        $upload_data{$name} = {
          %{$uploads->{$name}},
          'filehandle'  => $ifh,
          tempname      => $uploads->{$name}->{tempname},
        };
        $params{$name} = $ifh;
      }# end foreach()
    }# end if()
  }# end if()
  
  my $cookies = { };
  if( my $cookie_str = $ENV{HTTP_COOKIE} )
  {
    foreach my $part ( split /;\s*/, $cookie_str )
    {
      my ($name,$val) = map { $s->unescape( $_ ) } split /\=/, $part;
      $cookies->{$name} = $val;
    }# end foreach()
  }# end if()
  
  return bless {
    params  => \%params,
    uploads => \%upload_data,
    cookies => $cookies,
    %args
  }, $s;
}# end new()


sub upload
{
  my ($s, $key) = @_;
  
  no warnings 'uninitialized';
  return exists( $s->{uploads}->{$key} ) ? $s->{uploads}->{$key}->{filehandle} : undef;
}# end upload()


sub param
{
  my ($s, $key) = @_;
  
  if( defined($key) )
  {
    if( ref($s->{params}->{$key}) )
    {

lib/ASP4/SimpleCGI.pm  view on Meta::CPAN

  defined($1)? chr hex($1) : utf8_chr(hex($2))/ge;
  return $todecode;
}# end unescape()


sub DESTROY
{
  my $s = shift;
  
  map {
    close($s->{uploads}->{$_}->{filehandle});
    unlink($s->{uploads}->{$_}->{tempname});
  } keys(%{$s->{uploads}});
  undef(%$s);
}# end DESTROY()


1;# return true:

=pod

=head1 NAME

ASP4::SimpleCGI - Basic CGI functionality

=head1 SYNOPSIS

  use ASP4::SimpleCGI;
  
  my $cgi = ASP4::SimpleCGI->new(
    content_type    => 'multipart/form-data',
    content_length  => 1200,
    querystring     => 'mode=create&uploadID=234234',
    body            => ...
  );
  
  my $val = $cgi->param('mode');
  foreach my $key ( $cgi->param )
  {
    print $key . ' --> ' . $cgi->param( $key ) . "\n";
  }# end foreach()
  
  my $escaped = $cgi->escape( 'Hello world' );
  my $unescaped = $cgi->unescape( 'Hello+world' );
  
  my $upload = $cgi->upload('filename');
  
  my $filehandle = $cgi->upload_info('filename', 'filehandle' );

=head1 DESCRIPTION

This package provides basic CGI functionality and is also used for testing and
in the API enironment.

C<ASP4::SimpleCGI> uses L<HTTP::Body> under the hood.

=head1 PUBLIC METHODS

lib/ASP4/SimpleCGI.pm  view on Meta::CPAN

If C<$key> is not given, returns a list of all parameter names.

=head2 escape( $str )

Returns a URL-encoded version of C<$str>.

=head2 unescape( $str )

Returns a URL-decoded version of C<$str>.

=head2 upload( $field_name )

Returns all of the information we have about a file upload named C<$field_name>.

=head2 upload_info( $field_name, $item_name )

Returns just that part of C<$field_name>'s upload info.

=head1 BUGS

It's possible that some bugs have found their way into this release.

Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.

=head1 HOMEPAGE

Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples

lib/ASP4/UserAgent.pm  view on Meta::CPAN

  my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring );
  $s->{context} = ASP4::HTTPContext->new( is_subrequest => $ASP4::HTTPContext::_instance ? 1 : 0 );
  return do {
    local $ASP4::HTTPContext::_instance = $s->context;
    $s->context->setup_request( $r, $cgi );
    $s->_setup_response( $s->context->execute() );
  };
}# end post()


sub upload
{
  my ($s, $uri, $args) = @_;
  
  chdir( $s->{cwd} );
  
  $args ||= [ ];
  my $req = POST $uri, Content_Type => 'form-data', Content => $args;
  my $referer = $ENV{HTTP_REFERER};
  %ENV = (
    %{ $s->{env} },

lib/ASP4/UserAgent.pm  view on Meta::CPAN

  );
  my $cgi = $s->_setup_cgi( $req );
  my ($uri_no_args, $querystring) = split /\?/, $uri;
  my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring );
  $s->{context} = ASP4::HTTPContext->new( is_subrequest => $ASP4::HTTPContext::_instance ? 1 : 0 );
  return do {
    local $ASP4::HTTPContext::_instance = $s->context;
    $s->context->setup_request( $r, $cgi );
    $s->_setup_response( $s->context->execute() );
  };
}# end upload()


sub submit_form
{
  my ($s, $form) = @_;
  
  chdir( $s->{cwd} );
  
  my $temp_referrer = $ENV{HTTP_REFERER};
  my $req = $form->click;

lib/ASP4/UserAgent.pm  view on Meta::CPAN


B<NOTE:> 99.99% of the time you will access this via L<ASP4::API>.

  my HTTP::Response $res = $api->ua->get('/index.asp?foo=bar');
  
  my $res = $api->ua->post('/handlers/user.login', [
    username  => 'willy',
    password  => 'wonka',
  ]);
  
  my $res = $api->ua->upload('/handlers/file.upload', [
    foo   => 'bar',
    baz   => 'bux',
    file  => ['/home/john/avatar.jpg']
  ]);
  
  # Some form testing:
  my ($form) = HTML::Form->parse( $res->content, '/' );
  $form->find_input('username')->value('bob');
  my $res = $api->ua->submit_form( $form );
  

lib/ASP4/UserAgent.pm  view on Meta::CPAN

=head1 PUBLIC METHODS

=head2 get( $url )

Calls C<$url> and returns the L<HTTP::Response> result.

=head2 post( $url, $args )

Calls C<$url> with C<$args> and returns the L<HTTP::Response> result.

=head2 upload( $url, $args )

Calls C<$url> with C<$args> and returns the L<HTTP::Response> result.

=head2 submit_form( HTML::Form $form )

Submits the C<$form> and returns the L<HTTP::Response> result.

=head2 add_cookie( $name, $value )

Adds the cookie to all subsequent requests.

t/010-basic/050-useragent.t  view on Meta::CPAN

  $form->find_input('color')->value('Red');
  $form->find_input('pet_name')->value('Fluffy');
  $res = $ua->submit_form( $form );
  ($form) = HTML::Form->parse( $res->content, '/' );
  ok( $form, 'found form again!' );
  is( $form->find_input('color')->value => 'Red', 'color is Red' );
  is( $form->find_input('pet_name')->value => 'Fluffy', 'pet_name is Fluffy' );
};

TEST7: {
  my $res = $ua->get('/useragent/upload-form.asp');
  my ($form) = HTML::Form->parse( $res->content, '/' );
  ok( $form, 'found form' );
  
  my $filename = ( $ENV{TEMP} || $ENV{TMP} || '/tmp' ) . '/' . rand() . '.txt';
  open my $ofh, '>', $filename
    or die "Cannot open '$filename' for writing: $!";
  my $data = join "\n", map {
    "$_: " . rand()
  } 1..100;
  print $ofh $data;
  close($ofh);
  open my $ifh, '<', $filename
    or die "Cannot open '$filename' for reading: $!";
  
  $form->find_input('filename')->value( $filename );
  $res = $ua->submit_form( $form );
  ($form) = HTML::Form->parse( $res->content, '/' );
  is(
    $form->find_input('file_contents')->value => $data,
    "File upload successful"
  );
  unlink($filename);
};

TEST8: {
  my $filename = ( $ENV{TEMP} || $ENV{TMP} || '/tmp' ) . '/' . rand() . '.txt';
  open my $ofh, '>', $filename
    or die "Cannot open '$filename' for writing: $!";
  my $data = join "\n", map {
    "$_: " . rand()
  } 1..100;
  print $ofh $data;
  close($ofh);
  open my $ifh, '<', $filename
    or die "Cannot open '$filename' for reading: $!";
  
  my $res = $ua->upload('/useragent/upload-form.asp', [
    filename  => [$filename]
  ]);
  
  my ($form) = HTML::Form->parse( $res->content, '/' );
  is(
    $form->find_input('file_contents')->value => $data,
    "File upload successful"
  );
  unlink($filename);
};

TEST9: {
  my $res = $ua->get('/masters/deep.asp');
  my $expected = q(<html>
  <head>
    <title>
      My Title!

t/htdocs/useragent/upload-form.asp  view on Meta::CPAN

%>
<form name="form1">
<%= $file->FileName %> -- <%= $file->FileExtension %> -- <%= $file->FileSize %><br/>
<textarea name="file_contents"><%= $file->FileContents %></textarea>
</form>
<%
  }
  else
  {
%>
<form name="form1" action="/useragent/upload-form.asp" method="post" enctype="multipart/form-data">
  <input type="file" name="filename">
  <br>
  <input type="submit" value="Submit" >
</form>
<%
  }# end if()
%>
</body>
</html>



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