-
Notifications
You must be signed in to change notification settings - Fork 5
/
internet.cpp
82 lines (69 loc) · 2.23 KB
/
internet.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
#include "stdafx.h"
#include "internet.hpp"
internetConnection::internetConnection(string version) {
hasError = false;
isRunning = false;
pluginVersion = version;
}
internetConnection::~internetConnection() {
// Closing all handlers when closing the plugin
if (hResourceHandle)
InternetCloseHandle(hResourceHandle);
if (hConnectHandle)
InternetCloseHandle(hConnectHandle);
if (hOpenHandle)
InternetCloseHandle(hOpenHandle);
}
string internetConnection::getData() {
string answer;
DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
string AGENT = "VCH/";
AGENT += pluginVersion;
hOpenHandle = InternetOpen(AGENT.c_str(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, dwFlags);
if (!hOpenHandle) {
isRunning = false;
hasError = true;
return to_string(GetLastError());
}
hConnectHandle = InternetConnect(hOpenHandle, "raw.githubusercontent.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, dwFlags, 0);
if (!hConnectHandle) {
InternetCloseHandle(hOpenHandle);
isRunning = false;
hasError = true;
return to_string(GetLastError());
}
string rtypestr = "GET";
const char* rtype = rtypestr.c_str();
hResourceHandle = HttpOpenRequest(hConnectHandle, rtype, "/DrFreas/VCH/master/version", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
string ws_Headers = "Content-Type: application/x-www-form-urlencoded\r\n Cache-Control: no-cache";
DWORD dwStatus;
DWORD dwStatusSize = sizeof(dwStatus);
bool res = HttpSendRequest(hResourceHandle, ws_Headers.c_str(), ws_Headers.size(), NULL, NULL);
if (res) {
DWORD dwBytes;
char tmp;
while (InternetReadFile(hResourceHandle, &tmp, 1, &dwBytes)) {
if (dwBytes != 1) break;
answer += tmp;
}
HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
switch (dwStatus) {
case HTTP_STATUS_OK:
break;
case HTTP_STATUS_DENIED:
answer = "ERROR: Access denied!";
break;
default:
answer = "ERROR: ";
answer += to_string(dwStatus);
break;
}
} else {
answer += "ERROR: Request didn't work!";
}
InternetCloseHandle(hResourceHandle);
InternetCloseHandle(hConnectHandle);
InternetCloseHandle(hOpenHandle);
answer.erase(answer.find_last_not_of("\n\r") + 1);
return answer;
}