aboutsummaryrefslogtreecommitdiffstats
path: root/include/httpclient.h
blob: a861ae615b8bd55bde1bc666ab4ae8dfae09ab44 (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
/* Copyright (C) 2022 The Qt Company Ltd.
 *
 * SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0
*/
#pragma once

#include <iostream>
#include <sstream>
#include <map>
#include "version.h"

#define SERVER_CONN_TIMEOUT 10

#if _WIN32
    //#include <windows.h>
    #include "curl/curl.h"
    //#pragma comment(lib, "../3rdParty/curl/lib/libcurl.lib")
    //#pragma comment(lib, "ws2_32.lib")
    //#pragma comment(lib, "winmm.lib")
    //pragma comment(lib, "wldap32.lib")
    //#include <winsock2.h>
    //#include <ws2tcpip.h>
    //#include <iphlpapi.h>
    //#include <ws2def.h>
    //#include <io.h>
#else
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <curl/curl.h>
#endif

struct HttpRequest {
    std::string payload;
    std::string url;
    std::string authKey;
    std::string reply;
    curl_slist *headers = nullptr;
};

/*********************
    HttpClient class
*/

class HttpClient
{
public:
    explicit HttpClient(const std::string &serverUrl,
                        const std::string &requestAccessPoint,
                        const std::string &longtermAccessPoint,
                        const std::string &versionAccessPoint);
    ~HttpClient() {}

    int sendRequest(std::string &reply, const std::string &payload,
                const std::string &server, const std::string &authKey="");
    std::string error() { return m_lastError; }
private:
    std::string m_serverUrl;
    std::string m_requestAccessPoint;
    std::string m_longtermAccessPoint;
    std::string m_versionAccessPoint;
    std::string m_userAgent = "License daemon / ";
    std::string m_lastError;
    int doRequest(CURL *curl, HttpRequest &request);
};