⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wintransportagent.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        }

        #if defined(WIN32) && !defined(_WIN32_WCE)
        //
        // Proxy Authentication Required (407) / Server Authentication Required (401).
        // Need to set username/password.
        //
        else if(status == HTTP_STATUS_PROXY_AUTH_REQ ||
                status == HTTP_STATUS_DENIED) {
            LOG.debug("HTTP Authentication required.");
            DWORD dwError;

            // Automatic authentication (user/pass stored in win reg key).
            if (strcmp(proxy.user, "") && strcmp(proxy.password, "")) {
                WCHAR* wUser = toWideChar(proxy.user);
                WCHAR* wPwd  = toWideChar(proxy.password);

                InternetSetOption(request, INTERNET_OPTION_PROXY_USERNAME, wUser, wcslen(wUser)+1);
                InternetSetOption(request, INTERNET_OPTION_PROXY_PASSWORD, wPwd,  wcslen(wPwd)+1);

                delete [] wUser;
                delete [] wPwd;
                dwError = ERROR_INTERNET_FORCE_RETRY;
            }

            // Prompt dialog box.
            else {
                dwError = InternetErrorDlg(GetDesktopWindow(), request, NULL,
                                           FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
                                           FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS |
                                           FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
                                           NULL);
            }

            if (dwError == ERROR_INTERNET_FORCE_RETRY) {
                continue;
            }
            else {
                LOG.error("HTTP Authentication failed.");
                break;
            }
        }
        #endif  // #if defined(WIN32) && !defined(_WIN32_WCE)
        
        else 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);
            continue;
        }
        else if (status == HTTP_STATUS_SERVER_ERROR ) {     // 500 -> out code 2052
            lastErrorCode = ERR_SERVER_ERROR;
            sprintf(lastErrorMsg, "HTTP server error: %d. Server failure.", status);
            LOG.debug(lastErrorMsg);
            goto exit;
        }

        #ifdef _WIN32_WCE
        // To handle the http error code for the tcp/ip notification with wrong credential
        else if (status == ERR_CREDENTIAL) {                // 401 -> out code 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 -> out code 420
            lastErrorCode = ERR_CLIENT_NOT_NOTIFIABLE;
            sprintf(lastErrorMsg, "HTTP server error: %d. Client not notifiable.", status);
            LOG.debug(lastErrorMsg);
            goto exit;
        }
        #endif
        
        else if (status == HTTP_STATUS_NOT_FOUND) {         // 404 -> out code 2060
            lastErrorCode = ERR_HTTP_NOT_FOUND;
            sprintf(lastErrorMsg, "HTTP request error: resource not found (status %d)", status);
            LOG.debug(lastErrorMsg);
            goto exit;
        }
        else if (status == HTTP_STATUS_REQUEST_TIMEOUT) {   // 408 -> out code 2061
            lastErrorCode = ERR_HTTP_REQUEST_TIMEOUT;
            sprintf(lastErrorMsg, "HTTP request error: server timed out waiting for request (status %d)", status);
            LOG.debug(lastErrorMsg);
            goto exit;
        }
        else {
            // Other HTTP errors -> OUT
            lastErrorCode = ERR_HTTP_STATUS_NOT_OK;         // else -> out code 2053
            DWORD code = GetLastError();
            char* tmp = createHttpErrorMessage(code);
            sprintf(lastErrorMsg, "HTTP request error: status received = %d): %s (code %d)", status, tmp, code);
		    LOG.debug(lastErrorMsg);
		    delete [] tmp;
		    goto exit;
        }
    } // for(numretries = 0; numretries < MAX_RETRIES; numretries++)
    

    // Too much retries -> exit
    if (numretries == MAX_RETRIES) {                        // Network error -> out code 2001
        lastErrorCode = ERR_CONNECT;
        sprintf(lastErrorMsg, "HTTP request error: %d attempts failed.", numretries);
        LOG.error(lastErrorMsg);
        goto exit;
    }

    //Initialize response
    contentLength=0;
    HttpQueryInfo (request,
                   HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
                   (LPDWORD)&contentLength,
                   (LPDWORD)&size,
                   NULL);



#ifdef USE_ZLIB
    int uncompressedContentLenght = 0;

    if(compression){
        // 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.
	    //
	    wbuffer = new WCHAR[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;
	    buffer = new WCHAR[dwSize];
	    memset(buffer, 0, dwSize*sizeof(WCHAR));
	
	    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;
	    }
	
	    memset(buffer, 0, dwSize*sizeof(WCHAR));
	    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) {
	    	memset(buffer, 0, dwSize*sizeof(WCHAR));
	    	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.");
                    uncompressedContentLenght = -1;
                }
                else {
                    uncompressedContentLenght = wcstol(buffer, NULL, 10);
                    LOG.debug("Uncompressed-Content-Length: %ld", uncompressedContentLenght);
                }

                // Check header value, use MAX_MSG_SIZE if not valid.
                if(uncompressedContentLenght <= 0) {
                    LOG.error("Invalid value, using max message size.");
                    uncompressedContentLenght = maxmsgsize * 2;
                }

            }
	
	    delete [] buffer;
	    buffer = NULL;
    }  //end if compression
#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;
    }

    // Allocate a block of memory for response read.
    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);
	p = response;
    int realResponseLenght = 0;

    // Fire Data Received Transport Event.
    fireTransportEvent(contentLength, RECEIVE_DATA_BEGIN);

    do {
        if (!InternetReadFile(request, (LPVOID)bufferA, readBufferSize, &read)) {
            DWORD code = GetLastError();
            lastErrorCode = ERR_READING_CONTENT;
            char* tmp = createHttpErrorMessage(code);
            sprintf(lastErrorMsg, "InternetReadFile Error: %d - %s", code, tmp);
            delete [] tmp;
            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.debug("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 = NULL;

    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
    // (should be already the same...)
    if (realResponseLenght != contentLength) {
        LOG.info("Bytes read: ", realResponseLenght);
    	contentLength = realResponseLenght;
    }

    // Fire Receive Data End Transport Event
    fireTransportEvent(contentLength, RECEIVE_DATA_END);

    //------------------------------------------------------------- Response read

#ifdef USE_ZLIB
    if(compression){
	    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[uncompressedContentLenght] = 0;
	        }
	        else if (err < 0) {
	            LOG.error("Error from zlib: %s", zError(err));
	            delete [] response;
	            response = NULL;
	            status = ERR_HTTP_INFLATE;
                    setError(
                        ERR_HTTP_INFLATE,
                        "ZLIB: error occurred decompressing data from Server.");
	            goto exit;
	        }
	    }
    }  //end if compression
#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;
}


/**
 * Utility function to retrieve the correspondant message for the Wininet error code passed.
 * Pointer returned is allocated new, must be freed by caller.
 * @param errorCode  the code of the last error
 * @return           the error message for the passed code, new allocated buffer
 */
char* WinTransportAgent::createHttpErrorMessage(DWORD errorCode) {

    WCHAR* errorMessage = new WCHAR[512];
    memset(errorMessage, 0, 512);

    FormatMessage(
                FORMAT_MESSAGE_FROM_HMODULE,
                GetModuleHandle(L"wininet.dll"),
                errorCode,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
                errorMessage,
                512,
                NULL);

    if (!errorMessage || wcslen(errorMessage) == 0) {
        wsprintf(errorMessage, L"Unknown error.");
    }

    char* ret = toMultibyte(errorMessage);
    if (errorMessage) delete [] errorMessage;
    return ret;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -