gsoapwininet.cpp

来自「linux下简单对象应用协议的开发库」· C++ 代码 · 共 1,106 行 · 第 1/3 页

CPP
1,106
字号
        disconnected by now. This is because the response is checked by the         wininet_fsend function to ensure that we didn't need any special         authentication. At that time the connection would have been         disconnected. This is okay however as we can still read the response        from the request handle.     */    do    {        /* read from the connection up to our maximum amount of data */        _ASSERTE( a_uiBufferLen <= ULONG_MAX );        bResult = InternetReadFile(             hHttpRequest,             &a_pBuffer[uiTotalBytesRead],             (DWORD) a_uiBufferLen - uiTotalBytesRead,             &dwBytesRead );        if ( bResult )        {            uiTotalBytesRead += dwBytesRead;        }        else        {            soap->error = GetLastError();            DBGLOG(TEST, SOAP_MESSAGE(fdebug,                 "wininet %p: frecv, error %d (%s) in InternetReadFile\n",                 soap, soap->error, wininet_error_message(soap,soap->error) ));        }    }     while ( bResult && dwBytesRead && uiTotalBytesRead < a_uiBufferLen );    DBGLOG(TEST, SOAP_MESSAGE(fdebug,         "wininet %p: recv, received %lu bytes\n", soap, uiTotalBytesRead ));    return uiTotalBytesRead;} /* gsoap documentation:    Called by client proxy multiple times, to close a socket connection before    a new socket connection is established and at the end of communications     when the SOAP_IO_KEEPALIVE flag is not set and soap.keep_alive = 0     (indicating that the other party supports keep alive). Should return     SOAP_OK, or a gSOAP error code. Built-in gSOAP function: tcp_disconnect */static int wininet_disconnect(     struct soap *   soap ){    struct wininet_data * pData =         (struct wininet_data *) soap_lookup_plugin( soap, wininet_id );    soap->error = SOAP_OK;    DBGLOG(TEST, SOAP_MESSAGE(fdebug, "wininet %p: disconnect\n", soap ));    /* force a disconnect by setting the disconnect flag to TRUE */    pData->bDisconnect = TRUE;    wininet_have_connection( soap, pData );    return SOAP_OK;}/* this is mostly for debug tracing */void CALLBACKwininet_callback(    HINTERNET   hInternet,    DWORD_PTR   dwContext,    DWORD       dwInternetStatus,    LPVOID      lpvStatusInformation,    DWORD       dwStatusInformationLength ){    struct soap * soap = (struct soap *) dwContext;    UNUSED_ARG( hInternet );    UNUSED_ARG( lpvStatusInformation );    UNUSED_ARG( dwStatusInformationLength );    switch ( dwInternetStatus )    {    case INTERNET_STATUS_RESOLVING_NAME:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_RESOLVING_NAME\n", soap ));        break;    case INTERNET_STATUS_NAME_RESOLVED:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_NAME_RESOLVED\n", soap ));        break;    case INTERNET_STATUS_CONNECTING_TO_SERVER:         DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_CONNECTING_TO_SERVER\n", soap));        break;    case INTERNET_STATUS_CONNECTED_TO_SERVER:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_CONNECTED_TO_SERVER\n", soap));        break;    case INTERNET_STATUS_SENDING_REQUEST:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_SENDING_REQUEST\n", soap));        break;    case INTERNET_STATUS_REQUEST_SENT:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_REQUEST_SENT, bytes sent = %lu\n",             soap, *(DWORD *)lpvStatusInformation ));        break;    case INTERNET_STATUS_RECEIVING_RESPONSE:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_RECEIVING_RESPONSE\n", soap));        break;    case INTERNET_STATUS_RESPONSE_RECEIVED:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_RESPONSE_RECEIVED, bytes received = %lu\n",             soap, *(DWORD *)lpvStatusInformation ));        break;    case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_CTL_RESPONSE_RECEIVED\n", soap));        break;    case INTERNET_STATUS_PREFETCH:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_PREFETCH\n", soap));        break;    case INTERNET_STATUS_CLOSING_CONNECTION:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_CLOSING_CONNECTION\n", soap));        break;    case INTERNET_STATUS_CONNECTION_CLOSED:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_CONNECTION_CLOSED\n", soap));        {            /* the connection has been closed, so we close the handle here */            struct wininet_data * pData =                 (struct wininet_data *) soap_lookup_plugin( soap, wininet_id );            if ( pData->hConnection )            {                /*                     we only mark this for disconnection otherwise we get                     errors when reading the data from the handle. In every                     function that we use the connection we will check first to                     see if it has been disconnected.                 */                pData->bDisconnect = TRUE;            }        }        break;    case INTERNET_STATUS_HANDLE_CREATED:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_HANDLE_CREATED\n", soap));        break;    case INTERNET_STATUS_HANDLE_CLOSING:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_HANDLE_CLOSING\n", soap));        break;// Removed to avoid compile errors//    case INTERNET_STATUS_DETECTING_PROXY://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_DETECTING_PROXY\n", soap));//        break;    case INTERNET_STATUS_REQUEST_COMPLETE:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_REQUEST_COMPLETE\n", soap));        break;    case INTERNET_STATUS_REDIRECT:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_REDIRECT, new url = %s\n",             soap, (char*) lpvStatusInformation ));        break;    case INTERNET_STATUS_INTERMEDIATE_RESPONSE:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_INTERMEDIATE_RESPONSE\n", soap));        break;// Removed to avoid compile errors//    case INTERNET_STATUS_USER_INPUT_REQUIRED://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_USER_INPUT_REQUIRED\n", soap));//        break;    case INTERNET_STATUS_STATE_CHANGE:        DBGLOG(TEST, SOAP_MESSAGE(fdebug,            "wininet %p: INTERNET_STATUS_STATE_CHANGE\n", soap));        break;// Removed to avoid compile errors//    case INTERNET_STATUS_COOKIE_SENT://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_COOKIE_SENT\n", soap));//        break;//    case INTERNET_STATUS_COOKIE_RECEIVED://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_COOKIE_RECEIVED\n", soap));//        break;//    case INTERNET_STATUS_PRIVACY_IMPACTED://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_PRIVACY_IMPACTED\n", soap));//        break;//    case INTERNET_STATUS_P3P_HEADER://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_P3P_HEADER\n", soap));//        break;//    case INTERNET_STATUS_P3P_POLICYREF://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_P3P_POLICYREF\n", soap));//        break;//    case INTERNET_STATUS_COOKIE_HISTORY://        DBGLOG(TEST, SOAP_MESSAGE(fdebug,//            "wininet %p: INTERNET_STATUS_COOKIE_HISTORY\n", soap));//        break;    }}/*     check to ensure that our connection hasn't been disconnected     and disconnect remaining handles if necessary. */static BOOLwininet_have_connection(    struct soap *           soap,    struct wininet_data *   a_pData ){    /* close the http request if we don't have a connection */    BOOL bCloseRequest = a_pData->bDisconnect || !a_pData->hConnection;    if ( bCloseRequest && soap->socket != SOAP_INVALID_SOCKET )    {        DBGLOG(TEST, SOAP_MESSAGE(fdebug,             "wininet %p: closing request\n", soap));        InternetCloseHandle( (HINTERNET) soap->socket );        soap->socket = SOAP_INVALID_SOCKET;    }    /* close the connection if we don't have a request */    if ( soap->socket == SOAP_INVALID_SOCKET && a_pData->hConnection )    {        DBGLOG(TEST, SOAP_MESSAGE(fdebug,             "wininet %p: closing connection\n", soap));        InternetCloseHandle( a_pData->hConnection );        a_pData->hConnection = NULL;    }    a_pData->bDisconnect = FALSE;    /* clean up the send details if we don't have a request */    if ( soap->socket == SOAP_INVALID_SOCKET )    {        if ( a_pData->pBuffer )        {            free( a_pData->pBuffer );            a_pData->pBuffer = 0;        }        a_pData->uiBufferLen = 0;        a_pData->uiBufferLenMax = INVALID_BUFFER_LENGTH;    }    /* we now either still have both request and connection, or neither */    return (a_pData->hConnection != NULL);}static DWORDwininet_set_timeout(    struct soap *           soap,     struct wininet_data *   a_pData,    const char *            a_pszTimeout,    DWORD                   a_dwOption,    int                     a_nTimeout ){    UNUSED_ARG( soap );    UNUSED_ARG( a_pszTimeout );    if ( a_nTimeout > 0 )    {        DWORD dwTimeout = a_nTimeout * 1000;        if ( !InternetSetOption( a_pData->hInternet,             a_dwOption, &dwTimeout, sizeof(DWORD) ) )        {            DWORD dwErrorCode = GetLastError();            DBGLOG(TEST, SOAP_MESSAGE(fdebug,                 "wininet %p: failed to set %s timeout, error %d (%s)\n",                 soap, a_pszTimeout, dwErrorCode,                 wininet_error_message(soap,dwErrorCode) ));            return dwErrorCode;        }    }    return 0;}static BOOLwininet_resolve_send_error(     HINTERNET   a_hHttpRequest,     DWORD       a_dwErrorCode ){    DWORD dwResult = InternetErrorDlg(        GetDesktopWindow(),         a_hHttpRequest,         a_dwErrorCode,        FLAGS_ERROR_UI_FILTER_FOR_ERRORS |        FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |        FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,        NULL );    return (a_dwErrorCode == ERROR_INTERNET_INCORRECT_PASSWORD) ?        (dwResult == ERROR_INTERNET_FORCE_RETRY) :        (dwResult == ERROR_SUCCESS);}#ifdef SOAP_DEBUGstatic const char *wininet_error_message(    struct soap *   soap,    DWORD           a_dwErrorMsgId ){    HINSTANCE   hModule;    DWORD       dwResult;    DWORD       dwFormatFlags;    struct wininet_data * pData =         (struct wininet_data *) soap_lookup_plugin( soap, wininet_id );    /* free any existing error message */    wininet_free_error_message( pData );    dwFormatFlags =         FORMAT_MESSAGE_ALLOCATE_BUFFER |        FORMAT_MESSAGE_IGNORE_INSERTS |        FORMAT_MESSAGE_FROM_SYSTEM;    /* load wininet.dll for the error messages */    hModule = LoadLibraryExA( "wininet.dll", NULL,        LOAD_LIBRARY_AS_DATAFILE | DONT_RESOLVE_DLL_REFERENCES );    if ( hModule )    {        dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;    }    /* format the messages */    dwResult = FormatMessageA(         dwFormatFlags,         hModule,         a_dwErrorMsgId,         MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),        (LPSTR) &pData->pszErrorMessage,        0,        NULL );    /* free the library if we loaded it */    if ( hModule )    {        FreeLibrary( hModule );    }    /* remove the CR LF from the error message */    if ( dwResult > 2 )    {        pData->pszErrorMessage[dwResult-2] = 0;        return pData->pszErrorMessage;    }    else    {        const static char szUnknown[] = "(unknown)";        return szUnknown;    }}static voidwininet_free_error_message(    struct wininet_data *   a_pData ){    if ( a_pData->pszErrorMessage )    {        LocalFree( a_pData->pszErrorMessage );        a_pData->pszErrorMessage = 0;    }}#endif /* SOAP_DEBUG */

⌨️ 快捷键说明

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