#!/usr/bin/perl

package SDH::HResponse;
use strict;
use warnings;

sub new {
	my $self = shift;
	my $class = ref($self) || $self;
	return bless({
		uh => { }, # unique headers
		shk => [ ], # serial header keys
		shv => [ ], # serial header values
		body => 0, # SDH::Doc
	}, $class);
}

sub body {
	my $self = shift;
	$self->{body} = shift if @_;
	return $self->{body};
}

# TODO verify this list somehow
our %multi_headers = (
	'Cache-Control' => undef,
	Connection => undef,
	Pragma => undef,
	Via => undef,
	Warning => undef,
);

sub head {
	my ($self, $key) = (shift, shift);
	if (exists($multi_headers{$key})) {
		if (@_ == 1) {
			my $val = shift;
			push @{$self->{shk}}, $key;
			push @{$self->{shv}}, $val;
		} elsif (@_ > 1) {
			push @{$self->{shk}}, $key foreach @_;
			push @{$self->{shv}}, $_ foreach @_;
		}
		return undef;
	} else {
		return $self->{uh}{$key} = shift;
	}
}

sub headers {
	my $self = shift;
	my @result;
	for (0 .. (@{$self->{shk}} - 1)) {
		push @result, $self->{shk}[$_];
		push @result, $self->{shv}[$_];
	}
	while (my ($key, $val) = each %{$self->{uh}}) {
		push @result, $key;
		push @result, $val;
	}
	return @result;
}

1;
