You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Miguel Rentes <mi...@efacec.pt> on 2008/06/03 12:52:54 UTC

[users@httpd] Running cgi that uses SQLDriverConnect (unixODC) makes Apache throw Error 500

Hi everyone!

I'm trying to create a .cgi that makes a connection to Oracle Database 
using unixODBC (in a Red Hat Enterprise Linux 4 machine).

I've successfully compiled Apache (APR 1.2.12, APR-Util 1.2.12, HTTPD 
2.2.8), unixODBC (2.2.12) and PHP (5.2.6) but when I try to run the cgi 
from the web browser I get "Internal Server Error". If I run the same 
cgi from command line it works wih no errors.

I don't know why Apache is giving me Error 500 but if I run the .cgi 
from command line everything works fine.

My .cgi is just doing a simple select (using SQLDriverConnect) and then 
it disconnects.

 >From apache logs I can see:

[Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62] Debug: 
connect_odbc.c [connect_odbc] - ODBC string 
'DSN=SXDB;UID=scatex;PWD=Scatex;', referer: 
http://172.18.200.153/cgi-bin/get_ret.cgi
[Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62] connection 
string is: DSN=SXDB;UID=scatex;PWD=Scatex;, referer: 
http://172.18.200.153/cgi-bin/get_ret.cgi
[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

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

Any help would be very appreciated. I have no more clues or ideas to 
solve this right now...

Best regards,

Miguel Rentes

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

Posted by Miguel Rentes <mi...@efacec.pt>.
Hi Krist,

The problem was in httpd.conf. I added the "PassEnv ORACLE_HOME" 
directive and my .cgi code (listed bellow) worked fine.


Thanks for everything,

Miguel Rentes

Miguel Rentes wrote:
> Hi Krist,
>
>
> Krist van Besien wrote:
>>
>> On Tue, Jun 3, 2008 at 12:52 PM, Miguel Rentes 
>> <mi...@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

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

Posted by Miguel Rentes <mi...@efacec.pt>.
Hi Krist,


Krist van Besien wrote:
>
> On Tue, Jun 3, 2008 at 12:52 PM, Miguel Rentes 
> <mi...@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

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

Posted by Krist van Besien <kr...@gmail.com>.
On Tue, Jun 3, 2008 at 12:52 PM, Miguel Rentes <mi...@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?


> 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?

Krist

-- 
krist.vanbesien@gmail.com
krist@vanbesien.org
Bremgarten b. Bern, Switzerland
--
A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org