26 #include <curl/curl.h>
27 #include <curl/easy.h>
49 if (out && !out->fd && !out->destBuf) {
56 int s = (int)out->destBuf->size();
57 out->destBuf->size(s+(size*nmemb));
58 memcpy(out->destBuf->getRawData()+s,
buffer, size*nmemb);
65 static int my_httpfprogress(
void *clientp,
double dltotal,
double dlnow,
double ultotal,
double ulnow) {
67 if (dltotal < 0) dltotal = 0;
68 if (dlnow < 0) dlnow = 0;
69 if (dlnow > dltotal) dlnow = dltotal;
76 static int myhttp_trace(CURL *handle, curl_infotype type,
unsigned char *data,
size_t size,
void *userp) {
82 case CURLINFO_TEXT: header =
"TEXT";
break;
83 case CURLINFO_HEADER_OUT: header =
"=> Send header";
break;
84 case CURLINFO_HEADER_IN: header =
"<= Recv header";
break;
87 case CURLINFO_DATA_OUT: header =
"=> Send data";
88 case CURLINFO_SSL_DATA_OUT: header =
"=> Send SSL data";
89 case CURLINFO_DATA_IN: header =
"<= Recv data";
90 case CURLINFO_SSL_DATA_IN: header =
"<= Recv SSL data";
95 if (size > 120) size = 120;
106 session = (CURL *)curl_easy_init();
116 signed char retVal = 0;
117 struct FtpFile ftpfile = {destPath, 0, destBuf};
122 curl_easy_setopt(
session, CURLOPT_URL, sourceURL);
124 SWBuf credentials =
u +
":" +
p;
125 curl_easy_setopt(
session, CURLOPT_USERPWD, credentials.
c_str());
128 curl_easy_setopt(
session, CURLOPT_FTPPORT,
"-");
129 curl_easy_setopt(
session, CURLOPT_NOPROGRESS, 0);
130 curl_easy_setopt(
session, CURLOPT_FAILONERROR, 1);
135 curl_easy_setopt(
session, CURLOPT_FILE, &ftpfile);
138 curl_easy_setopt(
session, CURLOPT_VERBOSE,
true);
149 curl_easy_setopt(
session, CURLOPT_SSL_VERIFYPEER,
false);
154 #if (LIBCURL_VERSION_MAJOR > 7) || \
155 ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
156 ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
157 # define EPRT_AVAILABLE 1
160 #ifdef EPRT_AVAILABLE
161 curl_easy_setopt(
session, CURLOPT_FTP_USE_EPRT, 0);
162 SWLOGD(
"***** using CURLOPT_FTP_USE_EPRT\n");
166 SWLOGD(
"***** About to perform curl easy action. \n");
167 SWLOGD(
"***** destPath: %s \n", destPath);
168 SWLOGD(
"***** sourceURL: %s \n", sourceURL);
169 res = curl_easy_perform(
session);
170 SWLOGD(
"***** Finished performing curl easy action. \n");
172 if(CURLE_OK != res) {
173 if (CURLE_OPERATION_TIMEDOUT == res
174 #ifdef CURLE_FTP_ACCEPT_TIMEOUT
175 || CURLE_FTP_ACCEPT_TIMEOUT == res
195 const char *listing =
buffer;
198 pEnd = strstr(listing,
"<td");
203 pEnd = strstr(listing,
"<td");
207 pEnd = strchr(listing,
'>');
217 vector<struct DirEntry> dirList;
224 int possibleNameLength = 0;
226 if (!
getURL(
"", dirURL, &dirBuf)) {
227 pBuf = strstr(dirBuf,
"<a href=\"");
228 while (pBuf !=
NULL) {
230 pBufRes = (
char *)strchr(pBuf,
'\"');
233 possibleNameLength = (int)(pBufRes - pBuf);
234 possibleName.
setFormatted(
"%.*s", possibleNameLength, pBuf);
235 if (isalnum(possibleName[0])) {
236 SWLOGD(
"getDirListHTTP: Found a file: %s", possibleName.
c_str());
240 if(pBufRes !=
NULL) {
242 fSize = strtod(pBuf, &pBufRes);
243 if (pBufRes[0] ==
'K')
245 else if (pBufRes[0] ==
'M')
250 i.
name = possibleName;
251 i.
size = (
long unsigned int)fSize;
253 dirList.push_back(i);
255 pBuf += possibleNameLength;
258 pBuf = strstr(pBuf,
"<a href=\"");
#define SWORD_NAMESPACE_START
static SWLog * getSystemLog()
static void closeFile(int fd)
CURLHTTPTransport(const char *host, StatusReporter *statusReporter=0)
static int my_httpfprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
bool endsWith(const SWBuf &postfix) const
const char * findSizeStart(const char *buffer)
const char * c_str() const
virtual std::vector< struct DirEntry > getDirList(const char *dirURL)
unsigned long size() const
static long write(int fd, const void *buf, long count)
void logWarning(const char *fmt,...) const
bool isUnverifiedPeerAllowed()
static int createPathAndFile(const char *fName)
static int my_httpfwrite(void *buffer, size_t size, size_t nmemb, void *stream)
static int myhttp_trace(CURL *handle, curl_infotype type, unsigned char *data, size_t size, void *userp)
#define SWORD_NAMESPACE_END
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
SWBuf & setFormatted(const char *format,...)
StatusReporter * statusReporter