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

📄 winhttpdohttp.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
      */
      bStatus = WinHttpReceiveResponse (hRequest, NULL);
      if (bStatus == FALSE)
      {
        VOLT_SET_ERROR_TYPE (
          errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_NETWORK_CONNECT;
        sysRet = GetLastError ();
        break;
      }   

      /* Get the response code from the response.
       */
      dwSize = sizeof (respCode);
      bStatus = WinHttpQueryHeaders (
        hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
        NULL, &respCode, &dwSize, NULL);
      *responseCode = (unsigned int)respCode;
      if (bStatus == FALSE)
      {
        VOLT_SET_ERROR_TYPE (
          errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_DOWNLOAD_FAILURE;
        sysRet = GetLastError ();
        break;
      }

      /* If the response code is 401, we'll try to do some
       * authentication. Any other code, we're done with this loop. If
       * the code is 200, success, any other code is a response we
       * can't handle, so we'll let the rest of the code sort it out.
       */
      if (respCode != 401)
        break;

      /* We need authentication, what kinds of auth does the server
       * support?
       */
      bStatus = WinHttpQueryAuthSchemes (
        hRequest, &supportedSchemes, &firstScheme, &authTarget);
      if (bStatus == FALSE)
      {
        VOLT_SET_ERROR_TYPE (
          errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_DOWNLOAD_FAILURE;
        sysRet = GetLastError ();
        break;
      }

      /* If the server doesn't support a scheme we're familiar with,
       * error.
       */
      if ((supportedSchemes &
          (WINHTTP_AUTH_SCHEME_BASIC | WINHTTP_AUTH_SCHEME_NTLM)) == 0)
      {
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_UNKNOWN_AUTH_SCHEME;
        break;
      }

      if ((supportedSchemes & WINHTTP_AUTH_SCHEME_NTLM) != 0)
      {
        /* Try this once. If that doesn't succeed, don't try it again.
         * We initialized logonLevel to HIGH, so if it's still HIGH,
         * this is the first time through. If not, we've gone through
         * this clause before.
         */
        if (logonLevel == WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH)
        {
          logonLevel = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
          bStatus = WinHttpSetOption (
            hRequest, WINHTTP_OPTION_AUTOLOGON_POLICY, (LPVOID)&logonLevel,
            sizeof (logonLevel));
          if (bStatus == FALSE)
          {
            VOLT_SET_ERROR_TYPE (
              errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
            VOLT_SET_FNCT_LINE (fnctLine)
            status = VT_ERROR_DOWNLOAD_FAILURE;
            sysRet = GetLastError ();
            break;
          }

          /* Send the request again.
           */
          continue;
        }
      }

      if ((supportedSchemes & WINHTTP_AUTH_SCHEME_BASIC) != 0)
      {
        /* If this is a RETRY, release the password from the last call.
         */
        if (purpose == VT_USER_PASS_COLLECTOR_PURPOSE_COLLECT_RETRY)
        {
          transCtx->userPassCtx.UserPassFunction (
            transCtx->voltObject.libraryCtx, transCtx->userPassCtx.appData,
            idObj, VT_USER_PASS_COLLECTOR_PURPOSE_RELEASE,
            &username, &usernameLen, &password, &passwordLen);
          releaseFlag = 0;
        }

        /* Get the username and password. If no UserPassCtx is loaded,
         * error.
         */
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_NEED_USERNAME_PASSWORD;
        if (transCtx->userPassCtx.UserPassFunction == (VtUserPassCollector)0)
          break;

        usernameLen = 0;
        passwordLen = 0;
        releaseFlag = 1;

        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = transCtx->userPassCtx.UserPassFunction (
          transCtx->voltObject.libraryCtx, transCtx->userPassCtx.appData,
          idObj, purpose, &username, &usernameLen, &password, &passwordLen);
        if (status != 0)
          break;

        /* If we call again, make sure the purpose is RETRY.
         */
        purpose = VT_USER_PASS_COLLECTOR_PURPOSE_COLLECT_RETRY;

        /* Make sure the username and password are NULL-terminated.
         */
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_MEMORY;
        userPass = (unsigned char *)Z2Realloc (
          userPass, usernameLen + passwordLen + 2);
        if (userPass == (unsigned char *)0)
          break;

        status = 0;

        Z2Memcpy (userPass, username, usernameLen);
        userPass[usernameLen] = 0;
        Z2Memcpy (userPass + usernameLen + 1, password, passwordLen);
        userPass[usernameLen + passwordLen + 1] = 0;

        bStatus = WinHttpSetCredentials (
          hRequest, authTarget, WINHTTP_AUTH_SCHEME_BASIC,
          (LPCWSTR)userPass, (LPCWSTR)(userPass + usernameLen + 1), (LPVOID)0);
        if (bStatus == FALSE)
        {
          VOLT_SET_ERROR_TYPE (
            errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
          VOLT_SET_FNCT_LINE (fnctLine)
          status = VT_ERROR_DOWNLOAD_FAILURE;
          sysRet = GetLastError ();
          break;
        }

        /* Send the request again.
         */
        continue;
      }
    } while (1);
    if (status != 0)
      break;

    /* Keep reading until we're out of data.
     */
    totalSize = 0;
    currentPos = 0;
    do
    {
      dwSize = 0;
      bStatus = WinHttpQueryDataAvailable (hRequest, &dwSize);
      if (bStatus == FALSE)
      {
        VOLT_SET_ERROR_TYPE (
          errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_DOWNLOAD_FAILURE;
        sysRet = GetLastError ();
        break;
      }      

      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      totalSize += dwSize;
      buffer = (char *)Z2Realloc (buffer, totalSize + 1);
      if (buffer == (char *)0)
        break;      

      bStatus = WinHttpReadData (
        hRequest, buffer + currentPos, dwSize, &bytesRead);
      if (bStatus == FALSE)
      {
        VOLT_SET_ERROR_TYPE (
          errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_DOWNLOAD_FAILURE;
        sysRet = GetLastError ();
        break;
      }
      buffer[currentPos + bytesRead] = 0;
      currentPos += bytesRead;

      status = 0;

      /* If all the data has been read, we're done.
       */
      if (bytesRead == 0)
        break;
    } while (1);
    if (status != 0)
      break;

    /* At this point we have read all the data
     */
    *response = buffer;

  } while (0);

  if (releaseFlag == 1)
    transCtx->userPassCtx.UserPassFunction (
      transCtx->voltObject.libraryCtx, transCtx->userPassCtx.appData,
      idObj, VT_USER_PASS_COLLECTOR_PURPOSE_RELEASE,
      &username, &usernameLen, &password, &passwordLen);

  if (hSession != NULL)
    WinHttpCloseHandle (hSession);
  if (hConnect != NULL)
    WinHttpCloseHandle (hConnect);
  if (hRequest != NULL)
    WinHttpCloseHandle (hRequest);

  /* Free any data we allocated 
   */
  if (temp != (char *)0)
    Z2Free (temp);
  if (wideUrl != (LPWSTR)0)
    Z2Free (wideUrl);
  if (host != (LPWSTR)0)
    Z2Free (host);
  if (path != (LPWSTR)0)
    Z2Free (path);
  if (userPass != (unsigned char *)0)
    Z2Free (userPass);

  if ( (status == 0) && (sysRet == 0) )
    return (0);

  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  if (sysRet == ERROR_NOT_ENOUGH_MEMORY)
    status = VT_ERROR_MEMORY;

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, status, sysRet, errorType,
    (char *)0, "winHttpDoHTTP", fnctLine, (char *)0)

  return (status);
}

#endif  /* VOLT_OS == VOLT_WINDOWS_32 */

⌨️ 快捷键说明

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