nsresult nsHTTPResponseListener::ParseHTTPHeader(nsIBufferInputStream* in, PRUint32 aLength, PRUint32 *aBytesRead)
nsresult nsHTTPServerListener::ParseHTTPHeader(nsIBufferInputStream* in, PRUint32 aLength, PRUint32 *aBytesRead)
nsresult nsHTTPResponseListener::ParseHTTPHeader(nsIBufferInputStream* in,                                                 PRUint32 aLength,                                                 PRUint32 *aBytesRead){  nsresult rv = NS_OK;  PRBool bFoundString;  PRUint32 offsetOfEnd, totalBytesToRead, actualBytesRead;  *aBytesRead = 0;  if (kMAX_HEADER_SIZE < mHeaderBuffer.Length()) {    // This server is yanking our chain...    return NS_ERROR_FAILURE;  }  //  // Read the header from the input buffer...  A header is terminated by   // a CRLF.  Header values may be extended over multiple lines by preceeding  // each extra line with linear white space...  //  do {    //    // If last character in the header string is a LF, then the header     // may be complete...    //    if (mHeaderBuffer.Last() == '\n' ) {      // This line is either LF or CRLF so the header is complete...      if (mHeaderBuffer.Length() <= 2) {          break;      }      rv = in->Search(" ", PR_FALSE, &bFoundString, &offsetOfEnd);      if (NS_FAILED(rv)) return rv;      if (bFoundString && offsetOfEnd >= aLength) bFoundString = PR_FALSE;      if (!bFoundString && offsetOfEnd == 0)           return NS_OK;     // Need to wait for more data to see if the header is complete      if (!bFoundString || offsetOfEnd != 0) {          // then check for tab too          rv = in->Search("\t", PR_FALSE, &bFoundString, &offsetOfEnd);          if (NS_FAILED(rv)) return rv;          if (bFoundString && offsetOfEnd >= aLength) bFoundString = PR_FALSE;          NS_ASSERTION(!(!bFoundString && offsetOfEnd == 0), "should have been checked above");          if (!bFoundString || offsetOfEnd != 0) {              break; // neither space nor tab, so jump out of the loop          }      }      // else, go around the loop again and accumulate the rest of the header...    }    // Look for the next LF in the buffer...    rv = in->Search("\n", PR_FALSE, &bFoundString, &offsetOfEnd);    if (NS_FAILED(rv)) return rv;    if (bFoundString && offsetOfEnd >= aLength) bFoundString = PR_FALSE;    if (!bFoundString) {      //      // The buffer contains a partial header.  Read the entire buffer       // and wait for more data...      //      totalBytesToRead = aLength;    } else {    // Do not forget to include the LF character in the read...      totalBytesToRead = offsetOfEnd+1;    }    // Append the buffer into the header string...    rv = in->ReadSegments(nsWriteToString,                           (void*)&mHeaderBuffer,                           totalBytesToRead,                           &actualBytesRead);    if (NS_FAILED(rv)) return rv;    *aBytesRead += actualBytesRead;    // Partial header - wait for more data to arrive...    if (!bFoundString) return NS_OK;  } while (PR_TRUE);  PR_LOG(gHTTPLog, PR_LOG_ALWAYS,          ("\tParseHTTPHeader [this=%x].\tGot header string:%s\n",          this, mHeaderBuffer.GetBuffer()));  //  // Replace all LWS with single SP characters.  And remove all of the CRLF  // characters...  //  mHeaderBuffer.CompressSet(" \t", ' ');  mHeaderBuffer.StripChars("\r\n");  if (mHeaderBuffer.Length() == 0) {    mHeadersDone = PR_TRUE;    return NS_OK;  }  return mResponse->ParseHeader(mHeaderBuffer);}