From ed4d2e70f154be0badfcc44afa04c0d8e984f03d Mon Sep 17 00:00:00 2001 From: Sergey Bolhovskoy Date: Wed, 10 Sep 2014 04:27:14 +0000 Subject: VKontakte: redesign PARAM structure attempt 2, redesign AsyncHttpRequest structure git-svn-id: http://svn.miranda-ng.org/main/trunk@10415 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/vk_chats.cpp | 8 ++--- protocols/VKontakte/src/vk_proto.cpp | 2 +- protocols/VKontakte/src/vk_proto.h | 20 ++++++------ protocols/VKontakte/src/vk_queue.cpp | 61 ++++++++++++++--------------------- protocols/VKontakte/src/vk_thread.cpp | 3 +- 5 files changed, 38 insertions(+), 56 deletions(-) (limited to 'protocols/VKontakte/src') diff --git a/protocols/VKontakte/src/vk_chats.cpp b/protocols/VKontakte/src/vk_chats.cpp index c8d041157a..00976c57e9 100644 --- a/protocols/VKontakte/src/vk_chats.cpp +++ b/protocols/VKontakte/src/vk_chats.cpp @@ -338,12 +338,10 @@ int CVkProto::OnChatEvent(WPARAM, LPARAM lParam) UnEscapeChatTags(buf); // need rework - change reply format and type not supported yet AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/messages.send.json", true, &CVkProto::OnSendChatMsg) - << INT_PARAM("type", 1) << INT_PARAM("chat_id", cc->m_chatid); + << INT_PARAM("type", 1) + << INT_PARAM("chat_id", cc->m_chatid) + << CHAR_PARAM("message", mir_utf8encodeT(buf)); pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); - - CMStringA szBody(FORMAT, "message=%s", ptrA(mir_urlEncode(mir_utf8encodeT(buf)))); - pReq->pData = mir_strdup(szBody); - pReq->dataLength = szBody.GetLength(); Push(pReq); } diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index fad6a277e7..59f896c6f5 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -205,7 +205,7 @@ int CVkProto::SendMsg(MCONTACT hContact, int flags, const char *msg) ULONG msgId = ::InterlockedIncrement(&m_msgId); AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/messages.send.json", true, &CVkProto::OnSendMessage) << INT_PARAM("user_id", userID) - << CHAR_PARAM("message", szMsg, reqPOST) + << CHAR_PARAM("message", szMsg) << VER_API; pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); pReq->pUserInfo = new CVkSendMsgParam(hContact, msgId); diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index c789a24138..d1ff3aa750 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -32,25 +32,23 @@ struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject void Redirect(NETLIBHTTPREQUEST*); CMStringA m_szUrl; - bool bNeedsRestart, bIsMainConn, m_bHasGParams, m_bHasPParams; + CMStringA m_szParam; + bool bNeedsRestart, bIsMainConn; VK_REQUEST_HANDLER m_pFunc; void *pUserInfo; }; -enum ReqType {reqGET, reqPOST}; - struct PARAM { LPCSTR szName; - ReqType rtType; - __forceinline PARAM(LPCSTR _name, ReqType _type = reqGET) : szName(_name), rtType(_type) {} + __forceinline PARAM(LPCSTR _name) : szName(_name) {} }; struct INT_PARAM : public PARAM { int iValue; - __forceinline INT_PARAM(LPCSTR _name, int _value, ReqType _type = reqGET) : - PARAM(_name, _type), iValue(_value) + __forceinline INT_PARAM(LPCSTR _name, int _value) : + PARAM(_name), iValue(_value) { } }; @@ -59,8 +57,8 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT_PARAM&); struct CHAR_PARAM : public PARAM { LPCSTR szValue; - __forceinline CHAR_PARAM(LPCSTR _name, LPCSTR _value, ReqType _type = reqGET) : - PARAM(_name, _type), szValue(_value) + __forceinline CHAR_PARAM(LPCSTR _name, LPCSTR _value) : + PARAM(_name), szValue(_value) { } }; @@ -69,8 +67,8 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest*, const CHAR_PARAM&); struct TCHAR_PARAM : public PARAM { LPCTSTR tszValue; - __forceinline TCHAR_PARAM(LPCSTR _name, LPCTSTR _value, ReqType _type = reqGET) : - PARAM(_name, _type), tszValue(_value) + __forceinline TCHAR_PARAM(LPCSTR _name, LPCTSTR _value) : + PARAM(_name), tszValue(_value) { } }; diff --git a/protocols/VKontakte/src/vk_queue.cpp b/protocols/VKontakte/src/vk_queue.cpp index 5b9fc4b282..8240b45c84 100644 --- a/protocols/VKontakte/src/vk_queue.cpp +++ b/protocols/VKontakte/src/vk_queue.cpp @@ -35,7 +35,18 @@ void CVkProto::UninitQueue() void CVkProto::ExecuteRequest(AsyncHttpRequest *pReq) { LBL_Restart: - pReq->szUrl = pReq->m_szUrl.GetBuffer(); + if (pReq->m_szParam.IsEmpty()) + pReq->szUrl = pReq->m_szUrl.GetBuffer(); + else if (pReq->requestType == REQUEST_GET){ + CMStringA str(FORMAT, "%s?%s", pReq->m_szUrl, pReq->m_szParam); + pReq->szUrl = str.GetBuffer(); + } + else{ + pReq->szUrl = pReq->m_szUrl.GetBuffer(); + pReq->pData = pReq->m_szParam.GetBuffer(); + pReq->dataLength = pReq->m_szParam.GetLength(); + } + NETLIBHTTPREQUEST *reply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)pReq); if (reply != NULL) { if (pReq->m_pFunc != NULL) @@ -113,52 +124,28 @@ void CVkProto::WorkerThread(void*) AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT_PARAM ¶m) { - if (param.rtType == reqGET){ - CMStringA &s = pReq->m_szUrl; - s.AppendFormat("%c%s=%i", pReq->m_bHasGParams ? '&' : '?', param.szName, param.iValue); - pReq->m_bHasGParams = true; - } - else { - CMStringA s = pReq->pData; - s.AppendFormat("%s%s=%i", pReq->m_bHasPParams ? "&" : "?", param.szName, param.iValue); - pReq->pData = mir_strdup(s); - pReq->dataLength = s.GetLength(); - pReq->m_bHasPParams = true; - } + CMStringA &s = pReq->m_szParam; + if (!s.IsEmpty()) + s.AppendChar('&'); + s.AppendFormat("%s=%i", param.szName, param.iValue); return pReq; } AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const CHAR_PARAM ¶m) { - if (param.rtType == reqGET){ - CMStringA &s = pReq->m_szUrl; - s.AppendFormat("%c%s=%s", pReq->m_bHasGParams ? '&' : '?', param.szName, ptrA(mir_urlEncode(param.szValue))); - pReq->m_bHasGParams = true; - } - else { - CMStringA s = pReq->pData; - s.AppendFormat("%s%s=%s", pReq->m_bHasPParams ? "&" : "", param.szName, ptrA(mir_urlEncode(param.szValue))); - pReq->pData = mir_strdup(s); - pReq->dataLength = s.GetLength(); - pReq->m_bHasPParams = true; - } + CMStringA &s = pReq->m_szParam; + if (!s.IsEmpty()) + s.AppendChar('&'); + s.AppendFormat("%s=%s", param.szName, ptrA(mir_urlEncode(param.szValue))); return pReq; } AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const TCHAR_PARAM ¶m) { ptrA szValue(mir_utf8encodeT(param.tszValue)); - if (param.rtType == reqGET){ - CMStringA &s = pReq->m_szUrl; - s.AppendFormat("%c%s=%s", pReq->m_bHasGParams ? '&' : '?', param.szName, ptrA(mir_urlEncode(szValue))); - pReq->m_bHasGParams = true; - } - else{ - CMStringA s = pReq->pData; - s.AppendFormat("%s%s=%s", pReq->m_bHasGParams ? "&" : "?", param.szName, ptrA(mir_urlEncode(szValue))); - pReq->pData = mir_strdup(s); - pReq->dataLength = s.GetLength(); - pReq->m_bHasPParams = true; - } + CMStringA &s = pReq->m_szParam; + if (!s.IsEmpty()) + s.AppendChar('&'); + s.AppendFormat("%s=%s", param.szName, ptrA(mir_urlEncode(szValue))); return pReq; } diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index cdb229d591..06db4ab44d 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -186,8 +186,7 @@ LBL_NoForm: pReq = new AsyncHttpRequest(); pReq->requestType = REQUEST_POST; pReq->flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11; - pReq->pData = mir_strdup(szBody); - pReq->dataLength = szBody.GetLength(); + pReq->m_szParam = szBody; pReq->m_szUrl = szAction; m_prevUrl = pReq->m_szUrl; pReq->m_pFunc = &CVkProto::OnOAuthAuthorize; pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); -- cgit v1.2.3