| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Web::Request::Role::Response; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Generate various HTTP responses from a Web::Request | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '1.007'; # VERSION | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 4216 | use 5.010; | 
|  | 1 |  |  |  |  | 4 |  | 
| 8 | 1 |  |  | 1 |  | 6 | use Moose::Role; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 9 | 1 |  |  | 1 |  | 6252 | use Web::Response; | 
|  | 1 |  |  |  |  | 142847 |  | 
|  | 1 |  |  |  |  | 382 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub redirect { | 
| 12 | 6 |  |  | 6 | 1 | 56579 | my ( $self, $target, $status ) = @_; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 6 | 100 |  |  |  | 21 | if ( ref($target) eq 'HASH' ) { | 
| 15 | 1 |  |  |  |  | 5 | $target = $self->uri_for($target); | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 6 |  |  |  |  | 28 | my $res = $self->new_response(); | 
| 18 | 6 |  |  |  |  | 1539 | $res->redirect( $target, $status ); | 
| 19 | 6 |  |  |  |  | 1565 | $res->content("Redirecting to: $target"); | 
| 20 | 6 |  |  |  |  | 2339 | return $res; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub permanent_redirect { | 
| 24 | 1 |  |  | 1 | 1 | 7656 | my ( $self, $target ) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 1 |  |  |  |  | 4 | return $self->redirect( $target, 301 ); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub file_download_response {    # TODO make it stream? | 
| 30 | 1 |  |  | 1 | 1 | 8757 | my ( $self, $content_type, $data, $filename ) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 |  |  |  |  | 33 | return Web::Response->new( | 
| 33 |  |  |  |  |  |  | status  => 200, | 
| 34 |  |  |  |  |  |  | headers => [ | 
| 35 |  |  |  |  |  |  | 'content_type'        => $content_type, | 
| 36 |  |  |  |  |  |  | 'content_disposition' => 'attachment; filename=' . $filename | 
| 37 |  |  |  |  |  |  | ], | 
| 38 |  |  |  |  |  |  | content => $data, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub no_content_response { | 
| 43 | 1 |  |  | 1 | 1 | 10044 | my $self = shift; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 5 | return $self->new_response( status => 204, ); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | my $transparent_gif = pack( 'H*', | 
| 49 |  |  |  |  |  |  | '47494638396101000100800000000000ffffff21f90401000000002c000000000100010000020144003b' | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub transparent_gif_response { | 
| 53 | 1 |  |  | 1 | 1 | 7988 | my $self = shift; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # cannot use $self->new_reponse here, because this would reuse the | 
| 56 |  |  |  |  |  |  | # encoding_object of the request, which will mangle the binary | 
| 57 |  |  |  |  |  |  | # response | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 1 |  |  |  |  | 46 | return Web::Response->new( | 
| 60 |  |  |  |  |  |  | status  => 200, | 
| 61 |  |  |  |  |  |  | headers => [ | 
| 62 |  |  |  |  |  |  | 'content-type'   => 'image/gif', | 
| 63 |  |  |  |  |  |  | 'content-length' => length($transparent_gif), | 
| 64 |  |  |  |  |  |  | ], | 
| 65 |  |  |  |  |  |  | content => $transparent_gif, | 
| 66 |  |  |  |  |  |  | ); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | 1; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | __END__ | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =pod | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =encoding UTF-8 | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =head1 NAME | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | Web::Request::Role::Response - Generate various HTTP responses from a Web::Request | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =head1 VERSION | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | version 1.007 | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # Create a request handler | 
| 88 |  |  |  |  |  |  | package My::App::Request; | 
| 89 |  |  |  |  |  |  | use Moose; | 
| 90 |  |  |  |  |  |  | extends 'Web::Request'; | 
| 91 |  |  |  |  |  |  | with 'Web::Request::Role::Response'; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Make sure your app uses your request handler, e.g. using OX: | 
| 94 |  |  |  |  |  |  | package My::App::OX; | 
| 95 |  |  |  |  |  |  | sub request_class {'My::App::Request'} | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # in some controller action: | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # redirect | 
| 100 |  |  |  |  |  |  | $req->redirect('/'); | 
| 101 |  |  |  |  |  |  | $req->permanent_redirect('/foo'); | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | # return 204 no content | 
| 104 |  |  |  |  |  |  | $req->no_content_response; | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | # return a transparent 1x1 gif (eg as a tracking pixle) | 
| 107 |  |  |  |  |  |  | $req->transparent_gif_response; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | # file download | 
| 110 |  |  |  |  |  |  | $req->file_download_response( 'text/csv', $data, 'your_export.csv' ); | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | C<Web::Request::Role::JSON> provides a few methods that make generating HTTP responses easier when using L<Web::Request>. | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Please note that all methods return a L<Web::Response> object. | 
| 117 |  |  |  |  |  |  | Depending on the framework you use (or lack thereof), you might have | 
| 118 |  |  |  |  |  |  | to call C<finalize> on the response object to turn it into a valid | 
| 119 |  |  |  |  |  |  | PSGI response. | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head2 METHODS | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head3 redirect | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | $req->redirect( '/some/location' ); | 
| 126 |  |  |  |  |  |  | $req->redirect( $ref_uri_for ); | 
| 127 |  |  |  |  |  |  | $req->redirect( 'http://example.com', 307 ); | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | Redirect to the given location. The location can be a string | 
| 130 |  |  |  |  |  |  | representing an absolute or relative URL. You can also pass a ref, | 
| 131 |  |  |  |  |  |  | which will be resolved by calling C<uri_for> on the request object - | 
| 132 |  |  |  |  |  |  | so be sure that your request object has this method (extra points if | 
| 133 |  |  |  |  |  |  | the method also returns something meaningful)! | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | You can pass a HTTP status code as a second parameter. It's probably | 
| 136 |  |  |  |  |  |  | smart to use one that makes sense in a redirecting context... | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head3 permanent_redirect | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | $req->permanent_redirect( 'http://we.moved.here' ); | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Similar to C<redirect>, but will issue a permanent redirect (who would | 
| 143 |  |  |  |  |  |  | have thought!) using HTTP status code C<301>. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head3 file_download_response | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | $req->file_download_response( $content-type, $data, $filename ); | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Generate a "Download-File" response. Useful if your app returns a | 
| 150 |  |  |  |  |  |  | CSV/Spreadsheet/MP3 etc. You have to provide the correct content-type, | 
| 151 |  |  |  |  |  |  | the data in the correct encoding and a meaningful filename. | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head3 no_content_response | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | $req->no_content_response | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | Returns C<204 No Content>. | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =head3 transparent_gif_response | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | $req->transparent_gif_response | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Returns a transparent 1x1 pixel GIF. Useful as the response of a | 
| 164 |  |  |  |  |  |  | tracking URL. | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head1 THANKS | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | Thanks to | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =over | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =item * | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | L<validad.com|https://www.validad.com/> for supporting Open Source. | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =item * L<choroba|https://github.com/choroba> for improvments to the test suite. | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =back | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head1 AUTHOR | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | Thomas Klausner <domm@plix.at> | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | This software is copyright (c) 2017 - 2021 by Thomas Klausner. | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 189 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =cut |