1 : /******************************************************************************
2 : * $Id: gdalhttp.cpp 25661 2013-02-22 11:35:12Z 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 : void CPLHTTPSetOptions(CURL *http_handle, char** papszOptions);
34 :
35 : /* CURLINFO_RESPONSE_CODE was known as CURLINFO_HTTP_CODE in libcurl 7.10.7 and earlier */
36 : #if LIBCURL_VERSION_NUM < 0x070a07
37 : #define CURLINFO_RESPONSE_CODE CURLINFO_HTTP_CODE
38 : #endif
39 :
40 76 : static size_t CPLHTTPWriteFunc(void *buffer, size_t count, size_t nmemb, void *req) {
41 76 : CPLHTTPRequest *psRequest = reinterpret_cast<CPLHTTPRequest *>(req);
42 76 : size_t size = count * nmemb;
43 :
44 76 : if (size == 0) return 0;
45 :
46 76 : const size_t required_size = psRequest->nDataLen + size + 1;
47 76 : if (required_size > psRequest->nDataAlloc) {
48 22 : size_t new_size = required_size * 2;
49 22 : if (new_size < 512) new_size = 512;
50 22 : psRequest->nDataAlloc = new_size;
51 22 : GByte * pabyNewData = reinterpret_cast<GByte *>(VSIRealloc(psRequest->pabyData, new_size));
52 22 : if (pabyNewData == NULL) {
53 0 : VSIFree(psRequest->pabyData);
54 0 : psRequest->pabyData = NULL;
55 0 : psRequest->pszError = CPLStrdup(CPLString().Printf("Out of memory allocating %u bytes for HTTP data buffer.", static_cast<int>(new_size)));
56 0 : psRequest->nDataAlloc = 0;
57 0 : psRequest->nDataLen = 0;
58 0 : return 0;
59 : }
60 22 : psRequest->pabyData = pabyNewData;
61 : }
62 76 : memcpy(psRequest->pabyData + psRequest->nDataLen, buffer, size);
63 76 : psRequest->nDataLen += size;
64 76 : psRequest->pabyData[psRequest->nDataLen] = 0;
65 76 : return nmemb;
66 : }
67 :
68 7 : void CPLHTTPInitializeRequest(CPLHTTPRequest *psRequest, const char *pszURL, const char *const *papszOptions) {
69 7 : psRequest->pszURL = CPLStrdup(pszURL);
70 7 : psRequest->papszOptions = CSLDuplicate(const_cast<char **>(papszOptions));
71 7 : psRequest->nStatus = 0;
72 7 : psRequest->pszContentType = 0;
73 7 : psRequest->pszError = 0;
74 7 : psRequest->pabyData = 0;
75 7 : psRequest->nDataLen = 0;
76 7 : psRequest->nDataAlloc = 0;
77 7 : psRequest->m_curl_handle = 0;
78 7 : psRequest->m_headers = 0;
79 7 : psRequest->m_curl_error = 0;
80 :
81 7 : psRequest->m_curl_handle = curl_easy_init();
82 7 : if (psRequest->m_curl_handle == NULL) {
83 0 : CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPInitializeRequest(): Unable to create CURL handle.");
84 : }
85 :
86 7 : char** papszOptionsDup = CSLDuplicate(const_cast<char **>(psRequest->papszOptions));
87 :
88 : /* Set User-Agent */
89 7 : const char *pszUserAgent = CSLFetchNameValue(papszOptionsDup, "USERAGENT");
90 7 : if (pszUserAgent == NULL)
91 : papszOptionsDup = CSLAddNameValue(papszOptionsDup, "USERAGENT",
92 7 : "GDAL WMS driver (http://www.gdal.org/frmt_wms.html)");
93 :
94 : /* Set URL */
95 7 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_URL, psRequest->pszURL);
96 :
97 : /* Set Headers (copied&pasted from cpl_http.cpp, but unused by callers of CPLHTTPInitializeRequest) .*/
98 7 : const char *headers = CSLFetchNameValue(const_cast<char **>(psRequest->papszOptions), "HEADERS");
99 7 : if (headers != NULL) {
100 0 : psRequest->m_headers = curl_slist_append(psRequest->m_headers, headers);
101 0 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_HTTPHEADER, psRequest->m_headers);
102 : }
103 :
104 7 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEDATA, psRequest);
105 7 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_WRITEFUNCTION, CPLHTTPWriteFunc);
106 :
107 7 : psRequest->m_curl_error = reinterpret_cast<char *>(CPLMalloc(CURL_ERROR_SIZE + 1));
108 7 : psRequest->m_curl_error[0] = '\0';
109 7 : curl_easy_setopt(psRequest->m_curl_handle, CURLOPT_ERRORBUFFER, psRequest->m_curl_error);
110 :
111 7 : CPLHTTPSetOptions(psRequest->m_curl_handle, papszOptionsDup);
112 :
113 7 : CSLDestroy(papszOptionsDup);
114 7 : }
115 :
116 7 : void CPLHTTPCleanupRequest(CPLHTTPRequest *psRequest) {
117 7 : if (psRequest->m_curl_handle) {
118 7 : curl_easy_cleanup(psRequest->m_curl_handle);
119 7 : psRequest->m_curl_handle = 0;
120 : }
121 7 : if (psRequest->m_headers) {
122 0 : curl_slist_free_all(psRequest->m_headers);
123 0 : psRequest->m_headers = 0;
124 : }
125 7 : if (psRequest->m_curl_error) {
126 7 : CPLFree(psRequest->m_curl_error);
127 7 : psRequest->m_curl_error = 0;
128 : }
129 :
130 7 : if (psRequest->pszContentType) {
131 7 : CPLFree(psRequest->pszContentType);
132 7 : psRequest->pszContentType = 0;
133 : }
134 7 : if (psRequest->pszError) {
135 0 : CPLFree(psRequest->pszError);
136 0 : psRequest->pszError = 0;
137 : }
138 7 : if (psRequest->pabyData) {
139 7 : CPLFree(psRequest->pabyData);
140 7 : psRequest->pabyData = 0;
141 7 : psRequest->nDataLen = 0;
142 7 : psRequest->nDataAlloc = 0;
143 : }
144 7 : if (psRequest->papszOptions) {
145 7 : CSLDestroy(psRequest->papszOptions);
146 7 : psRequest->papszOptions = 0;
147 : }
148 7 : if (psRequest->pszURL) {
149 7 : CPLFree(psRequest->pszURL);
150 7 : psRequest->pszURL = 0;
151 : }
152 7 : }
153 :
154 4 : CPLErr CPLHTTPFetchMulti(CPLHTTPRequest *pasRequest, int nRequestCount, const char *const *papszOptions) {
155 4 : CPLErr ret = CE_None;
156 4 : CURLM *curl_multi = 0;
157 : int still_running;
158 : int max_conn;
159 : int i, conn_i;
160 :
161 4 : const char *max_conn_opt = CSLFetchNameValue(const_cast<char **>(papszOptions), "MAXCONN");
162 8 : if (max_conn_opt && (max_conn_opt[0] != '\0')) {
163 4 : max_conn = MAX(1, MIN(atoi(max_conn_opt), 1000));
164 : } else {
165 0 : max_conn = 5;
166 : }
167 :
168 4 : curl_multi = curl_multi_init();
169 4 : if (curl_multi == NULL) {
170 0 : CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): Unable to create CURL multi-handle.");
171 : }
172 :
173 : // add at most max_conn requests
174 11 : for (conn_i = 0; conn_i < MIN(nRequestCount, max_conn); ++conn_i) {
175 7 : CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
176 7 : CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
177 7 : curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
178 : }
179 :
180 12 : while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
181 125 : while (still_running || (conn_i != nRequestCount)) {
182 : struct timeval timeout;
183 : fd_set fdread, fdwrite, fdexcep;
184 : int maxfd;
185 : CURLMsg *msg;
186 : int msgs_in_queue;
187 :
188 120 : do {
189 120 : msg = curl_multi_info_read(curl_multi, &msgs_in_queue);
190 120 : if (msg != NULL) {
191 3 : if (msg->msg == CURLMSG_DONE) { // transfer completed, check if we have more waiting and add them
192 3 : if (conn_i < nRequestCount) {
193 0 : CPLHTTPRequest *const psRequest = &pasRequest[conn_i];
194 0 : CPLDebug("HTTP", "Requesting [%d/%d] %s", conn_i + 1, nRequestCount, pasRequest[conn_i].pszURL);
195 0 : curl_multi_add_handle(curl_multi, psRequest->m_curl_handle);
196 0 : ++conn_i;
197 : }
198 : }
199 : }
200 : } while (msg != NULL);
201 117 : FD_ZERO(&fdread);
202 117 : FD_ZERO(&fdwrite);
203 117 : FD_ZERO(&fdexcep);
204 117 : curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
205 117 : timeout.tv_sec = 0;
206 117 : timeout.tv_usec = 100000;
207 117 : select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
208 174 : while (curl_multi_perform(curl_multi, &still_running) == CURLM_CALL_MULTI_PERFORM);
209 : }
210 :
211 4 : if (conn_i != nRequestCount) { // something gone really really wrong
212 0 : CPLError(CE_Fatal, CPLE_AppDefined, "CPLHTTPFetchMulti(): conn_i != nRequestCount, this should never happen ...");
213 : }
214 11 : for (i = 0; i < nRequestCount; ++i) {
215 7 : CPLHTTPRequest *const psRequest = &pasRequest[i];
216 :
217 7 : long response_code = 0;
218 7 : curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
219 7 : psRequest->nStatus = response_code;
220 :
221 7 : char *content_type = 0;
222 7 : curl_easy_getinfo(psRequest->m_curl_handle, CURLINFO_CONTENT_TYPE, &content_type);
223 7 : if (content_type) psRequest->pszContentType = CPLStrdup(content_type);
224 :
225 7 : if ((psRequest->pszError == NULL) && (psRequest->m_curl_error != NULL) && (psRequest->m_curl_error[0] != '\0')) {
226 0 : psRequest->pszError = CPLStrdup(psRequest->m_curl_error);
227 : }
228 :
229 : /* In the case of a file:// URL, curl will return a status == 0, so if there's no */
230 : /* error returned, patch the status code to be 200, as it would be for http:// */
231 7 : if (strncmp(psRequest->pszURL, "file://", 7) == 0 && psRequest->nStatus == 0 &&
232 : psRequest->pszError == NULL)
233 : {
234 0 : psRequest->nStatus = 200;
235 : }
236 :
237 : CPLDebug("HTTP", "Request [%d] %s : status = %d, content type = %s, error = %s",
238 : i, psRequest->pszURL, psRequest->nStatus,
239 : (psRequest->pszContentType) ? psRequest->pszContentType : "(null)",
240 7 : (psRequest->pszError) ? psRequest->pszError : "(null)");
241 :
242 7 : curl_multi_remove_handle(curl_multi, pasRequest[i].m_curl_handle);
243 : }
244 4 : curl_multi_cleanup(curl_multi);
245 :
246 4 : return ret;
247 : }
|