Previous Next

Thread: IIS unable to read Stream include CR LF with STDIN (C/C++)

Last post 03-19-2008 12:45 PM by anilr. 3 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (4 items)

Sort Posts:

  • 01-10-2008, 5:32 PM

    • monoid
    • Not Ranked
    • Joined on 01-10-2008, 9:54 PM
    • Posts 4
    • monoid

    IIS unable to read Stream include CR LF with STDIN (C/C++)

    I have developed a Webservice in C++, which receive streams from STDIN via POST and saves it to a file.

    If there is no CR LF in the stream, IIS read the stream from stdin perfectly on IIS webserver. But if there is a CR LF inside the stream, the following code example hang on. On Apache Webserver it works always good.

    int main(void)

    {

    size_t readbytes;

    long content_length = 0;

    content_length = atol(getenv("CONTENT_LENGTH"));

    char * tmp = NULL;

    tmp = (char*)malloc(1);

    tmp[0]='\0';

    tmp = (char*)realloc(tmp,content_length + 1);

    readbytes = fread(tmp,1,content_length,stdin);

    readbytes[content_length]='\0';

    return 0;

    }

    Does anyone have an idea, that it will not work with a stream including CR LF on IIS?`

    Thank you for your help!

     

  • 01-14-2008, 1:40 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 988
    • anilr

    Re: IIS unable to read Stream include CR LF with STDIN (C/C++)

    You probably need to call _setmode(_fileno(stdin), _O_BINARY) - otherwise the crt routine will convert CRLF to just LF and then you have less than Content-Length bytes available, so the read would never complete.

    Also, unless you need to maintain this code for non-windows systems, I would use win32 APIs rather than crt APIs for system type stuff like this - the crt APIs on windows have lot of quirks which are not fun when you come across them.

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
  • 03-19-2008, 6:55 AM In reply to

    • monoid
    • Not Ranked
    • Joined on 01-10-2008, 9:54 PM
    • Posts 4
    • monoid

    Re: IIS unable to read Stream include CR LF with STDIN (C/C++)

    Hi, thanks for you answer. I hava found a article by Microsoft:

     

    http://support.microsoft.com/kb/203298/en-us

     But the article isnt ok. It should be a solution for the CRLF Problem on IIS.

    This should be the solution:

      int main(int argc, char* argv[])
       {
          char*   pCL = getenv("CONTENT_LENGTH");
          if(pCL != NULL)
          {
             int   nCL = atoi(pCL);
             if(nCL > 0)
             {
                // WARNING:  Error checking removed for clarity...
                printf("Content-Type: text/html\r\n\r\n");
                _setmode( _fileno( stdin ), _O_BINARY );  
                char* szBuf = new char[nCL + 1];
                fread(szBuf + nRead, sizeof(char), nCL-nRead, stdin);
                szBuf[nCL] = '\0';
                // Do something with szBuf...
                delete [] szBuf;
             }
          }
          return 0;
       }

     

    But what is "nRead"?

    I hope, somebody could help me! Its very urgent.

  • 03-19-2008, 12:45 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 988
    • anilr

    Re: IIS unable to read Stream include CR LF with STDIN (C/C++)

    What the sample is missing is that you have to call fread in a loop until nCL bytes have been read (since fread can return before reading all the bytes) - the return value of fread will give you number of bytes read so far - which is what nRead is supposed to be.

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
Page 1 of 1 (4 items)
Page view counter