Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.1 KiB

  1. package HTTP::Headers::Auth;
  2. use strict;
  3. use vars qw($VERSION);
  4. $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
  5. require HTTP::Headers;
  6. package HTTP::Headers;
  7. BEGIN {
  8. # we provide a new (and better) implementations below
  9. undef(&www_authenticate);
  10. undef(&proxy_authenticate);
  11. }
  12. require HTTP::Headers::Util;
  13. sub _parse_authenticate
  14. {
  15. my @ret;
  16. for (HTTP::Headers::Util::split_header_words(@_)) {
  17. if (!defined($_->[1])) {
  18. # this is a new auth scheme
  19. push(@ret, lc(shift @$_) => {});
  20. shift @$_;
  21. }
  22. if (@ret) {
  23. # this a new parameter pair for the last auth scheme
  24. while (@$_) {
  25. my $k = lc(shift @$_);
  26. my $v = shift @$_;
  27. $ret[-1]{$k} = $v;
  28. }
  29. } else {
  30. # something wrong, parameter pair without any scheme seen
  31. # IGNORE
  32. }
  33. }
  34. @ret;
  35. }
  36. sub _authenticate
  37. {
  38. my $self = shift;
  39. my $header = shift;
  40. my @old = $self->_header($header);
  41. if (@_) {
  42. $self->remove_header($header);
  43. my @new = @_;
  44. while (@new) {
  45. my $a_scheme = shift(@new);
  46. if ($a_scheme =~ /\s/) {
  47. # assume complete valid value, pass it through
  48. $self->push_header($header, $a_scheme);
  49. } else {
  50. my @param;
  51. if (@new) {
  52. my $p = $new[0];
  53. if (ref($p) eq "ARRAY") {
  54. @param = @$p;
  55. shift(@new);
  56. } elsif (ref($p) eq "HASH") {
  57. @param = %$p;
  58. shift(@new);
  59. }
  60. }
  61. my $val = ucfirst(lc($a_scheme));
  62. if (@param) {
  63. my $sep = " ";
  64. while (@param) {
  65. my $k = shift @param;
  66. my $v = shift @param;
  67. if ($v =~ /[^0-9a-zA-Z]/ || lc($k) eq "realm") {
  68. # must quote the value
  69. $v =~ s,([\\\"]),\\$1,g;
  70. $v = qq("$v");
  71. }
  72. $val .= "$sep$k=$v";
  73. $sep = ", ";
  74. }
  75. }
  76. $self->push_header($header, $val);
  77. }
  78. }
  79. }
  80. return unless defined wantarray;
  81. wantarray ? _parse_authenticate(@old) : join(", ", @old);
  82. }
  83. sub www_authenticate { shift->_authenticate("WWW-Authenticate", @_) }
  84. sub proxy_authenticate { shift->_authenticate("Proxy-Authenticate", @_) }
  85. 1;