📄 wmtransportagent.cpp
字号:
goto exit;
}
LOG.info("Network error (%d) writing data from client: retry %i time...",
code, numretries + 1);
continue;
}
LOG.debug(MESSAGE_SENT);
// Fire Send Data End Transport Event
fireTransportEvent(contentLength, SEND_DATA_END);
// Check the status code.
size = sizeof(status);
HttpQueryInfo (request,
HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
(LPDWORD)&status,
(LPDWORD)&size,
NULL);
if (status == HTTP_ERROR) { // 400 bad request error. retry to send the message
LOG.info("Network error in server receiving data. "
"Server responds 400: retry %i time...", numretries + 1);
}
else if (status == HTTP_STATUS_SERVER_ERROR ) {
lastErrorCode = ERR_SERVER_ERROR;
sprintf(lastErrorMsg, "HTTP server error: %d. Server failure.", status);
LOG.debug(lastErrorMsg);
goto exit;
}
// to handle the http error code for the tcp/ip notification with wrong credential
else if (status == ERR_CREDENTIAL) { // 401
lastErrorCode = ERR_CREDENTIAL;
sprintf(lastErrorMsg, "HTTP server error: %d. Wrong credential.", status);
LOG.debug(lastErrorMsg);
goto exit;
}
// to handle the http error code for the tcp/ip notification and client not notifiable
else if (status == ERR_CLIENT_NOT_NOTIFIABLE) { // 420
lastErrorCode = ERR_CLIENT_NOT_NOTIFIABLE;
sprintf(lastErrorMsg, "HTTP server error: %d. Client not notifiable.", status);
LOG.debug(lastErrorMsg);
goto exit;
}
else if (status != STATUS_OK) {
lastErrorCode = ERR_HTTP_STATUS_NOT_OK;
//lastErrorCode = ERR_READING_CONTENT;
sprintf(lastErrorMsg, "HTTP request error: %d", status);
LOG.debug(lastErrorMsg);
goto exit;
}
else { //status_ok
LOG.debug("Data sent succesfully to server. Server responds OK");
break;
}
}
// If wrong status, exit immediately.
if (status != HTTP_STATUS_OK) {
if (status == HTTP_STATUS_NOT_FOUND) {
lastErrorCode = ERR_HTTP_NOT_FOUND;
sprintf(lastErrorMsg, "HTTP request error: resource not found (status %d)", status);
}
else {
DWORD code = GetLastError();
lastErrorCode = ERR_HTTP_STATUS_NOT_OK;
sprintf(lastErrorMsg, "HTTP request error (status received = %d): code %d", status, code);
}
goto exit;
}
//Initialize response
contentLength=0;
HttpQueryInfo (request,
HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
(LPDWORD)&contentLength,
(LPDWORD)&size,
NULL);
#ifdef USE_ZLIB
// Release the send buffer (also set msgToSend to NULL, to
// avoid leaving a dangling pointer around.
if (compr) {
delete [] compr; compr = NULL;
msgToSend = NULL;
}
//
// Read headers: get contentLenght/Uncompressed-Content-Length.
//
long uncompressedContentLenght = 0;
wchar_t* wbuffer = new wchar_t[1024];
DWORD ddsize = 1024;
if (!HttpQueryInfo(request,HTTP_QUERY_RAW_HEADERS_CRLF ,(LPVOID)wbuffer,&ddsize,NULL)) {
if (ERROR_HTTP_HEADER_NOT_FOUND == GetLastError()) {
isToDeflate = FALSE;
}
}
LOG.debug("Header: %ls", wbuffer);
delete [] wbuffer; wbuffer = NULL;
// isToDeflate to be set
DWORD dwSize = 512;
wchar_t* buffer = new wchar_t[dwSize];
wcscpy(buffer, TEXT("Accept-Encoding"));
HttpQueryInfo(request,HTTP_QUERY_CUSTOM,(LPVOID)buffer,&dwSize,NULL);
if (GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND) {
isToDeflate = FALSE;
} else {
isToDeflate = TRUE;
}
wcscpy(buffer, TEXT("Content-Encoding"));
HttpQueryInfo(request,HTTP_QUERY_CUSTOM,(LPVOID)buffer,&dwSize,NULL);
if (GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND) {
isToInflate = FALSE;
} else {
if (wcscmp(buffer, TEXT("deflate")) == 0)
isToInflate = TRUE;
else
isToInflate = FALSE;
}
if(isToInflate) {
wcscpy(buffer, TEXT("Uncompressed-Content-Length"));
HttpQueryInfo(request,HTTP_QUERY_CUSTOM,(LPVOID)buffer,&dwSize,NULL);
if (GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND) {
LOG.error("Error reading 'Uncompressed-Content-Length' header. "
"Can't inflate data.");
status = ERR_HTTP_INFLATE;
lastErrorCode = ERR_HTTP_INFLATE;
goto exit;
} else {
uncompressedContentLenght = wcstol(buffer, NULL, 10);
LOG.debug("Uncompressed-Content-Length: %ld", uncompressedContentLenght);
if(uncompressedContentLenght < 0) {
LOG.error("Invalid 'Uncompressed-Content-Length' header. "
"Can't inflate data.");
status = ERR_HTTP_INFLATE;
lastErrorCode = ERR_HTTP_INFLATE;
goto exit;
}
}
}
delete [] buffer;
buffer = NULL;
#endif
// ================================== Reading Response ==============================
LOG.debug(READING_RESPONSE);
LOG.debug("Content-length: %u", contentLength);
if (contentLength <= 0) {
LOG.debug("Undefined content-length = %u. Using the maxMsgSize = %u.", contentLength, maxmsgsize);
contentLength = maxmsgsize;
}
response = new char[contentLength+1];
if (response == NULL) {
lastErrorCode = ERR_NOT_ENOUGH_MEMORY;
sprintf(lastErrorMsg, "Not enough memory to allocate a buffer for the server response: %d required.", contentLength);
LOG.error(lastErrorMsg);
goto exit;
}
memset(response, 0, contentLength);
char* p = response;
int realResponseLenght = 0;
// Allocate a block of memory for response read.
const unsigned int bufsize = getReadBufferSize();
char* bufferA = new char[bufsize+1];
// Fire Data Received Transport Event.
fireTransportEvent(contentLength, RECEIVE_DATA_BEGIN);
do {
if (!InternetReadFile(request, (LPVOID)bufferA, bufsize, &read)) {
DWORD code = GetLastError();
lastErrorCode = ERR_READING_CONTENT;
sprintf(lastErrorMsg, "InternetReadFile Error: %d", code);
goto exit;
}
// Sanity check: some proxy could send additional bytes.
// Correct 'read' value to be sure we won't overflow the 'response' buffer.
if ((realResponseLenght + read) > contentLength) {
LOG.info("Warning! %d bytes read -> truncating data to content-lenght = %d.",
(realResponseLenght + read), contentLength);
read = contentLength - realResponseLenght;
}
if (read > 0) {
memcpy(p, bufferA, read); // Note: memcopy exactly the bytes read (could be no readable chars...)
p += read;
realResponseLenght += read;
// Fire Data Received Transport Event
fireTransportEvent(read, DATA_RECEIVED);
}
} while (read);
// free read buffer
delete [] bufferA; bufferA = 0;
if (realResponseLenght <= 0) {
lastErrorCode = ERR_READING_CONTENT;
sprintf(lastErrorMsg,
"Error reading HTTP response from Server: received data of size = %d.",
realResponseLenght);
goto exit;
}
// Log bytes read if different from content length
if (realResponseLenght != contentLength) {
LOG.info("Bytes read: ", realResponseLenght);
// Should be already the same...
contentLength = realResponseLenght;
}
// Fire Receive Data End Transport Event
fireTransportEvent(contentLength, RECEIVE_DATA_END);
//------------------------------------------------------------- Response read
#ifdef USE_ZLIB
if (isToInflate) {
//
// INFLATE (decompress data)
//
uLong uncomprLen = uncompressedContentLenght;
Bytef* uncompr = new Bytef[uncomprLen + 1];
// Decompresses the source buffer into the destination buffer.
int err = uncompress(uncompr, &uncomprLen, (Bytef*)response, contentLength);
if (err == Z_OK) {
delete [] response;
response = (char*)uncompr;
response[uncomprLen] = 0;
}
else if (err < 0) {
LOG.error("Error from zlib: %s", zError(err));
delete [] response;
response = NULL;
status = ERR_HTTP_INFLATE;
lastErrorCode = ERR_HTTP_INFLATE;
sprintf(lastErrorMsg, "ZLIB: error occurred decompressing data from Server.");
goto exit;
}
}
#endif
LOG.debug("Response read:\n%s", response);
exit:
// Close the Internet handles.
if (inet) {
InternetCloseHandle (inet);
}
if (connection) {
InternetCloseHandle (connection);
}
if (request) {
InternetCloseHandle (request);
}
if ((status != STATUS_OK) && (response != NULL)) {
delete [] response; response = NULL;
}
if (wurlHost) delete [] wurlHost;
if (wurlResource) delete [] wurlResource;
if (bufferA) delete [] bufferA;
#ifdef USE_ZLIB
if (compr) delete [] compr;
if (buffer) delete [] buffer;
if (wbuffer) delete [] wbuffer;
#endif
EXITING(L"TransportAgent::sendMessage");
return response;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -