package NCGI::Response; use 5.006; use strict; use warnings; use Carp; use NCGI::Response::Header; use XML::API; our $VERSION = '0.12'; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = { header => NCGI::Response::Header->new, }; bless($self, $class); return $self; } sub header { my $self = shift; return $self->{header}; } sub content { my $self = shift; if ($self->{content}) { return $self->{content}; } return $self->xhtml; } sub xhtml { my $self = shift; if ($self->{xhtml}) { return $self->{xhtml}; } $self->{xhtml} = XML::API->new(doctype => 'xhtml'); $self->{xhtml}->html_open; $self->{xhtml}->_set_id('html'); $self->{xhtml}->head_open(''); $self->{xhtml}->_set_id('head'); $self->{xhtml}->head_close(); $self->{xhtml}->body_open(''); $self->{xhtml}->_set_id('body'); $self->{content} = $self->{xhtml}; return $self->{xhtml}; } sub rss { my $self = shift; if ($self->{rss}) { return $self->{rss}; } $self->{rss} = XML::API->new(doctype => 'rss'); $self->{content} = $self->{rss}; return $self->{rss}; } sub text { croak 'text not implemented yet'; # $self->{content} = $self->{text}; } sub as_string { my $self = shift; my $action = shift || '_as_string'; if ($action !~ m/_as_string|_fast_string/) { croak 'usage: as_string([_fast_string])'; } if (!$self->{content}) { croak 'No content to display (must call xhtml/rss/text first)'; } # # Set some interesting headers # my $x = $self->{content}; if ($x->_langs) { $self->{header}->content_language(join(',', $x->_langs)); } my $type = $x->_content_type; if (ref($x) eq 'XML::API::XHTML') { if ($ENV{HTTP_ACCEPT} and $ENV{HTTP_ACCEPT} !~ m/application\/xhtml\+xml/) { $type = 'text/html'; } } $self->{header}->content_type($type . '; charset='. $x->_encoding); my $doc = $self->{content}->$action; do { # don't want number of unicode characters... use bytes; $self->{header}->content_length(length($doc)); }; return $self->{header}->_as_string() . $doc; } sub fast_string { my $self = shift; return $self->as_string('_fast_string'); } 1; __END__ =head1 NAME NCGI::Response - Represent A CGI Response =head1 SYNOPSIS use NCGI::Response; my $r = NCGI::Response->new(); my $h = $r->header; $h->custom_header('value'); my $x = $r->xhtml; $x->_encoding('ISO-8859-1'); $x->_goto('head'); $x->_set_lang('en'); $x->title('A Simple Example'); print $r->as_string(); # Content-Length: 302 # Content-Type: application/xhtml+xml; charset=ISO-8859-1 # Custom-Header: value # Content-Language: en # # # # #
#