1 : /******************************************************************************
2 : * $Id: gdalhttp.cpp 18020 2009-11-14 14:33:20Z 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 16 : static size_t CPLHTTPWriteFunc(void *buffer, size_t count, size_t nmemb, void *req) {
39 16 : CPLHTTPRequest *psRequest = reinterpret_cast<CPLHTTPRequest *>(req);
40 16 : size_t size = count * nmemb;
41 :
42 16 : if (size == 0) return 0;
43 :
44 16 : const size_t required_size = psRequest->nDataLen + size + 1;
45 16 : if (required_size > psRequest->nDataAlloc) {
46 8 : size_t new_size = required_size * 2;
47 8 : if (new_size < 512) new_size = 512;
48 8 : psRequest->nDataAlloc = new_size;
49 8 : GByte * pabyNewData = reinterpret_cast<GByte *>(VSIRealloc(psRequest->pabyData, new_size));
50 8 : 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 8 : psRequest->pabyData = pabyNewData;
59 : }
60 16 : memcpy(psRequest->pabyData + psRequest->nDataLen, buffer, size);
61 16 : psRequest->nDataLen += size;
62 16 : psRequest->pabyData[psRequest->nDataLen] = 0;
63 16 : 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 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_USERAGENT, "GDAL WMS driver (http://www.gdal.org/frmt_wms.html)");
85 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_URL, psRequest->pszURL);
86 :
87 3 : const char *timeout = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "TIMEOUT");
88 3 : if (timeout != NULL) {
89 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_TIMEOUT, atoi(timeout));
90 : }
91 :
92 3 : const char *headers = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "HEADERS");
93 3 : if (headers != NULL) {
94 0 : psRequest->m_headers = curl_slist_append(psRequest->m_headers, headers);
95 0 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_HTTPHEADER, psRequest->m_headers);
96 : }
97 :
98 : /* Enable following redirections. Requires libcurl 7.10.1 at least */
99 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_FOLLOWLOCATION, 1);
100 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_MAXREDIRS, 10);
101 :
102 : /* NOSIGNAL should be set to true for timeout to work in multithread
103 : environments on Unix, requires libcurl 7.10 or more recent.
104 : (this force avoiding the use of sgnal handlers) */
105 : #ifdef CURLOPT_NOSIGNAL
106 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_NOSIGNAL, 1);
107 : #endif
108 :
109 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEDATA, psRequest);
110 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEFUNCTION, CPLHTTPWriteFunc);
111 :
112 3 : psRequest->m_curl_error = reinterpret_cast<char *>(CPLMalloc(CURL_ERROR_SIZE + 1));
113 3 : psRequest->m_curl_error[0] = '\0';
114 3 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_ERRORBUFFER, psRequest->m_curl_error);
115 3 : }
116 :
117 3 : void CPLHTTPCleanupRequest(CPLHTTPRequest *psRequest) {
118 3 : if (psRequest->m_curl_handle) {
119 3 : curl_easy_cleanup(psRequest->m_curl_handle);
120 3 : psRequest->m_curl_handle = 0;
121 : }
122 3 : if (psRequest->m_headers) {
123 0 : curl_slist_free_all(psRequest->m_headers);
124 0 : psRequest->m_headers = 0;
125 : }
126 3 : if (psRequest->m_curl_error) {
127 3 : CPLFree(psRequest->m_curl_error);
128 3 : psRequest->m_curl_error = 0;
129 : }
130 :
131 3 : if (psRequest->pszContentType) {
132 3 : CPLFree(psRequest->pszContentType);
133 3 : psRequest->pszContentType = 0;
134 : }
135 3 : if (psRequest->pszError) {
136 0 : CPLFree(psRequest->pszError);
137 0 : psRequest->pszError = 0;
138 : }
139 3 : if (psRequest->pabyData) {
140 3 : CPLFree(psRequest->pabyData);
141 3 : psRequest->pabyData = 0;
142 3 : psRequest->nDataLen = 0;
143 3 : psRequest->nDataAlloc = 0;
144 : }
145 3 : if (psRequest->papszOptions) {
146 3 : CSLDestroy(psRequest->papszOptions);
147 3 : psRequest->papszOptions = 0;
148 : }
149 3 : if (psRequest->pszURL) {
150 3 : CPLFree(psRequest->pszURL);
151 3 : psRequest->pszURL = 0;
152 : }
153 3 : }
154 :
155 2 : CPLErr CPLHTTPFetchMulti(CPLHTTPRequest *pasRequest, int nRequestCount, const char *const *papszOptions) {
156 2 : CPLErr ret = CE_None;
157 2 : CURLM *curl_multi = 0;
158 : int still_running;
159 : int max_conn;
160 : int i, conn_i;
161 :
162 2 : const char *max_conn_opt = CSLFetchNameValue(const_cast<char **>(papszOptions), "MAXCONN");
163 4 : if (max_conn_opt && (max_conn_opt[0] != '\0')) {
164 2 : max_conn = MAX(1, MIN(atoi(max_conn_opt), 1000));
165 : } else {
166 0 : max_conn = 5;
167 : }
168 :
169 2 : curl_multi = curl_multi_init();
170 2 : if (curl_multi == NULL) {
171 0 : CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): Unable to create CURL multi-handle.");
172 : }
173 :
174 : // add at most max_conn requests
175 5 : for (conn_i = 0; conn_i < MIN(nRequestCount, max_conn); ++conn_i) {
176 3 : CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
177 3 : CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
178 3 : curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
179 : }
180 :
181 6 : while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
182 27 : while (still_running || (conn_i != nRequestCount)) {
183 : struct timeval timeout;
184 : fd_set fdread, fdwrite, fdexcep;
185 : int maxfd;
186 : CURLMsg *msg;
187 : int msgs_in_queue;
188 :
189 24 : do {
190 24 : msg = curl_multi_info_read(curl_multi, &msgs_in_queue);
191 24 : if (msg != NULL) {
192 1 : if (msg->msg == CURLMSG_DONE) { // transfer completed, check if we have more waiting and add them
193 1 : if (conn_i < nRequestCount) {
194 0 : CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
195 0 : CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
196 0 : curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
197 0 : ++conn_i;
198 : }
199 : }
200 : }
201 : } while (msg != NULL);
202 23 : FD_ZERO(&fdread);
203 23 : FD_ZERO(&fdwrite);
204 23 : FD_ZERO(&fdexcep);
205 23 : curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
206 23 : timeout.tv_sec = 0;
207 23 : timeout.tv_usec = 100000;
208 23 : select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
209 38 : while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
210 : }
211 :
212 2 : if (conn_i != nRequestCount) { // something gone really really wrong
213 0 : CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): conn_i != nRequestCount, this should never happen ...");
214 : }
215 5 : for (i = 0; i < nRequestCount; ++i) {
216 3 : CPLHTTPRequest *const psRequest = &pasRequest[i];
217 :
218 3 : long response_code = 0;
219 3 : curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
220 3 : psRequest->nStatus = response_code;
221 :
222 3 : char *content_type = 0;
223 3 : curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_CONTENT_TYPE, &content_type);
224 3 : if (content_type) psRequest->pszContentType = CPLStrdup(content_type);
225 :
226 3 : if ((psRequest->pszError == NULL) && (psRequest->m_curl_error != NULL) && (psRequest->m_curl_error[0] != '\0')) {
227 0 : psRequest->pszError = CPLStrdup(psRequest->m_curl_error);
228 : }
229 :
230 : CPLDebug("HTTP", "Request [%d] %s : status = %d, content type = %s, error = %s",
231 : conn_i, psRequest->pszURL, psRequest->nStatus,
232 : (psRequest->pszContentType) ? psRequest->pszContentType : "(null)",
233 3 : (psRequest->pszError) ? psRequest->pszError : "(null)");
234 :
235 3 : curl_multi_remove_handle(curl_multi, pasRequest[i].m_curl_handle);
236 : }
237 2 : curl_multi_cleanup(curl_multi);
238 :
239 2 : return ret;
240 : }
|