Debian Codegraph @

lib/HTTP/Headers/Auth.pm

raw
1 package HTTP::Headers::Auth;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '7.02';
7
8 use HTTP::Headers;
9
10 package
11 HTTP::Headers;
12
13 BEGIN {
14 # we provide a new (and better) implementations below
15 undef(&www_authenticate);
16 undef(&proxy_authenticate);
17 }
18
19 require HTTP::Headers::Util;
20
21 sub _parse_authenticate
22 {
23 my @ret;
24 for (HTTP::Headers::Util::split_header_words(@_)) {
25 if (!defined($_->[1])) {
26 # this is a new auth scheme
27 push(@ret, shift(@$_) => {});
28 shift @$_;
29 }
30 if (@ret) {
31 # this a new parameter pair for the last auth scheme
32 while (@$_) {
33 my $k = shift @$_;
34 my $v = shift @$_;
35 $ret[-1]{$k} = $v;
36 }
37 }
38 else {
39 # something wrong, parameter pair without any scheme seen
40 # IGNORE
41 }
42 }
43 @ret;
44 }
45
46 sub _authenticate
47 {
48 my $self = shift;
49 my $header = shift;
50 my @old = $self->_header($header);
51 if (@_) {
52 $self->remove_header($header);
53 my @new = @_;
54 while (@new) {
55 my $a_scheme = shift(@new);
56 if ($a_scheme =~ /\s/) {
57 # assume complete valid value, pass it through
58 $self->push_header($header, $a_scheme);
59 }
60 else {
61 my @param;
62 if (@new) {
63 my $p = $new[0];
64 if (ref($p) eq "ARRAY") {
65 @param = @$p;
66 shift(@new);
67 }
68 elsif (ref($p) eq "HASH") {
69 @param = %$p;
70 shift(@new);
71 }
72 }
73 my $val = ucfirst(lc($a_scheme));
74 if (@param) {
75 my $sep = " ";
76 while (@param) {
77 my $k = shift @param;
78 my $v = shift @param;
79 if ($v =~ /[^0-9a-zA-Z]/ || lc($k) eq "realm") {
80 # must quote the value
81 $v =~ s,([\\\"]),\\$1,g;
82 $v = qq("$v");
83 }
84 $val .= "$sep$k=$v";
85 $sep = ", ";
86 }
87 }
88 $self->push_header($header, $val);
89 }
90 }
91 }
92 return unless defined wantarray;
93 wantarray ? _parse_authenticate(@old) : join(", ", @old);
94 }
95
96
97 sub www_authenticate { shift->_authenticate("WWW-Authenticate", @_) }
98 sub proxy_authenticate { shift->_authenticate("Proxy-Authenticate", @_) }
99
100 1;
101
102 __END__
103
104 =pod
105
106 =encoding UTF-8
107
108 =head1 NAME
109
110 HTTP::Headers::Auth
111
112 =head1 VERSION
113
114 version 7.02
115
116 =head1 AUTHOR
117
118 Gisle Aas <[email protected]>
119
120 =head1 COPYRIGHT AND LICENSE
121
122 This software is copyright (c) 1994 by Gisle Aas.
123
124 This is free software; you can redistribute it and/or modify it under
125 the same terms as the Perl 5 programming language system itself.
126
127 =cut