cURL / Mailing Lists / curl-users / Single Mail

curl-users

Source Code as requested (Unable to get HTTP_CODE via curl_easy_getinfo)

From: jamie page <jtpage33_at_yahoo.com>
Date: Fri, 18 Nov 2005 07:19:05 -0800 (PST)

 #include "https.h"
  
  /*+G
  **************************************************************************
  ** FUNCTION NAME:
  ** HttpsGet
  **________________________________________________________________________
  **
  ** PURPOSE:
  ** This function performs the HTTPS Get functionality.
  **________________________________________________________________________
  **
  ** FORMAT:
  ** HttpsInitialize
  ** ctxHttps
  ** strUrl
  ** strGetData
  ** strError
  ** strOutputFilename
  ** strSeedFile
  ** lConnectTimeout
  ** lTimeout
  ** bVerbose
  ** bVerifyHost
  ** bVerifyPeer
  ** ptrTraceFunction
  **________________________________________________________________________
  **
  ** ARGUMENTS:
  ** name: ctxHttps
  ** type: pointer to structure
  ** access: read
  ** mechanism: by reference
  **
  ** The ctxHttps argument contains the address of the HTTPS context
  ** block.
  **
  ** name: strUrl
  ** type: character string
  ** access: read
  ** mechanism: by reference
  **
  ** The strUrl argument contains the url to connect to.
  **
  ** name: strGetData
  ** type: character string
  ** access: read
  ** mechanism: by reference
  **
  ** The strGetData argument contains the data to use in the HTTPS Get.
  **
  ** name: strError
  ** type: character string
  ** access: write
  ** mechanism: by reference
  **
  ** The strError argument will return the error message.
  **
  ** name: strOutputFilename
  ** type: character string
  ** access: read
  ** mechanism: by reference
  **
  ** The strOutputFilename argument contains the file name to be created.
  **
  ** name: strSeedFile
  ** type: character string
  ** access: read
  ** mechanism: by reference
  **
  ** The strSeedFile argument contains the filename of the seed file.
  **
  ** name: lConnectTimeout
  ** type: long
  ** access: read
  ** mechanism: by value
  **
  ** The lConnectTimeout argument contains the connect timeout value.
  **
  ** name: lTimeout
  ** type: long
  ** access: read
  ** mechanism: by value
  **
  ** The lTimeout argument contains the timeout value.
  **
  ** name: bVerbose
  ** type: boolean
  ** access: read
  ** mechanism: by value
  **
  ** The bVerbose argument is set to TRUE to return verbose messages
  ** from libcurl.
  **
  ** name: bVerifyHost
  ** type: boolean
  ** access: read
  ** mechanism: by value
  **
  ** If the bVerifyHost argument is set to TRUE, libcurl will verify
  ** the host connection.
  **
  ** name: bVerifyPeer
  ** type: boolean
  ** access: read
  ** mechanism: by value
  **
  ** If the bVerifyPeer argument is set to TRUE, libcurl will verify
  ** the peer connection.
  **
  ** name: lCurlResponse
  ** type: long
  ** access: read
  ** mechanism: by value
  **
  ** The lCurlRespone argument contains the curl returned value if any.
  ** ** This value has to be checked before pages are imported into the
  ** ** Database.
  **
  ** name: ptrTraceFunction
  ** type: function pointer
  ** access: read
  ** mechanism: by value
  **
  ** The ptrTraceFunction argument can be a poiinter to a function to
  ** perform traceing or logging. Pass a NULL if this is not used.
  ** *NOTE* This is currently under development.
  **________________________________________________________________________
  **
  ** DESCRIPTION:
  ** This function performs the HTTPS Get functionality.
  ** Retrieving pages from the Send server, (Documents to be imported)
  **________________________________________________________________________
  **
  ** RETURNS:
  ** long
  ** HTTPS__SUCCESS
  ** HTTPS__ALLOCATIONERROR
  ** HTTPS__CURLSETOPTERROR
  ** HTTPS__CURLPERFORMERROR
  **________________________________________________________________________
  **
  ** MODIFICATIONS BY:
  **________________________________________________________________________
  **#@
  **************************************************************************
  -*/
  
  #ifdef __cplusplus
  extern "C" HTTPS_API long CALLBACK HttpsImportGet(
      DT_HTTPS_CTX *ctxHttps,
      char *strUrl,
      char *strGetData,
      char *strError,
      char *strOutputFilename,
      char *strSeedFile,
      long lConnectTimeout,
      long lTimeout,
      bool bVerbose,
      bool bVerifyHost,
      bool bVerifyPeer,
      long lCurlResponse,
      void *ptrTraceFunction)
  
  #else
  HTTPS_API long CALLBACK HttpsImportGet(
      DT_HTTPS_CTX *ctxHttps,
      char *strUrl,
      char *strGetData,
      char *strError,
      char *strOutputFilename,
      char *strSeedFile,
      long lConnectTimeout,
      long lTimeout,
      bool bVerbose,
      bool bVerifyHost,
      bool bVerifyPeer,
      long lCurlResponse,
      void *ptrTraceFunction)
  #endif
  {
      long lGetDataLength;
      char strCurlError[CURL_ERROR_SIZE + 1];
      char *ptrGetUrl;
      CURLcode res;
  
      /*
       * Initialize variables.
       */
      memset(strCurlError, 0, sizeof(strCurlError));
  
  
  
      /*--------------------------------------------------------------*/
      /*-- For a get operation, the parameters are part of the url. --*/
      /*--------------------------------------------------------------*/
      lGetDataLength = strlen(strUrl) + strlen(strGetData) + 2;
      ptrGetUrl = (char *) malloc(lGetDataLength);
      if (ptrGetUrl == NULL)
      {
          sprintf(strError, "Error allocating memory for the new url");
          return (HTTPS__ALLOCATIONERROR);
      }
  
      memset(ptrGetUrl, 0, lGetDataLength);
      if (strlen(strGetData))
      {
          sprintf(ptrGetUrl, "%s?%s", strUrl, strGetData);
      }
      else
      {
          sprintf(ptrGetUrl, "%s", strUrl);
      }
  
  
  
      /*---------------------------------------------*/
      /*-- Formatting URL to become http friendly --*/
      /*---------------------------------------------*/
      HttpsUrlFormatter(ptrGetUrl);
  
  
      /*----------------------------------------------------*/
      /*-- Set up the information for the write function. --*/
      /*----------------------------------------------------*/
      ctxHttps->fpFile = NULL;
      ctxHttps->strFilename = strOutputFilename;
  
  
  
      /*----------------------------------------------------*/
      /*-- Set up to do a Get. --*/
      /*----------------------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_HTTPGET, TRUE);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_HTTPGET), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
      /*----------------------------------------------------*/
      /*-- Set up the error buffer that curl should use --*/
      /*----------------------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_ERRORBUFFER, strCurlError);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_ERRORBUFFER), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
  
      /*----------------------------------------------------------*/
      /*-- Set up to pass our information to out write function --*/
      /*-- via curl --*/
      /*----------------------------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_WRITEDATA, ctxHttps);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_WRITEDATA), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
  
      /*----------------------------------------------------*/
      /*-- Set up the write function that curl will use. --*/
      /*----------------------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_WRITEFUNCTION, httpsFwrite);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_WRITEFUNCTION), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
      /*---------------------------------*/
      /*-- Set up for verbose message --*/
      /*---------------------------------*/
      if (bVerbose)
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_VERBOSE, TRUE);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_VERBOSE), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
  
  
      /*---------------------------------*/
      /*-- Set up the Connect timeout --*/
      /*---------------------------------*/
      if (lConnectTimeout)
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_CONNECTTIMEOUT, lConnectTimeout);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_CONNECTTIMEOUT), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
  
      /*---------------------------------*/
      /*-- Set up the max timeout. --*/
      /*---------------------------------*/
      if (lTimeout)
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_TIMEOUT, lTimeout);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_TIMEOUT), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
  
  
  
      /*--------------------------------------*/
      /*-- Set in the url to get data from. --*/
      /*--------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_URL, ptrGetUrl);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_URL), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
  
      /*--------------------------------------*/
      /*-- Set up the random seed file. --*/
      /*--------------------------------------*/
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_RANDOM_FILE, strSeedFile);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_RANDOM_FILE), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  
  
  
      /*----------------------------------------*/
      /*-- Set up to verify the peer for ssl. --*/
      /*----------------------------------------*/
      if (bVerifyPeer)
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_SSL_VERIFYPEER, TRUE);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_SSL_VERIFYPEER), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
      else
      {
  
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_SSL_VERIFYPEER), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
  
  
  
  
      /*---------------------------------------------*/
      /*-- Set up to verify the host for ssl --*/
      /*---------------------------------------------*/
      if (bVerifyHost)
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_SSL_VERIFYHOST, TRUE);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_SSL_VERIFYHOST), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
      else
      {
          res = curl_easy_setopt(ctxHttps->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
          if (res)
          {
               sprintf(strError, "Error in curl_easy_setopt(CURLOPT_SSL_VERIFYHOST), res: %ld", (long) res);
              return (HTTPS__CURLSETOPTERROR);
          }
      }
  
      /*---------------------------------*/
      /*-- Setting CURLOPT_FAILONERROR --*/
      /*---------------------------------*/
  /*--
      res = curl_easy_setopt(ctxHttps->curl, CURLOPT_FAILONERROR, 1);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_setopt(CURLOPT_FAILONERROR), res: %ld", (long) res);
          return (HTTPS__CURLSETOPTERROR);
      }
  --*/
  
      /*---------------------------------------------*/
      /*-- Perform the post operation --*/
      /*---------------------------------------------*/
      res = curl_easy_perform(ctxHttps->curl);
      if (res)
      {
          sprintf(strError, "Error in curl_easy_perform: %ld, %s", (long) res, strCurlError);
          return (HTTPS__CURLPERFORMERROR);
      }
  
  
  
      /*---------------------------------------------*/
      /*-- Perform the get info operation --*/
      /*---------------------------------------------*/
      if(curl_easy_getinfo(ctxHttps->curl, CURLINFO_HTTP_CODE, &lCurlResponse) != CURLE_OK)
      {
          sprintf(strError, "Error in curl_easy_getinfo: %ld", lCurlResponse);
      }
  
  
  
      /*---------------------------------------------*/
      /*-- Close the output file if it is open --*/
      /*---------------------------------------------*/
      if (ctxHttps->fpFile != NULL)
      {
          fclose(ctxHttps->fpFile);
          ctxHttps->fpFile = NULL;
      }
  
      return (HTTPS__SUCCESS);
  }
  
  

                
---------------------------------
 Yahoo! FareChase - Search multiple travel sites in one click.
Received on 2005-11-18