summaryrefslogtreecommitdiff
path: root/linux/request.cpp
blob: 0c5eb4ac168b64b14f20f2b0452c31622c59ce93 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "request.hpp"

#include <atomic>
#include <cassert>

#include <llmr/util/threadpool.hpp>
#include <llmr/platform/platform.hpp>

using namespace llmr::platform;


CURLSH *Request::curl_share = nullptr;
pthread_mutex_t Request::curl_share_mutex = PTHREAD_MUTEX_INITIALIZER;

void Request::curl_share_lock(CURL *, curl_lock_data, curl_lock_access, void *) {
    pthread_mutex_lock(&curl_share_mutex);
}

void Request::curl_share_unlock(CURL *, curl_lock_data, void *) {
    pthread_mutex_unlock(&curl_share_mutex);
}

size_t Request::curl_write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
    ((std::string *)userp)->append((char *)contents, size * nmemb);
    return size * nmemb;
}

int Request::curl_progress_callback(void *ptr, double dltotal, double dlnow, double ultotal, double ulnow) {
    Request *req = static_cast<Request *>(ptr);
    return req->cancelled;
}

void Request::initialize() {
    // curl init
    curl_global_init(CURL_GLOBAL_ALL);

    curl_share = curl_share_init();
    curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_share_lock);
    curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_share_unlock);
}

void Request::finish() {
    pthread_key_delete(key);
    curl_share_cleanup(curl_share);
}

Request::Request(std::string url, std::function<void(platform::Response&)> bg, std::function<void()> fg)
    : done(false),
      cancelled(false),
      url(url),
      background_function(bg),
      foreground_callback(fg) {
    llmr::util::threadpool->add(request, this);
}

pthread_key_t Request::key;
pthread_once_t Request::key_once = PTHREAD_ONCE_INIT;

void Request::create_key() {
    pthread_key_create(&key, delete_key);
}

void Request::delete_key(void *ptr) {
    if (ptr != nullptr) {
        curl_easy_cleanup(ptr);
    }
}

void Request::request(void *ptr) {
    assert(curl_share);

    Request *req = static_cast<Request *>(ptr);
    Response res;

    pthread_once(&key_once, create_key);
    // TODO: use curl multi to be able to cancel, or to

    CURL *curl = nullptr;
    if ((curl = pthread_getspecific(key)) == nullptr) {
        curl = curl_easy_init();
        pthread_setspecific(key, curl);
    }

    curl_easy_reset(curl);

    CURLcode code;

    curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res.body);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress_callback);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, req);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate");
    curl_easy_setopt(curl, CURLOPT_SHARE, curl_share);
    code = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res.code);

    if (code != CURLE_ABORTED_BY_CALLBACK) {
        req->background_function(res);
    }

    req->done = true;
}

void Request::cancel() {
    cancelled = true;
}