-
Notifications
You must be signed in to change notification settings - Fork 37
/
AimpHTTP.cpp
251 lines (206 loc) · 9.3 KB
/
AimpHTTP.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
#include "AimpHTTP.h"
#include "AIMPYouTube.h"
#include "AIMPString.h"
#include "Tools.h"
#include <process.h>
#include "SDK/apiFileManager.h"
bool AimpHTTP::m_initialized = false;
IAIMPServiceHTTPClient *AimpHTTP::m_httpClient = nullptr;
std::set<AimpHTTP::EventListener *> AimpHTTP::m_handlers;
AimpHTTP::EventListener::EventListener(CallbackFunc callback, bool isFile) : m_isFileStream(isFile), m_callback(callback) {
AimpHTTP::m_handlers.insert(this);
}
AimpHTTP::EventListener::~EventListener() {
AimpHTTP::m_handlers.erase(this);
}
void WINAPI AimpHTTP::EventListener::OnAccept(IAIMPString *ContentType, const INT64 ContentSize, BOOL *Allow) {
ContentType->AddRef();
ContentType->Release();
*Allow = AimpHTTP::m_initialized && Plugin::instance()->core();
}
void WINAPI AimpHTTP::EventListener::OnAcceptHeaders(IAIMPString *Header, BOOL *Allow) {
Header->AddRef();
Header->Release();
*Allow = AimpHTTP::m_initialized && Plugin::instance()->core();
}
void WINAPI AimpHTTP::EventListener::OnComplete(IAIMPErrorInfo *ErrorInfo, BOOL Canceled) {
if (m_stream) {
if (AimpHTTP::m_initialized && Plugin::instance()->core()) {
if (m_isFileStream) {
m_stream->Release();
if (m_callback)
m_callback(nullptr, 0);
return;
}
if (m_callback || m_imageContainer) {
int s = (int)m_stream->GetSize();
unsigned char *buf = new unsigned char[s + 1];
buf[s] = 0;
m_stream->Seek(0, AIMP_STREAM_SEEKMODE_FROM_BEGINNING);
m_stream->Read(buf, s);
if (m_callback) {
m_callback(buf, s);
} else if (m_imageContainer) {
if (s <= m_maxSize) {
(*m_imageContainer)->SetDataSize(s);
memcpy((*m_imageContainer)->GetData(), buf, s);
} else {
(*m_imageContainer)->Release();
*m_imageContainer = nullptr;
}
}
delete[] buf;
}
}
m_stream->Release();
}
}
void WINAPI AimpHTTP::EventListener::OnProgress(const INT64 Downloaded, const INT64 Total) {
}
bool AimpHTTP::Get(const std::wstring &url, CallbackFunc callback, bool synchronous) {
if (!AimpHTTP::m_initialized || !Plugin::instance()->core())
return false;
EventListener *listener = new EventListener(callback);
Plugin::instance()->core()->CreateObject(IID_IAIMPMemoryStream, reinterpret_cast<void **>(&(listener->m_stream)));
return SUCCEEDED(m_httpClient->Get(AIMPString(url), synchronous ? AIMP_SERVICE_HTTPCLIENT_FLAGS_WAITFOR : 0, listener->m_stream, listener, 0, reinterpret_cast<void **>(&(listener->m_taskId))));
}
bool AimpHTTP::Download(const std::wstring &url, const std::wstring &destination, CallbackFunc callback) {
if (!AimpHTTP::m_initialized || !Plugin::instance()->core())
return false;
EventListener *listener = new EventListener(callback, true);
IAIMPServiceFileStreaming *fileStreaming = nullptr;
if (SUCCEEDED(Plugin::instance()->core()->QueryInterface(IID_IAIMPServiceFileStreaming, reinterpret_cast<void **>(&fileStreaming)))) {
fileStreaming->CreateStreamForFile(AIMPString(destination), AIMP_SERVICE_FILESTREAMING_FLAG_CREATENEW, -1, -1, &(listener->m_stream));
fileStreaming->Release();
}
return SUCCEEDED(m_httpClient->Get(AIMPString(url), 0, listener->m_stream, listener, 0, reinterpret_cast<void **>(&(listener->m_taskId))));
}
bool AimpHTTP::DownloadImage(const std::wstring &url, IAIMPImageContainer **Image, int maxSize) {
if (!AimpHTTP::m_initialized || !Plugin::instance()->core())
return false;
EventListener *listener = new EventListener(nullptr);
Plugin::instance()->core()->CreateObject(IID_IAIMPMemoryStream, reinterpret_cast<void **>(&(listener->m_stream)));
if (SUCCEEDED(Plugin::instance()->core()->CreateObject(IID_IAIMPImageContainer, reinterpret_cast<void **>(Image)))) {
listener->m_imageContainer = Image;
listener->m_maxSize = maxSize;
return SUCCEEDED(m_httpClient->Get(AIMPString(url), AIMP_SERVICE_HTTPCLIENT_FLAGS_WAITFOR, listener->m_stream, listener, 0, reinterpret_cast<void **>(&(listener->m_taskId))));
}
return false;
}
bool AimpHTTP::Post(const std::wstring &url, const std::string &body, CallbackFunc callback, bool synchronous) {
if (!AimpHTTP::m_initialized || !Plugin::instance()->core())
return false;
IAIMPStream *postData = nullptr;
if (SUCCEEDED(Plugin::instance()->core()->CreateObject(IID_IAIMPMemoryStream, reinterpret_cast<void **>(&postData)))) {
postData->Write((unsigned char *)(body.data()), body.size(), nullptr);
EventListener *listener = new EventListener(callback);
Plugin::instance()->core()->CreateObject(IID_IAIMPMemoryStream, reinterpret_cast<void **>(&(listener->m_stream)));
bool ok = SUCCEEDED(m_httpClient->Post(AIMPString(url), synchronous ? AIMP_SERVICE_HTTPCLIENT_FLAGS_WAITFOR : 0, listener->m_stream, postData, listener, 0, reinterpret_cast<void **>(&(listener->m_taskId))));
postData->Release();
return ok;
}
return false;
}
bool AimpHTTP::Init(IAIMPCore *Core) {
m_initialized = SUCCEEDED(Core->QueryInterface(IID_IAIMPServiceHTTPClient, reinterpret_cast<void **>(&m_httpClient)));
return m_initialized;
}
void AimpHTTP::Deinit() {
m_initialized = false;
std::unordered_set<uintptr_t *> ids;
for (auto x : m_handlers) ids.insert(x->m_taskId);
for (auto x : ids) m_httpClient->Cancel(x, AIMP_SERVICE_HTTPCLIENT_FLAGS_WAITFOR);
if (m_httpClient) {
m_httpClient->Release();
m_httpClient = nullptr;
}
}
bool AimpHTTP::Put(const std::wstring &url, CallbackFunc callback) {
return RawRequest("PUT", url, callback);
}
bool AimpHTTP::Delete(const std::wstring &url, CallbackFunc callback) {
return RawRequest("DELETE", url, callback);
}
void AimpHTTP::RawRequestThread(void *args) {
ThreadParams *params = static_cast<ThreadParams *>(args);
std::string request = params->request;
std::string host = params->host;
AimpHTTP::CallbackFunc callback = params->callback;
delete params;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
DebugA("WSAStartup failed.\n");
return;
}
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *targetAdressInfo = NULL;
DWORD getAddrRes = getaddrinfo(host.c_str(), NULL, &hints, &targetAdressInfo);
if (getAddrRes != 0 || targetAdressInfo == NULL) {
DebugA("Could not resolve the Host Name\n");
WSACleanup();
return;
}
SOCKADDR_IN sockAddr;
sockAddr.sin_addr = ((struct sockaddr_in *)targetAdressInfo->ai_addr)->sin_addr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(80);
freeaddrinfo(targetAdressInfo);
SOCKET webSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (webSocket == INVALID_SOCKET) {
DebugA("Creation of the Socket Failed\n");
WSACleanup();
return;
}
if (connect(webSocket, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) != 0) {
DebugA("Could not connect\n");
closesocket(webSocket);
WSACleanup();
return;
}
const char *httpRequest = request.c_str();
int sentBytes = send(webSocket, httpRequest, strlen(httpRequest), 0);
if (sentBytes < (int)request.size() || sentBytes == SOCKET_ERROR) {
DebugA("Could not send the request to the Server");
closesocket(webSocket);
WSACleanup();
return;
}
char buffer[10240];
ZeroMemory(buffer, sizeof(buffer));
int dataLen;
if ((dataLen = recv(webSocket, buffer, sizeof(buffer), 0)) > 0) {
if (char *body = strstr(buffer, "\r\n\r\n")) {
body += 4;
if (callback && m_initialized && Plugin::instance()->core())
callback(reinterpret_cast<unsigned char *>(body), strlen(body));
}
}
closesocket(webSocket);
WSACleanup();
};
bool AimpHTTP::RawRequest(const std::string &method, const std::wstring &url, CallbackFunc callback) {
// Not perfect but does its job
std::string narrow_url = Tools::ToString(url);
const char *urlc = narrow_url.c_str();
if (urlc = strstr(urlc, "://")) {
urlc += 3;
if (const char *request = strstr(urlc, "/")) {
ThreadParams *p = new ThreadParams();
p->request += method + " ";
p->request += request;
p->host = std::string(urlc, request);
p->request += " HTTP/1.1\r\nHost: " + p->host + "\r\nConnection: close\r\n\r\n";
p->callback = callback;
HANDLE hThread = (HANDLE)_beginthread(AimpHTTP::RawRequestThread, 0, p);
return hThread != INVALID_HANDLE_VALUE;
}
}
return false;
}