1 package Catalyst::Engine::HTTP::Base;
4 use base 'Catalyst::Engine';
6 use Catalyst::Exception;
8 use HTTP::Headers::Util 'split_header_words';
14 __PACKAGE__->mk_accessors(qw/http/);
16 Class::Struct::struct 'Catalyst::Engine::HTTP::Base::struct' => {
17 request => 'HTTP::Request',
18 response => 'HTTP::Response',
25 Catalyst::Engine::HTTP::Base - Base class for HTTP Engines
29 This is a base class for HTTP Engines.
31 =head1 OVERLOADED METHODS
33 This class overloads some methods from C<Catalyst::Engine>.
37 =item $c->finalize_body
43 $c->http->response->content( $c->response->body );
46 =item $c->finalize_headers
50 sub finalize_headers {
53 $c->http->response->code( $c->response->status );
55 for my $name ( $c->response->headers->header_field_names ) {
56 $c->http->response->push_header( $name => [ $c->response->header($name) ] );
60 =item $c->prepare_body
66 $c->request->body( $c->http->request->content );
69 =item $c->prepare_connection
73 sub prepare_connection {
75 $c->request->address( $c->http->address );
76 $c->request->hostname( $c->http->hostname );
77 $c->request->protocol( $c->http->request->protocol );
80 =item $c->prepare_headers
86 $c->request->method( $c->http->request->method );
87 $c->request->headers( $c->http->request->headers );
90 =item $c->prepare_parameters
94 sub prepare_parameters {
97 my ( @params, @uploads );
99 my $request = $c->http->request;
101 push( @params, $request->uri->query_form );
103 if ( $request->content_type eq 'application/x-www-form-urlencoded' ) {
104 my $uri = URI->new('http:');
105 $uri->query( $request->content );
106 push( @params, $uri->query_form );
109 if ( $request->content_type eq 'multipart/form-data' ) {
111 for my $part ( $request->parts ) {
113 my $disposition = $part->header('Content-Disposition');
114 my %parameters = @{ ( split_header_words($disposition) )[0] };
116 if ( $parameters{filename} ) {
118 my $fh = File::Temp->new( UNLINK => 0 );
120 unless ( $fh->write( $part->content ) ) {
121 Catalyst::Exception->throw( message => $! );
124 unless ( $fh->flush ) {
125 Catalyst::Exception->throw( message => $! );
128 my $upload = Catalyst::Request::Upload->new(
129 filename => $parameters{filename},
130 size => ( $fh->stat )[7],
131 tempname => $fh->filename,
132 type => $part->content_type
135 unless ( $fh->close ) {
136 Catalyst::Exception->throw( message => $! );
139 push( @uploads, $parameters{name}, $upload );
140 push( @params, $parameters{name}, $parameters{filename} );
143 push( @params, $parameters{name}, $part->content );
148 $c->request->param(@params);
149 $c->request->upload(@uploads);
152 =item $c->prepare_path
161 my $scheme = $c->http->request->uri->scheme;
162 my $host = $c->http->request->uri->host;
163 my $port = $c->http->request->uri->port;
166 $base->scheme($scheme);
170 $base = $base->canonical->as_string;
173 my $path = $c->http->request->uri->path || '/';
174 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
177 $c->req->base($base);
178 $c->req->path($path);
181 =item $c->prepare_request($r)
185 sub prepare_request {
186 my ( $c, $http ) = @_;
190 =item $c->prepare_uploads
194 sub prepare_uploads {
206 Sebastian Riedel, C<sri@cpan.org>
207 Christian Hansen, C<ch@ngmedia.com>
211 This program is free software, you can redistribute it and/or modify it under
212 the same terms as Perl itself.