Hi Krist,


Krist van Besien wrote:
Re: [users@httpd] Running cgi that uses SQLDriverConnect (unixODC) makes Apache throw Error 500

On Tue, Jun 3, 2008 at 12:52 PM, Miguel Rentes <miguel.rentes@efacec.pt> wrote:

> [Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62] Premature end of
> script headers: lista_alarmes.cgi, referer:
> http://172.18.200.153/cgi-bin/get_ret.cgi

youd CGI must start by writing a "content-type" header, followed by a
blank line. Is this the case?

Yes, my .cgi starts by writing this header (see below).


> I can only think that Apache doesn't know how to execute SQLDriverConnect.
> But if this is true,how do I make it work?

It could be that the user apache runs under is unable to locate some
binary or library needed for this script. Compare your environment
with that of apache. What language has the script been written in?

It is written in C. I think the environment is not the problem because LD_LIBRARY_PATH points to the same search paths as the user in which I run the .cgi from the command line. This is my simple .cgi code:

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>


void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type)
{
    SQLINTEGER     i = 0;
    SQLINTEGER     native;
    SQLCHAR     state[ 7 ];
    SQLCHAR     text[256];
    SQLSMALLINT     len;
    SQLRETURN     ret;

    memset(text,0,256);

    fprintf(stderr,
            "\n"
            "The driver reported the following diagnostics whilst running "
            "%s\n\n",
            fn);

    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len );
        if (!SQL_SUCCEEDED(ret))
            printf("%s:%ld:%ld:%s\n", state, i, native, text);
    }
    while( ret == SQL_SUCCESS );

}



main() {
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; /* ODBC API return status */
    SQLSMALLINT columns; /* number of columns in result-set */
    int row = 0;

//MLR - cgi stuff
printf("Content-Type: text/html\n\n");
printf("<html>\n");
printf("<head>");
printf("<link rel=\"stylesheet\" href=\"../styles.css\" type=\"text/css\">");

/* Allocate an environment handle */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
/* We want ODBC 3 support */
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
/* Allocate a connection handle */
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
/* Connect to the DSN mydsn */
/* You will need to change mydsn to one you have created and tested */
SQLDriverConnect(dbc, NULL, "DSN=SXDB;PWD=Scatex;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
/* Allocate a statement handle */
SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);

/* Retrieve a list of tables */
SQLTables(stmt, NULL, 0, "SCATEX", SQL_NTS, "MEDIDAS", SQL_NTS, NULL, 0);
/* How many columns are there */
SQLNumResultCols(stmt, &columns);
/* Loop through the rows in the result-set */
while (SQL_SUCCEEDED(ret = SQLFetch(stmt))) {
    SQLUSMALLINT i;
    printf("Row %d\n", row++);
    /* Loop through the columns */
    for (i = 1; i <= columns; i++) {
        SQLINTEGER indicator;
        char buf[512];
        /* retrieve column data as a string */
    ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
        if (SQL_SUCCEEDED(ret)) {
            /* Handle null columns */
            if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
        printf("  Column %u : %s\n", i, buf);
        } else {
        extract_error("SQLAllocHandle for dbc", env, SQL_HANDLE_ENV);
                exit(-1);
    }
    }
}

/* Free all the handles */
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
/* disconnect */
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);


printf("</head>");
printf("<body bgcolor=\"#CCCCCC\" align=\"center\" class=\"fText\">\n");

printf("</body>\n</html>\n");

}

Any ideas on what can be the problem would be very appreciated.

Best regards,

Miguel Rentes