Skip to content

Commit

Permalink
Merge 64ee405 into db4148f
Browse files Browse the repository at this point in the history
  • Loading branch information
ap committed Feb 7, 2015
2 parents db4148f + 64ee405 commit 29fd825
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions lib/Plack/Util.pm
Expand Up @@ -171,29 +171,35 @@ sub header_iter {
sub header_get {
my($headers, $key) = (shift, lc shift);

my @val;
header_iter $headers, sub {
push @val, $_[1] if lc $_[0] eq $key;
};
if (wantarray) {
return map {
my $i = $_+$_;
$key eq lc $headers->[$i] ? $headers->[$i+1] : ();
} 0 .. $#$headers/2;
}

return wantarray ? @val : $val[0];
for (0 .. $#$headers/2) {
my $i = $_+$_;
return $headers->[$i+1] if $key eq lc $headers->[$i];
}

();
}

sub header_set {
my($headers, $key, $val) = @_;

my($set, @new_headers);
header_iter $headers, sub {
if (lc $key eq lc $_[0]) {
return if $set;
$_[1] = $val;
$set++;
}
push @new_headers, $_[0], $_[1];
};
my($_key, $set) = lc $key;
# if $set gets set it is always odd so always true, and it will always
# point into the segment before the first "hole", where indices are stable

my @keep = map {
my $i = $_+$_;
$_key ne lc $headers->[$i] ? ($i, $i+1) : $set ? () : ($i, $set = $i+1);
} 0 .. $#$headers/2;

push @new_headers, $key, $val unless $set;
@$headers = @new_headers;
@$headers = @$headers[@keep];
$set ? $headers->[$set] = $val : push @$headers, $key, $val;
}

sub header_push {
Expand All @@ -204,24 +210,23 @@ sub header_push {
sub header_exists {
my($headers, $key) = (shift, lc shift);

my $exists;
header_iter $headers, sub {
$exists = 1 if lc $_[0] eq $key;
};
for (0 .. $#$headers/2) {
return 1 if $key eq lc $headers->[$_+$_];
}

return $exists;
return !1;
}

sub header_remove {
my($headers, $key) = (shift, lc shift);

my @new_headers;
header_iter $headers, sub {
push @new_headers, $_[0], $_[1]
unless lc $_[0] eq $key;
};
my @keep = map {
my $i = $_+$_;
$key ne lc $headers->[$i] ? ($i, $i+1) : ()
} 0 .. $#$headers/2;

@$headers = @new_headers;
@$headers = @$headers[@keep];
0+@$headers;
}

sub status_with_no_entity_body {
Expand Down

0 comments on commit 29fd825

Please sign in to comment.