Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 andreas 1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef __THTTPCLIENT_H__
20
#define __THTTPCLIENT_H__
21
 
22
#include <string>
23
 
24
#include <netdb.h>
25
#include <openssl/ssl.h>
26
 
27
typedef enum METHOD_t
28
{
29
    UNSUPPORTED,
30
    GET,
31
    PUT,
32
    POST,
33
    HEAD
34
}METHOD_t;
35
 
36
typedef struct URL_t
37
{
38
    std::string scheme;
39
    std::string host;
40
    int port{0};
41
    std::string path;
42
    std::string user;
43
    std::string pw;
44
 
45
    void clear()
46
    {
47
        scheme.clear();
48
        host.clear();
49
        port = 0;
50
        path.clear();
51
        user.clear();
52
        pw.clear();
53
    }
54
}URL_t;
55
 
56
typedef struct HTTPHEAD_t
57
{
58
    std::string name;
59
    std::string content;
60
}HTTPHEAD_t;
61
 
62
typedef struct HTTPBODY_t
63
{
64
    char *body{nullptr};
65
    size_t len{0};
66
 
67
    void clear()
68
    {
69
        if (body != nullptr)
70
            delete[] body;
71
 
72
        body = nullptr;
73
        len = 0;
74
    }
75
}HTTPBODY_t;
76
 
77
typedef struct HTTPREQUEST_t
78
{
79
    METHOD_t method{UNSUPPORTED};
80
    int direction{0};
81
    int code{0};            // on receive
82
    char *status{nullptr};  // on receive
83
    char *path{nullptr};
84
    char *version{nullptr};
85
 
86
    void clear()
87
    {
88
        direction = 0;
89
        code = 0;
90
        method = UNSUPPORTED;
91
 
92
        if (status != nullptr)
93
            delete[] status;
94
 
95
        status = nullptr;
96
 
97
        if (path != nullptr)
98
            delete[] path;
99
 
100
        path = nullptr;
101
 
102
        if (version != nullptr)
103
            delete[] version;
104
 
105
        version = nullptr;
106
    }
107
}HTTPREQUEST_t;
108
 
109
class THTTPClient
110
{
111
    public:
112
        THTTPClient();
113
        ~THTTPClient();
114
 
115
        char *tcall(size_t *size, const std::string& URL, const std::string& user, const std::string& pw);
116
        std::string makeURL(const std::string& scheme, const std::string& host, int port, const std::string& path);
117
        char *getContent(const char *buffer);
118
        char *getContent() { return mBody.body; }
119
        size_t getContentSize() { return mBody.len; }
120
 
121
    protected:
122
        URL_t& parseURL(const std::string& URL);
123
        int socket_connect();
124
        std::string getSocketError(int err, const std::string data);
125
        void initSSL();
126
        SSL_CTX *initCTX();
127
        void log_ssl_error();
128
        int sockWrite(int fd, char *buffer, size_t len);
129
        int sockRead(int fd, char *buffer, size_t len);
130
        int parseHeader(const char *buffer, size_t len);
131
        std::string getHeadParameter(const std::string& name);
132
        std::string makeRequest(const std::string& url);
133
        struct addrinfo *lookup_host (const std::string& host, int port);
134
        int input_timeout (int filedes, unsigned int seconds);
135
 
136
    private:
137
        URL_t mURL;
138
        bool mSSLInitialized{false};
139
        SSL_CTX *mCtx{nullptr};
140
        SSL *mSsl{nullptr};
141
        std::string mUser;
142
        std::string mPassword;
143
        std::vector<HTTPHEAD_t> mHeader;
144
        HTTPBODY_t mBody;
145
        HTTPREQUEST_t mRequest;
146
};
147
 
148
#endif