| 1 | package Web::Request::Role::Response; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Generate various HTTP responses from a Web::Request | ||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.000'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Plack::Response; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | sub redirect { | ||||||||||
| 12 | 5 | ▶ | my ( $self, $target, $status ) = @_; | ||||||||
|
Subroutine
redirect: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | if ( ref($target) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 1 | $target = $self->uri_for($target); | |||||||||
| 16 | } | ||||||||||
| 17 | 5 | my $res = $self->new_response(); | |||||||||
| 18 | 5 | $res->redirect( $target, $status ); | |||||||||
| 19 | 5 | $res->content("Redirecting to: $target"); | |||||||||
| 20 | 5 | return $res; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub permanent_redirect { | ||||||||||
| 24 | 1 | ▶ | my ( $self, $target ) = @_; | ||||||||
|
Subroutine
permanent_redirect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | return $self->redirect( $target, 301 ); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub new_file_download_response { # TODO make it stream? | ||||||||||
| 30 | 1 | ▶ | my ( $self, $content_type, $data, $filename ) = @_; | ||||||||
|
Subroutine
new_file_download_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | |||||||||||
| 32 | 1 | 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 new_no_content_response { | ||||||||||
| 43 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
new_no_content_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | |||||||||||
| 45 | 1 | return $self->new_response( status => 204, ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 1 | my $transparent_gif = pack( 'H*', | |||||||||
| 49 | '47494638396101000100800000000000ffffff21f90401000000002c000000000100010000020144003b' | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | sub new_transparent_gif_response { | ||||||||||
| 53 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
new_transparent_gif_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 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 | 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 | 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.000 | ||||||||||
| 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->new_no_content_response; | ||||||||||
| 105 | |||||||||||
| 106 | # return a transparent 1x1 gif (eg as a tracking pixle) | ||||||||||
| 107 | $req->new_transparent_gif_response; | ||||||||||
| 108 | |||||||||||
| 109 | # file download | ||||||||||
| 110 | $req->new_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 new_file_download_response | ||||||||||
| 146 | |||||||||||
| 147 | $req->new_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 new_no_content_response | ||||||||||
| 154 | |||||||||||
| 155 | $req->new_no_content_response | ||||||||||
| 156 | |||||||||||
| 157 | Returns C<2014 No Content>. | ||||||||||
| 158 | |||||||||||
| 159 | =head3 new_transparent_gif_response | ||||||||||
| 160 | |||||||||||
| 161 | $req->new_transparent_gif_response | ||||||||||
| 162 | |||||||||||
| 163 | Returns a transparent 1x1 pixle 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 | =back | ||||||||||
| 177 | |||||||||||
| 178 | =head1 AUTHOR | ||||||||||
| 179 | |||||||||||
| 180 | Thomas Klausner <domm@cpan.org> | ||||||||||
| 181 | |||||||||||
| 182 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 183 | |||||||||||
| 184 | This software is copyright (c) 2017 by Thomas Klausner. | ||||||||||
| 185 | |||||||||||
| 186 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 187 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 188 | |||||||||||
| 189 | =cut | ||||||||||