I've looked at the demo, which got me to come up with the following
code, however it doesn't work yet.
I get the PostDocument event, but it doesn't appear to progress to the
PostedData event (I don't get any PostedData logs)
Any thoughts that may fix me?
// Extract from setup:
#define MAX_POST_BUFFER_SIZE 1024
co_HttpServer = new THttpServer(NULL);
co_HttpServer->Addr = cl_ConfigClass->me_GetString("http_address");
co_HttpServer->Port = cl_ConfigClass->me_GetInteger("http_port");
co_HttpServer->MaxClients =
cl_ConfigClass->me_GetInteger("http_max_clients");
co_HttpServer->DocDir = ExtractFileDir(ParamStr(0));
co_HttpServer->OnPostDocument = me_HttpServerPostDocument;
co_HttpServer->OnPostedData = me_HttpServerPostedData;
co_HttpServer->Start( );
//---------------------------------------------------------------------------
// Handler for 'post' requests from device
//---------------------------------------------------------------------------
void __fastcall cBluetoothClass::me_HttpServerPostDocument(TObject
*Sender, TObject *Client, THttpGetFlag &Flags)
{
cLoggerClass * cl_LoggerClass = cLoggerClass::Instance( );
AnsiString web_page = ((THttpConnection *)Client)->Path; // full
web page & path
if (web_page == "/alert")
{
at_PostedDataLength = ((THttpConnection
*)Client)->RequestContentLength;
at_PostedDataReceived = 0;
if (at_PostedDataLength > MAX_POST_BUFFER_SIZE)
{
cl_LoggerClass->me_AddLog("POST TOO BIG: " +
IntToStr(at_PostedDataLength), LOG_ERROR);
}
else
{
Flags = hgAcceptData;
((THttpConnection *)Client)->LineMode = FALSE;
cl_LoggerClass->me_AddLog("ALERT DETECTED - Waiting for
POST data (" + IntToStr(at_PostedDataLength) + " bytes)", LOG_INFO);
}
} // else incorrect post address
else
{
cl_LoggerClass->me_AddLog("FAILED POST: " + web_page, LOG_ERROR);
}
}
//---------------------------------------------------------------------------
// Handler for the posted data from 'post' requests
//---------------------------------------------------------------------------
void __fastcall cBluetoothClass::me_HttpServerPostedData(TObject
*Sender, TObject *Client, WORD Error)
{
cLoggerClass * cl_LoggerClass = cLoggerClass::Instance( );
int buffer_space_left = MAX_POST_BUFFER_SIZE - at_PostedDataReceived;
int bytes_received;
bytes_received = ((THttpConnection
*)Client)->Receive(&at_PostedDataBuffer[at_PostedDataReceived],
buffer_space_left);
at_PostedDataReceived += bytes_received;
if (at_PostedDataReceived >= at_PostedDataLength)
{
AnsiString received_string = "";
AnsiString Header, Body;
TMemoryStream *Stream = new TMemoryStream( );
for (int i = 0; i < at_PostedDataLength; i++)
{
received_string = received_string + at_PostedDataBuffer[i];
} // for i
cl_LoggerClass->me_AddLog("POST Complete: '" + received_string
+ "'", LOG_INFO);
at_PostedDataReceived = 0;
at_PostedDataLength = 0;
Body = "POST success\r\n";
Header = ((THttpConnection *)Client)->Version + " 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-Length: " +
IntToStr(Body.Length()) + "\r\n\r\n";
// Send response back to sensor
Stream->Write(Header.data(), Header.Length());
Stream->Write(Body.data(), Body.Length());
Stream->Seek(0, 0);
((THttpConnection *)Client)->DocStream = Stream;
((THttpConnection *)Client)->SendStream( );
}
else // Not Finished Yet!
{
cl_LoggerClass->me_AddLog("POST segment received. Awaiting
more...", LOG_INFO);
}
}
On 09/09/2010 13:49, Arno Garrels wrote:
David Lewis wrote:
I'm putting together a basic web server for a small application to
communicate with.
I have the GET event working as intended, but am having difficulty
with
the POST event.
The event fires, but I can't seem to work out where to find the POST
data...
I tried: ((THttpConnection *)Client)->ReceiveStr( ) and
((THttpConnection *)Client)->Params but both appear to be empty.
Where would I find the actual posted data?
Have a look at the Delphi OverbyteIcsWebServ1 demo, it's Delphi however
you get the idea for sure.
On event OnPostDocument do some checks and prepare or allocate
your receive buffer. OnPostedData is the event to Receive() data chunks
and check for correct data length etc..
--
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