# -------------------------------------------------------------------------------------
# flo::HTTP::Header
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@cisedi.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden.
#
#    Description:
#
#      A class that is (mainly) used by the flo::Template to set HTTP headers
#
# -------------------------------------------------------------------------------------
package flo::HTTP::Header;
use flo::Standard;
use strict;
use vars qw /$ACCESSOR/;
$ACCESSOR = undef;


##
# $class->new;
# ------------
#   Constructs a new flo::HTTP::Header. This should be done once
#   per query, other bits of code accessing the object via
#   flo::HTTP::Header::Accessor
##
sub new
{
    return $ACCESSOR if (defined $ACCESSOR);
    my $class = shift;
    $class = ref $class || $class;
    $ACCESSOR = bless { @_ }, $class;
    return $ACCESSOR;
}


##
# $self->set ($name, $value);
# ---------------------------
#   i.e. $flo::HTTP::Header::ACCESSOR->set ('Content-Type', 'text/plain');
##
sub set
{
    my $self = shift;
    my $line = shift;
    my ($key, $val) = $line =~ /^\s*(.*?)\s*:\s*(.*?)\s*$/;
    if (defined $key and defined $val) { $self->{$key} = $val }
}


##
# $self->header;
# --------------
#   Returns the HTTP headers string representing that flo::HTTP::Header object
##
sub header
{
    my $self = shift;
    return (join "\n", map { $_ . ": " . $self->{$_} } sort keys %{$self}) . "\n\n";
}


##
# $self->data ($tmpl);
# --------------------
#   Deprecated - Returns HTTP headers + data of the parsed template
##
sub data
{
    my $class  = shift;
    my $tmpl   = shift;
    my $compress = shift;
    my $header = new $class;
    my $data   = $tmpl->parse;
    $header->set ("Content-Length: " . length ($data));
    return $header->header . $data;
}


##
# $self->data ($out);
# -------------------
#   Deprecated - STDIN prints HTTP headers + data of the parsed template
##
sub out
{
    my $class  = shift;
    my $tmpl   = shift;
    my $compress = shift;
    print $class->data ($tmpl, $compress);
}


1;
