//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/http
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	phil at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 112 columns wide.
//-----------------------------------------------------------------------------

#define EXPORT_COMMON_GLOBAL_DATA

#include "http_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	http_commit_put
//
// purpose	Commit the HTTP headers and output them to stdout.
//
// arguments	-none-
//
// returns	(int) -1 : error
//		(int)  0 : OK
//		(int)  1 : already done
//-----------------------------------------------------------------------------
int
http_commit_put()
__PROTO_END__
{
    const char *	header_str	;


    //-- Do not do this twice.
    if ( http_done ) return 1;

    //-- If no response code is given, use 200 OK.
    if ( ! http_response ) http_response = HTTP_RESPONSE_200;

    //-- Output HTTP response (full mode) or Status header.
    fputs( http_full_mode ? "HTTP/1.1 " : "Status: ", stdout );
    fputs( http_response, stdout );
    fputc( '\r', stdout );
    fputc( '\n', stdout );

    //-- Output each queued HTTP header in priority order.
    if ( http_header_map ) {
	map_loop_forward( http_header_map ) {
	    if ( ( header_str = map_str_fetch_ptr( http_header_map ) ) ) {
		fputs( header_str, stdout );
		fputs( "\r\n", stdout );
	    }
	}
	map_destroy( http_header_map );
	http_header_map = NULL;
    } else {
	fputs( "Content-type: text/plain\r\n", stdout );
    }
    fputs( "\r\n", stdout );
    fflush( stdout );

    //-- We have now done all HTTP.
    http_done = 1;
    return 0;
}

