extensions/curl/curlapi.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 08 22:02:42 2013 -0700 (2013-08-08)
changeset 3980 dc7dfcd855dc
parent 3920 e4fa6b583893
permissions -rw-r--r--
More Windows build fix.
     1 #include "curlapi.h"
     2 
     3 Webternet g_webternet;
     4 
     5 WebForm::WebForm() : first(NULL), last(NULL), lastError(CURL_FORMADD_OK)
     6 {
     7 }
     8 
     9 bool WebForm::AddString(const char *name, const char *data)
    10 {
    11 	lastError = curl_formadd(&first,
    12 		&last,
    13 		CURLFORM_COPYNAME,
    14 		name,
    15 		CURLFORM_COPYCONTENTS,
    16 		data,
    17 		CURLFORM_END);
    18 	return lastError == CURL_FORMADD_OK;
    19 }
    20 
    21 bool WebForm::AddFile(const char *name, const char *path)
    22 {
    23 	lastError = curl_formadd(&first,
    24 		&last,
    25 		CURLFORM_COPYNAME,
    26 		name,
    27 		CURLFORM_FILE,
    28 		path,
    29 		CURLFORM_END);
    30 	return lastError == CURL_FORMADD_OK;
    31 }
    32 
    33 curl_httppost *WebForm::GetFormData()
    34 {
    35 	return first;
    36 }
    37 
    38 WebForm::~WebForm()
    39 {
    40 	curl_formfree(first);
    41 }
    42 
    43 WebTransfer *WebTransfer::CreateWebSession()
    44 {
    45 	CURL *curl;
    46 
    47 	curl = curl_easy_init();
    48 	if (curl == NULL)
    49 	{
    50 		return NULL;
    51 	}
    52 
    53 	WebTransfer *easy = new WebTransfer(curl);
    54 	
    55 	if (curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, easy->errorBuffer))
    56 	{
    57 		delete easy;
    58 		return NULL;
    59 	}
    60 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    61 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    62 
    63 	return easy;
    64 }
    65 
    66 WebTransfer::WebTransfer(CURL *_curl)
    67 {
    68 	this->curl = _curl;
    69 }
    70 
    71 const char *WebTransfer::LastErrorMessage()
    72 {
    73 	return errorBuffer;
    74 }
    75 
    76 int WebTransfer::LastErrorCode()
    77 {
    78 	return lastError;
    79 }
    80 
    81 bool WebTransfer::SetHeaderReturn(bool recv_hdr)
    82 {
    83 	lastError = curl_easy_setopt(curl, CURLOPT_HEADER, recv_hdr ? 1 : 0);
    84 	return lastError == 0;
    85 }
    86 
    87 bool WebTransfer::SetFailOnHTTPError(bool fail)
    88 {
    89 	lastError = curl_easy_setopt(curl, CURLOPT_FAILONERROR, fail ? 1 : 0);
    90 	return lastError == 0;
    91 }
    92 
    93 static size_t curl_write_to_sm(void *ptr, size_t bytes, size_t nmemb, void *stream)
    94 {
    95 	void **userdata = (void **)stream;
    96 	IWebTransfer *session = (IWebTransfer *)userdata[0];
    97 	ITransferHandler *handler = (ITransferHandler *)userdata[1];
    98 
    99 	DownloadWriteStatus status;
   100 	status = handler->OnDownloadWrite(session, userdata[2], ptr, bytes, nmemb);
   101 	if (status != DownloadWrite_Okay)
   102 	{
   103 		/* Return a differing amount */
   104 		return (bytes == 0 || nmemb == 0) ? 1 : 0;
   105 	}
   106 
   107 	return bytes * nmemb;
   108 }
   109 
   110 bool WebTransfer::Download(const char *url, ITransferHandler *handler, void *data)
   111 {
   112 	lastError = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_to_sm);
   113 	if (lastError)
   114 	{
   115 		return false;
   116 	}
   117 
   118 	void *userdata[3];
   119 	userdata[0] = this;
   120 	userdata[1] = handler;
   121 	userdata[2] = data;
   122 	lastError = curl_easy_setopt(curl, CURLOPT_WRITEDATA, userdata);
   123 	if (lastError)
   124 	{
   125 		return false;
   126 	}
   127 
   128 	lastError = curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
   129 	if (lastError)
   130 	{
   131 		return false;
   132 	}
   133 
   134 	lastError = curl_easy_setopt(curl, CURLOPT_URL, url);
   135 	if (lastError)
   136 	{
   137 		return false;
   138 	}
   139 
   140 	lastError = curl_easy_perform(curl);
   141 	
   142 	return (lastError == 0);
   143 }
   144 
   145 bool WebTransfer::PostAndDownload(const char *url,
   146 								  IWebForm *form,
   147 								  ITransferHandler *handler,
   148 								  void *data)
   149 {
   150 	WebForm *realform = (WebForm*)form;
   151 
   152 	lastError = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_to_sm);
   153 	if (lastError)
   154 	{
   155 		return false;
   156 	}
   157 
   158 	void *userdata[3];
   159 	userdata[0] = this;
   160 	userdata[1] = handler;
   161 	userdata[2] = data;
   162 	lastError = curl_easy_setopt(curl, CURLOPT_WRITEDATA, userdata);
   163 	if (lastError)
   164 	{
   165 		return false;
   166 	}
   167 
   168 	lastError = curl_easy_setopt(curl, CURLOPT_HTTPPOST, realform->GetFormData());
   169 	if (lastError)
   170 	{
   171 		return false;
   172 	}
   173 
   174 	lastError = curl_easy_setopt(curl, CURLOPT_URL, url);
   175 	if (lastError)
   176 	{
   177 		return false;
   178 	}
   179 
   180 	lastError = curl_easy_perform(curl);
   181 
   182 	return (lastError == 0);
   183 }
   184 
   185 WebTransfer::~WebTransfer()
   186 {
   187 	curl_easy_cleanup(curl);
   188 }
   189 
   190 unsigned int Webternet::GetInterfaceVersion()
   191 {
   192 	return SMINTERFACE_WEBTERNET_VERSION;
   193 }
   194 
   195 const char *Webternet::GetInterfaceName()
   196 {
   197 	return SMINTERFACE_WEBTERNET_NAME;
   198 }
   199 
   200 IWebTransfer *Webternet::CreateSession()
   201 {
   202 	return WebTransfer::CreateWebSession();
   203 }
   204 
   205 IWebForm *Webternet::CreateForm()
   206 {
   207 	return new WebForm();
   208 }