while programming a script I am experiencing some problems I can't solve on my own, so I registered to this forum to ask for some help.
What I'm trying to accomplish with this script is to send a HTTP GET-request with tcpWrite via a TCP-connection to a gateway. The server (= gateway) sends a response which generally consists of (at least) two TCP-segments, where the first segment is the HTTP-header and the others are containing the requested data. When receiving an error response (HTTP state "400 NOT FOUND" and equal) everything works as intended, the header is read first and the data-segment containing the error-code and message is read by a second tcpRead function.
Of course the programm is supposed to receive valid responses in the first place, but unfortunately when the server is sending a "200 OK" HTTP-message back, the header and the data-segment are read at once. In some rare cases everything is working as intended (correct segmentation, header and data read step by step), but this occurs normally only after starting up the PVSS II project anew.
When I tried to figure out what causes the problem I analysed the TCP-traffic using wireshark. At first I thought that the fast series of the TCP-segments (small time difference between the segments) is causing the tcpRead function to mess up, but I figured that this wasn't the case with the "400 *" responses so why should it be an issue with "200 OK" messages?
Here is how I coded the programm (without the error-handling):
Code: Select all
#using "CtrlXML"
#using "CtrlZlib"
int iSocket, iBytesSent, iReadCycle;
blob bResponse, bBlobEnding;
string sRequest;
dyn_string dsResponse;
iReadCycle = 0;
sRequest = "GET /addUPI?function=login&user=&passwd= HTTP/1.0\\r\\nConnection: Keep-Alive\\r\\n\\r\\n";
iSocket = tcpOpen("host-address", 80);
iBytesSent = tcpWrite(iSocket, sRequest);
// Reading HTTP-header
tcpRead(iSocket, bResponse, 5);
// BlobToString ... function to convert a blob variable into a string
dsResponse[1] = BlobToString(bResponse);
if ( patternMatch("HTTP/1.[01] 20[01234567] *", dsResponse[1]) )
{
while ( (string)bBlobEnding != "0D0A" ) // 0x0D0A specifies the ending of the data, 0D = CR (\\r), 0A = LF (\\n)
{
iReadCycle++;
// Reading remaining TCP-segments
tcpRead(iSocket, bResponse, 5);
blobGetValue(bResponse, bloblen(bResponse) - 2, bBlobEnding, 2, false);
gunzip(bResponse, dsResponse[(iReadCylce + 1)]);
}
}
else if ( patternMatch("HTTP/1.[01] 4[0125][0123456789] *", dsResponse[1]) )
{
while ( (string)bBlobEnding != "0D0A" )
{
iReadCycle++;
// Reading remaining TCP-segments
tcpRead(iSocket, bResponse, 5);
blobGetValue(bResponse, bloblen(bResponse) - 2, bBlobEnding, 2, false);
dsResponse[(iReadCylce + 1)] = BlobToString(bResponse);
}
}
// Connection usually terminated by server
try
{
tcpClose(iSocket);
}
case {}
At this point any help or advice will be appreciated.
Best regards
Maximilian Ferstl