The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CURLHTTPTransport Class Reference

#include <curlhttpt.h>

+ Inheritance diagram for CURLHTTPTransport:
+ Collaboration diagram for CURLHTTPTransport:

Public Member Functions

int copyDirectory (const char *urlPrefix, const char *dir, const char *dest, const char *suffix)
 
 CURLHTTPTransport (const char *host, StatusReporter *statusReporter=0)
 
virtual std::vector< struct
DirEntry
getDirList (const char *dirURL)
 
long getTimeoutMillis ()
 
virtual char getURL (const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
 
bool isPassive ()
 
bool isUnverifiedPeerAllowed ()
 
virtual char putURL (const char *destURL, const char *sourcePath, SWBuf *sourceBuf=0)
 
void setPassive (bool passive)
 
void setPasswd (const char *passwd)
 
void setTimeoutMillis (long timeoutMillis)
 
void setUnverifiedPeerAllowed (bool val)
 
void setUser (const char *user)
 
void terminate ()
 
 ~CURLHTTPTransport ()
 

Protected Attributes

SWBuf host
 
SWBuf p
 
bool passive
 
StatusReporterstatusReporter
 
bool term
 
long timeoutMillis
 
SWBuf u
 
bool unverifiedPeerAllowed
 

Private Attributes

CURL * session
 

Detailed Description

Definition at line 34 of file curlhttpt.h.

Constructor & Destructor Documentation

CURLHTTPTransport::CURLHTTPTransport ( const char *  host,
StatusReporter statusReporter = 0 
)

Definition at line 105 of file curlhttpt.cpp.

105  : RemoteTransport(host, sr) {
106  session = (CURL *)curl_easy_init();
107 }
RemoteTransport(const char *host, StatusReporter *statusReporter=0)
Definition: remotetrans.cpp:62
CURLHTTPTransport::~CURLHTTPTransport ( )

Definition at line 110 of file curlhttpt.cpp.

110  {
111  curl_easy_cleanup(session);
112 }

Member Function Documentation

int RemoteTransport::copyDirectory ( const char *  urlPrefix,
const char *  dir,
const char *  dest,
const char *  suffix 
)
inherited

network copy recursively a remote directly

Returns
error status 0: OK; -1: operation error, -2: connection error; -3: user requested termination

Definition at line 140 of file remotetrans.cpp.

140  {
141 SWLOGD("RemoteTransport::copyDirectory");
142  int retVal = 0;
143 
144  SWBuf url = SWBuf(urlPrefix) + SWBuf(dir);
145  removeTrailingSlash(url);
146  url += '/';
147 
148 SWLOGD("NetTransport: getting dir %s\n", url.c_str());
149  vector<struct DirEntry> dirList = getDirList(url.c_str());
150 
151  if (!dirList.size()) {
152  SWLog::getSystemLog()->logWarning("NetTransport: failed to read dir %s\n", url.c_str());
153  return -1;
154  }
155 
156  // append files in sub directories and calculate total download size
157  unsigned int i = 0;
158  long totalBytes = 0;
159  for (;;) {
160  if (i == dirList.size())
161  break;
162 
163  struct DirEntry &e = dirList.at(i);
164 
165  if (e.isDirectory) {
166  SWBuf name(e.name); // &e will be invalidated after first insertion
167  vector<struct DirEntry> sd = getDirList((url + name + '/').c_str());
168  for (unsigned int ii = 0; ii < sd.size(); ii++) {
169  sd[ii].name = name + '/' + sd[ii].name;
170  dirList.push_back(sd[ii]);
171  }
172  dirList.erase(dirList.begin() + i);
173  }
174  else {
175  totalBytes += e.size;
176  i++;
177  }
178  }
179 
180  long completedBytes = 0;
181  for (i = 0; i < dirList.size(); i++) {
182  struct DirEntry &dirEntry = dirList[i];
183  SWBuf buffer = (SWBuf)dest;
184  removeTrailingSlash(buffer);
185  buffer += "/";
186  buffer += dirEntry.name;
187  if (!strcmp(&buffer.c_str()[buffer.length()-strlen(suffix)], suffix)) {
188  SWBuf buffer2 = "Downloading (";
189  buffer2.appendFormatted("%d", i+1);
190  buffer2 += " of ";
191  buffer2.appendFormatted("%d", dirList.size());
192  buffer2 += "): ";
193  buffer2 += dirEntry.name;
194  if (statusReporter)
195  statusReporter->preStatus(totalBytes, completedBytes, buffer2.c_str());
196  FileMgr::createParent(buffer.c_str()); // make sure parent directory exists
197  SWTRY {
198  SWBuf url = (SWBuf)urlPrefix + (SWBuf)dir;
199  removeTrailingSlash(url);
200  url += "/";
201  url += dirEntry.name;
202  retVal = getURL(buffer.c_str(), url.c_str());
203  if (retVal) {
204  SWLog::getSystemLog()->logWarning("copyDirectory: failed to get file %s\n", url.c_str());
205  return retVal;
206  }
207  completedBytes += dirEntry.size;
208  }
209  SWCATCH (...) {}
210  if (term) {
211  retVal = -3;
212  break;
213  }
214  }
215  }
216  return retVal;
217 }
bool isDirectory
Definition: filemgr.h:43
SWBuf & appendFormatted(const char *format,...)
Definition: swbuf.cpp:81
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
unsigned long size
Definition: filemgr.h:42
static SWLog * getSystemLog()
Definition: swlog.cpp:53
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
Definition: remotetrans.cpp:78
preg buffer
Definition: regex.c:8089
#define SWTRY
Definition: defs.h:57
#define SWCATCH(x)
Definition: defs.h:58
const char * c_str() const
Definition: swbuf.h:158
SWBuf name
Definition: filemgr.h:41
virtual void preStatus(long totalBytes, long completedBytes, const char *message)
Definition: remotetrans.cpp:54
static void removeTrailingSlash(SWBuf &buf)
Definition: installmgr.cpp:57
static int createParent(const char *pName)
Definition: filemgr.cpp:426
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
virtual std::vector< struct DirEntry > getDirList(const char *dirURL)
Definition: remotetrans.cpp:92
#define SWLOGD(...)
Definition: defs.h:187
StatusReporter * statusReporter
Definition: remotetrans.h:59
vector< struct DirEntry > CURLHTTPTransport::getDirList ( const char *  dirURL)
virtual

Reimplemented from RemoteTransport.

Definition at line 215 of file curlhttpt.cpp.

215  {
216 
217  vector<struct DirEntry> dirList;
218 
219  SWBuf dirBuf;
220  const char *pBuf;
221  char *pBufRes;
222  SWBuf possibleName;
223  double fSize;
224  int possibleNameLength = 0;
225 
226  if (!getURL("", dirURL, &dirBuf)) {
227  pBuf = strstr(dirBuf, "<a href=\"");//Find the next link to a possible file name.
228  while (pBuf != NULL) {
229  pBuf += 9;//move to the start of the actual name.
230  pBufRes = (char *)strchr(pBuf, '\"');//Find the end of the possible file name
231  if (!pBufRes)
232  break;
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());
237  pBuf = pBufRes;
238  pBufRes = (char *)findSizeStart(pBuf);
239  fSize = 0;
240  if(pBufRes != NULL) {
241  pBuf = pBufRes;
242  fSize = strtod(pBuf, &pBufRes);
243  if (pBufRes[0] == 'K')
244  fSize *= 1024;
245  else if (pBufRes[0] == 'M')
246  fSize *= 1048576;
247  pBuf = pBufRes;
248  }
249  struct DirEntry i;
250  i.name = possibleName;
251  i.size = (long unsigned int)fSize;
252  i.isDirectory = possibleName.endsWith("/");
253  dirList.push_back(i);
254  } else {
255  pBuf += possibleNameLength;
256  }
257  pBuf++;
258  pBuf = strstr(pBuf, "<a href=\"");//Find the next link to a possible file name.
259  }
260  }
261  else
262  {
263  SWLog::getSystemLog()->logWarning("FTPURLGetDir: failed to get dir %s\n", dirURL);
264  }
265  return dirList;
266 }
Definition: swbuf.h:47
static SWLog * getSystemLog()
Definition: swlog.cpp:53
bool endsWith(const SWBuf &postfix) const
Definition: swbuf.h:501
const char * findSizeStart(const char *buffer)
Definition: curlhttpt.cpp:194
return NULL
Definition: regex.c:7953
const char * c_str() const
Definition: swbuf.h:158
SWBuf name
Definition: filemgr.h:41
unsigned long size() const
Definition: swbuf.h:185
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
#define SWLOGD(...)
Definition: defs.h:187
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
Definition: curlhttpt.cpp:115
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
long RemoteTransport::getTimeoutMillis ( )
inlineinherited

Definition at line 94 of file remotetrans.h.

94 { return timeoutMillis; }
char CURLHTTPTransport::getURL ( const char *  destPath,
const char *  sourceURL,
SWBuf destBuf = 0 
)
virtual

Reimplemented from RemoteTransport.

Definition at line 115 of file curlhttpt.cpp.

115  {
116  signed char retVal = 0;
117  struct FtpFile ftpfile = {destPath, 0, destBuf};
118 
119  CURLcode res;
120 
121  if (session) {
122  curl_easy_setopt(session, CURLOPT_URL, sourceURL);
123 
124  SWBuf credentials = u + ":" + p;
125  curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
126  curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_httpfwrite);
127  if (!passive)
128  curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
129  curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
130  curl_easy_setopt(session, CURLOPT_FAILONERROR, 1);
131  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, statusReporter);
132  curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_httpfprogress);
133  curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, myhttp_trace);
134  /* Set a pointer to our struct to pass to the callback */
135  curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
136 
137  /* Switch on full protocol/debug output */
138  curl_easy_setopt(session, CURLOPT_VERBOSE, true);
139 #ifndef OLDCURL
140  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT_MS, timeoutMillis);
141  curl_easy_setopt(session, CURLOPT_TIMEOUT_MS, timeoutMillis);
142 #else
143  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT, timeoutMillis/1000);
144  curl_easy_setopt(session, CURLOPT_TIMEOUT, timeoutMillis/1000);
145 #endif
146 
147  /* Disable checking host certificate */
148  if (isUnverifiedPeerAllowed()) {
149  curl_easy_setopt(session, CURLOPT_SSL_VERIFYPEER, false);
150  }
151 
152  /* FTP connection settings */
153 
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
158 #endif
159 
160 #ifdef EPRT_AVAILABLE
161  curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
162 SWLOGD("***** using CURLOPT_FTP_USE_EPRT\n");
163 #endif
164 
165 
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");
171 
172  if(CURLE_OK != res) {
173  if (CURLE_OPERATION_TIMEDOUT == res
174 #ifdef CURLE_FTP_ACCEPT_TIMEOUT
175  || CURLE_FTP_ACCEPT_TIMEOUT == res
176 #endif
177  ) {
178  retVal = -2;
179  }
180  else {
181  retVal = -1;
182  }
183  }
184  }
185 
186  if (ftpfile.fd > 0)
187  FileMgr::closeFile(ftpfile.fd); /* close the local file */
188 
189  return retVal;
190 }
Definition: swbuf.h:47
static void closeFile(int fd)
Definition: filemgr.cpp:491
static int my_httpfprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: curlhttpt.cpp:65
const char * c_str() const
Definition: swbuf.h:158
bool isUnverifiedPeerAllowed()
Definition: remotetrans.h:99
static int my_httpfwrite(void *buffer, size_t size, size_t nmemb, void *stream)
Definition: curlhttpt.cpp:47
static int myhttp_trace(CURL *handle, curl_infotype type, unsigned char *data, size_t size, void *userp)
Definition: curlhttpt.cpp:76
#define SWLOGD(...)
Definition: defs.h:187
StatusReporter * statusReporter
Definition: remotetrans.h:59
bool RemoteTransport::isPassive ( )
inlineinherited

Definition at line 95 of file remotetrans.h.

95 { return passive; }
bool RemoteTransport::isUnverifiedPeerAllowed ( )
inlineinherited

Definition at line 99 of file remotetrans.h.

99 { return unverifiedPeerAllowed; }
bool unverifiedPeerAllowed
Definition: remotetrans.h:63
char RemoteTransport::putURL ( const char *  destURL,
const char *  sourcePath,
SWBuf sourceBuf = 0 
)
virtualinherited

Reimplemented in RTransportGDrive.

Definition at line 85 of file remotetrans.cpp.

85  {
86  SWLog::getSystemLog()->logWarning("RemoteTransport::putURL called but unsupported");
87  char retVal = -1;
88  return retVal;
89 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
void RemoteTransport::setPassive ( bool  passive)
inlineinherited

Definition at line 92 of file remotetrans.h.

92 { this->passive = passive; }
void RemoteTransport::setPasswd ( const char *  passwd)
inlineinherited

Definition at line 97 of file remotetrans.h.

97 { p = passwd; }
void RemoteTransport::setTimeoutMillis ( long  timeoutMillis)
inlineinherited

Definition at line 93 of file remotetrans.h.

93 { this->timeoutMillis = timeoutMillis; }
void RemoteTransport::setUnverifiedPeerAllowed ( bool  val)
inlineinherited

Definition at line 98 of file remotetrans.h.

98 { this->unverifiedPeerAllowed = val; }
bool unverifiedPeerAllowed
Definition: remotetrans.h:63
void RemoteTransport::setUser ( const char *  user)
inlineinherited

Definition at line 96 of file remotetrans.h.

96 { u = user; }
void RemoteTransport::terminate ( )
inlineinherited

Definition at line 100 of file remotetrans.h.

100 { term = true; }

Member Data Documentation

SWBuf RemoteTransport::host
protectedinherited

Definition at line 64 of file remotetrans.h.

SWBuf RemoteTransport::p
protectedinherited

Definition at line 66 of file remotetrans.h.

bool RemoteTransport::passive
protectedinherited

Definition at line 60 of file remotetrans.h.

CURL* CURLHTTPTransport::session
private

Definition at line 35 of file curlhttpt.h.

StatusReporter* RemoteTransport::statusReporter
protectedinherited

Definition at line 59 of file remotetrans.h.

bool RemoteTransport::term
protectedinherited

Definition at line 62 of file remotetrans.h.

long RemoteTransport::timeoutMillis
protectedinherited

Definition at line 61 of file remotetrans.h.

SWBuf RemoteTransport::u
protectedinherited

Definition at line 65 of file remotetrans.h.

bool RemoteTransport::unverifiedPeerAllowed
protectedinherited

Definition at line 63 of file remotetrans.h.


The documentation for this class was generated from the following files: