Dear All,

I have recently written a small Web Server application using the THttpServer
component in ICS 5 with C++ Builder 5.

It serves GET requests perfectly, but when I added POST functionality,
(following the WebServ example) it has started generating EAccessViolations.

CodeGuard reports that these are due to my PostedDataBuffer having only a 1
byte heap length when I am writing the NULL string termination character in
the "HttpServerPostedData" event handler.

I have managed to improve the code reliability by not dynamically allocating
the PostedDataBuffer at all, (but this is not an acceptable solution). In
the code snippets below I pre-allocate once in the constructor but this
still generates AV's.

Can anyone shed some light on what might be happening here?


Regards,
Keith Willis.



<<< CODE SNIPPETS >>>
//--------------------------------------------------------------------------
-
class TMyHttpConnection : public THttpConnection
{
 public:
  char* Junk;
  char *PostedDataBuffer;     // Will hold dynamically allocated buffer
  int  PostedDataSize;        // Databuffer size
  int  RcvdByteCount;               // Keep track of received byte count.
  
 public:
  __fastcall TMyHttpConnection(Classes::TComponent* AOwner);
  virtual __fastcall ~TMyHttpConnection();
};
//--------------------------------------------------------------------------
-



//--------------------------------------------------------------------------
-

__fastcall TMyHttpConnection::TMyHttpConnection(Classes::TComponent* AOwner)
: THttpConnection(AOwner)
{
 try
 {
  Junk = new char(65536);
  PostedDataBuffer = new char(65536);  // Pre-allocate here in a desperate
attempt to avoid EAccessViolations
  PostedDataSize = 65536;
 }
 catch(...) {}
}
//--------------------------------------------------------------------------
-

__fastcall TMyHttpConnection::~TMyHttpConnection()
{
 try
 {
  delete [] Junk;
  delete [] PostedDataBuffer;

  PostedDataBuffer = NULL;
  PostedDataSize = 0;
 }
 catch(...) {}
}
//--------------------------------------------------------------------------
-



//--------------------------------------------------------------------------
-

void __fastcall TAegisWebServerForm::HttpServerPostedData(TObject *Sender,
      TObject *Client, WORD Error)
{
 try
 {
  int Len, Remains;

  TMyHttpConnection* Connection = (TMyHttpConnection*)Client;

  Remains = Connection->RequestContentLength - Connection->RcvdByteCount;

  if (Remains <= 0)
  {
   Len = Connection->Receive(Connection->Junk, sizeof(Connection->Junk)- 1);
   if (Len >= 0) Connection->Junk[Len] = 0;
   return;
  }

//  Len = Connection->Receive(Connection->PostedDataBuffer +
Connection->RcvdByteCount, Remains);
  Len =
Connection->Receive(&(Connection->PostedDataBuffer[Connection->RcvdByteCount
]), Remains);
  if (Len <= 0) return;

  Connection->RcvdByteCount += Len;

  if (Connection->RcvdByteCount > Connection->RequestContentLength)
Connection->RcvdByteCount = Connection->RequestContentLength;

  if (Connection->RcvdByteCount == Connection->RequestContentLength)
  {
   Connection->PostedDataBuffer[Connection->RcvdByteCount] = 0;  // <=
CRASHES HERE! CodeGuard reports that the Heap length of is only 1 byte at
this point???

   if (CompareText(Connection->Path, "/content/home.html") == 0)
ProcessPostedData(Connection);
//   else Connection->Answer404();  // I need to make Answer404 accessible
   else
   {
    AnsiString Body = AnsiString("<HTML><HEAD><TITLE>404 Not
Found</TITLE></HEAD><BODY><H1>404 Not Found</H1>The requested URL ") +
TextToHtmlText(Connection->Path) + " was not found on this
server.<P></BODY></HTML>\r\n";
    Connection->SendHeader(Connection->Version + " 404 Not
Found\r\nContent-Type: text/html\r\nContent-Length: " +
Utility->IntToStrEx(Body.Length()) + "\r\n\r\n");
    Connection->SendStr(Body);
   }

   Connection->PostedDataReceived();
  }
 }
 catch (Exception &E)
{HandleError("TAegisWebServerForm::HttpServerPostedData", E.Message);}
 catch (const exception &e)
{HandleError("TAegisWebServerForm::HttpServerPostedData", e.what());}
 catch (...) {HandleError("TAegisWebServerForm::HttpServerPostedData",
"Unknown Exception");}
}
//--------------------------------------------------------------------------
-

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to