Update of /cvsroot/http-webtest/HTTP-WebTest-Recorder/lib/HTTP/WebTest/Recorder
In directory sc8-pr-cvs1:/tmp/cvs-serv29286/lib/HTTP/WebTest/Recorder
Modified Files:
Controller.pm Actions.pm
Log Message:
Refactor Actions.pm and Controller.pm: I use chain of handlers now and
handlers have direct access to response object
Index: Controller.pm
===================================================================
RCS file: /cvsroot/http-webtest/HTTP-WebTest-Recorder/lib/HTTP/WebTest/Recorder/Controller.pm,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** Controller.pm 22 Apr 2003 20:10:19 -0000 1.8
--- Controller.pm 22 Apr 2003 21:43:48 -0000 1.9
***************
*** 9,16 ****
use HTTP::Status;
use HTTP::WebTest::Utils qw(make_access_method);
- use Template;
use HTTP::WebTest::Recorder::Config qw(%DISPATCH);
- use HTTP::WebTest::Recorder::Exceptions;
# constructor
--- 9,14 ----
***************
*** 23,28 ****
# recorder object
*recorder = make_access_method('RECORDER');
- # template toolkit object
- *template = make_access_method('TEMPLATE', 'create_template');
# view (template filename)
*view = make_access_method('VIEW');
--- 21,24 ----
***************
*** 31,34 ****
--- 27,32 ----
# request object
*request = make_access_method('REQUEST');
+ # heap object
+ *heap = make_access_method('HEAP');
# serves requests for web interface of the proxy
***************
*** 43,110 ****
$self->view($param{action});
! my $action = $DISPATCH{$param{action}} || $DISPATCH{default};
! my $response;
! eval {
! $response = $self->process_template($action->($self));
! };
! if($@) {
! if($@->isa('HTTP::WebTest::Recorder::Exception::Redirect')) {
! $response = HTTP::Response->new(RC_FOUND);
! $response->header(Location => $@->url);
! } elsif($@->isa('HTTP::WebTest::Recorder::Exception::RawResponse')) {
! $response = HTTP::Response->new(RC_OK);
! $response->header(@{$@->headers});
! $response->content($@->content);
} else {
! $@->rethrow;
}
- };
- $response->header(Pragma => 'No-Cache');
- return $response;
- }
-
- # populates template with data returned by action object and generates
- # response based on template output
- sub process_template {
- my $self = shift;
- my %data = @_;
-
- my $content = '';
- if($self->template->process($self->view, \%data, \$content)) {
- my $response = HTTP::Response->new(RC_OK);
- $response->header(Content_Type => 'text/html');
- $response->content($content);
- return $response;
- } elsif($self->template->error =~ /file error - .*: not found/) {
- my $response = HTTP::Response->new(RC_NOT_FOUND);
- $response->header(Content_Type => 'text/plain');
- $response->content('Not Found');
- return $response;
- } else {
- my $response = HTTP::Response->new(RC_INTERNAL_SERVER_ERROR);
- $response->header(Content_Type => 'text/plain');
- $response->content($self->template->error);
- return $response;
}
}
! # generates redirect exception
sub redirect {
my $self = shift;
my $url = shift;
! HTTP::WebTest::Recorder::Exception::Redirect->throw(message => 'Redirect',
! url => $url);
! }
!
! # generates raw response exception
! sub raw_response {
! my $self = shift;
! my %param = @_;
!
! $param{message} = 'Raw Response';
!
! HTTP::WebTest::Recorder::Exception::RawResponse->throw(%param);
}
--- 41,72 ----
$self->view($param{action});
! # init heap
! $self->heap({});
! $self->heap->{controller} = $self;
! $self->heap->{response} = HTTP::Response->new(RC_OK);
! # proceed request through handler chain
! my $action = $param{action};
! $action = 'render_template' unless exists $DISPATCH{$action};
! while(defined $action) {
! my $handler = $DISPATCH{$action};
! if(defined $handler) {
! $action = $handler->($self);
} else {
! undef $action;
}
}
+
+ $self->heap->{response}->header(Pragma => 'No-Cache');
+ return $self->heap->{response};
}
! # configures correct HTTP headers for redirect
sub redirect {
my $self = shift;
my $url = shift;
! $self->heap->{response}->code(RC_FOUND);
! $self->heap->{response}->header(Location => $url);
}
***************
*** 122,137 ****
return CGI->new($query);
- }
-
- # creates template toolkit object
- sub create_template {
- my $self = shift;
-
- # search template files in standart Perl include directories
- my @template_dir = qw(HTTP WebTest Recorder template);
- my $include_path = (join ':',
- map File::Spec->catdir($_, @template_dir),
- @INC);
- return Template->new(INCLUDE_PATH => $include_path);
}
--- 84,87 ----
Index: Actions.pm
===================================================================
RCS file: /cvsroot/http-webtest/HTTP-WebTest-Recorder/lib/HTTP/WebTest/Recorder/Actions.pm,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Actions.pm 5 Apr 2003 09:59:22 -0000 1.6
--- Actions.pm 22 Apr 2003 21:43:48 -0000 1.7
***************
*** 3,21 ****
use strict;
use base qw(Exporter);
use vars qw(@EXPORT_OK %EXPORT_TAGS);
! @EXPORT_OK = qw(default_action enable_action list_action
request_action response_content_action
delete_action wtscript_action filter_action);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
! use HTTP::WebTest::Parser;
!
! sub default_action {
my $controller = shift;
! return(controller => $controller);
}
--- 3,50 ----
use strict;
+ use HTTP::Status;
+ use Template;
+
+ use HTTP::WebTest::Parser;
+
use base qw(Exporter);
use vars qw(@EXPORT_OK %EXPORT_TAGS);
! @EXPORT_OK = qw(render_template_action enable_action list_action
request_action response_content_action
delete_action wtscript_action filter_action);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
! sub render_template_action {
my $controller = shift;
! my $content = '';
! my $template = create_template();
!
! if($template->process($controller->view, $controller->heap, \$content)) {
! $controller->heap->{response}->header(Content_Type => 'text/html');
! $controller->heap->{response}->content($content);
! } elsif($template->error =~ /file error - .*: not found/) {
! $controller->heap->{response}->code(RC_NOT_FOUND);
! $controller->heap->{response}->header(Content_Type => 'text/plain');
! $controller->heap->{response}->content('Not Found');
! } else {
! $controller->heap->{response}->code(RC_INTERNAL_SERVER_ERROR);
! $controller->heap->{response}->header(Content_Type => 'text/plain');
! $controller->heap->{response}->content($template->error);
! }
!
! return;
! }
!
! # creates template toolkit object
! sub create_template {
! # search template files in standart Perl include directories
! my @template_dir = qw(HTTP WebTest Recorder template);
! my $include_path = (join ':',
! map File::Spec->catdir($_, @template_dir),
! @INC);
! return Template->new(INCLUDE_PATH => $include_path);
}
***************
*** 27,30 ****
--- 56,60 ----
$controller->redirect('list');
+ return;
}
***************
*** 32,37 ****
my $controller = shift;
! return(default_action($controller),
! tests => $controller->recorder->tests);
}
--- 62,68 ----
my $controller = shift;
! $controller->heap->{tests} = $controller->recorder->tests;
!
! return 'render_template';
}
***************
*** 40,47 ****
my $num = $controller->cgi->param('num');
- my $test = $controller->recorder->tests->[$num];
! return(default_action($controller),
! test => $test, num => $num);
}
--- 71,79 ----
my $num = $controller->cgi->param('num');
! $controller->heap->{num} = $num;
! $controller->heap->{test} = $controller->recorder->tests->[$num];
!
! return 'render_template';
}
***************
*** 49,63 ****
my $controller = shift;
! my $num = $controller->cgi->param('num');
my $view = $controller->cgi->param('view');
my $test = $controller->recorder->tests->[$num];
if($view eq 'raw') {
! $controller->raw_response(headers => [ 'Content-Type' =>
! $test->response->content_type ],
! content => $test->response->content);
} else {
! return(default_action($controller),
! content => $test->response->content);
}
}
--- 81,98 ----
my $controller = shift;
! my $num = $controller->cgi->param('num');
my $view = $controller->cgi->param('view');
my $test = $controller->recorder->tests->[$num];
if($view eq 'raw') {
! $controller->heap->{response}->header('Content-Type' =>
! $test->response->content_type);
! $controller->heap->{response}->content($test->response->content);
!
! return;
} else {
! $controller->heap->{content} = $test->response->content;
!
! return 'render_template';
}
}
***************
*** 69,74 ****
@{$controller->recorder->tests});
! return(default_action($controller),
! wtscript => $wtscript);
}
--- 104,110 ----
@{$controller->recorder->tests});
! $controller->heap->{wtscript} = $wtscript;
!
! return 'render_template';
}
***************
*** 80,83 ****
--- 116,120 ----
$controller->redirect('list');
+ return;
}
***************
*** 96,99 ****
--- 133,137 ----
$controller->redirect('list');
+ return;
}
|