Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 andreas 1
/*
88 andreas 2
 * Copyright (C) 2021, 2022 by Andreas Theofilu <andreas@theosys.at>
21 andreas 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>
88 andreas 23
#include <vector>
21 andreas 24
 
25
#include <netdb.h>
26
#include <openssl/ssl.h>
27
 
88 andreas 28
#include "tsocket.h"
29
 
21 andreas 30
typedef enum METHOD_t
31
{
32
    UNSUPPORTED,
33
    GET,
34
    PUT,
35
    POST,
36
    HEAD
37
}METHOD_t;
38
 
39
typedef struct URL_t
40
{
41
    std::string scheme;
42
    std::string host;
43
    int port{0};
44
    std::string path;
45
    std::string user;
46
    std::string pw;
47
 
48
    void clear()
49
    {
50
        scheme.clear();
51
        host.clear();
52
        port = 0;
53
        path.clear();
54
        user.clear();
55
        pw.clear();
56
    }
57
}URL_t;
58
 
59
typedef struct HTTPHEAD_t
60
{
61
    std::string name;
62
    std::string content;
63
}HTTPHEAD_t;
64
 
65
typedef struct HTTPBODY_t
66
{
67
    char *body{nullptr};
68
    size_t len{0};
69
 
70
    void clear()
71
    {
72
        if (body != nullptr)
73
            delete[] body;
74
 
75
        body = nullptr;
76
        len = 0;
77
    }
78
}HTTPBODY_t;
79
 
80
typedef struct HTTPREQUEST_t
81
{
82
    METHOD_t method{UNSUPPORTED};
83
    int direction{0};
84
    int code{0};            // on receive
85
    char *status{nullptr};  // on receive
86
    char *path{nullptr};
87
    char *version{nullptr};
88
 
89
    void clear()
90
    {
91
        direction = 0;
92
        code = 0;
93
        method = UNSUPPORTED;
94
 
95
        if (status != nullptr)
96
            delete[] status;
97
 
98
        status = nullptr;
99
 
100
        if (path != nullptr)
101
            delete[] path;
102
 
103
        path = nullptr;
104
 
105
        if (version != nullptr)
106
            delete[] version;
107
 
108
        version = nullptr;
109
    }
110
}HTTPREQUEST_t;
111
 
88 andreas 112
class THTTPClient : public TSocket
21 andreas 113
{
114
    public:
115
        THTTPClient();
116
        ~THTTPClient();
117
 
118
        char *tcall(size_t *size, const std::string& URL, const std::string& user, const std::string& pw);
119
        std::string makeURL(const std::string& scheme, const std::string& host, int port, const std::string& path);
94 andreas 120
        static std::string makeURLs(const std::string& scheme, const std::string& host, int port, const std::string& path);
93 andreas 121
//        char *getContent(const char *buffer);
94 andreas 122
        char *getContent() { return mBody.body; }
21 andreas 123
        size_t getContentSize() { return mBody.len; }
124
 
125
    protected:
126
        URL_t& parseURL(const std::string& URL);
127
        int sockWrite(int fd, char *buffer, size_t len);
128
        int sockRead(int fd, char *buffer, size_t len);
129
        int parseHeader(const char *buffer, size_t len);
130
        std::string getHeadParameter(const std::string& name);
131
        std::string makeRequest(const std::string& url);
132
 
133
    private:
134
        URL_t mURL;
135
        std::string mUser;
136
        std::string mPassword;
137
        std::vector<HTTPHEAD_t> mHeader;
138
        HTTPBODY_t mBody;
139
        HTTPREQUEST_t mRequest;
140
};
141
 
142
#endif