File Coverage

blib/lib/Parse/WWWAuthenticate.pm
Criterion Covered Total %
statement 40 40 100.0
branch 8 8 100.0
condition 9 9 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 66 66 100.0


line stmt bran cond sub pod time code
1             package Parse::WWWAuthenticate;
2              
3             # ABSTRACT: Parse the WWW-Authenticate HTTP header
4              
5 1     1   83846 use strict;
  1         11  
  1         30  
6 1     1   6 use warnings;
  1         2  
  1         27  
7              
8 1     1   5 use base 'Exporter';
  1         1  
  1         90  
9              
10 1     1   7 use Carp qw(croak);
  1         2  
  1         45  
11 1     1   503 use HTTP::Headers::Util qw(_split_header_words);
  1         967  
  1         438  
12              
13             our $VERSION = '0.04';
14              
15             our @EXPORT_OK = qw(parse_wwwa);
16              
17             sub parse_wwwa {
18 32     32 1 39544 my ($string) = @_;
19              
20 32         66 my @parts = split_header_words( $string);
21              
22 32         53 my $challenge;
23             my @challenges;
24              
25             PART:
26 32         53 for my $part ( @parts ) {
27 58         74 my ($maybe_challenge, $challenge_check) = @{$part};
  58         131  
28              
29 58 100       123 if ( !defined $challenge_check ) {
30 41         121 $challenge = ucfirst lc $maybe_challenge;
31 41         120 push @challenges, { name => $challenge, params => {} };
32             }
33              
34 58         123 my ($key, $value) = ($part->[-2], $part->[-1]);
35 58 100       141 if ( !defined $value ) {
36 5         20 next PART;
37             }
38              
39 53         86 my $lc_key = lc $key;
40 53 100 100     185 if ( $challenge eq 'Basic' &&
      100        
41             $lc_key eq 'realm' &&
42             exists $challenges[-1]->{params}->{$lc_key}
43             ) {
44 1         10 croak 'only one realm is allowed';
45             }
46              
47 52         159 $challenges[-1]->{params}->{lc $key} = $value;
48             }
49              
50 31         70 for my $challenge ( @challenges ) {
51 40 100 100     131 if ( $challenge->{name} eq 'Basic' && !exists $challenge->{params}->{realm} ) {
52 1         30 croak 'realm parameter is missing';
53             }
54             }
55              
56 30         145 return @challenges;
57             }
58              
59             sub split_header_words {
60 32     32 1 72 my @res = &_split_header_words;
61 32         1946 for my $arr (@res) {
62 58         146 for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
63 94         225 $arr->[$i] = $arr->[$i];
64             }
65             }
66 32         60 return @res;
67             }
68              
69             1;
70              
71             __END__