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   67519 use strict;
  1         12  
  1         30  
4 1     1   5 use warnings;
  1         2  
  1         28  
5              
6 1     1   451 use parent 'Plack::Middleware';
  1         303  
  1         6  
7              
8 1     1   15032 use Plack::Util;
  1         3  
  1         237  
9              
10             sub call {
11 1     1 1 1131 my $self = shift;
12 1         2 my ($env) = @_;
13              
14 1         6 my $res = $self->app->(@_);
15              
16             return $self->response_cb(
17             $res => sub {
18 1     1   36 my $res = shift;
19              
20 1         5 my $h = Plack::Util::headers( $res->[1] );
21              
22 1         38 my $origin = $env->{HTTP_ORIGIN};
23 1 50       4 $origin = '' unless defined $origin;
24              
25 1 50       12 my %cors_headers = (
26             'Access-Control-Allow-Origin' => ( $origin eq '' )
27             ? '*'
28             : $origin,
29             'Access-Control-Allow-Credentials' => 'true'
30             );
31              
32 1 50       5 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       3 if ($env->{'sockjs.cacheable'}) {
45 0         0 $cors_headers{'Access-Control-Max-Age'} = '31536000';
46             }
47              
48 1         4 foreach my $header ( keys %cors_headers ) {
49 2         43 $h->push( $header => $cors_headers{$header} );
50             }
51             }
52 1         51 );
53             }
54              
55             1;