CURLHTTPTransport Class Reference

#include <curlhttpt.h>

Inheritance diagram for CURLHTTPTransport:
Inheritance graph
[legend]
Collaboration diagram for CURLHTTPTransport:
Collaboration graph
[legend]

List of all members.

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)
virtual char getURL (const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
void setPassive (bool passive)
void setPasswd (const char *passwd)
void setUser (const char *user)
void terminate ()
 ~CURLHTTPTransport ()

Protected Attributes

SWBuf host
SWBuf p
bool passive
StatusReporterstatusReporter
bool term
SWBuf u

Private Attributes

CURL * session

Detailed Description

Definition at line 39 of file curlhttpt.h.


Constructor & Destructor Documentation

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

Definition at line 113 of file curlhttpt.cpp.

00113                                                                          : FTPTransport(host, sr) {
00114     session = (CURL *)curl_easy_init();
00115 }

CURLHTTPTransport::~CURLHTTPTransport (  ) 

Definition at line 118 of file curlhttpt.cpp.

00118                                       {
00119     curl_easy_cleanup(session);
00120 }


Member Function Documentation

int FTPTransport::copyDirectory ( const char *  urlPrefix,
const char *  dir,
const char *  dest,
const char *  suffix 
) [inherited]

Definition at line 125 of file ftptrans.cpp.

00125                                                                                                             {
00126     unsigned int i;
00127     int retVal = 0;
00128     
00129     SWBuf url = SWBuf(urlPrefix) + SWBuf(dir);
00130     removeTrailingSlash(url);
00131     url += '/';
00132     
00133     SWLog::getSystemLog()->logWarning("NetTransport: getting dir %s\n", url.c_str());
00134     vector<struct DirEntry> dirList = getDirList(url.c_str());
00135 
00136     if (!dirList.size()) {
00137         SWLog::getSystemLog()->logWarning("NetTransport: failed to read dir %s\n", url.c_str());
00138         return -1;
00139     }
00140                 
00141     long totalBytes = 0;
00142     for (i = 0; i < dirList.size(); i++)
00143         totalBytes += dirList[i].size;
00144     long completedBytes = 0;
00145     for (i = 0; i < dirList.size(); i++) {
00146         struct DirEntry &dirEntry = dirList[i];
00147         SWBuf buffer = (SWBuf)dest;
00148         removeTrailingSlash(buffer);
00149         buffer += "/";
00150         buffer += dirEntry.name;
00151         if (!strcmp(&buffer.c_str()[buffer.length()-strlen(suffix)], suffix)) {
00152             SWBuf buffer2 = "Downloading (";
00153             buffer2.appendFormatted("%d", i+1);
00154             buffer2 += " of ";
00155             buffer2.appendFormatted("%d", dirList.size());
00156             buffer2 += "): ";
00157             buffer2 += dirEntry.name;
00158             if (statusReporter)
00159                 statusReporter->preStatus(totalBytes, completedBytes, buffer2.c_str());
00160             FileMgr::createParent(buffer.c_str());  // make sure parent directory exists
00161             SWTRY {
00162                 SWBuf url = (SWBuf)urlPrefix + (SWBuf)dir;
00163                 removeTrailingSlash(url);
00164                 url += "/";
00165                 url += dirEntry.name; //dont forget the final slash
00166                 if (!dirEntry.isDirectory) {
00167                     if (getURL(buffer.c_str(), url.c_str())) {
00168                         SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", url.c_str());
00169                         return -2;
00170                     }
00171                     completedBytes += dirEntry.size;
00172                 }
00173                 else {
00174                     SWBuf subdir = (SWBuf)dir;
00175                     removeTrailingSlash(subdir);
00176                     subdir += (SWBuf)"/" + dirEntry.name;
00177                     if (copyDirectory(urlPrefix, subdir, buffer.c_str(), suffix)) {
00178                         SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", subdir.c_str());
00179                         return -2;
00180                     }
00181                 }
00182             }
00183             SWCATCH (...) {}
00184             if (term) {
00185                 retVal = -3;
00186                 break;
00187             }
00188         }
00189     }
00190     return retVal;
00191 }

vector< struct DirEntry > CURLHTTPTransport::getDirList ( const char *  dirURL  )  [virtual]

Reimplemented from FTPTransport.

Definition at line 204 of file curlhttpt.cpp.

00204                                                                         {
00205     
00206     vector<struct DirEntry> dirList;
00207     
00208     SWBuf dirBuf;
00209     const char *pBuf;
00210     char *pBufRes;
00211     char possibleName[400];
00212     double fSize;
00213     int possibleNameLength = 0;
00214     
00215     if (!getURL("", dirURL, &dirBuf)) {
00216         pBuf = strstr(dirBuf, "<a href=\"");//Find the next link to a possible file name.
00217         while (pBuf != NULL) {
00218             pBuf += 9;//move to the start of the actual name.
00219             pBufRes = (char *)strchr(pBuf, '\"');//Find the end of the possible file name
00220             possibleNameLength = pBufRes - pBuf;
00221             sprintf(possibleName, "%.*s", possibleNameLength, pBuf);
00222             if (isalnum(possibleName[0])) {
00223                 SWLog::getSystemLog()->logDebug("getDirListHTTP: Found a file: %s", possibleName);
00224                 pBuf = pBufRes;
00225                 pBufRes = (char *)findSizeStart(pBuf);
00226                 fSize = 0;
00227                 if(pBufRes != NULL) {
00228                     pBuf = pBufRes;
00229                     fSize = strtod(pBuf, &pBufRes);
00230                     if (pBufRes[0] == 'K')
00231                         fSize *= 1024;
00232                     else if (pBufRes[0] == 'M')
00233                         fSize *= 1048576;
00234                 }
00235                 struct DirEntry i;
00236                 i.name = possibleName;
00237                 i.size = (long unsigned int)fSize;
00238                 i.isDirectory = (possibleName[possibleNameLength-1] == '/');
00239                 dirList.push_back(i);
00240                 pBuf = pBufRes;
00241             } else {
00242                 pBuf += possibleNameLength;
00243             }
00244             pBuf++;
00245             pBuf = strstr(pBuf, "<a href=\"");//Find the next link to a possible file name.
00246         }
00247     }
00248     else
00249     {
00250         SWLog::getSystemLog()->logWarning("FTPURLGetDir: failed to get dir %s\n", dirURL);
00251     }
00252     return dirList;
00253 }

char CURLHTTPTransport::getURL ( const char *  destPath,
const char *  sourceURL,
SWBuf destBuf = 0 
) [virtual]

Reimplemented from FTPTransport.

Definition at line 123 of file curlhttpt.cpp.

00123                                                                                           {
00124     signed char retVal = 0;
00125     struct FtpFile ftpfile = {destPath, 0, destBuf};
00126 
00127     CURLcode res;
00128 
00129     if (session) {
00130         curl_easy_setopt(session, CURLOPT_URL, sourceURL);
00131 
00132         SWBuf credentials = u + ":" + p;
00133         curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
00134         curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_httpfwrite);
00135         if (!passive)
00136             curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
00137         curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
00138         curl_easy_setopt(session, CURLOPT_PROGRESSDATA, statusReporter);
00139         curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_httpfprogress);
00140         curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, myhttp_trace);
00141         /* Set a pointer to our struct to pass to the callback */
00142         curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
00143 
00144         /* Switch on full protocol/debug output */
00145         curl_easy_setopt(session, CURLOPT_VERBOSE, true);
00146         
00147         /* Disable checking host certificate */
00148         curl_easy_setopt(session, CURLOPT_SSL_VERIFYPEER, false);
00149 
00150         /* FTP connection settings */
00151 
00152 #if (LIBCURL_VERSION_MAJOR > 7) || \
00153    ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
00154    ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
00155 #      define EPRT_AVAILABLE 1
00156 #endif
00157 
00158 #ifdef EPRT_AVAILABLE
00159         curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
00160         SWLog::getSystemLog()->logDebug("***** using CURLOPT_FTP_USE_EPRT\n");
00161 #endif
00162 
00163 
00164         SWLog::getSystemLog()->logDebug("***** About to perform curl easy action. \n");
00165         SWLog::getSystemLog()->logDebug("***** destPath: %s \n", destPath);
00166         SWLog::getSystemLog()->logDebug("***** sourceURL: %s \n", sourceURL);
00167         res = curl_easy_perform(session);
00168         SWLog::getSystemLog()->logDebug("***** Finished performing curl easy action. \n");
00169 
00170         if(CURLE_OK != res) {
00171             retVal = -1;
00172         }
00173     }
00174 
00175     if (ftpfile.stream)
00176         fclose(ftpfile.stream); /* close the local file */
00177 
00178     return retVal;
00179 }

void FTPTransport::setPassive ( bool  passive  )  [inline, inherited]

Definition at line 75 of file ftptrans.h.

00075 { this->passive = passive; }

void FTPTransport::setPasswd ( const char *  passwd  )  [inline, inherited]

Definition at line 77 of file ftptrans.h.

00077 { p = passwd; }

void FTPTransport::setUser ( const char *  user  )  [inline, inherited]

Definition at line 76 of file ftptrans.h.

00076 { u = user; }

void FTPTransport::terminate (  )  [inline, inherited]

Definition at line 78 of file ftptrans.h.

00078 { term = true; }


Member Data Documentation

SWBuf FTPTransport::host [protected, inherited]

Definition at line 56 of file ftptrans.h.

SWBuf FTPTransport::p [protected, inherited]

Definition at line 58 of file ftptrans.h.

bool FTPTransport::passive [protected, inherited]

Definition at line 54 of file ftptrans.h.

CURL* CURLHTTPTransport::session [private]

Definition at line 40 of file curlhttpt.h.

Definition at line 53 of file ftptrans.h.

bool FTPTransport::term [protected, inherited]

Definition at line 55 of file ftptrans.h.

SWBuf FTPTransport::u [protected, inherited]

Definition at line 57 of file ftptrans.h.


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

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1