File Coverage

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


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   22029 use strict;
  1         2  
  1         22  
6 1     1   3 use warnings;
  1         1  
  1         19  
7              
8 1     1   2 use base 'Exporter';
  1         6  
  1         52  
9              
10 1     1   3 use Carp qw(croak);
  1         1  
  1         39  
11 1     1   375 use HTTP::Headers::Util qw(_split_header_words);
  1         577  
  1         263  
12              
13             our $VERSION = 0.03;
14              
15             our @EXPORT_OK = qw(parse_wwwa);
16              
17             sub parse_wwwa {
18 32     32 1 20009 my ($string) = @_;
19              
20 32         43 my @parts = split_header_words( $string);
21              
22 32         22 my $challenge;
23             my @challenges;
24              
25             PART:
26 32         28 for my $part ( @parts ) {
27 58         41 my ($maybe_challenge, $challenge_check) = @{$part};
  58         78  
28              
29 58 100       89 if ( !defined $challenge_check ) {
30 41         64 $challenge = ucfirst lc $maybe_challenge;
31 41         70 push @challenges, { name => $challenge, params => {} };
32             }
33              
34 58         63 my ($key, $value) = ($part->[-2], $part->[-1]);
35 58 100       69 if ( !defined $value ) {
36 5         7 next PART;
37             }
38              
39 53         42 my $lc_key = lc $key;
40 53 100 100     150 if ( $challenge eq 'Basic' &&
      66        
41             $lc_key eq 'realm' &&
42             exists $challenges[-1]->{params}->{$lc_key}
43             ) {
44 1         13 croak 'only one realm is allowed';
45             }
46              
47 52         88 $challenges[-1]->{params}->{lc $key} = $value;
48             }
49              
50 31         29 for my $challenge ( @challenges ) {
51 40 100 100     120 if ( $challenge->{name} eq 'Basic' && !exists $challenge->{params}->{realm} ) {
52 1         18 croak 'realm parameter is missing';
53             }
54             }
55              
56 30         77 return @challenges;
57             }
58              
59             sub split_header_words {
60 32     32 1 50 my @res = &_split_header_words;
61 32         1090 for my $arr (@res) {
62 58         111 for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
63 94         144 $arr->[$i] = $arr->[$i];
64             }
65             }
66 32         44 return @res;
67             }
68              
69             1;
70              
71             __END__