| 1 | =head1 NAME | ||
| 2 | |||
| 3 | MKDoc::Core::Response - MKDoc Response object. | ||
| 4 | |||
| 5 | |||
| 6 | =head1 SUMMARY | ||
| 7 | |||
| 8 | This object defines the response which is sent back to the client. | ||
| 9 | |||
| 10 | It is not mandatory to use it, but it is quite nice and convenient. | ||
| 11 | |||
| 12 | =cut | ||
| 13 | package MKDoc::Core::Response; | ||
| 14 | 0 | ▶ | use Apache; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Apache::Constants qw/:common/; | ||
| 16 | use MKDoc::Core::Request; | ||
| 17 | use Digest::MD5; | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | our $AUTOLOAD; | ||
| 21 | use Encode; | ||
| 22 | |||
| 23 | |||
| 24 | =head1 API | ||
| 25 | |||
| 26 | =head2 $class->instance(); | ||
| 27 | |||
| 28 | Returns the L<MKDoc::Core::Response> singleton - or creates it if necessary. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | sub instance | ||
| 32 | { | ||
| 33 | my $class = shift; | ||
| 34 | $::MKD_Response ||= $class->new(); | ||
| 35 | return $::MKD_Response; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | =head2 $class->new(); | ||
| 41 | |||
| 42 | Instanciates an MKDoc::Core::Response object from either a $string | ||
| 43 | as returned by $self->get(), or @args which is a hash. | ||
| 44 | |||
| 45 | my $response = new MKDoc::Core::Response | ||
| 46 | Status => '200 OK', | ||
| 47 | Set-Cookie => 'EvilCookie', | ||
| 48 | Content-Type => 'text/plain', | ||
| 49 | BODY => 'Hello, World!'; | ||
| 50 | |||
| 51 | my $clone = new MKDoc::Core::Response ($response->get()); | ||
| 52 | |||
| 53 | =cut | ||
| 54 | sub new | ||
| 55 | { | ||
| 56 | my $class = shift; | ||
| 57 | @_ == 1 and return $class->_new_from_string (@_); | ||
| 58 | return $class->_new_from_args (@_); | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub _new_from_string | ||
| 63 | { | ||
| 64 | my $class = shift; | ||
| 65 | my $lines = shift; | ||
| 66 | my @lines = split /\n/, $lines; | ||
| 67 | my $self = bless {}, $class; | ||
| 68 | while (my $line = shift (@lines)) | ||
| 69 | { | ||
| 70 | chomp ($line); | ||
| 71 | chomp ($line); | ||
| 72 | last unless ($line); | ||
| 73 | |||
| 74 | my ($key, $value) = $line =~ /^(.*?)\:\s*(.*?)\s*$/; | ||
| 75 | defined $key || next; | ||
| 76 | $self->$key ($value); | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->Body (join "\n", @lines); | ||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub _new_from_args | ||
| 85 | { | ||
| 86 | my $class = shift; | ||
| 87 | my $args = shift; | ||
| 88 | my $self = bless {}, $class; | ||
| 89 | $self->Body (delete $args->{'Body'}); | ||
| 90 | while (my ($key, $value) = each %{$args}) | ||
| 91 | { | ||
| 92 | $self->$key ($value); | ||
| 93 | } | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | =head2 $self->HEAD(); | ||
| 100 | |||
| 101 | Returns the head of the HTTP query. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | sub HEAD | ||
| 105 | { | ||
| 106 | my $self = shift; | ||
| 107 | my $req = MKDoc::Core::Request->instance(); | ||
| 108 | |||
| 109 | my %hash = (); | ||
| 110 | my $status = $self->Status() || '200 OK'; | ||
| 111 | $hash{'-status'} = $status; | ||
| 112 | |||
| 113 | foreach my $key (sort $self->header_keys()) | ||
| 114 | { | ||
| 115 | my $val = $self->{$key}; | ||
| 116 | $hash{"-$key"} = $val; | ||
| 117 | } | ||
| 118 | |||
| 119 | my $body = $self->Body(); | ||
| 120 | Encode::_utf8_off ($body); | ||
| 121 | $hash{"-content_length"} = length ($body); | ||
| 122 | $hash{"-etag"} = Digest::MD5::md5_hex ($body); | ||
| 123 | |||
| 124 | return $req->header (%hash); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub header_keys | ||
| 129 | { | ||
| 130 | my $self = shift; | ||
| 131 | return map { ($_ !~ /^(?:Status|Body)$/) ? $_ : () } keys %{$self}; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | =head2 $self->GET(); | ||
| 137 | |||
| 138 | Returns the head plus the body of the HTTP query. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | sub GET | ||
| 142 | { | ||
| 143 | my $self = shift; | ||
| 144 | return $self->HEAD() . $self->Body(); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | =head2 $self->Status(); | ||
| 150 | |||
| 151 | Setter / Getter for the response status code. | ||
| 152 | |||
| 153 | $self->Status ("404 Not Found"); | ||
| 154 | |||
| 155 | =cut | ||
| 156 | sub Status | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | $self->{Status} = shift if (@_); | ||
| 160 | return $self->{Status} || '200 OK'; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | =head2 $self->Body(); | ||
| 166 | |||
| 167 | Setter / Getter for the message body. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | sub Body | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | $self->{Body} = shift if (@_); | ||
| 174 | return $self->{Body} || ''; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | sub clear | ||
| 179 | { | ||
| 180 | my $self = shift; | ||
| 181 | for (keys %{$self}) { delete $self->{$_} }; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | =head2 $self->Xxx(); | ||
| 187 | |||
| 188 | Setter / Getter for any other header. | ||
| 189 | |||
| 190 | Any other header can be set through the AUTOLOAD method. e.g. | ||
| 191 | |||
| 192 | $self->X_Foo ("Bar"); | ||
| 193 | |||
| 194 | Will automagically add the header: | ||
| 195 | |||
| 196 | X-Foo: Bar | ||
| 197 | |||
| 198 | =cut | ||
| 199 | sub AUTOLOAD | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | my ($pkg, $meth) = $AUTOLOAD =~ /(.*)::(.*)/; | ||
| 203 | |||
| 204 | if ($meth =~ /^delete_/) | ||
| 205 | { | ||
| 206 | $meth =~ s/delete_//g; | ||
| 207 | $meth =~ s/_/-/g; | ||
| 208 | return delete $self->{$meth}; | ||
| 209 | } | ||
| 210 | elsif ($meth =~ /^[A-Z]/) | ||
| 211 | { | ||
| 212 | $meth =~ s/_/-/g; | ||
| 213 | $self->{$meth} = shift if (@_); | ||
| 214 | return $self->{$meth}; | ||
| 215 | } | ||
| 216 | else | ||
| 217 | { | ||
| 218 | die "Can't locate object method '$meth' via package '$pkg'"; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | sub DESTROY | ||
| 225 | { | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | =head2 $self->out(); | ||
| 231 | |||
| 232 | Outputs the response to STDOUT. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | sub out | ||
| 236 | { | ||
| 237 | my $self = shift; | ||
| 238 | $self->{'.sent'} && do { | ||
| 239 | warn $self . "::out() called more than once?"; | ||
| 240 | }; | ||
| 241 | |||
| 242 | my $meth = $ENV{REQUEST_METHOD} || 'GET'; | ||
| 243 | $meth =~ /HEAD/ ? print $self->HEAD() : print $self->GET(); | ||
| 244 | |||
| 245 | # explicitly tell Apache that we're done | ||
| 246 | $ENV{MOD_PERL} and do { | ||
| 247 | my $r = Apache->request(); | ||
| 248 | $r->status (DONE); | ||
| 249 | }; | ||
| 250 | |||
| 251 | $self->{'.sent'} = 1; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub redirect | ||
| 256 | { | ||
| 257 | my $self = shift; | ||
| 258 | $self->{'.sent'} && do { | ||
| 259 | warn $self . "::out() called more than once?"; | ||
| 260 | }; | ||
| 261 | |||
| 262 | my $uri = shift; | ||
| 263 | my $req = MKDoc::Core::Request->instance(); | ||
| 264 | print $req->redirect ($uri); | ||
| 265 | |||
| 266 | $self->{'.sent'} = 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | 1; | ||
| 271 | |||
| 272 | |||
| 273 | __END__ | ||