LTP GCOV extension - code coverage report
Current view: directory - frmts/wms - gdalhttp.cpp
Test: gdal_filtered.info
Date: 2010-07-12 Instrumented lines: 134
Code covered: 84.3 % Executed lines: 113

       1                 : /******************************************************************************
       2                 :  * $Id: gdalhttp.cpp 19082 2010-03-14 19:05:28Z rouault $
       3                 :  *
       4                 :  * Project:  WMS Client Driver
       5                 :  * Purpose:  Implementation of Dataset and RasterBand classes for WMS
       6                 :  *           and other similar services.
       7                 :  * Author:   Adam Nowacki, nowak@xpam.de
       8                 :  *
       9                 :  ******************************************************************************
      10                 :  * Copyright (c) 2007, Adam Nowacki
      11                 :  *
      12                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      13                 :  * copy of this software and associated documentation files (the "Software"),
      14                 :  * to deal in the Software without restriction, including without limitation
      15                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16                 :  * and/or sell copies of the Software, and to permit persons to whom the
      17                 :  * Software is furnished to do so, subject to the following conditions: 
      18                 :  *
      19                 :  * The above copyright notice and this permission notice shall be included
      20                 :  * in all copies or substantial portions of the Software.
      21                 :  *
      22                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28                 :  * DEALINGS IN THE SOFTWARE.
      29                 :  ****************************************************************************/
      30                 : 
      31                 : #include "stdinc.h"
      32                 : 
      33                 : /* CURLINFO_RESPONSE_CODE was known as CURLINFO_HTTP_CODE in libcurl 7.10.7 and earlier */
      34                 : #if LIBCURL_VERSION_NUM < 0x070a07
      35                 : #define CURLINFO_RESPONSE_CODE CURLINFO_HTTP_CODE
      36                 : #endif
      37                 : 
      38              28 : static size_t CPLHTTPWriteFunc(void *buffer, size_t count, size_t nmemb, void *req) {
      39              28 :     CPLHTTPRequest *psRequest = reinterpret_cast<CPLHTTPRequest *>(req);
      40              28 :     size_t size = count * nmemb;
      41                 : 
      42              28 :     if (size == 0) return 0;
      43                 : 
      44              28 :     const size_t required_size = psRequest->nDataLen + size + 1;
      45              28 :     if (required_size > psRequest->nDataAlloc) {
      46              10 :         size_t new_size = required_size * 2;
      47              10 :         if (new_size < 512) new_size = 512;
      48              10 :         psRequest->nDataAlloc = new_size;
      49              10 :         GByte * pabyNewData = reinterpret_cast<GByte *>(VSIRealloc(psRequest->pabyData, new_size));
      50              10 :         if (pabyNewData == NULL) {
      51               0 :             VSIFree(psRequest->pabyData);
      52               0 :             psRequest->pabyData = NULL;
      53               0 :             psRequest->pszError = CPLStrdup(CPLString().Printf("Out of memory allocating %u bytes for HTTP data buffer.", static_cast<int>(new_size)));
      54               0 :             psRequest->nDataAlloc = 0;
      55               0 :             psRequest->nDataLen = 0;
      56               0 :             return 0;
      57                 :         }
      58              10 :         psRequest->pabyData = pabyNewData;
      59                 :     }
      60              28 :     memcpy(psRequest->pabyData + psRequest->nDataLen, buffer, size);
      61              28 :     psRequest->nDataLen += size;
      62              28 :     psRequest->pabyData[psRequest->nDataLen] = 0;
      63              28 :     return nmemb;
      64                 : }
      65                 : 
      66               3 : void CPLHTTPInitializeRequest(CPLHTTPRequest *psRequest, const char *pszURL, const char *const *papszOptions) {
      67               3 :     psRequest->pszURL = CPLStrdup(pszURL);
      68               3 :     psRequest->papszOptions = CSLDuplicate(const_cast<char **>(papszOptions));
      69               3 :     psRequest->nStatus = 0;
      70               3 :     psRequest->pszContentType = 0;
      71               3 :     psRequest->pszError = 0;
      72               3 :     psRequest->pabyData = 0;
      73               3 :     psRequest->nDataLen = 0;
      74               3 :     psRequest->nDataAlloc = 0;
      75               3 :     psRequest->m_curl_handle = 0;
      76               3 :     psRequest->m_headers = 0;
      77               3 :     psRequest->m_curl_error = 0;
      78                 : 
      79               3 :     psRequest->m_curl_handle = curl_easy_init();
      80               3 :     if (psRequest->m_curl_handle == NULL) {
      81               0 :         CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPInitializeRequest(): Unable to create CURL handle.");
      82                 :     }
      83                 : 
      84                 :     /* Set User-Agent */
      85               3 :     const char *pszUserAgent = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "USERAGENT");
      86               3 :     if (pszUserAgent == NULL)
      87               3 :         pszUserAgent = "GDAL WMS driver (http://www.gdal.org/frmt_wms.html)";
      88               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_USERAGENT, pszUserAgent);
      89                 : 
      90                 :     /* Set URL */
      91               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_URL, psRequest->pszURL);
      92                 : 
      93                 :     /* Set timeout.*/
      94               3 :     const char *timeout = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "TIMEOUT");
      95               3 :     if (timeout != NULL) {
      96               3 :         curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_TIMEOUT, atoi(timeout));
      97                 :     }
      98                 : 
      99                 :     /* Set Headers (copied&pasted from cpl_http.cpp, but unused by callers of CPLHTTPInitializeRequest) .*/
     100               3 :     const char *headers = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "HEADERS");
     101               3 :     if (headers != NULL) {
     102               0 :         psRequest->m_headers = curl_slist_append(psRequest->m_headers, headers);
     103               0 :         curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_HTTPHEADER, psRequest->m_headers);
     104                 :     }
     105                 : 
     106                 :     /* Enable following redirections.  Requires libcurl 7.10.1 at least */
     107               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_FOLLOWLOCATION, 1);
     108               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_MAXREDIRS, 10);
     109                 : 
     110                 :     /* NOSIGNAL should be set to true for timeout to work in multithread
     111                 :     environments on Unix, requires libcurl 7.10 or more recent.
     112                 :     (this force avoiding the use of sgnal handlers) */
     113                 : #ifdef CURLOPT_NOSIGNAL
     114                 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_NOSIGNAL, 1);
     115                 : #endif
     116                 : 
     117               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEDATA, psRequest);
     118               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEFUNCTION, CPLHTTPWriteFunc);
     119                 : 
     120               3 :     psRequest->m_curl_error = reinterpret_cast<char *>(CPLMalloc(CURL_ERROR_SIZE + 1));
     121               3 :     psRequest->m_curl_error[0] = '\0';
     122               3 :     curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_ERRORBUFFER, psRequest->m_curl_error);
     123               3 : }
     124                 : 
     125               3 : void CPLHTTPCleanupRequest(CPLHTTPRequest *psRequest) {
     126               3 :     if (psRequest->m_curl_handle) {
     127               3 :         curl_easy_cleanup(psRequest->m_curl_handle);
     128               3 :         psRequest->m_curl_handle = 0;
     129                 :     }
     130               3 :     if (psRequest->m_headers) {
     131               0 :         curl_slist_free_all(psRequest->m_headers);
     132               0 :         psRequest->m_headers = 0;
     133                 :     }
     134               3 :     if (psRequest->m_curl_error) {
     135               3 :         CPLFree(psRequest->m_curl_error);
     136               3 :         psRequest->m_curl_error = 0;
     137                 :     }
     138                 : 
     139               3 :     if (psRequest->pszContentType) {
     140               3 :         CPLFree(psRequest->pszContentType);
     141               3 :         psRequest->pszContentType = 0;
     142                 :     }
     143               3 :     if (psRequest->pszError) {
     144               0 :         CPLFree(psRequest->pszError);
     145               0 :         psRequest->pszError = 0;
     146                 :     }
     147               3 :     if (psRequest->pabyData) {
     148               3 :         CPLFree(psRequest->pabyData);
     149               3 :         psRequest->pabyData = 0;
     150               3 :         psRequest->nDataLen = 0;
     151               3 :         psRequest->nDataAlloc = 0;
     152                 :     }
     153               3 :     if (psRequest->papszOptions) {
     154               3 :         CSLDestroy(psRequest->papszOptions);
     155               3 :         psRequest->papszOptions = 0;
     156                 :     }
     157               3 :     if (psRequest->pszURL) {
     158               3 :         CPLFree(psRequest->pszURL);
     159               3 :         psRequest->pszURL = 0;
     160                 :     }
     161               3 : }
     162                 : 
     163               2 : CPLErr CPLHTTPFetchMulti(CPLHTTPRequest *pasRequest, int nRequestCount, const char *const *papszOptions) {
     164               2 :     CPLErr ret = CE_None;
     165               2 :     CURLM *curl_multi = 0;
     166                 :     int still_running;
     167                 :     int max_conn;
     168                 :     int i, conn_i;
     169                 : 
     170               2 :     const char *max_conn_opt = CSLFetchNameValue(const_cast<char **>(papszOptions), "MAXCONN");
     171               4 :     if (max_conn_opt && (max_conn_opt[0] != '\0')) {
     172               2 :         max_conn = MAX(1, MIN(atoi(max_conn_opt), 1000));
     173                 :     } else {
     174               0 :         max_conn = 5;
     175                 :     }
     176                 : 
     177               2 :     curl_multi = curl_multi_init();
     178               2 :     if (curl_multi == NULL) {
     179               0 :         CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): Unable to create CURL multi-handle.");
     180                 :     }
     181                 : 
     182                 :     // add at most max_conn requests
     183               5 :     for (conn_i = 0; conn_i < MIN(nRequestCount, max_conn); ++conn_i) {
     184               3 :         CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
     185               3 :         CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
     186               3 :         curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
     187                 :     }
     188                 : 
     189               6 :     while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
     190              44 :     while (still_running || (conn_i != nRequestCount)) {
     191                 :         struct timeval timeout;
     192                 :         fd_set fdread, fdwrite, fdexcep;
     193                 :         int maxfd;
     194                 :         CURLMsg *msg;
     195                 :         int msgs_in_queue;
     196                 : 
     197              41 :         do {
     198              41 :             msg = curl_multi_info_read(curl_multi, &msgs_in_queue);
     199              41 :             if (msg != NULL) {
     200               1 :                 if (msg->msg == CURLMSG_DONE) { // transfer completed, check if we have more waiting and add them
     201               1 :                     if (conn_i < nRequestCount) {
     202               0 :                         CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
     203               0 :                         CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
     204               0 :                         curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
     205               0 :                         ++conn_i;
     206                 :                     }
     207                 :                 }
     208                 :             }
     209                 :         } while (msg != NULL);
     210              40 :         FD_ZERO(&fdread);
     211              40 :         FD_ZERO(&fdwrite);
     212              40 :         FD_ZERO(&fdexcep);
     213              40 :         curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
     214              40 :         timeout.tv_sec = 0;
     215              40 :         timeout.tv_usec = 100000;
     216              40 :         select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
     217              58 :         while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
     218                 :     }
     219                 : 
     220               2 :     if (conn_i != nRequestCount) { // something gone really really wrong
     221               0 :         CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): conn_i != nRequestCount, this should never happen ...");
     222                 :     }
     223               5 :     for (i = 0; i < nRequestCount; ++i) {
     224               3 :         CPLHTTPRequest *const psRequest = &pasRequest[i];
     225                 : 
     226               3 :         long response_code = 0;
     227               3 :         curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
     228               3 :         psRequest->nStatus = response_code;
     229                 : 
     230               3 :         char *content_type = 0;
     231               3 :         curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_CONTENT_TYPE, &content_type);
     232               3 :         if (content_type) psRequest->pszContentType = CPLStrdup(content_type);
     233                 : 
     234               3 :         if ((psRequest->pszError == NULL) && (psRequest->m_curl_error != NULL) && (psRequest->m_curl_error[0] != '\0')) {
     235               0 :             psRequest->pszError = CPLStrdup(psRequest->m_curl_error);
     236                 :         }
     237                 :         
     238                 :         CPLDebug("HTTP", "Request [%d] %s : status = %d, content type = %s, error = %s",
     239                 :                  conn_i, psRequest->pszURL, psRequest->nStatus,
     240                 :                  (psRequest->pszContentType) ? psRequest->pszContentType : "(null)",
     241               3 :                  (psRequest->pszError) ? psRequest->pszError : "(null)");
     242                 : 
     243               3 :         curl_multi_remove_handle(curl_multi, pasRequest[i].m_curl_handle);
     244                 :     }
     245               2 :     curl_multi_cleanup(curl_multi);
     246                 : 
     247               2 :     return ret;
     248                 : }

Generated by: LTP GCOV extension version 1.5