File Coverage

blib/lib/SockJS/Middleware/Cors.pm
Criterion Covered Total %
statement 26 29 89.6
branch 5 10 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package SockJS::Middleware::Cors;
2              
3 1     1   66224 use strict;
  1         11  
  1         29  
4 1     1   5 use warnings;
  1         2  
  1         27  
5              
6 1     1   427 use parent 'Plack::Middleware';
  1         296  
  1         6  
7              
8 1     1   14360 use Plack::Util;
  1         2  
  1         227  
9              
10             sub call {
11 1     1 1 1243 my $self = shift;
12 1         2 my ($env) = @_;
13              
14 1         7 my $res = $self->app->(@_);
15              
16             return $self->response_cb(
17             $res => sub {
18 1     1   40 my $res = shift;
19              
20 1         5 my $h = Plack::Util::headers( $res->[1] );
21              
22 1         37 my $origin = $env->{HTTP_ORIGIN};
23 1 50       5 $origin = '' unless defined $origin;
24              
25 1 50       7 my %cors_headers = (
26             'Access-Control-Allow-Origin' => ( $origin eq '' )
27             ? '*'
28             : $origin,
29             'Access-Control-Allow-Credentials' => 'true'
30             );
31              
32 1 50       4 if ( my $request_headers =
33             $env->{HTTP_ACCESS_CONTROL_REQUEST_HEADERS} )
34             {
35 0         0 $cors_headers{'Access-Control-Allow-Headers'} =
36             $request_headers;
37             }
38              
39 1 50       4 if ( my $allowed_methods = $env->{'sockjs.allowed_methods'} ) {
40 0         0 $cors_headers{'Access-Control-Allow-Methods'} =
41             join( ', ', @$allowed_methods );
42             }
43              
44 1 50       4 if ($env->{'sockjs.cacheable'}) {
45 0         0 $cors_headers{'Access-Control-Max-Age'} = '31536000';
46             }
47              
48 1         5 foreach my $header ( keys %cors_headers ) {
49 2         41 $h->push( $header => $cors_headers{$header} );
50             }
51             }
52 1         52 );
53             }
54              
55             1;