diff options
author | George Hazan <george.hazan@gmail.com> | 2023-09-04 19:40:46 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-09-04 19:40:46 +0300 |
commit | 27ec5fe7a4be3b1b6e5f99a705ebe2694ffb8ad1 (patch) | |
tree | e0fc872852f85aaec2b7013e196d668b24424204 /protocols/Twitter/src/http.cpp | |
parent | ac719164dbdbc142e15bebc0813d781d0e61b44c (diff) |
Twitter: API 2.0 auth (beginning)
Diffstat (limited to 'protocols/Twitter/src/http.cpp')
-rw-r--r-- | protocols/Twitter/src/http.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/protocols/Twitter/src/http.cpp b/protocols/Twitter/src/http.cpp new file mode 100644 index 0000000000..888afdfa84 --- /dev/null +++ b/protocols/Twitter/src/http.cpp @@ -0,0 +1,124 @@ +/* +Copyright © 2012-23 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"); + + pReq->dataLength = (int)pReq->m_szParam.GetLength(); + pReq->pData = pReq->m_szParam.Detach(); + } + else { + if (pReq->requestType == REQUEST_PATCH) + pReq->requestType = REQUEST_POST; + + pReq->m_szUrl.AppendChar('?'); + pReq->m_szUrl += pReq->m_szParam; + } + } + + // 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->szUrl = pReq->m_szUrl.GetBuffer(); + 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"); +} |