Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 1st, 2005, 03:25 PM
Steve B
Guest
 
Posts: n/a
Default CGI/C++ Problem with Microsoft IIS

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


  #2  
Old September 1st, 2005, 03:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: CGI/C++ Problem with Microsoft IIS

Steve B wrote:[color=blue]
> 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?[/color]

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

V
  #3  
Old September 1st, 2005, 06:35 PM
Default User
Guest
 
Posts: n/a
Default Re: CGI/C++ Problem with Microsoft IIS

Steve B wrote:
[color=blue]
> 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.[/color]

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




Brian
  #4  
Old September 1st, 2005, 11:15 PM
John Harrison
Guest
 
Posts: n/a
Default Re: CGI/C++ Problem with Microsoft IIS

Steve B wrote:[color=blue]
> 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?
>[/color]

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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles