httpclient.hpp
1.42 KB
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
#pragma once
#include <string>
#include <functional>
#include <list>
#include <account.h>
#include <util.h>
enum class HTTPFlag {
NONE = 0,
AUTH = 1 << 0,
LARGE = 1 << 1,
};
inline constexpr HTTPFlag operator|(HTTPFlag a, HTTPFlag b) {
return (HTTPFlag)((int)a | (int)b);
};
inline constexpr bool operator&(HTTPFlag a, HTTPFlag b) {
return ((int)a & (int)b) != 0;
}
class HTTPClient {
const int MAX_IN_FLIGHT = 4;
using CompleteFunc = std::function<void(int, const guchar *, gsize)>;
struct Request {
HTTPClient *client;
std::string url;
std::string content_type;
std::string body;
HTTPFlag flags;
CompleteFunc callback;
PurpleUtilFetchUrlData *handle;
};
PurpleAccount *acct;
std::list<Request *> request_queue;
int in_flight;
void execute_next();
void complete(Request *req, const gchar *url_text, gsize len, const gchar *error_message);
static void purple_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
const gchar *url_text, gsize len, const gchar *error_message);
public:
HTTPClient(PurpleAccount *acct);
~HTTPClient();
void request(std::string url, CompleteFunc callback);
void request(std::string url, HTTPFlag flags, CompleteFunc callback);
void request(std::string url, HTTPFlag flags,
std::string content_type, std::string body,
CompleteFunc callback);
};