diff Lib/IMPL/Web/Application/Response.pm @ 63:76b878ad6596

Added serialization support for the IMPL::Object::List More intelligent Exception message Fixed encoding support in the actions Improoved tests Minor fixes
author wizard
date Mon, 15 Mar 2010 02:38:09 +0300
parents 0f3e369553bd
children 2840c4c85db8
line wrap: on
line diff
--- a/Lib/IMPL/Web/Application/Response.pm	Fri Mar 12 16:23:46 2010 +0300
+++ b/Lib/IMPL/Web/Application/Response.pm	Mon Mar 15 02:38:09 2010 +0300
@@ -8,8 +8,11 @@
 require CGI::Cookie;
 
 use Carp;
+use Encode;
 use IMPL::Class::Property;
 
+#todo: add binary method to set a binary encoding, set it automatic when type isn't a text 
+
 BEGIN {
 	public property query => prop_get | owner_set; # cgi query
 	public property status => prop_all, { validator => \&_checkHeaderPrinted };
@@ -33,9 +36,10 @@
 	my ($this,%args) = @_;
 	
 	$this->query(CGI->new($this->query() | {})) unless $this->query;
-	$this->charset($this->query->charset) unless $this->charset;
 	
 	$this->streamOut(*STDOUT) unless $this->streamOut;
+	$this->buffered(1) unless defined $this->buffered;
+	binmode $this->streamOut, ":encoding(".$this->charset.")";
 }
 
 sub _checkHeaderPrinted {
@@ -53,7 +57,18 @@
 sub _charset {
 	my $this = shift;
 	
-	return $this->query->charset(@_);
+	if (@_) {
+		my $charset = $this->query->charset(@_);
+		
+		my $hout = $this->streamOut;
+		
+		binmode $hout;
+		binmode $hout, ":encoding($charset)";
+		
+		return $charset;
+	} else {
+		return $this->query->charset;
+	}
 }
 
 sub _PrintHeader {
@@ -87,9 +102,12 @@
 	unless ($this->_streamBody) {
 		if ($this->buffered) {
 			my $buffer = "";
+			
 			$this->_bufferBody(\$buffer);
 				
-			open my $hBody, ">", \$buffer or die new IMPL::Exception("Failed to create buffer",$!);
+			open my $hBody, ">:encoding(utf-8)", \$buffer or die new IMPL::Exception("Failed to create buffer",$!);
+			
+			Encode::_utf8_on($buffer);
 				
 			$this->_streamBody($hBody);
 		} else {
@@ -110,11 +128,12 @@
 	
 	$this->_PrintHeader();
 	
+	$this->_streamBody(undef);
+	
 	if ($this->buffered) {
 		print $hOut ${$this->_bufferBody};	
-	}
+	}	
 	
-	$this->_streamBody(undef);
 	$this->_bufferBody(undef);
 	$this->streamOut(undef);