/*
 * request_ap.c -- get request data from apaches request object
 * nca-073-9
 *
 * Copyright (c) 1996-2000 by Netcetera AG.
 * Copyright (c) 2001 by Apache Software Foundation.
 * All rights reserved.
 *
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * @(#) $Id: request_ap.c 330538 2005-11-03 12:41:34Z ronnie $
 *
 */

#include "tcl.h"
#include "hashutl.h"
#include "webutl.h"
#include "request.h"
#include "paramlist.h"

#include "mod_websh.h"

#ifdef APACHE2
#include "apr_strings.h"
#endif

/* ----------------------------------------------------------------------------
 * web::request -channel: where input for request obj comes from
 * ------------------------------------------------------------------------- */
Tcl_Obj *requestGetDefaultChannelName()
{
    return Tcl_NewStringObj(APCHANNEL, -1);
}

/* default output channel */

char *requestGetDefaultOutChannelName()
{
    return APCHANNEL;
}

int requestFillRequestValues(Tcl_Interp * interp, RequestData * requestData)
{

    int res = 0;
    /*Tcl_Obj *reso = NULL;*/
    request_rec *r = NULL;
#ifndef APACHE2
    array_header *hdrs_arr = NULL;
    table_entry *hdrs = NULL;
#else /* APACHE2 */
    apr_array_header_t *hdrs_arr = NULL;
    apr_table_entry_t *hdrs = NULL;
#endif /* APACHE2 */
    int i = 0;

    if (requestData->requestIsInitialized)
        return TCL_OK;
    requestData->requestIsInitialized = 1;

    if (interp == NULL)
        return TCL_ERROR;

    /* fetch request object */
    r = (request_rec *) Tcl_GetAssocData(interp, WEB_AP_ASSOC_DATA, NULL);
    if (r == NULL) {
        Tcl_SetResult(interp, "error accessing httpd request object", NULL);
        return TCL_ERROR;
    }

    /*reso = Tcl_NewObj();*/

#ifndef APACHE2
    hdrs_arr = ap_table_elts(r->subprocess_env);
    hdrs = (table_entry *) hdrs_arr->elts;
#else /* APACHE2 */
    hdrs_arr = (apr_array_header_t *) apr_table_elts(r->subprocess_env);
    hdrs = (apr_table_entry_t *) hdrs_arr->elts;
#endif /* APACHE2 */
    for (i = 0; i < hdrs_arr->nelts; ++i) {

        Tcl_Obj *valo = NULL;
        if (!hdrs[i].key)
            continue;

        if (!hdrs[i].val)
            valo = Tcl_NewObj();
        else
            valo = Tcl_NewStringObj(hdrs[i].val, -1);

        if (paramListAdd(requestData->request, hdrs[i].key, valo) != TCL_OK)
            /* fatal case */
            return TCL_ERROR;
    }

    paramListSetAsWhole(requestData->request, "GATEWAY_INTERFACE",
                        Tcl_NewStringObj("CGI-websh/1.1", -1));

    {
      /* added to make auth data available */
      char *val;
      Tcl_Obj *valo = NULL;

      /* Check to see if a Authorization header is there */
#ifndef APACHE2
      val = (char *)ap_table_get( r->headers_in, "Authorization" );
#else /* APACHE2 */
      val = (char *)apr_table_get( r->headers_in, "Authorization" );
#endif
      if (val) {
       valo =  Tcl_NewStringObj(val, -1);
       if (paramListAdd(requestData->request, "AUTHORIZATION", valo) != TCL_OK)
         /* fatal case */
         return TCL_ERROR;
      }
    }
    return TCL_OK;
}

