| 1 | package WWW::Pipeline::Services::Output; | ||
| 2 | $VERSION = '0.1'; | ||
| 3 | |||
| 4 | #-- pragmas ---------------------------- | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | =head1 WWW::Pipeline::Services::Output; | ||
| 9 | |||
| 10 | This plugin for WWW::Pipeline provide services and mechanisms for delivering | ||
| 11 | the response from the application to the client. | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | #== Plugin ===================================================================== | ||
| 16 | |||
| 17 | sub load { | ||
| 18 | my( $class, $pipeline ) = @_; | ||
| 19 | |||
| 20 | my $self ||= bless { _sent => 0 }, $class; | ||
| 21 | |||
| 22 | $pipeline->addServices( | ||
| 23 | headers => sub { $self->headers(@_) }, | ||
| 24 | flush => sub { $self->flush(@_) } | ||
| 25 | ); | ||
| 26 | |||
| 27 | $pipeline->addHandler( 'SendResponse', \&send ); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | #== Services =================================================================== | ||
| 33 | |||
| 34 | =head2 Services | ||
| 35 | |||
| 36 | =head3 headers | ||
| 37 | |||
| 38 | #set: | ||
| 39 | $pipeline->headers( -status=>'403 Not Authorized' | ||
| 40 | -cookie => $cookie, | ||
| 41 | -expires => '+3h' | ||
| 42 | ); | ||
| 43 | |||
| 44 | #retrieve: | ||
| 45 | my $loc = $pipeline->headers( '-status' ); | ||
| 46 | |||
| 47 | #remove: | ||
| 48 | $pipeline->('-expires' => undef ); | ||
| 49 | |||
| 50 | The headers service is used to build a hash of http headers in the style of | ||
| 51 | the CGI.pm module. Setting and Deleting header entries can be mixed, however | ||
| 52 | retrieval must be done one at a time. | ||
| 53 | |||
| 54 | The headers are stored until the SendResponse phase, when the plugin's installed | ||
| 55 | handler sends them as part of the response. They may be sent earlier by way of | ||
| 56 | the plugin's c<flush()> service. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub headers { | ||
| 61 | |||
| 62 | my( $self, $pipeline, @params ) = @_; | ||
| 63 | |||
| 64 | if( @params == 1 ) { | ||
| 65 | return defined $self->{_headers}{$params[0]} | ||
| 66 | ? $self->{_headers}{$params[0]} | ||
| 67 | : undef; | ||
| 68 | } | ||
| 69 | |||
| 70 | die "headers expects a hash of headers if you pass it more than one parameter" | ||
| 71 | if @params % 2 != 0; | ||
| 72 | |||
| 73 | my %headers = @params; | ||
| 74 | while( my( $header,$value ) = each %headers ) { | ||
| 75 | if( defined $value ) { | ||
| 76 | $self->{_headers}{$header} = $value; | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | delete $self->{_headers}{$header}; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | #------------------------------------------------------------------------------- | ||
| 85 | |||
| 86 | =head3 flush | ||
| 87 | |||
| 88 | $pipeline->flush(); | ||
| 89 | |||
| 90 | Sends any stored headers and any content stored in the application's C<response> | ||
| 91 | service ( which is part of the standard WWW::Pipeline::Services package). | ||
| 92 | C<flush> may be used more than once, but will only send the headers the first | ||
| 93 | time. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub flush { | ||
| 98 | my( $self, $pipeline ) = @_; | ||
| 99 | |||
| 100 | unless( $self->{_sent} == 1 ) { | ||
| 101 | print $pipeline->query->header( %{$self->{_headers}} ); | ||
| 102 | $self->{_sent} = 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | print $pipeline->response(); | ||
| 106 | $pipeline->response(''); | ||
| 107 | } | ||
| 108 | |||
| 109 | #=============================================================================== | ||
| 110 | |||
| 111 | =head2 Handlers | ||
| 112 | |||
| 113 | =head3 send | ||
| 114 | |||
| 115 | The plugin installs the C<send> handler during the host application's | ||
| 116 | SendResponse phase. The whole of its purpose is to invoke the C<flush()> | ||
| 117 | service as described above. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub send { | ||
| 122 | my $pipeline = shift; | ||
| 123 | $pipeline->flush(); | ||
| 124 | } | ||
| 125 | |||
| 126 | #======== | ||
| 127 | 1; | ||
| 128 | |||
| 129 | =head2 Authors | ||
| 130 | |||
| 131 | Stephen Howard <stephen@thunkit.com> | ||
| 132 | |||
| 133 | =head2 License | ||
| 134 | |||
| 135 | This module may be distributed under the same terms as Perl itself. | ||
| 136 | |||
| 137 | =cut |