summaryrefslogtreecommitdiff
path: root/src/CurlTask.cc
blob: 2c37514bc9075037d15bfe60bb2e9ddd124de8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "CurlTask.h"

Balau::CurlTask::CurlTask() {
    m_curlHandle = curl_easy_init();
    curl_easy_setopt(m_curlHandle, CURLOPT_HEADERFUNCTION, reinterpret_cast<curl_write_callback>(headerFunctionStatic));
    curl_easy_setopt(m_curlHandle, CURLOPT_HEADERDATA, this);
    curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, reinterpret_cast<curl_write_callback>(writeFunctionStatic));
    curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
    curl_easy_setopt(m_curlHandle, CURLOPT_READFUNCTION, reinterpret_cast<curl_read_callback>(readFunctionStatic));
    curl_easy_setopt(m_curlHandle, CURLOPT_READDATA, this);
    curl_easy_setopt(m_curlHandle, CURLOPT_DEBUGFUNCTION, reinterpret_cast<curl_debug_callback>(debugFunctionStatic));
    curl_easy_setopt(m_curlHandle, CURLOPT_DEBUGDATA, this);
}

Balau::CurlTask::~CurlTask() {
    unregisterCurlHandle();
    curl_easy_cleanup(m_curlHandle);
}

size_t Balau::CurlTask::headerFunctionStatic(char * ptr, size_t size, size_t nmemb, void * userdata) {
    CurlTask * curlTask = (CurlTask *) userdata;
    return curlTask->headerFunction(ptr, size * nmemb);
}

size_t Balau::CurlTask::writeFunctionStatic(char * ptr, size_t size, size_t nmemb, void * userdata) {
    CurlTask * curlTask = (CurlTask *) userdata;
    return curlTask->writeFunction(ptr, size * nmemb);
}

size_t Balau::CurlTask::readFunctionStatic(void * ptr, size_t size, size_t nmemb, void * userdata) {
    CurlTask * curlTask = (CurlTask *) userdata;
    return curlTask->readFunction(ptr, size * nmemb);
}

int Balau::CurlTask::debugFunctionStatic(CURL * easy, curl_infotype info, char * str, size_t str_len, void * userdata) {
    CurlTask * curlTask = (CurlTask *) userdata;
    IAssert(easy == curlTask->m_curlHandle, "Got a debug callback for a handle that isn't our own.");
    return curlTask->debugFunction(info, str, str_len);
}

Balau::DownloadTask::DownloadTask(const Balau::String & url) {
    curl_easy_setopt(m_curlHandle, CURLOPT_URL, url.to_charp());
    m_name.set("DownloadTask(%s)", url.to_charp());
}

void Balau::DownloadTask::Do() {
    if (m_state) {
        downloadDone();
        return;
    }

    m_state = 1;
    registerCurlHandle();
    waitFor(&m_evt);
    yield();
}

void Balau::DownloadTask::curlDone(CURLcode result) {
    m_curlResult = result;
    curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &m_responseCode);
    m_evt.doSignal();
    m_done = true;
}

Balau::HttpDownloadTask::~HttpDownloadTask() {
    curl_slist_free_all(m_headers);
    m_headers = NULL;
}

void Balau::HttpDownloadTask::addHeader(const String & header) {
    m_headers = curl_slist_append(m_headers, header.to_charp());
}

void Balau::HttpDownloadTask::prepareRequest() {
    if (m_headers)
        curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_headers);
}