CGI/C++ Problem with Microsoft IIS

Discussion in 'C++' started by Steve B, Sep 1, 2005.

  1. Steve B

    Steve B Guest

    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
    Steve B, Sep 1, 2005
    #1
    1. Advertising

  2. Steve B wrote:
    > 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
    Victor Bazarov, Sep 1, 2005
    #2
    1. Advertising

  3. Steve B

    Default User Guest

    Steve B wrote:

    > 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
    Default User, Sep 1, 2005
    #3
  4. Steve B wrote:
    > 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
    John Harrison, Sep 1, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,330
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    533
    SpamProof
    Oct 21, 2003
  3. Dennis
    Replies:
    1
    Views:
    119
    [MSFT]
    Feb 21, 2005
  4. Luigi Ma
    Replies:
    0
    Views:
    124
    Luigi Ma
    Nov 21, 2007
  5. BestFriend
    Replies:
    2
    Views:
    746
Loading...

Share This Page