PRB: CGI Application Hangs When Reading Data Posted from Browser

ID: Q189280

The information in this article applies to:

SYMPTOMS

When data is posted from the Internet Explorer to a CGI application, the browser will display an hourglass. At some point, the browser will time out and display an error message indicating that it has done so.

CAUSE

The behavior of the Internet Explorer was changed with Internet Explorer 4.0 to more closely reflect the HTTP specification (RFC 2068). This comment is taken from RFC 2068.

NOTE: Certain buggy HTTP/1.0 client implementations generate an extra CRLFs after a POST request. To restate what is explicitly forbidden by the BNF, an HTTP/1.1 client must not preface or follow a request with an extra CRLF.

CGI applications that rely on the CRLF will not work correctly with Internet Explorer 4.0. The following code is an example that will fail:

   if (!gets(cgiinput)) {
      printf("Couldn't read CGI input from STDIN.\n") ;
      exit(1) ;
   }

Data posted to a CGI application is read by the CGI application from STDIN. The gets() call above causes the CGI application to hang as it is dependant on the CRLF.

RESOLUTION

A well-behaved CGI application will read exactly the number of bytes specified by the browser in the content-length header. The following code behaves correctly:

   if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) {
      printf("No Content-Length was sent with the POST request.\n") ;
      exit(1) ;
   }

   if (!fread(cgiinput, content_length, 1, stdin)) {
      printf("Couldn't read CGI input from STDIN.\n") ;
      exit(1) ;
   }

STATUS

This behavior is by design.

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Robert Duke, Microsoft Corporation

Additional query words:

Keywords          : iisscript 
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: July 11, 1998