Apache-ParseFormData

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

	  say "Apache::ParseFormData"
	  Thanks to Stas Bekman <stas@stason.org>

0.06  Fri Jul 25 15:39:30 WEST 2003
	- a small bug in "parse_content" was fixed
	- the documetation has been updated

0.05  Thu Jul 24 14:00:22 WEST 2003
	- the cookies are implemented
	- the "_parse_query" method has changed
	- a bug where upload multiple files has been fixed
	- a major bug where set values in param method has been fixed
	- fixed a bug in "DESTROY" method before clean temporary files
	- updated documentation

0.04  Tue Jul 22 18:44:04 WEST 2003
	- added "disable_uploads" and "post_max" keywords to the
	  constructor
	- new method "parse_result" was also added
	- a bug in documentation has been fixed

0.03  Mon Jul 21 17:52:07 WEST 2003
	- set the "PREREQ_PM" keyword in Makefile.PL file
	- the return value of method "upload" was changed
	- the documetation has been updated

0.02  Mon Jul 21 11:14:51 WEST 2003
	- "delete" and "delete_all" methods have been added
	- now, is possible set multiple values in param method like CGI.pm
	- the documetation was also updated

0.01  Fri Jul 18 18:09:51 WEST 2003
	- original version; created by h2xs 1.22 with options
		-X -n Apache::ParseFormData

ParseFormData.pm  view on Meta::CPAN


use constant NELTS => 10;
use constant BUFFLENGTH => 1024;

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = shift;
	my %args = (
		temp_dir        => "/tmp",
		disable_uploads => 0,
		post_max        => 0,
		@_,
	);
	my $table = APR::Table::make($self->pool, NELTS);
	$self->pnotes('apr_req' => $table);
	bless ($self, $class);

	if(my $data = $self->headers_in->get('cookie')) {
		&_parse_query($self, $data, " *; *");
	}

ParseFormData.pm  view on Meta::CPAN

	} elsif($self->method_number == Apache::M_GET) {
		my $data = $self->args();
		&_parse_query($self, $data) if($data);
		$self->pnotes('apr_req_result' => Apache::OK);
	}
	return($self);
}

sub DESTROY {  
	my $self = shift;
	for my $v (values(%{$self->pnotes('upload')})) {
		my $path = $v->[1];
		unlink($path) if(-e $path);
	}
}

sub parse_result { $_[0]->pnotes('apr_req_result') }

sub parms { $_[0]->pnotes('apr_req') }

sub _parse_query {

ParseFormData.pm  view on Meta::CPAN

}

sub cookie_expire {
	my $time = shift;
	my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime($time);
	my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
	my @weekday = qw(Sun Mon Tue Wed Thu Fri Sat);
	return sprintf("%3s, %02d-%3s-%04d %02d:%02d:%02d GMT", $weekday[$wday], $mday, $months[$mon], $year+1900, $hour, $min, $sec);
}

sub upload {
	my $self = shift;
	my $name = shift || "";
	return($name ? @{$self->pnotes('upload')->{$name}} : keys(%{$self->pnotes('upload')}));
}

sub parse_content {
	my $r = shift;
	my $args = shift;

	my $buf = "";
	$r->setup_client_block;
	$r->should_client_block or return '';
	my $ct = $r->headers_in->get('content-type');

	if($args->{'disable_uploads'} && index($ct, "multipart/form-data") > -1) {
		my $error_str = "[Apache::ParseFormData] file upload forbidden";
		$r->notes->set("error-notes" => $error_str);
		$r->log_error($error_str);
		return(Apache::FORBIDDEN);
	}
	my $rm = $r->remaining;
	if($args->{'post_max'} && ($rm > $args->{'post_max'})) {
		my $pm = $args->{'post_max'};
		my $error_str = "[Apache::ParseFormData] entity too large ($rm, max=$pm)";
		$r->notes->set("error-notes" => $error_str);
		$r->log_error($error_str);

ParseFormData.pm  view on Meta::CPAN

	}
	if($ct =~ /^multipart\/form-data; boundary=(.+)$/) {
		my $boundary = $1;
		my $lenbdr = length("--$boundary");
		$r->get_client_block($buf, $lenbdr+2);
		$buf = substr($buf, $lenbdr);
		$buf =~ s/[\n\r]+//;
		my $iter = -1;
		my @data = ();
		&multipart_data($r, $args, \@data, $boundary, BUFFLENGTH, 1, $buf, $iter);
		my %uploads = ();
		for(@data) {
			if(exists($_->{'headers'}->{'content-disposition'})) {
				my @a = split(/ *; */, $_->{'headers'}->{'content-disposition'});
				if(shift(@a) eq "form-data") {
					if(scalar(@a) == 1) {
						my ($key) = ($a[0] =~ /name=\"([^\"]+)\"/);
						$r->param($key => $_->{'values'} || "");
					} else {
						(ref($_->{'values'}) eq "ARRAY") or next;
						my ($fh, $path) = @{$_->{'values'}};
						seek($fh, 0, 0);
						my %hash = (
							filename => "",
							type     => exists($_->{'headers'}->{'content-type'}) ? $_->{'headers'}->{'content-type'} : "",
							size     => ($fh->stat())[7],
						);
						my $param = "";
						for(@a) {
							my ($name, $value) = (/([^=]+)=\"([^\"]+)\"/);
							if($name eq "name") {
								$uploads{$value} = [$fh, $path];
								$param = $value;
							} else {
								$hash{$name} = $value;
							}
						}
						$r->param($param => \%hash);
					}
				}
			}
		}
		$r->pnotes('upload' => \%uploads);
	} else {
		my $len = $r->headers_in->get('content-length');
		$r->get_client_block($buf, $len);
		&_parse_query($r, $buf) if($buf);
	}
	return(Apache::OK);
}

sub extract_headers {
	my $raw = shift;

ParseFormData.pm  view on Meta::CPAN

    print $h_test{'a'};

    $apr->notes->clear();

    return Apache::OK;
  }

=head1 ABSTRACT

The Apache::ParseFormData module allows you to easily decode and parse    
form and query data, even multipart forms generated by "file upload".
This module only work with mod_perl 2.

=head1 DESCRIPTION

C<Apache::ParseFormData> extension parses a GET and POST requests, with
multipart form data input stream, and saves any files/parameters
encountered for subsequent use.

=head1 Apache::ParseFormData METHODS 

ParseFormData.pm  view on Meta::CPAN

=head2 new

Create a new I<Apache::ParseFormData> object. The methods from I<Apache>
class are inherited. The optional arguments which can be passed to the 
method are the following:

=over 3

=item temp_dir

Directory where the upload files are stored.

=item disable_uploads

Disable file uploads.

  my $apr = Apache::ParseFormData->new($r, disable_uploads => 1);

  my $status = $apr->parse_result;
  unless($status == Apache::OK) {
    my $error = $apr->notes->get("error-notes");
    ...
    return $status;
  }

=item post_max

ParseFormData.pm  view on Meta::CPAN

  $apr->delete("color");

You can delete multiple values:

  $apr->delete("color", "nembers");

=head2 delete_all

This method clear all of the parameters

=head2 upload

You can access the name of an uploaded file with the param method, just
like the value of any other form element.

  my %file_hash = $apr->param('file');
  my $filename = $file_hash{'filename'};
  my $content_type = $file_hash{'type'};
  my $size = $file_hash{'size'};

  my ($fh, $path) = $apr->upload('file_0');

  for my $form_name ($apr->upload()) {
    my ($fh, $path) = $apr->upload($form_name);

    while(<$fh>) {
      print $_;
    }

    my %file_hash = $apr->param($form_name);
    my $filename = $file_hash{'filename'};
    my $content_type = $file_hash{'type'};
    my $size = $file_hash{'size'};
    unlink($path);

README  view on Meta::CPAN

Apache/ParseFormData version 0.09
=================================

The Apache::ParseFormData module allows you to easily decode and parse
form and query data, even multipart forms generated by "file upload".

This module only work with mod_perl-2.0

INSTALLATION

To install this module type the following:

   perl Makefile.PL
   make
   make test



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