wintransportagent.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 795 行 · 第 1/2 页

CPP
795
字号
        // 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
            setErrorF(ERR_SERVER_ERROR, "HTTP server error: %d. Server failure.", status);
            LOG.debug("%s", getLastErrorMsg());
            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
            setErrorF(ERR_CREDENTIAL, "HTTP server error: %d. Wrong credential.", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        // To handle the http error code for the tcp/ip notification when payment required
        else if (status == PAYMENT_REQUIRED) {              // 402 -> out code 402
            setErrorF(PAYMENT_REQUIRED, "HTTP server error: %d. Client not authenticated.", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        // To handle the http error code for the tcp/ip notification when client not activated (forbidden)
        else if (status == FORBIDDEN) {                     // 403 -> out code 403
            setErrorF(FORBIDDEN, "HTTP server error: %d. Connection forbidden, client not activated.", status);
            LOG.debug("%s", getLastErrorMsg());
            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
            setErrorF(ERR_CLIENT_NOT_NOTIFIABLE, "HTTP server error: %d. Client not notifiable.", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        // to handle the http error code for the tcp/ip notification and client not notifiable
        // code 421 is returned by newer Funambol Server to say "you're allowed to start CTP"
        else if (status == ERR_CTP_ALLOWED) {               // 421 -> out code 421
            setErrorF(ERR_CTP_ALLOWED, "HTTP server error: %d. Client not notifiable and CTP Server is available.", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        #endif
        
        else if (status == HTTP_STATUS_NOT_FOUND) {         // 404 -> out code 2060
            setErrorF(ERR_HTTP_NOT_FOUND, "HTTP request error: resource not found (status %d)", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        else if (status == HTTP_STATUS_REQUEST_TIMEOUT) {   // 408 -> out code 2061
            setErrorF(ERR_HTTP_REQUEST_TIMEOUT, "HTTP request error: server timed out waiting for request (status %d)", status);
            LOG.debug("%s", getLastErrorMsg());
            goto exit;
        }
        else {
            // Other HTTP errors -> OUT
            //lastErrorCode = ERR_HTTP_STATUS_NOT_OK;         // else -> out code 2053
            DWORD code = GetLastError();
            char* tmp = createHttpErrorMessage(code);
            setErrorF(ERR_HTTP_STATUS_NOT_OK, "HTTP request error: status received = %d): %s (code %d)", status, tmp, code);
		    LOG.debug("%s", getLastErrorMsg());
		    delete [] tmp;
		    goto exit;
        }
    } // for(numretries = 0; numretries < MAX_RETRIES; numretries++)
    

    // Too much retries -> exit
    if (numretries == MAX_RETRIES) {                        // Network error -> out code 2001
        setErrorF(ERR_CONNECT, "HTTP request error: %d attempts failed.", numretries); 
        LOG.error("%s", getLastErrorMsg());
        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;

    // 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;

#endif


//
// ====================================== Reading Response ======================================
//
    LOG.debug("%s", 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);
        setErrorF(ERR_NOT_ENOUGH_MEMORY, "Not enough memory to allocate a buffer for the server response: %d required.", contentLength);
        LOG.error("%s", getLastErrorMsg());
        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);
            setErrorF(ERR_READING_CONTENT, "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);
        setErrorF(ERR_READING_CONTENT, "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;
    }
    response[contentLength] = 0;

    // 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[uncompressedContentLenght] = 0;
        }
        else if (err < 0) {
            // Save the msg to file, for debugging...
            dumpMessage(response, contentLength);

            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;
        }
    }

#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;
}



/** 
 * Saves the msg passed into a file under "\dump" for debugging purpose.
 * The message is saved as a binary file.
 */
void WinTransportAgent::dumpMessage(const char* msg, const int msgLen) {

    if (msgLen == 0) {
        return;
    }
    char fileName[100], now[30];
    SYSTEMTIME sys_time;
    GetLocalTime(&sys_time);
    sprintf(now, "%04d-%02d-%02d_%02d.%02d.%02d", sys_time.wYear, sys_time.wMonth, sys_time.wDay,
                                                  sys_time.wHour, sys_time.wMinute, sys_time.wSecond);

    // TODO: store dump files under installDir instead of root folder?
    CreateDirectory(L"\\dump", NULL);
    sprintf(fileName, "\\dump\\%s.dmp", now);
    saveFile(fileName, msg, msgLen, true);
}

⌨️ 快捷键说明

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