00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef HAVE_CURL
00037
00038 #include <pthread.h>
00039 #include "common.h"
00040 #include "rexp.h"
00041 #include "url.h"
00042 #include "tools.h"
00043 #include "config_manager.h"
00044
00045 using namespace zmm;
00046
00047 URL::URL(size_t buffer_hint)
00048 {
00049 this->buffer_hint = buffer_hint;
00050 }
00051
00052 Ref<StringBuffer> URL::download(String URL, long *HTTP_retcode,
00053 CURL *curl_handle, bool only_header,
00054 bool verbose, bool redirect)
00055 {
00056 CURLcode res;
00057 bool cleanup = false;
00058 char error_buffer[CURL_ERROR_SIZE] = {'\0'};
00059
00060 if (curl_handle == NULL)
00061 {
00062 curl_handle = curl_easy_init();
00063 cleanup = true;
00064 if (curl_handle == NULL)
00065 throw _Exception(_("Invalid curl handle!\n"));
00066 }
00067
00068 Ref<StringBuffer> buffer(new StringBuffer(buffer_hint));
00069
00070 curl_easy_reset(curl_handle);
00071
00072 if (verbose)
00073 {
00074 bool logEnabled;
00075 #ifdef TOMBDEBUG
00076 logEnabled = !ConfigManager::isDebugLogging();
00077 #else
00078 logEnabled = ConfigManager::isDebugLogging();
00079 #endif
00080 if (logEnabled)
00081 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
00082 }
00083
00084
00085 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
00086 "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.6) Gecko/20091216 Fedora/3.5.6-1.fc12 Firefox/3.5.6");
00087 curl_easy_setopt(curl_handle, CURLOPT_URL, URL.c_str());
00088 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
00089 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
00090
00093 if (only_header)
00094 {
00095 curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
00096 curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, URL::dl);
00097 curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA,
00098 (void *)buffer.getPtr());
00099 }
00100 else
00101 {
00102 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, URL::dl);
00103 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA,
00104 (void *)buffer.getPtr());
00105 }
00106
00107 if (redirect)
00108 {
00109 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
00110 curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, -1);
00111 }
00112
00113 res = curl_easy_perform(curl_handle);
00114 if (res != CURLE_OK)
00115 {
00116 log_error("%s\n", error_buffer);
00117 if (cleanup)
00118 curl_easy_cleanup(curl_handle);
00119 throw _Exception(error_buffer);
00120 }
00121
00122 res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, HTTP_retcode);
00123 if (res != CURLE_OK)
00124 {
00125 log_error("%s\n", error_buffer);
00126 if (cleanup)
00127 curl_easy_cleanup(curl_handle);
00128 throw _Exception(error_buffer);
00129 }
00130
00131 if (cleanup)
00132 curl_easy_cleanup(curl_handle);
00133
00134 return buffer;
00135 }
00136
00137 Ref<URL::Stat> URL::getInfo(String URL, CURL *curl_handle)
00138 {
00139 long retcode;
00140 bool cleanup = false;
00141 CURLcode res;
00142 double cl;
00143 char *ct;
00144 char *c_url;
00145 char error_buffer[CURL_ERROR_SIZE] = {'\0'};
00146 String mt;
00147 String used_url;
00148
00149 if (curl_handle == NULL)
00150 {
00151 curl_handle = curl_easy_init();
00152 cleanup = true;
00153 if (curl_handle == NULL)
00154 throw _Exception(_("Invalid curl handle!\n"));
00155 }
00156
00157 Ref<StringBuffer> buffer = download(URL, &retcode, curl_handle, true, true, true);
00158 if (retcode != 200)
00159 {
00160 if (cleanup)
00161 curl_easy_cleanup(curl_handle);
00162 throw _Exception(_("Error retrieving information from ") +
00163 URL + _(" HTTP return code: ") +
00164 String::from(retcode));
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 res = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
00212 if (res != CURLE_OK)
00213 {
00214 log_error("%s\n", error_buffer);
00215 if (cleanup)
00216 curl_easy_cleanup(curl_handle);
00217 throw _Exception(error_buffer);
00218 }
00219
00220 res = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &ct);
00221 if (res != CURLE_OK)
00222 {
00223 log_error("%s\n", error_buffer);
00224 if (cleanup)
00225 curl_easy_cleanup(curl_handle);
00226 throw _Exception(error_buffer);
00227 }
00228
00229 if (ct == NULL)
00230 mt = _(MIMETYPE_DEFAULT);
00231 else
00232 mt = ct;
00233
00234 log_debug("Extracted content length: %lld\n", (long long)cl);
00235
00236 res = curl_easy_getinfo(curl_handle, CURLINFO_EFFECTIVE_URL, &c_url);
00237 if (res != CURLE_OK)
00238 {
00239 log_error("%s\n", error_buffer);
00240 if (cleanup)
00241 curl_easy_cleanup(curl_handle);
00242 throw _Exception(error_buffer);
00243 }
00244
00245 if (c_url == NULL)
00246 used_url = URL;
00247 else
00248 used_url = c_url;
00249
00250 Ref<Stat> st(new Stat(used_url, (off_t)cl, mt));
00251
00252 if (cleanup)
00253 curl_easy_cleanup(curl_handle);
00254
00255 return st;
00256 }
00257
00258
00259 size_t URL::dl(void *buf, size_t size, size_t nmemb, void *data)
00260 {
00261 StringBuffer *buffer = (StringBuffer *)data;
00262 if (buffer == NULL)
00263 return 0;
00264
00265 size_t s = size * nmemb;
00266 *buffer << String((char *)buf, s);
00267
00268 return s;
00269 }
00270
00271 #endif//HAVE_CURL