From: <pau...@us...> - 2008-07-11 20:26:51
|
Revision: 1010 http://everydevel.svn.sourceforge.net/everydevel/?rev=1010&view=rev Author: paul_the_nomad Date: 2008-07-11 13:26:45 -0700 (Fri, 11 Jul 2008) Log Message: ----------- Changes to ResponseFactory and Apache.pm to allow more flexible responses. Inclusion of a Nodeball response type to allow direct downloads of nodeballs. Modified Paths: -------------- trunk/ebase/TODO trunk/ebase/lib/Everything/Config.pm trunk/ebase/lib/Everything/HTTP/Apache.pm trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm trunk/ebase/lib/Everything/HTTP/ResponseFactory.pm trunk/ebase/lib/Everything/HTTP/Test/Apache.pm trunk/ebase/t/lib/everything.conf Modified: trunk/ebase/TODO =================================================================== --- trunk/ebase/TODO 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/TODO 2008-07-11 20:26:45 UTC (rev 1010) @@ -5,6 +5,9 @@ * Amend ecore to ensure XHTML 1.0 compliance +* Amend FromObject.pm so that form objects return labels with a 'for' + attribute and id in the input tag. + * Require Everything::HTML::Response classes to create certain HTTP headers * Everything::HTML::Response classes to return error values on error - Modified: trunk/ebase/lib/Everything/Config.pm =================================================================== --- trunk/ebase/lib/Everything/Config.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/Config.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -202,11 +202,22 @@ javascript => sub { my ( $url, $e ) = @_; return unless $url =~ /\/javascript\/([0-9]+)\.js$/; - my $js_node = $e->get_nodebase->get_node($1); + my $js_node = $e->get_nodebase->getNode($1); $e->set_response_type('javascript'); $e->set_node($js_node); 1; }, + + nodeball_download => sub { + my ( $url, $e ) = @_; + return unless $url =~ m{^/repositories/nodeballs/(\d+)}; + my $node = $e->get_nodebase->getNode( $1 ); + return unless ref $node; + return unless $node->isa( 'Everything::Node::nodeball' ); + $e->set_node( $node ); + $e->set_response_type( 'nodeball' ); + return 1; + } ); sub get_standard_modifier { $standard_modifiers{ $_[1] } } Modified: trunk/ebase/lib/Everything/HTTP/Apache.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Apache.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/HTTP/Apache.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -72,21 +72,34 @@ ### XXX- set in config file response factory ### XXX- response factory should set up the environment that htmlpage needs - my $response = Everything::HTTP::ResponseFactory->new( $e->get_response_type || 'htmlpage', $e ); - $response->create_http_body( { config => $config } ); - my $html = $response->get_http_body; + my $response = Everything::HTTP::ResponseFactory->new( $e->get_response_type || 'htmlpage', { config => $config, request => $e } ); + ### new actually creates the response - so get rid of 'create http body' + ### check response code + ### check headers + ### check content + ### return status_code + + my $html = $response->content(); + $r->content_type( $response->content_type ); $r->headers_out->set( 'Set-Cookie' => $e->get_user->{cookie} ); + my %headers = $response->headers; + + foreach ( keys %headers ) { + $r->headers_out->set( $_ => $headers{ $_ } ); + } + $r->print($html); - # To ensure any changes in VARS are saved to the db + # XXX: These should be set in the Response object + # NB: These lines ensure any changes in VARS are saved to the db $e->get_user->setVars( $e->get_user_vars, $e->get_user ); $e->get_user->update( $e->get_user ); - return OK; + return $response->status_code; } Modified: trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -6,7 +6,7 @@ use base 'Class::Accessor::Fast'; __PACKAGE__->follow_best_practice; __PACKAGE__->mk_accessors( - qw/http_header http_body request htmlpage theme allowed redirect/); + qw/http_header http_body request htmlpage theme allowed redirect config/); use strict; ### because this is called from a Class::Factory object new is not @@ -19,18 +19,19 @@ } sub init { - my ( $self, $e ) = @_; - $self->set_request($e); + my ( $self, $args ) = @_; + $self->set_request( $args->{request} ); + $self->set_config( $args->{config} ); $self->select_htmlpage; return $self; } -sub create_http_body { +sub content { my ( $self, $args ) = @_; my $htmlpage = $self->get_htmlpage; - my $config = $$args{ config }; + my $config = $self->get_config; my $ehtml = Everything::HTML->new; if ( $config ) { @@ -58,7 +59,31 @@ } +=head2 headers + +The headers other than Content-Type + +=cut + +sub headers { + + (); + +} + +=head2 status_code + +Returns the HTTP status code of the response. This is always 'OK', because HTMLPAGES always accept a request. + +=cut + +sub status_code { + + 0; ## Apache prefers this to 200. +} + sub charset { + 'utf-8' } Modified: trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -34,7 +34,7 @@ $mock->set_always( 'getType', $mock ); $mock->{title} = 'a title'; $self->{mock} = $mock; - isa_ok( $self->{instance} = $self->{class}->new($mock), $self->{class} ); + isa_ok( $self->{instance} = $self->{class}->new( { request => $mock } ), $self->{class} ); } @@ -42,7 +42,7 @@ my $self = shift; my $class = $self->{class}; my $instance = $self->{instance}; - can_ok( $class, 'create_http_body' ); + can_ok( $class, 'content' ); can_ok( $class, 'content_type' ); } Modified: trunk/ebase/lib/Everything/HTTP/ResponseFactory.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/ResponseFactory.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/HTTP/ResponseFactory.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -7,6 +7,7 @@ __PACKAGE__->add_factory_type('htmlpage' => 'Everything::HTTP::Response::Htmlpage'); +__PACKAGE__->add_factory_type('nodeball' => 'Everything::HTTP::Response::Nodeball'); =head1 Everything::HTTP::ResponseFactory @@ -18,58 +19,75 @@ Everything::HTTP::ResponseFactory->add_factory_type('anothertype' => 'Name::of::another::package'); -my $response = Everything::HTTP::ResponseFactory->new(<response type>, [@args]); +my $response = Everything::HTTP::ResponseFactory->new(<response type>, { args } ); -$response->create_http_body; +my $html = $response->content; -my $html = $response->get_http_body; +my $content_type = $response->content_type; -my $mime_type = $response->get_mime_type; -my $header = $response->create_http_header; - - =head1 DESCRIPTION This is a factory class, that is, the constructor returns instances that are blessed into other classes, not this one. This class inherits from C<Class::Factory>, so the rules for adding types and the rules instanciation are the same as they are in C<Class::Factory>. In essense, if you want to customise the way your classes are instanciated you should use the C<init> method. -In addition, the instances that this class provides must support the following methods: +The objects created by this class must return values that allow a +response be sent back to the client browser. =over 4 -=item get_http_body set_http_body +=item C<new> -Getters and setters for the http_body attribute. +This is the constructor. It takes two arguments: -=item get_http_header set_http_header +=over -Getters and setters for the http_header attribute. +=item -=item get_mime_type set_mime_type +The first is a string that determines the type of object return. -Getters and setters for the mime_type attribute. +=item +The second is a hash ref that is passed straight to the created objects. Attributes may include: + +=over + +=item config + +An Everything::Config object. + +=item request + +An Everything::HTTP::Request object. + =back -In addition, the followimg methods must be supported: +=back +=back + +In addition, the instances that this class provides must support the following methods: + =over 4 -=item create_http_header +=item content_type -Conjures a conforming http header and sets the http_header attribute. +Returns the data for the Content-Type header. -=item create_http_body +=item headers -Conjures up a conforming http body and sets the http_body attribute. +Returns a hash of headers. By default does not return the Content-Type header. -=item create_mime_type +=item content -Conjures up a mime type (from where is not important) and sets the mime_type attribute. +Returns the message body +=item status_code + +Returns the HTTP status code. + =back =cut Modified: trunk/ebase/lib/Everything/HTTP/Test/Apache.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Test/Apache.pm 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/lib/Everything/HTTP/Test/Apache.pm 2008-07-11 20:26:45 UTC (rev 1010) @@ -21,10 +21,11 @@ $mock->fake_module('Everything::HTTP::ResponseFactory'); $mock->fake_new('Everything::HTTP::ResponseFactory'); - $mock->set_true(qw/create_http_body/) + $mock->set_true(qw/content/) ->set_always( content_type => 'a mime type' ) - ->set_always( 'get_http_body', 'the html body' ); + ->set_always( 'content', 'the html body' ); + $mock->set_always( status_code => 0 ); $self->{class} = $self->module_class; use_ok( $self->{class} ); @@ -42,7 +43,7 @@ return $name; } -sub test_handler : Test(23) { +sub test_handler : Test(22) { my $self = shift; my $mock = $self->{mock}; my $fake_everything_request = $self->{fake_everything_request}; @@ -128,18 +129,15 @@ ( $method, $args ) = $fake_apache_request->next_call; is( $method, 'print', - '...currently do our own cookies until Auth.pm rewrite.' ); + '...prints response.' ); is( $args->[1], 'the html body', '...prints http header.' ); is( $result, 0, '...should return correct result' ); ( $method, $args ) = $mock->next_call; - is( $method, 'create_http_body', '...factory creates http body.' ); + is( $method, 'content', '...factory creates http body.' ); ( $method, $args ) = $mock->next_call; - is( $method, 'get_http_body', '...retrieves http body.' ); - - ( $method, $args ) = $mock->next_call; is( $method, 'content_type', '...returns mime type.' ); ( $method, $args ) = $mock->next_call; Modified: trunk/ebase/t/lib/everything.conf =================================================================== --- trunk/ebase/t/lib/everything.conf 2008-07-11 20:20:42 UTC (rev 1009) +++ trunk/ebase/t/lib/everything.conf 2008-07-11 20:26:45 UTC (rev 1010) @@ -2,6 +2,8 @@ database_type = sqlite location_schema_nodetype = /node/:node_id node +request_modifier_standard = nodeball_download + request_modifier_code = <<"FOOFOO" sub { my ($url, $e ) = @_; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |