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
|
/*
Copyright © 2012-24 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"
AsyncHttpRequest::AsyncHttpRequest(int type, const char *szUrl, MTHttpRequestHandler pHandler)
{
m_pFunc = pHandler;
requestType = type;
m_szUrl = szUrl;
}
void CTwitterProto::Execute(AsyncHttpRequest *pReq)
{
if (pReq->m_szUrl[0] == '/')
pReq->m_szUrl.Insert(0, "https://api.twitter.com");
bool bIsJson = false;
if (!pReq->m_szParam.IsEmpty()) {
if (pReq->requestType == REQUEST_POST) {
if (pReq->m_szParam[0] == '{') {
bIsJson = true;
pReq->AddHeader("Content-Type", "application/json");
}
else pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded");
pReq->AddHeader("Cache-Control", "no-cache");
}
else if (pReq->requestType == REQUEST_PATCH)
pReq->requestType = REQUEST_POST;
}
// CMStringA auth(FORMAT, "%s:%s", OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
// auth.Format("Basic: %s", ptrA(mir_base64_encode(auth, auth.GetLength())).get());
// if (pReq->requestType == REQUEST_GET)
// auth = OAuthWebRequestSubmit(pReq->m_szUrl, "GET", "");
//else
// auth = OAuthWebRequestSubmit(pReq->m_szUrl, "POST", (bIsJson) ? "" : pReq->pData);
// pReq->AddHeader("Authorization", auth);
pReq->flags = NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT;
pReq->nlc = m_hConnHttp;
NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, pReq));
if (resp) {
m_hConnHttp = resp->nlc;
if (pReq->m_pFunc != nullptr)
(this->*(pReq->m_pFunc))(resp, pReq);
}
else m_hConnHttp = nullptr;
delete pReq;
}
void CTwitterProto::Push(AsyncHttpRequest *pReq)
{
{
mir_cslock lck(m_csHttpQueue);
m_arHttpQueue.insert(pReq);
}
SetEvent(m_evRequestsQueue);
}
void __cdecl CTwitterProto::ServerThread(void *)
{
m_bTerminated = false;
m_hWorkerThreadId = GetCurrentThreadId();
debugLogA("CTwitterProto::ServerThread: %s", "entering");
while (true) {
WaitForSingleObject(m_evRequestsQueue, 1000);
if (m_bTerminated)
break;
while (true) {
bool bNeedSleep = false;
AsyncHttpRequest *pReq;
{
mir_cslock lck(m_csHttpQueue);
if (m_arHttpQueue.getCount() == 0)
break;
pReq = m_arHttpQueue[0];
m_arHttpQueue.remove(0);
bNeedSleep = (m_arHttpQueue.getCount() > 1);
}
if (m_bTerminated)
break;
Execute(pReq);
if (bNeedSleep)
Sleep(200);
}
}
m_hWorkerThreadId = 0;
debugLogA("CTwitterProto::ServerThread: %s", "leaving");
}
|