| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Pepper::PlackHandler; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | $Pepper::PlackHandler::VERSION = '1.4'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | # for being a good person | 
| 6 | 1 |  |  | 1 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 7 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 514 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub new { | 
| 10 | 0 |  |  | 0 | 0 |  | my ($class,$args) = @_; | 
| 11 |  |  |  |  |  |  | # %$args must have: | 
| 12 |  |  |  |  |  |  | #	'request' => $plack_request_object, | 
| 13 |  |  |  |  |  |  | #	'response' => $plack_response_object, | 
| 14 |  |  |  |  |  |  | #	'utils' => Pepper::Utilities object, | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # start the object | 
| 17 | 0 |  |  |  |  |  | my $self = bless $args, $class; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # gather up the PSGI environment | 
| 20 | 0 |  |  |  |  |  | $self->pack_psgi_variables(); | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  |  | return $self; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # routine to (re-)pack up the PSGI environment | 
| 27 |  |  |  |  |  |  | sub pack_psgi_variables { | 
| 28 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | # eject from this if we do not have the plack request and response objects | 
| 31 | 0 | 0 | 0 |  |  |  | return if !$self->{request} || !$self->{response}; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 0 |  |  |  |  |  | my (@vars, $value, @values, $v, $request_body_type, $request_body_content, $json_params, $plack_headers, @plack_uploads, $plack_upload, $uploaded_filename); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # stash the hostname, URI, and complete URL | 
| 36 | 0 |  |  |  |  |  | $self->{hostname} = lc($self->{request}->env->{HTTP_HOST}); | 
| 37 | 0 |  |  |  |  |  | $self->{uri} = $self->{request}->path_info(); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # you might want to allow an Authorization Header | 
| 40 | 0 |  |  |  |  |  | $plack_headers = $self->{request}->headers; | 
| 41 | 0 |  |  |  |  |  | $self->{auth_token} = $plack_headers->header('Authorization'); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # or you might test with cookies | 
| 44 | 0 |  |  |  |  |  | $self->{cookies} = $self->{request}->cookies; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # notice how, in a non-PSGI world, you could just pass these as arguments | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # now on to user parameters | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # accept JSON data structures | 
| 51 | 0 |  |  |  |  |  | $request_body_type = $self->{request}->content_type; | 
| 52 | 0 |  |  |  |  |  | $request_body_content = $self->{request}->content; | 
| 53 | 0 | 0 | 0 |  |  |  | if ($request_body_content && $request_body_type eq 'application/json') { | 
| 54 | 0 |  |  |  |  |  | $json_params = $self->{utils}->json_to_perl($request_body_content); | 
| 55 | 0 | 0 |  |  |  |  | if (ref($json_params) eq 'HASH') { | 
| 56 | 0 |  |  |  |  |  | $self->{params} = $json_params; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # the rest of this is to accept any POST / GET vars | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # create a hash of the PSGI params they've sent | 
| 63 | 0 |  |  |  |  |  | @vars = $self->{request}->parameters->keys; | 
| 64 | 0 |  |  |  |  |  | foreach $v (@vars) { | 
| 65 |  |  |  |  |  |  | # ony do it once! --> those multi values will get you | 
| 66 | 0 | 0 |  |  |  |  | next if $self->{params}{$v}; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # plack uses the hash::multivalue module, so multiple values can be sent via one param | 
| 69 | 0 |  |  |  |  |  | @values = $self->{request}->parameters->get_all($v); | 
| 70 | 0 | 0 | 0 |  |  |  | if (scalar(@values) > 1 && $v ne 'client_connection_id') { # must be a multi-select or similiar: two ways to access | 
|  |  | 0 |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # note that we left off 'client_connection_id' as we only want one of those, in case they got too excited in JS-land | 
| 72 | 0 |  |  |  |  |  | foreach $value (@values) { # via array, and I am paranoid to just reference, as we are resuing @values | 
| 73 | 0 |  |  |  |  |  | push(@{$self->{params}{multi}{$v}}, $value); | 
|  | 0 |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 0 |  |  |  |  |  | $self->{params}{$v} = join(',', @values);  # or comma-delimited list | 
| 76 | 0 |  |  |  |  |  | $self->{params}{$v} =~ s/^,//; # no leading commas | 
| 77 |  |  |  |  |  |  | } elsif (length($values[0])) { # single value, make a simple key->value hash | 
| 78 | 0 |  |  |  |  |  | $self->{params}{$v} = $values[0]; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | # did they upload any files? get the Plack::Request::Upload objects | 
| 83 | 0 |  |  |  |  |  | @plack_uploads = $self->{request}->uploads->get_all(); | 
| 84 | 0 |  |  |  |  |  | foreach $plack_upload (@plack_uploads) { | 
| 85 | 0 |  |  |  |  |  | $uploaded_filename = $plack_upload->filename; | 
| 86 | 0 |  |  |  |  |  | $self->{uploaded_files}{$uploaded_filename} = $plack_upload->path; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # maybe they sent the auth_token as a PSGI param? | 
| 90 | 0 |  | 0 |  |  |  | $self->{auth_token} ||= $self->{params}{auth_token}; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # utility to set a cookie | 
| 95 |  |  |  |  |  |  | sub set_cookie { | 
| 96 | 0 |  |  | 0 | 0 |  | my ($self,$cookie_details) = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | # need at least a name and value | 
| 99 | 0 | 0 | 0 |  |  |  | return if !$$cookie_details{name} || !$$cookie_details{value}; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # days to live is important; default to 10 days | 
| 102 | 0 |  | 0 |  |  |  | $$cookie_details{days_to_live} ||= 10; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # cookie domain won't work with port numbers at the end | 
| 105 | 0 |  |  |  |  |  | my $cookie_host = $self->{request}->env->{HTTP_HOST}; | 
| 106 | 0 |  |  |  |  |  | $cookie_host =~ s/\:\d+$//; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | $self->{response}->cookies->{ $$cookie_details{name} } = { | 
| 109 |  |  |  |  |  |  | value => $$cookie_details{value}, | 
| 110 |  |  |  |  |  |  | domain  => $cookie_host, | 
| 111 |  |  |  |  |  |  | path => '/', | 
| 112 | 0 |  |  |  |  |  | expires => time() + ($$cookie_details{days_to_live} * 86400) | 
| 113 |  |  |  |  |  |  | }; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | 1; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 NAME | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Pepper::PlackHandler | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | This package receives and packs the PSGI (~ CGI) environment for the Pepper quick-start | 
| 125 |  |  |  |  |  |  | kit.  The main Pepper object will create this object and execute pack_psgi_variables() | 
| 126 |  |  |  |  |  |  | automatically, placing the PSGI/Plack parameters appropriately. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | Please execute 'pepper help' in your shell for more details on what is available. |