diff Lib/IMPL/Web/Application.pm @ 97:964587c5183c

Added SecureCall to Web QueryHandlers stack many bug fixes to Security and Web Application modules
author wizard
date Tue, 04 May 2010 04:04:37 +0400
parents 2f31ecabe9ea
children 6dd659f6f66c
line wrap: on
line diff
--- a/Lib/IMPL/Web/Application.pm	Fri Apr 30 15:03:38 2010 +0400
+++ b/Lib/IMPL/Web/Application.pm	Tue May 04 04:04:37 2010 +0400
@@ -19,6 +19,7 @@
     public property responseCharset => prop_all;
     public property security => prop_all;
     public property options => prop_all;
+    public property fetchRequestMethod => prop_all;
 }
 
 sub CTOR {
@@ -26,6 +27,8 @@
 	
 	$this->actionFactory('IMPL::Web::Application::Action') unless $this->actionFactory;
 	$this->responseCharset('utf-8') unless $this->responseCharset;
+	$this->fetchRequestMethod(\&defaultFetchRequest) unless $this->fetchRequestMethod;
+	$this->handlerError(\&defaultHandlerError) unless $this->handlerError;
 }
 
 sub Run {
@@ -38,26 +41,55 @@
         	application => $this, 
         );
         
-        $action->response->charset($this->responseCharset);
-        
-        $action->ChainHandler($_) foreach $this->handlersQuery;
-        
-        $action->Invoke();
-        
-        $action->response->Complete;
+        eval {
+	        $action->response->charset($this->responseCharset);
+	        
+	        $action->ChainHandler($_) foreach $this->handlersQuery;
+	        
+	        $action->Invoke();
+	        
+	        $action->response->Complete;
+        };
+        if ($@) {
+        	my $e = $@; 
+        	eval { $this->handlerError()->($this,$action,$e); 1;} or warn "Error in handlerError: ",$@;
+        }
     }
 }
 
+sub FetchRequest {
+	my ($this) = @_;
+	
+	if( ref $this->fetchRequestMethod eq 'CODE' ) {
+		return $this->fetchRequestMethod->($this);
+	} else {
+		die new IMPL::Exception("Unknown fetchRequestMethod type",ref $this->fetchRequestMethod);
+	}
+}
+
 {
 	my $hasFetched = 0;
 
-	sub FetchRequest {
+	sub defaultFetchRequest {
 		return undef if $hasFetched;
 		$hasFetched = 1;
 		return CGI->new();
 	}
 }
 
+sub defaultHandlerError {
+	my ($this,$action,$e) = @_;
+	warn $e;
+	if ( eval {	$action->ReinitResponse(); 1; } ) {
+		$action->response->contentType('text/plain');
+		$action->response->charset($this->responseCharset);
+		$action->response->status(500);
+		my $hout = $action->response->streamBody;
+		print $hout $e;
+		$action->response->Complete();
+	}	
+}
+
 1;
 
 __END__
@@ -199,6 +231,13 @@
 
 =end code
 
+=item C< [get,set] fetchRequestMethod >
+
+Метод получения CGI запроса. Возвращает C<CGI> объект следующего запроса, если
+запросов больше нет, то возвращает C<undef>. По-умолчанию использует C<defaultFetchRequest>.
+
+Может быть как ссылкой на функцию, так и объектом типа C<IMPL::Web::Application::RequestFetcher>.
+
 =item C< [get,set,list] handlersQuery >
 
 Список обработчиков запросов, которые будут переданы созданному объекту-действию.