CGI/C++ Problem with Microsoft IIS

S

Steve B

Hi,

I've written a CGI program in C++ to handle form output via POST data. The
program works fine when Apache is the web server, but barfs when Microsoft
IIS is the web server. Here's the details.

The beginning of the program uses the following code to get the POST data.

int main()
{
// get the POST data string
int contentLength = atoi( getenv( "CONTENT_LENGTH" ) );
char* postString = new char[contentLength + 1];
string dataString;

string serverSoftware; // the web server software being used
( getenv( "SERVER_SOFTWARE" ) ? serverSoftware = ( char * ) ( getenv(
"SERVER_SOFTWARE" ) ) : serverSoftware = "" );

if ( contentLength )
{
// if ( serverSoftware.find( "IIS" ) != string::npos )
// {
// contentLength = contentLength - 1;
// }

cin.read( postString, contentLength );
postString[contentLength] = '\0';
dataString = postString;
}

.... rest of program ...

I've isolated exactly where the problem is with IIS -- it's the call to
cin.read. When using IIS and I uncomment the 4 lines in the if statement,
the call to cin.read works fine, but it doesn't read the last byte from the
POST data (which is critical). When these lines remain commented, the
program "hangs" -- I'm assuming because it is attempting to read past the
length of std input and just waits there indefinately.

When I run the above program with Apache, which works just fine with the 4
lines commented, here is some representative POST data that I output for
debugging purposes via the dataString variable:

quizFileName=..%2F098%2F098quiz.txt&C1=2&C2=4&C3=4&C4=2&C5=1&C6=2

After googling around for a day, I cannot find a reason for the difference
in behavior related to just using a different web server. Does anyone have
any ideas for me?

Thanks in advance,
Steve
 
V

Victor Bazarov

Steve said:
I've written a CGI program in C++ to handle form output via POST data. The
program works fine when Apache is the web server, but barfs when Microsoft
IIS is the web server. Here's the details.
[...]

After googling around for a day, I cannot find a reason for the difference
in behavior related to just using a different web server. Does anyone have
any ideas for me?

Doesn't seem to be a C++ _language_ problem. Please try to find more
appropriate newsgroup in the "microsoft.public." hierarchy.

V
 
D

Default User

Steve said:
Hi,

I've written a CGI program in C++ to handle form output via POST
data. The program works fine when Apache is the web server, but barfs
when Microsoft IIS is the web server. Here's the details.

It's unlikely you have a C++ problem, so you need a different
newsgroup. I'd start with comp.infosystems.www.authoring.cgi.




Brian
 
J

John Harrison

Steve said:
Hi,

I've written a CGI program in C++ to handle form output via POST data. The
program works fine when Apache is the web server, but barfs when Microsoft
IIS is the web server. Here's the details.

The beginning of the program uses the following code to get the POST data.

int main()
{
// get the POST data string
int contentLength = atoi( getenv( "CONTENT_LENGTH" ) );
char* postString = new char[contentLength + 1];
string dataString;

string serverSoftware; // the web server software being used
( getenv( "SERVER_SOFTWARE" ) ? serverSoftware = ( char * ) ( getenv(
"SERVER_SOFTWARE" ) ) : serverSoftware = "" );

if ( contentLength )
{
// if ( serverSoftware.find( "IIS" ) != string::npos )
// {
// contentLength = contentLength - 1;
// }

cin.read( postString, contentLength );
postString[contentLength] = '\0';
dataString = postString;
}

... rest of program ...

I've isolated exactly where the problem is with IIS -- it's the call to
cin.read. When using IIS and I uncomment the 4 lines in the if statement,
the call to cin.read works fine, but it doesn't read the last byte from the
POST data (which is critical). When these lines remain commented, the
program "hangs" -- I'm assuming because it is attempting to read past the
length of std input and just waits there indefinately.

When I run the above program with Apache, which works just fine with the 4
lines commented, here is some representative POST data that I output for
debugging purposes via the dataString variable:

quizFileName=..%2F098%2F098quiz.txt&C1=2&C2=4&C3=4&C4=2&C5=1&C6=2

After googling around for a day, I cannot find a reason for the difference
in behavior related to just using a different web server. Does anyone have
any ideas for me?

Yes the problem is that cin is not opened in binary mode. On some
platforms (e.g. Windows) conversion of line ending characters (which are
permitted because the stream is not open in binary mode) means that
exact counts of numbers of bytes to read are not accurate. Basically
Windows has converted at \r\n sequence into \n which means that you end
up with one less character thane xpected. This issue does not exist on Unix.

So, you question is perfectly legitimate for c.l.c++, the bad news
though is that there is no standard way to force cin into binary mode.
So your question has no answer in standard C++.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top