|
- #include "testcurl.h"
-
- #pragma GCC diagnostic ignored "-Wunused-function"
-
- testcurl::testcurl( wxWindow* parent )
- :
- testcurlGUI( parent )
- {
- texte_URL->SetFocus ();
- texte_URL->SetSelection (-1, -1);
- curlhandle = curl_easy_init ();
- curl_easy_setopt (curlhandle, CURLOPT_WRITEFUNCTION, WriteDataCallback);
- curl_easy_setopt (curlhandle, CURLOPT_WRITEDATA, (void*)&curldata);
- }
-
- testcurl::~testcurl()
- {
- curl_easy_cleanup (curlhandle);
- curl_global_cleanup ();
- }
-
- void testcurl::OnGo( wxCommandEvent& event )
- {
- if (texte_URL->IsEmpty())
- wxMessageBox("URL vide", "Erreur", wxOK|wxICON_EXCLAMATION);
- else
- {
- texte_Resultat->SetValue("");
- wxString url = texte_URL->GetValue();
- curl_easy_setopt (curlhandle, CURLOPT_URL, _C(url));
- printf ("url = %s\n", _C(url));
-
- if (checkbox_Proxy->IsChecked())
- {
- wxString proxy = texte_Proxy->GetValue();
- curl_easy_setopt (curlhandle, CURLOPT_PROXY, _C(proxy));
- curl_easy_setopt (curlhandle, CURLOPT_PROXYPORT, spin_Proxy->GetValue());
- printf ("proxy = %s:%i\n", _C(proxy), spin_Proxy->GetValue());
- }
- else
- curl_easy_setopt (curlhandle, CURLOPT_PROXY, "");
-
- curldata.content = (char*) malloc(1);
- curldata.size = 0;
-
- if (curlhandle)
- curl_easy_perform(curlhandle);
-
- printf ("Content = %s", curldata.content);
-
- wxString content = wxString::From8BitData(curldata.content);
- texte_Resultat->SetValue(content);
- wxMessageBox("Fini !", "Fini !", wxOK);
-
- if (curldata.content)
- free (curldata.content);
- }
- }
-
- static size_t WriteDataCallback(void *contents, size_t size, size_t nmemb, void *userp)
- {
- size_t realsize = size * nmemb;
- CurlData *mem = (CurlData *)userp;
-
- mem->content = (char*) realloc(mem->content, mem->size + realsize + 1);
- if (mem->content == NULL) {
- // out of memory!
- printf("not enough memory (realloc returned NULL)\n");
- exit(EXIT_FAILURE);
- }
-
- memcpy(&(mem->content[mem->size]), contents, realsize);
- mem->size += realsize;
- mem->content[mem->size] = 0;
-
- return realsize;
- }
|