forked from ErikMinekus/sm-ripext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpfilecontext.cpp
113 lines (99 loc) · 3.43 KB
/
httpfilecontext.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
/**
* 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:https://www.gnu.org/licenses/>.
*/
#include "httpfilecontext.h"
HTTPFileContext::HTTPFileContext(bool isUpload, const ke::AString &url, const ke::AString &path,
struct curl_slist *headers, IChangeableForward *forward, cell_t value,
long connectTimeout, long followLocation, long timeout, curl_off_t maxSendSpeed, curl_off_t maxRecvSpeed)
: isUpload(isUpload), url(url), path(path), headers(headers), forward(forward), value(value),
connectTimeout(connectTimeout), followLocation(followLocation), timeout(timeout),
maxSendSpeed(maxSendSpeed), maxRecvSpeed(maxRecvSpeed)
{}
HTTPFileContext::~HTTPFileContext()
{
forwards->ReleaseForward(forward);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
if (file)
{
fclose(file);
}
}
void HTTPFileContext::InitCurl()
{
curl = curl_easy_init();
if (curl == NULL)
{
smutils->LogError(myself, "Could not initialize cURL session.");
return;
}
char realpath[PLATFORM_MAX_PATH];
smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path.chars());
file = fopen(realpath, isUpload ? "rb" : "wb");
if (file == NULL)
{
smutils->LogError(myself, "Could not open file %s.", path.chars());
return;
}
if (isUpload)
{
curl_easy_setopt(curl, CURLOPT_READDATA, file);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
}
else
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
}
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_CAINFO, g_RipExt.caBundlePath);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connectTimeout);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, followLocation);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
curl_easy_setopt(curl, CURLOPT_PRIVATE, this);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_URL, url.chars());
if (maxRecvSpeed > 0)
{
curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, maxRecvSpeed);
}
if (maxSendSpeed > 0)
{
curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, maxSendSpeed);
}
}
void HTTPFileContext::OnCompleted()
{
/* Return early if the plugin was unloaded while the thread was running */
if (forward->GetFunctionCount() == 0)
{
return;
}
long status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
forward->PushCell(status);
forward->PushCell(value);
forward->PushString(error);
forward->Execute(NULL);
}