//-----------------------------------------------------------------------------
// 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
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 112 columns wide.
//-----------------------------------------------------------------------------

#include "http_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	http_redirect
//
// purpose	Redirect HTTP request to a specified URL.
//
// arguments	1 (const char *) format string for URL
//		2..N (...) variable arguments for formatting URL
//
// returns	(int) -1 : error
//		(int)  0 : OK
//-----------------------------------------------------------------------------
int
http_redirect (
    const char *	arg_url_format
    ,
    ...
    )
__PROTO_END__
{
    va_list		more_args	;
    char		url_string	[ HTTP_URL_MAX_SIZE + 1 ];

    //-- If HTTP is already done, return an error.
    if ( http_done ) return -1;

    //-- Build redirection URL.
    va_start( more_args, arg_url_format );
    vsnprintf( url_string, sizeof (url_string), arg_url_format, more_args );
    va_end( more_args );

    //-- Response code for 301 Moved Permanently.
    http_response_str( HTTP_RESPONSE_301 );
    http_header( -128, "Location", url_string, NULL );
    http_date_now();
    http_header( 0, "Content-Type", "text/html", NULL );
    http_commit_put();

    printf( "<html>\n"
	    "<head><title>Redirect</title></head>\n"
	    "<body bgcolor=white text=black link=blue vlink=blue alink=red>\n"
	    "Your web browser should jump to <b><a href=\"%s\">%s</a></b>.<br>\n"
	    "If not, please click <b><a href=\"%s\">here</a></b> to proceed.\n"
	    "</body>\n"
	    "</html>\n",
	    url_string,
	    url_string,
	    url_string
	);
    fflush( stdout );

    return 0;
}

