Apache2-ASP

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

  - Successfully installed on Windows, but it requires some work.  And performance is bad.

1.14 2007-06-30
  - $Form data is only forwarded if a page is posting back to itself 
    (i.e. - paging through a recordset) or if the form was posted to
    a FormHandler class.

1.13 2007-06-30
  - Apache2::ASP::Config is subclassable.
  - Added <settings> element in config XML file.
  - Added $Config->settings->(lib|dsn|username|password) to $Config object.
  - $Config->settings->lib is added to @INC automatically.

1.12 2007-06-29
  - Stabilized off-line web application testing API.

1.11 2007-06-29
  - Apache2::ASP::Test::MockRequest was missing Cwd::cwd().

1.09 2007-06-29
  - Added POD for Apache2::ASP::Test::UserAgent.

lib/Apache2/ASP/API.pm  view on Meta::CPAN

    
    print "'$file' uploaded successfully\n";
  }# end foreach()

If only logged-in users may upload files, simply log in before uploading anything:

  my $api = Apache2::ASP::API->new();
  
  my $res = $api->ua->post("/handlers/user.login", {
    user_email    => $email,
    user_password => $password,
  });
  
  # Assuming $Session->{user} is set upon successful login:
  unless( $api->session->{user} )
  {
    die "Invalid credentials";
  }# end unless()
  
  ... continue uploading files ...

Or...you could even subclass the API with your own:

  package MyApp::API;
  
  use strict;
  use warnings 'all';
  use base 'Apache2::ASP::API';
  
  sub login
  {
    my ($s, $email, $password) = @_;
    
    my $res = $s->ua->post("/handlers/user.login", {
      user_email    => $email,
      user_password => $password
    });
    
    # Assuming $Session->{user} is set upon successful login:
    unless( $api->session->{user} )
    {
      die "Invalid credentials";
    }# end unless()
    
    return 1;
  }# end login()

lib/Apache2/ASP/ApplicationStateManager.pm  view on Meta::CPAN

sub new
{
  my ($class, %args) = @_;
  
  my $s = bless { }, $class;

  my $conn = $s->context->config->data_connections->application;
  local $^W = 0;
  __PACKAGE__->set_db('Main', $conn->dsn,
    $conn->username,
    $conn->password
  );
  
  if( my $res = $s->retrieve )
  {
    return $res;
  }
  else
  {
    return $s->create;
  }# end if()

lib/Apache2/ASP/ApplicationStateManager.pm  view on Meta::CPAN


  <?xml version="1.0" ?>
  <config>
    ...
    <data_connections>
      ...
      <application>
        <manager>Apache2::ASP::ApplicationStateManager::SQLite</manager>
        <dsn>DBI:mysql:dbname:hostname</dsn>
        <username>sa</username>
        <password>s3cr3t!</password>
      </application>
      ...
    </data_connections>
    ...
  </config>

=head2 Database Storage

The table that the C<$Application> object is stored in has the following structure:

lib/Apache2/ASP/Config.pm  view on Meta::CPAN

  $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
    );
  }# end foreach()

=head1 XML Config File

Apache2::ASP keeps all of its configuration inside of C</conf/apache2-asp-conf.xml>

Here is an example:

  <?xml version="1.0" ?>

lib/Apache2/ASP/Config.pm  view on Meta::CPAN

      </request_filters>
      
    </web>
    
    <data_connections>
      <session>
        <manager>Apache2::ASP::SessionStateManager::SQLite</manager>
        <cookie_name>session-id</cookie_name>
        <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
        <username></username>
        <password></password>
        <session_timeout>30</session_timeout>
      </session>
      <application>
        <manager>Apache2::ASP::ApplicationStateManager::SQLite</manager>
        <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
        <username></username>
        <password></password>
      </application>
      <main>
        <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
        <username></username>
        <password></password>
      </main>
      
    </data_connections>
    
  </configuration>

=head1 DESCRIPTION

=head1 PUBLIC PROPERTIES

lib/Apache2/ASP/ConfigNode/System.pm  view on Meta::CPAN

it is considered "ready for use" by the rest of the application.

=head2 env_vars

A hash of C<%ENV> variables that should be set.

=head2 settings

A collection of special read-only values that should be available throughout the application.

Examples include encryption keys, API keys and username/password combos to access remote services.

=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=Apache2-ASP> to submit bug reports.

=head1 HOMEPAGE

Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples

lib/Apache2/ASP/Manual/BestPractices.pod  view on Meta::CPAN

  %>
  <div class="general_error"><%= $Server->HTMLEncode( $errors->{general} ) %></div>
  <%
    }# end if()
  %>
  
  <form action="/handlers/site1.user.login" method="post">
    <input type="text" name="username" value="<%= $Server->HTMLEncode( $Form->{username} ) %>" />
    <% $errLabel->( 'username' ); %>
    <br />
    <input type="password" name="password" />
    <% $errLabel->( 'password' ); %>
    <br />
    <input type="submit" value="Submit" />
  </form>

The form submits to the URI C</handlers/site1.user.login> which maps to the package
C<site1::user::login>.

It is recommended that inside your C</etc> folder you have a YAML file, C</etc/properties.yaml>:

B<The YAML File>: (C</etc/properties.yaml>)

  ---
  user_login:
    username:
      is_missing: Required
      is_invalid: Invalid username
    password:
      is_missing: Required
      is_invalid: Invalid password
    general:
      success: Successfully Logged In
      fail: Invalid username and/or password.  Please try again.

B<The Handler>: (C</handlers/site1/user/login.pm>)

  package site1::user::login;
  
  use strict;
  use warnings 'all';
  use base 'Apache2::ASP::FormHandler';
  use vars __PACKAGE__->VARS;
  use Data::Properties::YAML;

lib/Apache2/ASP/Manual/BestPractices.pod  view on Meta::CPAN

      if( $Form->{username} =~ m/\s/ )
      {
        $errors->{username} = $props->username->is_invalid;
      }# end if()
    }
    else
    {
      $errors->{username} = $props->username->is_missing;
    }# end if()
    
    # password:
    if( length($Form->{password}) )
    {
      # Password cannot contain whitespace:
      if( $Form->{password} =~ m/\s/ )
      {
        $errors->{password} = $props->password->is_invalid;
      }# end if()
    }
    else
    {
      $errors->{password} = $props->password->is_missing;
    }# end if()
    
    # Only check to see if the user exists if we haven't encountered other errors:
    unless( keys(%$errors) )
    {
      if( ! find_user( ... ) )
      {
        $errors->{general} = $props->general->fail;
      }# end if()
    }# end unless()

lib/Apache2/ASP/SessionStateManager.pm  view on Meta::CPAN

{
  my ($class, %args) = @_;
  
  my $s = bless {}, $class;
  my $conn = $s->context->config->data_connections->session;
  
  local $^W = 0;
  __PACKAGE__->set_db('Main',
    $conn->dsn,
    $conn->username,
    $conn->password
  );
  
  # Prepare our Session:
  if( my $id = $s->parse_session_id() )
  {
    if( $s->verify_session_id( $id ) )
    {
      $s->{SessionID} = $id;
      return $s->retrieve( $id );
    }

lib/Apache2/ASP/SessionStateManager.pm  view on Meta::CPAN

  <config>
    ...
    <data_connections>
      ...
      <session>
        <manager>Apache2::ASP::SessionStateManager::MySQL</manager>
        <cookie_name>session-id</cookie_name>
        <cookie_domain>.example.com</cookie_domain>
        <dsn>DBI:mysql:dbname:localhost</dsn>
        <username>sa</username>
        <password>s3cr3t!</password>
        <session_timeout>30</session_timeout>
      </session>
      ...
    </data_connections>
    ...
  </config>

=head2 Database Storage

The database named in the XML config file should contain a table like the following:

sbin/asphelper  view on Meta::CPAN


$args->{domain}           = prompt("What is the web application domain?", "www.example.com");
$args->{application_name} = prompt("Application name?", "MyApp");

$args->{mail_errors_to}   = prompt("Email errors to?", 'you@domain.com');
$args->{mail_errors_from} = prompt("Email errors from?", 'root@localhost.com');
$args->{smtp_server}      = prompt("SMTP Server?", 'localhost');

$args->{session_dsn}            = prompt("Session DSN?", "DBI:mysql:dbname:localhost");
$args->{session_user}           = prompt("Session db username?");
$args->{session_pass}           = prompt("Session db password?");
$args->{session_cookie_domain}  = prompt("Session cookie domain?", $args->{domain});
$args->{session_timeout}        = prompt("Session timeout? (in minutes)", 30);
$args->{session_cookie_name}    = prompt("Session cookie name?", 'session-id');

$args->{app_dsn}  = prompt("Application DSN?", $args->{session_dsn});
$args->{app_user} = prompt("Application db username?", $args->{session_user});
$args->{app_pass} = prompt("Application db password?", $args->{session_pass});

$args->{main_dsn}   = prompt("Main DSN?", $args->{session_dsn});
$args->{main_user}  = prompt("Main db username?", $args->{session_user});
$args->{main_pass}  = prompt("Main db password?", $args->{session_pass});


warn "="x79, "\n";
warn "Creating database tables...\n";
eval {
  my $dbh = DBI->connect( $args->{session_dsn}, $args->{session_user}, $args->{session_pass} );
  $dbh->do(q~
  DROP TABLE IF EXISTS asp_sessions
  ~);
  $dbh->do(q~

sbin/asphelper  view on Meta::CPAN

    </request_filters>
  </web>

  <data_connections>
    <session>
      <manager>Apache2::ASP::SessionStateManager::MySQL</manager>
      <cookie_domain>@{[ $args->{session_cookie_domain} ]}</cookie_domain>
      <cookie_name>@{[ $args->{session_cookie_name} ]}</cookie_name>
      <dsn>@{[ $args->{session_dsn} ]}</dsn>
      <username>@{[ $args->{session_user} ]}</username>
      <password>@{[ $args->{session_pass} ]}</password>
      <session_timeout>@{[ $args->{session_timeout} ]}</session_timeout>
    </session>
    <application>
      <manager>Apache2::ASP::ApplicationStateManager::MySQL</manager>
      <dsn>@{[ $args->{app_dsn} ]}</dsn>
      <username>@{[ $args->{app_user} ]}</username>
      <password>@{[ $args->{app_pass} ]}</password>
    </application>
    <main>
      <dsn>@{[ $args->{main_dsn} ]}</dsn>
      <username>@{[ $args->{main_user} ]}</username>
      <password>@{[ $args->{main_pass} ]}</password>
    </main>
  </data_connections>

</configuration>
CONFIG_XML
close($ofh);


open my $conf_ofh, '>', "$args->{domain}/conf/httpd.conf";
print $conf_ofh <<"CONF";

t/conf/apache2-asp-config.xml  view on Meta::CPAN

    </disable_persistence>
    
  </web>

  <data_connections>
    <session>
      <manager>Apache2::ASP::SessionStateManager::SQLite</manager>
      <cookie_name>session-id</cookie_name>
      <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
      <username></username>
      <password></password>
      <session_timeout>30</session_timeout>
    </session>
    <application>
      <manager>Apache2::ASP::ApplicationStateManager::SQLite</manager>
      <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
      <username></username>
      <password></password>
    </application>
    <main>
      <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_applications</dsn>
      <username></username>
      <password></password>
    </main>
  </data_connections>

</configuration>

t/conf/configuration.dtd  view on Meta::CPAN

<!ELEMENT application ( manager, dsn, username, password ) >

<!ELEMENT application_name ( #PCDATA ) >

<!ELEMENT application_root ( #PCDATA ) >

<!ELEMENT class ( #PCDATA ) >

<!ELEMENT configuration ( system, errors, web, data_connections ) >

<!ELEMENT cookie_domain ( #PCDATA ) >

t/conf/configuration.dtd  view on Meta::CPAN

<!ELEMENT filter ( (uri_match | uri_equals), class ) >

<!ELEMENT handler_root ( #PCDATA ) >

<!ELEMENT lib ( #PCDATA ) >

<!ELEMENT libs ( lib* ) >

<!ELEMENT load_modules ( module* ) >

<!ELEMENT main ( dsn, username, password ) >

<!ELEMENT manager ( #PCDATA ) >

<!ELEMENT media_manager_upload_root ( #PCDATA ) >

<!ELEMENT module ( #PCDATA ) >

<!ELEMENT name ( #PCDATA ) >

<!ELEMENT page_cache_root ( #PCDATA ) >

<!ELEMENT password ( #PCDATA ) >

<!ELEMENT post_processors ( class* ) >

<!ELEMENT request_filters ( filter* ) >

<!ELEMENT session ( manager, cookie_domain, cookie_name, dsn, username, password, session_timeout ) >

<!ELEMENT session_timeout ( #PCDATA ) >

<!ELEMENT setting ( name, value ) >

<!ELEMENT settings ( setting* ) >

<!ELEMENT system ( post_processors, libs, load_modules, env_vars, settings ) >

<!ELEMENT uri_equals ( #PCDATA ) >



( run in 0.606 second using v1.01-cache-2.11-cpan-49f99fa48dc )