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
|
/*
Copyright © 2012-19 Miranda NG team
Copyright © 2009 Jim Porter
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 2 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 "stdafx.h"
#include "utility.h"
#include <io.h>
std::string b64encode(const std::string &s)
{
return std::string(ptrA(mir_base64_encode(s.c_str(), s.length())));
}
std::string int2str(int32_t iVal)
{
char buf[100];
_itoa_s(iVal, buf, 10);
return std::string(buf);
}
std::string int2str(uint64_t iVal)
{
char buf[100];
_i64toa_s(iVal, buf, _countof(buf), 10);
return std::string(buf);
}
uint64_t str2int(const std::string &str)
{
return _atoi64(str.c_str());
}
http::response mir_twitter::slurp(const std::string &url, http::method meth, OAuthParameters postParams)
{
NETLIBHTTPREQUEST req = { sizeof(req) };
req.requestType = (meth == http::get) ? REQUEST_GET : REQUEST_POST;
req.szUrl = const_cast<char*>(url.c_str());
std::wstring url_WSTR = UTF8ToWide(url);
std::string pdata_STR;
std::wstring pdata_WSTR;
std::wstring auth;
if (meth == http::get) {
if (url_WSTR.size() > 0) { ppro_->debugLogW(L"**SLURP::GET - we have a URL: %s", url_WSTR.c_str()); }
if (consumerKey_.size() > 0) { ppro_->debugLogA("**SLURP::GET - we have a consumerKey"); }
if (consumerSecret_.size() > 0) { ppro_->debugLogA("**SLURP::GET - we have a consumerSecret"); }
if (oauthAccessToken_.size() > 0) { ppro_->debugLogA("**SLURP::GET - we have a oauthAccessToken"); }
if (oauthAccessTokenSecret_.size() > 0) { ppro_->debugLogA("**SLURP::GET - we have a oauthAccessTokenSecret"); }
if (pin_.size() > 0) { ppro_->debugLogA("**SLURP::GET - we have a pin"); }
auth = OAuthWebRequestSubmit(url_WSTR, L"GET", nullptr, consumerKey_, consumerSecret_,
oauthAccessToken_, oauthAccessTokenSecret_, pin_);
}
else if (meth == http::post) {
// OAuthParameters postParams;
if (url_WSTR.size() > 0) { ppro_->debugLogW(L"**SLURP::POST - we have a URL: %s", url_WSTR.c_str()); }
if (consumerKey_.size() > 0) { ppro_->debugLogA("**SLURP::POST - we have a consumerKey"); }
if (consumerSecret_.size() > 0) { ppro_->debugLogA("**SLURP::POST - we have a consumerSecret"); }
if (oauthAccessToken_.size() > 0) { ppro_->debugLogA("**SLURP::POST - we have a oauthAccessToken"); }
if (oauthAccessTokenSecret_.size() > 0) { ppro_->debugLogA("**SLURP::POST - we have a oauthAccessTokenSecret"); }
if (pin_.size() > 0) { ppro_->debugLogA("**SLURP::POST - we have a pin"); }
pdata_WSTR = BuildQueryString(postParams);
ppro_->debugLogW(L"**SLURP::POST - post data is: %s", pdata_WSTR.c_str());
auth = OAuthWebRequestSubmit(url_WSTR, L"POST", &postParams, consumerKey_, consumerSecret_, oauthAccessToken_, oauthAccessTokenSecret_);
}
else ppro_->debugLogA("**SLURP - There is something really wrong.. the http method was neither get or post.. WHY??");
std::string auth_STR = WideToUTF8(auth);
NETLIBHTTPHEADER hdr[3];
hdr[0].szName = "Authorization";
hdr[0].szValue = const_cast<char*>(auth_STR.c_str());
req.headers = hdr;
req.headersCount = 1;
if (meth == http::post) {
hdr[1].szName = "Content-Type";
hdr[1].szValue = "application/x-www-form-urlencoded";
hdr[2].szName = "Cache-Control";
hdr[2].szValue = "no-cache";
pdata_STR = WideToUTF8(pdata_WSTR);
req.headersCount = 3;
req.dataLength = (int)pdata_STR.size();
req.pData = const_cast<char*>(pdata_STR.c_str());
ppro_->debugLogA("**SLURP::POST - req.pdata is %s", req.pData);
}
req.flags = NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT;
req.nlc = httpPOST_;
http::response resp_data;
ppro_->debugLogA("**SLURP - just before calling HTTPTRANSACTION");
NETLIBHTTPREQUEST *resp = Netlib_HttpTransaction(handle_, &req);
ppro_->debugLogA("**SLURP - HTTPTRANSACTION complete.");
if (resp) {
ppro_->debugLogA("**SLURP - the server has responded!");
httpPOST_ = resp->nlc;
resp_data.code = resp->resultCode;
resp_data.data = resp->pData ? resp->pData : "";
Netlib_FreeHttpRequest(resp);
}
else {
httpPOST_ = nullptr;
ppro_->debugLogA("SLURP - there was no response!");
}
return resp_data;
}
bool save_url(HNETLIBUSER hNetlib, const std::string &url, const std::wstring &filename)
{
NETLIBHTTPREQUEST req = { sizeof(req) };
req.requestType = REQUEST_GET;
req.flags = NLHRF_HTTP11 | NLHRF_REDIRECT;
req.szUrl = const_cast<char*>(url.c_str());
NETLIBHTTPREQUEST *resp = Netlib_HttpTransaction(hNetlib, &req);
if (resp) {
bool success = (resp->resultCode == 200);
if (success) {
// Create folder if necessary
std::wstring dir = filename.substr(0, filename.rfind('\\'));
if (_waccess(dir.c_str(), 0))
CreateDirectoryTreeW(dir.c_str());
// Write to file
FILE *f = _wfopen(filename.c_str(), L"wb");
fwrite(resp->pData, 1, resp->dataLength, f);
fclose(f);
}
Netlib_FreeHttpRequest(resp);
return success;
}
return false;
}
|