added: some work in Export plugin
changed: in base redirect we are using 301 moved permanently status code now (was 303) git-svn-id: svn://ttmath.org/publicrep/winix/trunk@761 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -7,19 +7,47 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <curl/curl.h>
|
||||
#include <string.h>
|
||||
#include "exportthread.h"
|
||||
#include "core/log.h"
|
||||
#include "core/misc.h"
|
||||
#include "utf8.h"
|
||||
|
||||
|
||||
|
||||
namespace Export
|
||||
{
|
||||
ExportThread * ExportThread::exp_thread;
|
||||
|
||||
|
||||
ExportThread::ExportThread()
|
||||
{
|
||||
exp_thread = 0;
|
||||
utf8 = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ExportThread::SetUTF8(bool use_utf8)
|
||||
{
|
||||
utf8 = use_utf8;
|
||||
}
|
||||
|
||||
|
||||
void ExportThread::AddMessage(const Message & message)
|
||||
{
|
||||
message_tab.insert(message_tab.end(), message);
|
||||
WakeUpThread();
|
||||
|
||||
log << log1 << "yes ser" << logend;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void ExportThread::AddMessage(int type, const std::wstring & url, const std::wstring & path)
|
||||
{
|
||||
message_add_temp.type = type;
|
||||
@@ -27,6 +55,82 @@ void ExportThread::AddMessage(int type, const std::wstring & url, const std::wst
|
||||
message_add_temp.path = path;
|
||||
|
||||
AddMessage(message_add_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread
|
||||
// objects are locked
|
||||
bool ExportThread::SignalReceived()
|
||||
{
|
||||
log << log1 << "------------- a ---------------" << logend;
|
||||
|
||||
return !message_tab.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread
|
||||
// objects are not locked
|
||||
void ExportThread::Do()
|
||||
{
|
||||
MessageTab::iterator i;
|
||||
bool end;
|
||||
|
||||
Lock();
|
||||
log << log1 << "------------- swinka ---------------" << logend;
|
||||
i = message_tab.begin();
|
||||
Unlock();
|
||||
|
||||
do
|
||||
{
|
||||
Lock();
|
||||
|
||||
if( i != message_tab.end() )
|
||||
{
|
||||
message_work = *i;
|
||||
message_tab.erase(i++);
|
||||
end = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = true;
|
||||
}
|
||||
|
||||
Unlock();
|
||||
|
||||
if( !end )
|
||||
DoMessage();
|
||||
|
||||
}
|
||||
while( !end && !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
// second thread
|
||||
// objects are not locked
|
||||
// current message we have in 'message_work'
|
||||
void ExportThread::DoMessage()
|
||||
{
|
||||
Lock();
|
||||
|
||||
if( utf8 )
|
||||
Ezc::WideToUTF8(message_work.url, url_a);
|
||||
else
|
||||
AssignString(message_work.url, url_a);
|
||||
|
||||
Unlock();
|
||||
|
||||
Fetch(url_a.c_str());
|
||||
|
||||
Lock();
|
||||
log << log1 << "sciagnalem takie cos ---------------------------------------------------" << logend;
|
||||
|
||||
log << "rozmiar: " << buffer.size() << logend;
|
||||
|
||||
log << log1 << "koniec takiego cosia ---------------------------------------------------" << logend << logsave;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +138,76 @@ void ExportThread::AddMessage(int type, const std::wstring & url, const std::wst
|
||||
|
||||
|
||||
|
||||
// second thread
|
||||
// objects are not locked
|
||||
bool ExportThread::Fetch(const char * url)
|
||||
{
|
||||
CURL * curl;
|
||||
CURLcode res;
|
||||
long code;
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
if( !curl )
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Export: I can't use curl" << logend;
|
||||
Unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
exp_thread = this;
|
||||
buffer.clear();
|
||||
|
||||
/*
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // the http code will be from the last request
|
||||
*/
|
||||
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StaticSaveFunction);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Winix");
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if( res!=0 || code!=200 )
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Ezport: error: operation result: " << (int)res << ", http code: " << code << logend;
|
||||
Unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t ExportThread::StaticSaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
|
||||
{
|
||||
if( exp_thread )
|
||||
return exp_thread->SaveFunction(ptr, size, nmemb, userdata);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t ExportThread::SaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
|
||||
{
|
||||
size_t len = size * nmemb;
|
||||
|
||||
if( len > 0 )
|
||||
buffer.append(ptr, len);
|
||||
|
||||
Lock();
|
||||
log << log1 << "odebralem cosik: size: " << size << ", nmemb: " << nmemb << logend;
|
||||
Unlock();
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user