1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/curl/curlapi.cpp Sun Jan 11 23:48:17 2009 -0500
1.3 @@ -0,0 +1,114 @@
1.4 +#include "curlapi.h"
1.5 +
1.6 +Webternet g_webternet;
1.7 +
1.8 +WebTransfer *WebTransfer::CreateWebSession()
1.9 +{
1.10 + CURL *curl;
1.11 +
1.12 + curl = curl_easy_init();
1.13 + if (curl == NULL)
1.14 + {
1.15 + return NULL;
1.16 + }
1.17 +
1.18 + WebTransfer *easy = new WebTransfer(curl);
1.19 +
1.20 + if (curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, easy->errorBuffer))
1.21 + {
1.22 + delete easy;
1.23 + return NULL;
1.24 + }
1.25 + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
1.26 + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
1.27 +
1.28 + return easy;
1.29 +}
1.30 +
1.31 +WebTransfer::WebTransfer(CURL *_curl)
1.32 +{
1.33 + this->curl = _curl;
1.34 +}
1.35 +
1.36 +const char *WebTransfer::LastErrorMessage()
1.37 +{
1.38 + return errorBuffer;
1.39 +}
1.40 +
1.41 +int WebTransfer::LastErrorCode()
1.42 +{
1.43 + return lastError;
1.44 +}
1.45 +
1.46 +bool WebTransfer::SetHeaderReturn(bool recv_hdr)
1.47 +{
1.48 + lastError = curl_easy_setopt(curl, CURLOPT_HEADER, recv_hdr ? 1 : 0);
1.49 + return lastError == 0;
1.50 +}
1.51 +
1.52 +static size_t curl_write_to_sm(void *ptr, size_t bytes, size_t nmemb, void *stream)
1.53 +{
1.54 + void **userdata = (void **)stream;
1.55 + IWebTransfer *session = (IWebTransfer *)userdata[0];
1.56 + ITransferHandler *handler = (ITransferHandler *)userdata[1];
1.57 +
1.58 + DownloadWriteStatus status;
1.59 + status = handler->OnDownloadWrite(session, userdata[2], ptr, bytes, nmemb);
1.60 + if (status != DownloadWrite_Okay)
1.61 + {
1.62 + /* Return a differing amount */
1.63 + return (bytes == 0 || nmemb == 0) ? 1 : 0;
1.64 + }
1.65 +
1.66 + return bytes * nmemb;
1.67 +}
1.68 +
1.69 +bool WebTransfer::Download(const char *url, ITransferHandler *handler, void *data)
1.70 +{
1.71 + lastError = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_to_sm);
1.72 + if (lastError)
1.73 + {
1.74 + return false;
1.75 + }
1.76 +
1.77 + void *userdata[3];
1.78 + userdata[0] = this;
1.79 + userdata[1] = handler;
1.80 + userdata[2] = data;
1.81 + lastError = curl_easy_setopt(curl, CURLOPT_WRITEDATA, userdata);
1.82 + if (lastError)
1.83 + {
1.84 + return false;
1.85 + }
1.86 +
1.87 + lastError = curl_easy_setopt(curl, CURLOPT_URL, url);
1.88 + if (lastError)
1.89 + {
1.90 + return false;
1.91 + }
1.92 +
1.93 + lastError = curl_easy_perform(curl);
1.94 +
1.95 + return (lastError == 0);
1.96 +}
1.97 +
1.98 +WebTransfer::~WebTransfer()
1.99 +{
1.100 + curl_easy_cleanup(curl);
1.101 +}
1.102 +
1.103 +unsigned int Webternet::GetInterfaceVersion()
1.104 +{
1.105 + return SMINTERFACE_WEBTERNET_VERSION;
1.106 +}
1.107 +
1.108 +const char *Webternet::GetInterfaceName()
1.109 +{
1.110 + return SMINTERFACE_WEBTERNET_NAME;
1.111 +}
1.112 +
1.113 +IWebTransfer *Webternet::CreateSession()
1.114 +{
1.115 + return WebTransfer::CreateWebSession();
1.116 +}
1.117 +