//-----------------------------------------------------------------------------
// 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 Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307	 USA
//-----------------------------------------------------------------------------
// package	libh/cgi
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh 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 120 columns wide.
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cgi_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	cgi_form_fetch_query_or_post
//
// purpose	Fetch web page form variables from the query string, or if
//		not present in the query string, then from the post content,
//		in a CGI conforming web server environment (such as Apache).
//
// arguments	1 (const char *) variable name requested
//		2 (const char *) return this if variable is absent
//		3 (size_t *) where to store length in case value is binary
//
// returns	(const char *) pointer to value string, or NULL
//
// note		Use of cgi_form_fetch group functions is mutually exclusive
//		with use of cgi_form_map() or cgi_form_parse() functions.
//
// note		If the browser passes the same variable name multiple times,
//		only one of the values will be returned.  Usually it will be
//		the first one, but not always.  For access to multiple
//		variable instances, use cgi_form_parse().
//-----------------------------------------------------------------------------
const char *
cgi_form_fetch_query_or_post (
    const char *	arg_name_str
    ,
    const char *	arg_default_absent
    ,
    size_t *		arg_value_len_ptr
    )
    __PROTO_END__
{
    MAP			this_map	;
    const char *	value_ptr	;
    size_t		value_len	;

    int			result		;

    //----------------------------
    // Initialize and check cgi_form_state.
    //----------------------------
    if ( cgi_form_state == 0 ) cgi_form_fetch_init();
    if ( cgi_form_state < 0 ) return NULL;

    //-----------------------------------------------
    // Look up the requested variable in the mapping.
    //-----------------------------------------------
    this_map = cgi_form_post_content_map;
    result = map_str_find_exact( cgi_form_post_content_map, arg_name_str );
    if ( result == -1 ) {
	this_map = cgi_form_query_string_map;
	result = map_str_find_exact( cgi_form_query_string_map, arg_name_str );
    }
    if ( result  < -1 ) {
	value_ptr = NULL;
	value_len = 0;
    }
    else if ( result == -1 ) {
	value_ptr = arg_default_absent;
	value_len = strlen( arg_default_absent );
    }	
    else {
	value_ptr = map_str_fetch_ptr( this_map );
	value_len = map_str_fetch_len( this_map );
    }

    //----------------
    // Return results.
    //----------------
    if ( arg_value_len_ptr ) * arg_value_len_ptr = value_len;
    return value_ptr;
}

