forked from ErikMinekus/sm-ripext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.cpp
160 lines (132 loc) · 4.98 KB
/
httpclient.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod REST in Pawn Extension
* Copyright 2017-2020 Erik Minekus
* =============================================================================
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "httpclient.h"
#include "httprequestcontext.h"
#include "httpfilecontext.h"
const std::string HTTPClient::BuildURL(const std::string &endpoint) const
{
std::string url(this->baseURL);
url.append("/");
url.append(endpoint);
return url;
}
struct curl_slist *HTTPClient::BuildHeaders(const char *acceptTypes, const char *contentType)
{
struct curl_slist *headers = NULL;
char header[8192];
snprintf(header, sizeof(header), "Accept: %s", acceptTypes);
headers = curl_slist_append(headers, header);
snprintf(header, sizeof(header), "Content-Type: %s", contentType);
headers = curl_slist_append(headers, header);
for (HTTPHeaderMap::iterator iter = this->headers.iter(); !iter.empty(); iter.next())
{
snprintf(header, sizeof(header), "%s: %s", iter->key.chars(), iter->value.c_str());
headers = curl_slist_append(headers, header);
}
return headers;
}
void HTTPClient::Request(const char *method, const char *endpoint, char *data, IPluginFunction *callback, cell_t value)
{
IChangeableForward *forward = forwards->CreateForwardEx(NULL, ET_Ignore, 3, NULL, Param_Cell, Param_Cell, Param_String);
if (forward == NULL || !forward->AddFunction(callback))
{
smutils->LogError(myself, "Could not create forward.");
return;
}
const std::string url = this->BuildURL(std::string(endpoint));
struct curl_slist *headers = this->BuildHeaders("application/json", "application/json");
HTTPRequestContext *context = new HTTPRequestContext(std::string(method), url, data, headers, forward, value,
this->connectTimeout, this->followLocation ? 5 : 0, this->timeout, this->maxSendSpeed, this->maxRecvSpeed, false, "", "");
g_RipExt.AddRequestToQueue(context);
}
void HTTPClient::DownloadFile(const char *endpoint, const char *path, IPluginFunction *callback, cell_t value)
{
IChangeableForward *forward = forwards->CreateForwardEx(NULL, ET_Ignore, 3, NULL, Param_Cell, Param_Cell, Param_String);
if (forward == NULL || !forward->AddFunction(callback))
{
smutils->LogError(myself, "Could not create forward.");
return;
}
const std::string url = this->BuildURL(std::string(endpoint));
struct curl_slist *headers = this->BuildHeaders("*/*", "application/octet-stream");
HTTPFileContext *context = new HTTPFileContext(false, url, std::string(path), headers, forward, value,
this->connectTimeout, this->followLocation ? 5 : 0, this->timeout, this->maxSendSpeed, this->maxRecvSpeed, false, "", "");
g_RipExt.AddRequestToQueue(context);
}
void HTTPClient::UploadFile(const char *endpoint, const char *path, IPluginFunction *callback, cell_t value)
{
IChangeableForward *forward = forwards->CreateForwardEx(NULL, ET_Ignore, 3, NULL, Param_Cell, Param_Cell, Param_String);
if (forward == NULL || !forward->AddFunction(callback))
{
smutils->LogError(myself, "Could not create forward.");
return;
}
const std::string url = this->BuildURL(std::string(endpoint));
struct curl_slist *headers = this->BuildHeaders("*/*", "application/octet-stream");
HTTPFileContext *context = new HTTPFileContext(true, url, std::string(path), headers, forward, value,
this->connectTimeout, this->followLocation ? 5 : 0, this->timeout, this->maxSendSpeed, this->maxRecvSpeed, false, "", "");
g_RipExt.AddRequestToQueue(context);
}
void HTTPClient::SetHeader(const char *name, const char *value)
{
std::string vstr(value);
this->headers.replace(name, std::move(vstr));
}
int HTTPClient::GetConnectTimeout() const
{
return this->connectTimeout;
}
void HTTPClient::SetConnectTimeout(int connectTimeout)
{
this->connectTimeout = connectTimeout;
}
bool HTTPClient::GetFollowLocation() const
{
return this->followLocation;
}
void HTTPClient::SetFollowLocation(bool followLocation)
{
this->followLocation = followLocation;
}
int HTTPClient::GetTimeout() const
{
return this->timeout;
}
void HTTPClient::SetTimeout(int timeout)
{
this->timeout = timeout;
}
int HTTPClient::GetMaxSendSpeed() const
{
return this->maxSendSpeed;
}
void HTTPClient::SetMaxSendSpeed(int speed)
{
this->maxSendSpeed = speed;
}
int HTTPClient::GetMaxRecvSpeed() const
{
return this->maxRecvSpeed;
}
void HTTPClient::SetMaxRecvSpeed(int speed)
{
this->maxRecvSpeed = speed;
}