-
Notifications
You must be signed in to change notification settings - Fork 0
/
Imupager.cpp
170 lines (153 loc) · 4.05 KB
/
Imupager.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
#include "Imupager.h"
Imupager::Imupager(){
adr = L"api.imgur.com";
host = L"/3/image";
verb = L"POST";
bResults = FALSE;
uSuccess = FALSE;
};
Imupager::~Imupager()
{
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
};
void Imupager::initializeUpload()
{
bResults = FALSE;
uSuccess = FALSE;
bResults = initializeWinHTTP();
if (bResults)
{
std::cout << "Connection established." << std::endl;
hRequest = WinHttpOpenRequest(hConnect, verb, host, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH);
if (hRequest)
{
std::cout << "Request opened." << std::endl;
//Imgur Client ID authorization header
WinHttpAddRequestHeaders(hRequest, L"authorization: Client-ID _PLACEHOLDER_", -1L, WINHTTP_ADDREQ_FLAG_ADD);
}
else
printError("WinHttpAddRequestHeaders");
}
else
printError("WinHttpOpenRequest");
}
void Imupager::uploadFile(void* file, DWORD size)
{
if (bResults)
{
bResults = WinHttpSendRequest(hRequest, L"file:", -1L, NULL, 0, size, 0);
if (bResults)
{
std::cout << "Request sent." << std::endl;
bResults = WinHttpWriteData(hRequest, (LPVOID)file, size, &rSize);
if (bResults)
receiveResponse();
else
printError("WinHttpWriteData");
}
else
printError("WinHttpSendRequest");
}
}
void Imupager::uploadUrl(char* url)
{
if (bResults)
{
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, NULL, 0, strlen(url), 0);
if (bResults)
{
std::cout << "Request sent." << std::endl;
bResults = WinHttpWriteData(hRequest, url, strlen(url), &rSize);
if (bResults)
receiveResponse();
else
printError("WinHttpWriteData");
}
else
printError("WinHttpSendRequest");
}
}
bool Imupager::uploadSuccessful()
{
return uSuccess;
}
std::string Imupager::getUrl()
{
return uUrl;
}
bool Imupager::initializeWinHTTP()
{
//initializing the use of WinHTTP functions and getting WinHTTP-session handle.
hSession = WinHttpOpen(L"Imupager/1.1", WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession)
{
std::cout << "Session established." << std::endl;
hConnect = WinHttpConnect(hSession, adr, INTERNET_DEFAULT_HTTPS_PORT, 0);
if (hConnect != NULL)
return true;
else
printError("WinHttpConnect");
}
else
printError("WinHttpOpen");
return false;
}
void Imupager::receiveResponse()
{
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (bResults)
{
std::cout << "Receiving a response." << std::endl;
bResults = WinHttpQueryDataAvailable(hRequest, &rSize);
if (bResults)
{
response = new char[rSize]();
if (!WinHttpReadData(hRequest, (LPVOID)response, rSize, &rDownloaded))
printError("WinHttpReadData");
else
parseResponse(response, rDownloaded);
//std::cout << response << std::endl;
delete[] response;
}
else
printError("WinHttpQueryDataAvailable");
}
else
printError("WinHttpReceiveResponse");
}
void Imupager::parseResponse(char* response, DWORD size)
{
std::string r(response, size);
size_t succ = r.find("\"success\":true");
if (succ != std::string::npos)
{
//Upload successful
//Let's get the url
//Response looks like this: [...],"link":"http:\/\/i.imgur[...]"},[...]
size_t lBegin = r.find("link") + strlen("link\":\"");
//Searching for the ending '"' of link
size_t lEnd = r.find('"', lBegin);
if (lEnd != std::string::npos)
{
std::string url = std::string(r, lBegin, lEnd - lBegin + 1);
//Let's sanitize the url a little; '\' characters have to go
std::string sanitizedUrl;
std::remove_copy(url.begin(), url.end(), std::back_inserter(sanitizedUrl), '\\');
uUrl = std::string(sanitizedUrl);
uSuccess = TRUE;
}
else
std::cout << "Error while parsing the response." << std::endl;
}
else
{
std::cout << "Invalid response from server: " << std::endl << response << std::endl;
}
}
void Imupager::printError(char* name)
{
std::cout << "Error in " << name << " : " << GetLastError() << std::endl;
}