From 14c4e44a0a91e1ad701d4ae3c58185d25118e64e Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 5 Jan 2024 15:54:03 +0300 Subject: Netlib: - NETLIBHTTPHEADER & NETLIBHTTPREQUEST obsoleted; - NETLIBHTTPREQUEST divided into MHttpRequest & MHttpResponse; - MHttpHeaders now manager headers both for MHttpRequest & MHttpResponse; --- plugins/HTTPServer/src/GuiElements.cpp | 13 +- plugins/MirLua/src/Modules/m_http.cpp | 272 +++++---------------- plugins/PackUpdater/Src/Utils.cpp | 26 +- plugins/PasteIt/src/PasteToWeb.cpp | 44 +--- plugins/PluginUpdater/src/Utils.cpp | 25 +- plugins/SendScreenshotPlus/src/CSend.cpp | 175 +++++-------- plugins/SendScreenshotPlus/src/CSend.h | 3 +- .../src/CSendHost_ImageShack.cpp | 12 +- .../SendScreenshotPlus/src/CSendHost_ImageShack.h | 2 +- plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp | 11 +- plugins/SendScreenshotPlus/src/CSendHost_imgur.h | 2 +- .../SendScreenshotPlus/src/CSendHost_uploadpie.cpp | 12 +- .../SendScreenshotPlus/src/CSendHost_uploadpie.h | 2 +- plugins/SmileyAdd/src/download.cpp | 39 +-- 14 files changed, 175 insertions(+), 463 deletions(-) (limited to 'plugins') diff --git a/plugins/HTTPServer/src/GuiElements.cpp b/plugins/HTTPServer/src/GuiElements.cpp index 329258e1b9..a3f02f86bb 100644 --- a/plugins/HTTPServer/src/GuiElements.cpp +++ b/plugins/HTTPServer/src/GuiElements.cpp @@ -141,11 +141,9 @@ unsigned long GetExternIP(const char *szURL, const char *szPattern) { HCURSOR hPrevCursor = ::SetCursor(::LoadCursor(nullptr, IDC_WAIT)); - NETLIBHTTPREQUEST nlhr; - memset(&nlhr, 0, sizeof(nlhr)); - nlhr.requestType = REQUEST_GET; + MHttpRequest nlhr; nlhr.flags = NLHRF_DUMPASTEXT; - nlhr.szUrl = (char*)szURL; + nlhr.m_szUrl = szURL; IN_ADDR externIP; externIP.s_addr = 0; @@ -153,16 +151,15 @@ unsigned long GetExternIP(const char *szURL, const char *szPattern) NLHR_PTR nlreply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (nlreply) { if (nlreply->resultCode >= 200 && nlreply->resultCode < 300) { - nlreply->pData[nlreply->dataLength] = 0;// make sure its null terminated - char * pszIp = strstr(nlreply->pData, szPattern); + char *pszIp = strstr(nlreply->body.GetBuffer(), szPattern); if (pszIp == nullptr) - pszIp = nlreply->pData; + pszIp = nlreply->body.GetBuffer(); else pszIp += mir_strlen(szPattern); while ((*pszIp < '0' || *pszIp > '9') && *pszIp) pszIp++; - char * pszEnd = pszIp; + char *pszEnd = pszIp; while ((*pszEnd >= '0' && *pszEnd <= '9') || *pszEnd == '.') pszEnd++; *pszEnd = NULL; diff --git a/plugins/MirLua/src/Modules/m_http.cpp b/plugins/MirLua/src/Modules/m_http.cpp index 7526b29612..fcc0365078 100644 --- a/plugins/MirLua/src/Modules/m_http.cpp +++ b/plugins/MirLua/src/Modules/m_http.cpp @@ -2,154 +2,22 @@ #define MT_NETLIBHTTPHEADERS "NETLIBHTTPHEADERS" -static NETLIBHTTPREQUEST* CreateRequest() +static MHttpRequest* CreateRequest() { - NETLIBHTTPREQUEST *request = (NETLIBHTTPREQUEST*)mir_calloc(sizeof(NETLIBHTTPREQUEST)); + MHttpRequest *request = new MHttpRequest(); request->flags = NLHRF_HTTP11 | NLHRF_NODUMP; return request; } -static void SetUrl(NETLIBHTTPREQUEST *request, const char *url) +static void SetUrl(MHttpRequest *request, const char *url) { - request->szUrl = mir_strdup(url); - if (mir_strncmpi(request->szUrl, "https", 5) == 0) + request->m_szUrl = url; + if (mir_strncmpi(request->m_szUrl, "https", 5) == 0) request->flags |= NLHRF_SSL; else request->flags &= ~(NLHRF_SSL); } -static void SetHeader(NETLIBHTTPREQUEST *request, const char *name, const char *value) -{ - for (int i = 0; i < request->headersCount; i++) { - if (mir_strcmp(request->headers[i].szName, name) == 0) { - mir_free(request->headers[i].szValue); - request->headers[i].szValue = mir_strdup(value); - return; - } - } - - request->headers = (NETLIBHTTPHEADER*)mir_realloc(request->headers, - sizeof(NETLIBHTTPHEADER)*(request->headersCount + 1)); - NETLIBHTTPHEADER &header = request->headers[request->headersCount]; - header.szName = mir_strdup(name); - header.szValue = mir_strdup(value); - request->headersCount++; -} - -static void DropHeader(NETLIBHTTPREQUEST *request, const char *name) -{ - for (int i = 0; i < request->headersCount - 1; i++) { - if (mir_strcmp(request->headers[i].szName, name) == 0) { - mir_free(request->headers[i].szName); - mir_free(request->headers[i].szValue); - request->headersCount--; - request->headers[i].szName = request->headers[request->headersCount].szName; - request->headers[i].szValue = request->headers[request->headersCount].szValue; - } - } - request->headers = (NETLIBHTTPHEADER*)mir_realloc(request->headers, - sizeof(NETLIBHTTPHEADER)*(request->headersCount + 1)); -} - -static void ClearHeaders(NETLIBHTTPREQUEST *request) -{ - for (int i = 0; i < request->headersCount; i++) { - mir_free(request->headers[i].szName); - mir_free(request->headers[i].szValue); - } - request->headersCount = 0; - request->headers = (NETLIBHTTPHEADER*)mir_realloc(request->headers, - sizeof(NETLIBHTTPHEADER)*(request->headersCount + 1)); -} - -static void SetContent(NETLIBHTTPREQUEST *request, const char *data, size_t length) -{ - if (request->pData != nullptr) - mir_free(request->pData); - request->pData = mir_strdup(data); - request->dataLength = (int)length; -} - -/***********************************************/ - -struct NETLIBHTTPHEADERS -{ - const NETLIBHTTPHEADER *headers; - int count; -}; - -static int headers_Iterator(lua_State *L) -{ - NETLIBHTTPHEADER *headers = (NETLIBHTTPHEADER*)lua_touserdata(L, lua_upvalueindex(1)); - int count = lua_tointeger(L, lua_upvalueindex(2)); - int idx = lua_tointeger(L, lua_upvalueindex(3)); - - if (idx < count) { - lua_pushstring(L, headers[idx].szName); - lua_pushstring(L, headers[idx].szValue); - lua_pushinteger(L, idx + 1); - lua_replace(L, lua_upvalueindex(3)); - return 2; - } - - lua_pushnil(L); - - return 1; -} - -static int headers__pairs(lua_State *L) -{ - NETLIBHTTPHEADERS *headers = (NETLIBHTTPHEADERS*)luaL_checkudata(L, 1, MT_NETLIBHTTPHEADERS); - - lua_pushlightuserdata(L, (void*)headers->headers); - lua_pushinteger(L, headers->count); - lua_pushinteger(L, 0); - lua_pushcclosure(L, headers_Iterator, 3); - - return 1; -} - -static int headers__index(lua_State *L) -{ - NETLIBHTTPHEADERS *headers = (NETLIBHTTPHEADERS*)luaL_checkudata(L, 1, MT_NETLIBHTTPHEADERS); - - if (lua_isinteger(L, 2)) { - int idx = lua_tointeger(L, 2); - if (idx > 0 && idx <= headers->count) { - lua_pushstring(L, headers->headers[idx - 1].szValue); - return 1; - } - } - - const char *key = lua_tostring(L, 2); - for (int i = 0; i < headers->count; i++) { - if (mir_strcmp(headers->headers[i].szName, key) == 0) { - lua_pushstring(L, headers->headers[i].szValue); - return 1; - } - } - - lua_pushnil(L); - - return 1; -} - -static int headers__len(lua_State *L) -{ - NETLIBHTTPHEADERS *headers = (NETLIBHTTPHEADERS*)luaL_checkudata(L, 1, MT_NETLIBHTTPHEADERS); - lua_pushinteger(L, headers->count); - return 1; -} - -static const luaL_Reg headersApi[] = -{ - { "__pairs", headers__pairs }, - { "__index", headers__index }, - { "__len", headers__len }, - - { nullptr, nullptr } -}; - /***********************************************/ #define MT_NETLIBHTTPCONTENT "NETLIBHTTPCONTENT" @@ -202,10 +70,10 @@ static const luaL_Reg contentApi[] = #define MT_NETLIBHTTPRESPONSE "NETLIBHTTPRESPONSE" -static NETLIBHTTPREQUEST* response_Create(lua_State *L, NETLIBHTTPREQUEST *request) +static MHttpResponse* response_Create(lua_State *L, MHttpRequest *request) { - NETLIBHTTPREQUEST *response = Netlib_HttpTransaction(g_plugin.hNetlib, request); - NETLIBHTTPREQUEST **udata = (NETLIBHTTPREQUEST**)lua_newuserdata(L, sizeof(NETLIBHTTPREQUEST*)); + auto *response = Netlib_HttpTransaction(g_plugin.hNetlib, request); + MHttpResponse **udata = (MHttpResponse **)lua_newuserdata(L, sizeof(MHttpResponse *)); *udata = response; luaL_setmetatable(L, MT_NETLIBHTTPRESPONSE); return response; @@ -213,7 +81,7 @@ static NETLIBHTTPREQUEST* response_Create(lua_State *L, NETLIBHTTPREQUEST *reque static int response__index(lua_State *L) { - NETLIBHTTPREQUEST *response = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPRESPONSE); + MHttpResponse *response = *(MHttpResponse **)luaL_checkudata(L, 1, MT_NETLIBHTTPRESPONSE); const char *key = lua_tostring(L, 2); if (mir_strcmpi(key, "IsSuccess") == 0) { @@ -226,16 +94,16 @@ static int response__index(lua_State *L) } if (mir_strcmpi(key, "StatusCode") == 0) lua_pushinteger(L, response->resultCode); - else if (mir_strcmpi(key, "Headers") == 0) { + /*else if (mir_strcmpi(key, "Headers") == 0) { NETLIBHTTPHEADERS *headers = (NETLIBHTTPHEADERS*)lua_newuserdata(L, sizeof(NETLIBHTTPHEADERS)); headers->headers = response->headers; headers->count = response->headersCount; luaL_setmetatable(L, MT_NETLIBHTTPHEADERS); - } + }*/ else if (mir_strcmpi(key, "Content") == 0) { NETLIBHTTPCONTENT *content = (NETLIBHTTPCONTENT*)lua_newuserdata(L, sizeof(NETLIBHTTPCONTENT)); - content->data = response->pData; - content->length = response->dataLength; + content->data = response->body.GetBuffer(); + content->length = response->body.GetLength(); luaL_setmetatable(L, MT_NETLIBHTTPCONTENT); } else @@ -246,7 +114,7 @@ static int response__index(lua_State *L) static int response__gc(lua_State *L) { - NETLIBHTTPREQUEST **response = (NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPRESPONSE); + MHttpResponse **response = (MHttpResponse **)luaL_checkudata(L, 1, MT_NETLIBHTTPRESPONSE); Netlib_FreeHttpRequest(*response); return 0; } @@ -267,7 +135,7 @@ struct HttpRequestParam int threadRef; int callbackRef; - NETLIBHTTPREQUEST *request; + MHttpRequest *request; }; static void __cdecl SendHttpRequestThread(HttpRequestParam *param) @@ -280,11 +148,11 @@ static void __cdecl SendHttpRequestThread(HttpRequestParam *param) luaL_unref(param->L, LUA_REGISTRYINDEX, param->callbackRef); luaL_unref(param->L, LUA_REGISTRYINDEX, param->threadRef); - Netlib_FreeHttpRequest(param->request); + delete param->request; delete param; } -static void SendRequestAsync(lua_State *L, int idx, NETLIBHTTPREQUEST *request) +static void SendRequestAsync(lua_State *L, int idx, MHttpRequest *request) { HttpRequestParam *param = new HttpRequestParam(); param->request = request; @@ -297,13 +165,13 @@ static void SendRequestAsync(lua_State *L, int idx, NETLIBHTTPREQUEST *request) /***********************************************/ -#define MT_NETLIBHTTPREQUEST "NETLIBHTTPREQUEST" +#define MT_NETLIBHTTPREQUEST "MHttpRequest" static const char *httpMethods[] = { "GET", "POST", "PUT", "DELETE", nullptr }; static int request_SetMethod(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); request->requestType = (1 << (luaL_checkoption(L, 2, nullptr, httpMethods))); lua_pushvalue(L, 1); return 1; @@ -311,7 +179,7 @@ static int request_SetMethod(lua_State *L) static int request_SetUrl(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); const char *url = luaL_checkstring(L, 2); SetUrl(request, url); @@ -322,14 +190,14 @@ static int request_SetUrl(lua_State *L) static int request_SetHeaders(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); luaL_checktype(L, 2, LUA_TTABLE); - ClearHeaders(request); + request->destroy(); for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { const char *name = lua_tostring(L, -2); const char *value = lua_tostring(L, -1); - SetHeader(request, name, value); + request->AddHeader(name, value); } lua_pushvalue(L, 1); @@ -338,34 +206,34 @@ static int request_SetHeaders(lua_State *L) static int request_SetContent(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); switch (lua_type(L, 2)) { case LUA_TNONE: case LUA_TNIL: - SetContent(request, nullptr, 0); - DropHeader(request, "Content-Type"); + request->SetData(nullptr, 0); + request->DeleteHeader("Content-Type"); break; case LUA_TSTRING: - { - const char *data = lua_tostring(L, 2); - SetContent(request, data, mir_strlen(data)); - SetHeader(request, "Content-Type", "text/plain"); + { + const char *data = lua_tostring(L, 2); + request->SetData(data, mir_strlen(data)); + request->AddHeader("Content-Type", "text/plain"); + } break; - } case LUA_TTABLE: - { - CMStringA formData; - for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { - const char *name = lua_tostring(L, -2); - const char *value = lua_tostring(L, -1); - formData.AppendFormat("&%s=%s", name, value); + { + CMStringA formData; + for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { + const char *name = lua_tostring(L, -2); + const char *value = lua_tostring(L, -1); + formData.AppendFormat("&%s=%s", name, value); + } + formData.Delete(0); + request->SetData(formData.GetString(), formData.GetLength()); + request->AddHeader("Content-Type", "application/x-www-form-urlencoded"); } - formData.Delete(0); - SetContent(request, formData.GetString(), formData.GetLength()); - SetHeader(request, "Content-Type", "application/x-www-form-urlencoded"); break; - } default: luaL_argerror(L, 2, luaL_typename(L, 2)); } @@ -376,14 +244,14 @@ static int request_SetContent(lua_State *L) static int request_SetContentType(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); if (!lua_isstring(L, 2)) { lua_pushvalue(L, 1); return 1; } const char *type = lua_tostring(L, 2); - SetHeader(request, "Content-Type", type); + request->AddHeader("Content-Type", type); lua_pushvalue(L, 1); return 1; @@ -391,7 +259,7 @@ static int request_SetContentType(lua_State *L) static int request_SetTimeout(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); request->timeout = luaL_optinteger(L, -1, 0); lua_pushvalue(L, 1); return 1; @@ -399,22 +267,16 @@ static int request_SetTimeout(lua_State *L) static int request_Send(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); - NETLIBHTTPREQUEST *newRequest = (NETLIBHTTPREQUEST*)mir_calloc(sizeof(NETLIBHTTPREQUEST)); + MHttpRequest *newRequest = new MHttpRequest(); newRequest->flags = request->flags; newRequest->requestType = request->requestType; - newRequest->szUrl = mir_strdup(request->szUrl); - newRequest->headersCount = request->headersCount; - newRequest->headers = (NETLIBHTTPHEADER*)mir_calloc(sizeof(NETLIBHTTPHEADER)*(request->headersCount + 1)); - for (int i = 0; i < request->headersCount; i++) { - newRequest->headers[i].szName = mir_strdup(request->headers[i].szName); - newRequest->headers[i].szValue = mir_strdup(request->headers[i].szValue); - } - newRequest->dataLength = request->dataLength; - newRequest->pData = (char*)mir_calloc(request->dataLength + 1); - memcpy(newRequest->pData, request->pData, request->dataLength); + newRequest->m_szUrl = request->m_szUrl; + newRequest->m_szParam = request->m_szParam; newRequest->timeout = request->timeout; + for (auto &it : *request) + newRequest->AddHeader(it->szName, it->szValue); if (lua_isfunction(L, 2)) { SendRequestAsync(L, 2, newRequest); @@ -422,8 +284,7 @@ static int request_Send(lua_State *L) } response_Create(L, newRequest); - Netlib_FreeHttpRequest(newRequest); - + delete newRequest; return 1; } @@ -454,8 +315,8 @@ static int request__index(lua_State *L) static int request__gc(lua_State *L) { - NETLIBHTTPREQUEST *request = *(NETLIBHTTPREQUEST**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); - Netlib_FreeHttpRequest(request); + MHttpRequest *request = *(MHttpRequest**)luaL_checkudata(L, 1, MT_NETLIBHTTPREQUEST); + delete request; return 0; } @@ -471,8 +332,8 @@ static const luaL_Reg requestApi[] = static int http_Request(lua_State *L) { - NETLIBHTTPREQUEST *request = CreateRequest(); - NETLIBHTTPREQUEST **udata = (NETLIBHTTPREQUEST**)lua_newuserdata(L, sizeof(NETLIBHTTPREQUEST*)); + MHttpRequest *request = CreateRequest(); + MHttpRequest **udata = (MHttpRequest**)lua_newuserdata(L, sizeof(MHttpRequest*)); *udata = request; request->requestType = (1 << (luaL_checkoption(L, 1, nullptr, httpMethods))); SetUrl(request, luaL_checkstring(L, 2)); @@ -483,7 +344,7 @@ static int http_Request(lua_State *L) static int http_Get(lua_State *L) { - NETLIBHTTPREQUEST *request = CreateRequest(); + MHttpRequest *request = CreateRequest(); request->requestType = REQUEST_GET; const char *url = luaL_checkstring(L, 1); @@ -495,14 +356,13 @@ static int http_Get(lua_State *L) } response_Create(L, request); - Netlib_FreeHttpRequest(request); - + delete request; return 1; } static int http_Post(lua_State *L) { - NETLIBHTTPREQUEST *request = CreateRequest(); + MHttpRequest *request = CreateRequest(); request->requestType = REQUEST_POST; const char *url = luaL_checkstring(L, 1); @@ -524,14 +384,13 @@ static int http_Post(lua_State *L) } response_Create(L, request); - Netlib_FreeHttpRequest(request); - + delete request; return 1; } static int http_Put(lua_State *L) { - NETLIBHTTPREQUEST *request = CreateRequest(); + MHttpRequest *request = CreateRequest(); request->requestType = REQUEST_PUT; const char *url = luaL_checkstring(L, 1); @@ -553,14 +412,13 @@ static int http_Put(lua_State *L) } response_Create(L, request); - Netlib_FreeHttpRequest(request); - + delete request; return 1; } static int http_Delete(lua_State *L) { - NETLIBHTTPREQUEST *request = CreateRequest(); + MHttpRequest *request = CreateRequest(); request->requestType = REQUEST_DELETE; const char *url = luaL_checkstring(L, 1); @@ -609,9 +467,9 @@ LUAMOD_API int luaopen_m_http(lua_State *L) luaL_setfuncs(L, responseApi, 0); lua_pop(L, 1); - luaL_newmetatable(L, MT_NETLIBHTTPHEADERS); - luaL_setfuncs(L, headersApi, 0); - lua_pop(L, 1); + // luaL_newmetatable(L, MT_NETLIBHTTPHEADERS); + // luaL_setfuncs(L, headersApi, 0); + // lua_pop(L, 1); luaL_newmetatable(L, MT_NETLIBHTTPCONTENT); luaL_setfuncs(L, contentApi, 0); diff --git a/plugins/PackUpdater/Src/Utils.cpp b/plugins/PackUpdater/Src/Utils.cpp index 1c24905cac..9e6a563606 100644 --- a/plugins/PackUpdater/Src/Utils.cpp +++ b/plugins/PackUpdater/Src/Utils.cpp @@ -107,38 +107,28 @@ void LoadOptions() BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) { - NETLIBHTTPREQUEST nlhr = { 0 }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_REDIRECT | NLHRF_DUMPASTEXT | NLHRF_HTTP11; - char* szUrl = mir_u2a(tszURL); - nlhr.szUrl = szUrl; - nlhr.headersCount = 4; - nlhr.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount); - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = NETLIB_USER_AGENT; - nlhr.headers[1].szName = "Connection"; - nlhr.headers[1].szValue = "close"; - nlhr.headers[2].szName = "Cache-Control"; - nlhr.headers[2].szValue = "no-cache"; - nlhr.headers[3].szName = "Pragma"; - nlhr.headers[3].szValue = "no-cache"; + nlhr.m_szUrl = _T2A(tszURL); + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Connection", "close"); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); bool ret = false; NLHR_PTR pReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); if (pReply) { - if (200 == pReply->resultCode && pReply->dataLength > 0) { + if (200 == pReply->resultCode && !pReply->body.IsEmpty()) { HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); DWORD dwBytes; - WriteFile(hFile, pReply->pData, (uint32_t)pReply->dataLength, &dwBytes, nullptr); + WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr); ret = true; if (hFile) CloseHandle(hFile); } } - mir_free(szUrl); - mir_free(nlhr.headers); - DlgDld = ret; return ret; } diff --git a/plugins/PasteIt/src/PasteToWeb.cpp b/plugins/PasteIt/src/PasteToWeb.cpp index 9d1ad7ac5f..5fdfc7afec 100644 --- a/plugins/PasteIt/src/PasteToWeb.cpp +++ b/plugins/PasteIt/src/PasteToWeb.cpp @@ -426,47 +426,19 @@ char *PasteToWeb::SendToWeb(char *url, std::map &heade WideCharToMultiByte(CP_UTF8, 0, content.c_str(), -1, contentBytes, cbLen, nullptr, nullptr); --cbLen; - int nHeaders = 0; - for (std::map::iterator it = headers.begin(); it != headers.end(); ++it) { - ++nHeaders; - } - - NETLIBHTTPHEADER *httpHeaders = new NETLIBHTTPHEADER[nHeaders]; - - NETLIBHTTPREQUEST nlhr = { 0 }; + MHttpRequest nlhr; nlhr.requestType = REQUEST_POST; nlhr.flags = NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT | NLHPIF_HTTP11; - nlhr.szUrl = url; - nlhr.headers = httpHeaders; - nlhr.pData = contentBytes; - nlhr.dataLength = cbLen; - nHeaders = 0; - std::list mallBuf; - for (std::map::iterator it = headers.begin(); it != headers.end(); ++it) { - char *b1 = new char[it->first.length() + 1]; - char *b2 = new char[it->second.length() + 1]; - mir_strncpy(b1, it->first.c_str(), it->first.length() + 1); - mir_strncpy(b2, it->second.c_str(), it->second.length() + 1); - httpHeaders[nHeaders].szName = b1; - httpHeaders[nHeaders].szValue = b2; - mallBuf.push_back(b1); - mallBuf.push_back(b2); - ++nHeaders; - } + nlhr.m_szUrl = url; + nlhr.SetData(contentBytes, cbLen); + for (auto it : headers) + nlhr.AddHeader(it.first.c_str(), it.second.c_str()); char *resCont = nullptr; - nlhr.headersCount = nHeaders; NLHR_PTR nlhrReply(Netlib_HttpTransaction(g_hNetlibUser, &nlhr)); - if (nlhrReply != nullptr) { - if (nlhrReply->resultCode == 200) { - resCont = nlhrReply->pData; - nlhrReply->pData = 0; - } - } - - delete[] httpHeaders; - for (std::list::iterator it = mallBuf.begin(); it != mallBuf.end(); ++it) - delete *it; + if (nlhrReply != nullptr) + if (nlhrReply->resultCode == 200) + resCont = nlhrReply->body.Detach(); mir_free(contentBytes); return resCont; diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 9a30007c66..4cc80eaec1 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -143,22 +143,17 @@ int DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc) #endif szUserAgent.Append(")"); - NETLIBHTTPHEADER headers[4] = { - { "User-Agent", szUserAgent.GetBuffer() }, - { "Connection", "close" }, - { "Cache-Control", "no-cache" }, - { "Pragma", "no-cache" } - }; - ptrA szUrl(mir_u2a(pFileURL->wszDownloadURL)); - NETLIBHTTPREQUEST nlhr = {}; + MHttpRequest nlhr; nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_PERSISTENT; nlhr.requestType = REQUEST_GET; nlhr.nlc = nlc; - nlhr.szUrl = szUrl; - nlhr.headersCount = _countof(headers); - nlhr.headers = headers; + nlhr.m_szUrl = szUrl; + nlhr.AddHeader("User-Agent", szUserAgent); + nlhr.AddHeader("Connection", "close"); + nlhr.AddHeader("Cache-Control", "no-cache"); + nlhr.AddHeader("Pragma", "no-cache"); for (int i = 0; i < MAX_RETRIES; i++) { Netlib_LogfW(g_hNetlibUser, L"Downloading file %s to %s (attempt %d)", pFileURL->wszDownloadURL, pFileURL->wszDiskPath, i + 1); @@ -170,14 +165,14 @@ int DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc) } nlc = pReply->nlc; - if (pReply->resultCode != 200 || pReply->dataLength <= 0) { + if (pReply->resultCode != 200 || pReply->body.IsEmpty()) { Netlib_LogfW(g_hNetlibUser, L"Downloading file %s failed with error %d", pFileURL->wszDownloadURL, pReply->resultCode); return pReply->resultCode; } // Check CRC sum if (pFileURL->CRCsum) { - int crc = crc32(0, (unsigned char *)pReply->pData, pReply->dataLength); + int crc = crc32(0, (unsigned char *)pReply->body.c_str(), pReply->body.GetLength()); if (crc != pFileURL->CRCsum) { // crc check failed, try again Netlib_LogfW(g_hNetlibUser, L"crc check failed for file %s", pFileURL->wszDiskPath); @@ -190,7 +185,7 @@ int DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc) HANDLE hFile = CreateFile(pFileURL->wszDiskPath, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile != INVALID_HANDLE_VALUE) { // write the downloaded file directly - WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr); + WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr); CloseHandle(hFile); } else { @@ -199,7 +194,7 @@ int DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc) mir_snwprintf(wszTempFile, L"%s\\pulocal.tmp", g_wszTempPath); hFile = CreateFile(wszTempFile, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile != INVALID_HANDLE_VALUE) { - WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr); + WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr); CloseHandle(hFile); PU::SafeMoveFile(wszTempFile, pFileURL->wszDiskPath); } diff --git a/plugins/SendScreenshotPlus/src/CSend.cpp b/plugins/SendScreenshotPlus/src/CSend.cpp index 9290e8d923..23f2c19c5c 100644 --- a/plugins/SendScreenshotPlus/src/CSend.cpp +++ b/plugins/SendScreenshotPlus/src/CSend.cpp @@ -422,36 +422,10 @@ const char* CSend::GetHTMLContent(char* str, const char* startTag, const char* e return begin; } -static void HTTPFormAppendData(NETLIBHTTPREQUEST* nlhr, size_t* dataMax, char** dataPos, const char* data, size_t len) -{ - nlhr->dataLength = (*dataPos - nlhr->pData); - if (nlhr->dataLength + len >= *dataMax) { - *dataPos = nlhr->pData; - *dataMax += 0x1000 + 0x1000 * (len >> 12); - nlhr->pData = (char*)mir_realloc(nlhr->pData, *dataMax); - if (!nlhr->pData) mir_free(*dataPos); - *dataPos = nlhr->pData; - if (!*dataPos) - return; - *dataPos += nlhr->dataLength; - } - if (data) { - memcpy(*dataPos, data, sizeof(char)*len); *dataPos += len; - nlhr->dataLength += (int)len; // not necessary - } -} - -void CSend::HTTPFormDestroy(NETLIBHTTPREQUEST* nlhr) -{ - mir_free(nlhr->headers[0].szValue), nlhr->headers[0].szValue = nullptr; - mir_free(nlhr->headers), nlhr->headers = nullptr; - mir_free(nlhr->pData), nlhr->pData = nullptr; -} - -int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum) +int CSend::HTTPFormCreate(MHttpRequest* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum) { char boundary[16]; - memcpy(boundary, "--M461C/", 8); + strcpy(boundary, "--M461C/"); { union { @@ -467,57 +441,40 @@ int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* } nlhr->requestType = requestType; nlhr->flags = NLHRF_HTTP11; - if (!strncmp(url, "https://", 8)) nlhr->flags |= NLHRF_SSL; - nlhr->szUrl = (char*)url; - nlhr->headersCount = 3; + if (!strncmp(url, "https://", 8)) + nlhr->flags |= NLHRF_SSL; + nlhr->m_szUrl = url; + nlhr->AddHeader("Content-Type", CMStringA("multipart/form-data; boundary=") + boundary); + nlhr->AddHeader("User-Agent", __USER_AGENT_STRING); + nlhr->AddHeader("Accept-Language", "en-us,en;q=0.8"); for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) { - if (!(iter->flags&HTTPFF_HEADER)) break; - ++nlhr->headersCount; - } - nlhr->headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*nlhr->headersCount); - { - char* contenttype = (char*)mir_alloc(sizeof(char)*(30 + 1 + sizeof(boundary))); - memcpy(contenttype, "multipart/form-data; boundary=", 30); - memcpy(contenttype + 30, boundary, sizeof(boundary)); - contenttype[30 + sizeof(boundary)] = '\0'; - nlhr->headers[0].szName = "Content-Type"; - nlhr->headers[0].szValue = contenttype; - nlhr->headers[1].szName = "User-Agent"; - nlhr->headers[1].szValue = __USER_AGENT_STRING; - nlhr->headers[2].szName = "Accept-Language"; - nlhr->headers[2].szValue = "en-us,en;q=0.8"; - int i = 3; - for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) { - if (!(iter->flags&HTTPFF_HEADER)) break; - nlhr->headers[i].szName = (char*)iter->name; - nlhr->headers[i++].szValue = (char*)iter->value_str; - } + if (!(iter->flags & HTTPFF_HEADER)) + break; + nlhr->AddHeader(iter->name, iter->value_str); } - char* dataPos = nlhr->pData; - size_t dataMax = 0; - for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) { - if (iter->flags&HTTPFF_HEADER) continue; - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, 2 + sizeof(boundary) + 40); - memset(dataPos, '-', 2); dataPos += 2; - memcpy(dataPos, boundary, sizeof(boundary)); dataPos += sizeof(boundary); - memcpy(dataPos, "\r\nContent-Disposition: form-data; name=\"", 40); dataPos += 40; - size_t namelen = mir_strlen(iter->name), valuelen = 0; - if (!(iter->flags&HTTPFF_INT)) - valuelen = mir_strlen(iter->value_str); - if (iter->flags&HTTPFF_FILE) { - const char* filename = strrchr(iter->value_str, '\\'); + + auto &str = nlhr->m_szParam; + for (HTTPFormData *iter = frm, *end = frm + frmNum; iter != end; ++iter) { + if (iter->flags & HTTPFF_HEADER) + continue; + + str.AppendFormat("--%s", boundary); + str.Append("\r\nContent-Disposition: form-data; name=\""); + + if (iter->flags & HTTPFF_FILE) { + const char *filename = strrchr(iter->value_str, '\\'); if (!filename) filename = strrchr(iter->value_str, '/'); if (!filename) filename = iter->value_str; else ++filename; - valuelen = mir_strlen(filename); - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 13 + valuelen + 17); - memcpy(dataPos, iter->name, namelen); dataPos += namelen; - memcpy(dataPos, "\"; filename=\"", 13); dataPos += 13; - memcpy(dataPos, filename, valuelen); dataPos += valuelen; - memcpy(dataPos, "\"\r\nContent-Type: ", 17); dataPos += 17; + + str.Append(iter->name); + str.Append("\"; filename=\""); + str.Append(filename); + str.Append("\"\r\nContent-Type: "); + /// add mime type - const char* mime = "application/octet-stream"; - const char* fileext = strrchr(filename, '.'); + const char *mime = "application/octet-stream"; + const char *fileext = strrchr(filename, '.'); if (fileext) { if (!mir_strcmp(fileext, ".jpg") || !mir_strcmp(fileext, ".jpeg") || !mir_strcmp(fileext, ".jpe")) mime = "image/jpeg"; @@ -530,71 +487,49 @@ int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* else if (!mir_strcmp(fileext, ".tif") || !mir_strcmp(fileext, ".tiff")) mime = "image/tiff"; } - HTTPFormAppendData(nlhr, &dataMax, &dataPos, mime, mir_strlen(mime)); - HTTPFormAppendData(nlhr, &dataMax, &dataPos, "\r\n\r\n", 4); + str.Append(mime); + str.Append("\r\n\r\n"); + /// add file content size_t filesize = 0; - FILE* fp = fopen(iter->value_str, "rb"); + FILE *fp = fopen(iter->value_str, "rb"); if (fp) { fseek(fp, 0, SEEK_END); filesize = ftell(fp); fseek(fp, 0, SEEK_SET); - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, filesize + 2); - if (fread(dataPos, 1, filesize, fp) != filesize) { + ptrA buf((char *)mir_alloc(filesize)); + if (fread(buf, 1, filesize, fp) != filesize) { + str.Append(buf, filesize); fclose(fp), fp = nullptr; } } if (!fp) { - HTTPFormDestroy(nlhr); Error(L"Error occurred when opening local file.\nAborting file upload..."); Exit(ACKRESULT_FAILED); return 1; } - else - fclose(fp); - dataPos += filesize; - memcpy(dataPos, "\r\n", 2); dataPos += 2; + fclose(fp); + str.Append("\r\n"); } - else if (iter->flags&HTTPFF_8BIT) { - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 38 + valuelen + 2); - memcpy(dataPos, iter->name, namelen); dataPos += namelen; - memcpy(dataPos, "\"\r\nContent-Transfer-Encoding: 8bit\r\n\r\n", 38); dataPos += 38; - memcpy(dataPos, iter->value_str, valuelen); dataPos += valuelen; - memcpy(dataPos, "\r\n", 2); dataPos += 2; + else if (iter->flags & HTTPFF_8BIT) { + str.Append(iter->name); + str.Append("\"\r\nContent-Transfer-Encoding: 8bit\r\n\r\n"); + str.Append(iter->value_str); + str.Append("\r\n"); } - else if (iter->flags&HTTPFF_INT) { - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 5 + 17/*max numbers*/ + 2); - memcpy(dataPos, iter->name, namelen); dataPos += namelen; - memcpy(dataPos, "\"\r\n\r\n", 5); dataPos += 5; - int ret = snprintf(dataPos, 17, "%Id", iter->value_int); - if (ret < 17 && ret>0) dataPos += ret; - memcpy(dataPos, "\r\n", 2); dataPos += 2; + else if (iter->flags & HTTPFF_INT) { + str.Append(iter->name); + str.Append("\"\r\n\r\n"); + str.AppendFormat("%Id", iter->value_int); + str.Append("\r\n"); } else { - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 5 + valuelen + 2); - memcpy(dataPos, iter->name, namelen); dataPos += namelen; - memcpy(dataPos, "\"\r\n\r\n", 5); dataPos += 5; - memcpy(dataPos, iter->value_str, valuelen); dataPos += valuelen; - memcpy(dataPos, "\r\n", 2); dataPos += 2; + str.Append(iter->name); + str.Append("\"\r\n\r\n"); + str.Append(iter->value_str); + str.Append("\r\n"); } } - HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, 2 + sizeof(boundary) + 4); - memset(dataPos, '-', 2); dataPos += 2; - memcpy(dataPos, boundary, sizeof(boundary)); dataPos += sizeof(boundary); - memcpy(dataPos, "--\r\n", 4); dataPos += 4; - nlhr->dataLength = dataPos - nlhr->pData; -#ifdef _DEBUG /// print request content to "_sendss_tmp" file for debugging - { - FILE* fp = fopen("_sendss_tmp", "wb"); - if (fp) { - fprintf(fp, "--Target-- %s\n", nlhr->szUrl); - for (int i = 0; i < nlhr->headersCount; ++i) { - fprintf(fp, "%s: %s\n", nlhr->headers[i].szName, nlhr->headers[i].szValue); - } - fprintf(fp, "\n\n"); - fwrite(nlhr->pData, 1, nlhr->dataLength, fp); - fclose(fp); - } - } -#endif // _DEBUG + + str.AppendFormat("--%s--\r\n", boundary); return 0; } diff --git a/plugins/SendScreenshotPlus/src/CSend.h b/plugins/SendScreenshotPlus/src/CSend.h index 9301811f17..c694167eae 100644 --- a/plugins/SendScreenshotPlus/src/CSend.h +++ b/plugins/SendScreenshotPlus/src/CSend.h @@ -130,8 +130,7 @@ protected: }; static const char* GetHTMLContent(char* str, const char* startTag, const char* endTag); // changes "str", can be successfully used only once - void HTTPFormDestroy(NETLIBHTTPREQUEST* nlhr); // use to free data inside "nlhr" created by HTTPFormCreate - int HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum); // returns "0" on success, Exit() will be called on failure (stop processing) + int HTTPFormCreate(MHttpRequest* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum); // returns "0" on success, Exit() will be called on failure (stop processing) }; #endif diff --git a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp index 5a2a4d8a34..6b604c33ba 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp @@ -76,12 +76,10 @@ void CSendHost_ImageShack::SendThread() { // send DATA and wait for m_nlreply NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &m_nlhr)); - HTTPFormDestroy(&m_nlhr); if (reply) { - if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->dataLength) { - reply->pData[reply->dataLength - 1] = '\0'; // make sure its null terminated + if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->body.GetLength()) { const char* url = nullptr; - url = GetHTMLContent(reply->pData, "", ""); + url = GetHTMLContent(reply->body.GetBuffer(), "", ""); if (url && *url) { m_URLthumb = m_URL = url; @@ -95,19 +93,19 @@ void CSendHost_ImageShack::SendThread() return; } - url = GetHTMLContent(reply->pData, ""); + url = GetHTMLContent(reply->body.GetBuffer(), ""); wchar_t* err = nullptr; if (url) err = mir_a2u(url); if (!err || !*err) { // fallback to server response mess mir_free(err); - err = mir_a2u(reply->pData); + err = mir_a2u(reply->body); } Error(L"%s", err); mir_free(err); } else Error(SS_ERR_RESPONSE, m_pszSendTyp, reply->resultCode); } - else Error(SS_ERR_NORESPONSE, m_pszSendTyp, m_nlhr.resultCode); + else Error(SS_ERR_NORESPONSE, m_pszSendTyp, 500); Exit(ACKRESULT_FAILED); } diff --git a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h index 7c58094aac..5197e8718a 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h @@ -40,7 +40,7 @@ public: int Send() override; protected: - NETLIBHTTPREQUEST m_nlhr; + MHttpRequest m_nlhr; void SendThread(); static void SendThreadWrapper(void * Obj); diff --git a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp index bde9f158bb..c4bb3897f7 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp @@ -15,8 +15,8 @@ */ #include "stdafx.h" -CSendHost_Imgur::CSendHost_Imgur(HWND Owner, MCONTACT hContact, bool bAsync) - : CSend(Owner, hContact, bAsync) +CSendHost_Imgur::CSendHost_Imgur(HWND Owner, MCONTACT hContact, bool bAsync) : + CSend(Owner, hContact, bAsync) { m_EnableItem = SS_DLG_DESCRIPTION | SS_DLG_AUTOSEND | SS_DLG_DELETEAFTERSSEND; m_pszSendTyp = LPGENW("Image upload"); @@ -60,10 +60,9 @@ void CSendHost_Imgur::SendThread(void* obj) CSendHost_Imgur *self = (CSendHost_Imgur*)obj; // send DATA and wait for m_nlreply NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr)); - self->HTTPFormDestroy(&self->m_nlhr); if (reply) { - if (reply->dataLength) { - JSONROOT root(reply->pData); + if (reply->body.GetLength()) { + JSONROOT root(reply->body); if (root) { if ((*root)["success"].as_bool()) { self->m_URL = (*root)["data"]["link"].as_mstring(); @@ -80,7 +79,7 @@ void CSendHost_Imgur::SendThread(void* obj) } else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode); } - else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode); + else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, 500); self->Exit(ACKRESULT_FAILED); } diff --git a/plugins/SendScreenshotPlus/src/CSendHost_imgur.h b/plugins/SendScreenshotPlus/src/CSendHost_imgur.h index 544afecd53..fee457d00a 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_imgur.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_imgur.h @@ -24,7 +24,7 @@ class CSendHost_Imgur : public CSend { int Send() override; protected: - NETLIBHTTPREQUEST m_nlhr; + MHttpRequest m_nlhr; static void SendThread(void* obj); }; #endif diff --git a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp index 58651ae85d..4fcf8f7d8b 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp @@ -65,11 +65,9 @@ void CSendHost_UploadPie::SendThread(void* obj) CSendHost_UploadPie* self = (CSendHost_UploadPie*)obj; // send DATA and wait for m_nlreply NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr)); - self->HTTPFormDestroy(&self->m_nlhr); if (reply) { - if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->dataLength) { - reply->pData[reply->dataLength - 1] = '\0'; // make sure its null terminated - char* url = reply->pData; + if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->body.GetLength()) { + char* url = reply->body.GetBuffer(); do { char* pos; if ((url = strstr(url, kHostURL))) { @@ -89,17 +87,17 @@ void CSendHost_UploadPie::SendThread(void* obj) self->svcSendMsgExit(url); return; } else { // check error mess from server - const char* err = GetHTMLContent(reply->pData, "

"); + const char* err = GetHTMLContent(reply->body.GetBuffer(), "

"); wchar_t* werr; if (err) werr = mir_a2u(err); - else werr = mir_a2u(reply->pData); + else werr = mir_a2u(reply->body); self->Error(L"%s", werr); mir_free(werr); } } else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode); } - else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode); + else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, 500); self->Exit(ACKRESULT_FAILED); } diff --git a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h index 94cfdf1fbc..8a4abd350f 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h @@ -24,7 +24,7 @@ class CSendHost_UploadPie : public CSend { protected: int m_expire; - NETLIBHTTPREQUEST m_nlhr; + MHttpRequest m_nlhr; static void SendThread(void* obj); }; #endif diff --git a/plugins/SmileyAdd/src/download.cpp b/plugins/SmileyAdd/src/download.cpp index ef0e4a0682..1aa2ac1fd4 100644 --- a/plugins/SmileyAdd/src/download.cpp +++ b/plugins/SmileyAdd/src/download.cpp @@ -42,22 +42,17 @@ static bool threadRunning; bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwnl) { int result = 0xBADBAD; - char *szRedirUrl = nullptr; - NETLIBHTTPREQUEST nlhr = {}; // initialize the netlib request + MHttpRequest nlhr; nlhr.requestType = REQUEST_GET; nlhr.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT; - nlhr.szUrl = (char*)szUrl; + nlhr.m_szUrl = szUrl; nlhr.nlc = hHttpDwnl; // change the header so the plugin is pretended to be IE 6 + WinXP - nlhr.headersCount = 2; - nlhr.headers = (NETLIBHTTPHEADER*)alloca(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount); - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = NETLIB_USER_AGENT; - nlhr.headers[1].szName = "Connection"; - nlhr.headers[1].szValue = "close"; + nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT); + nlhr.AddHeader("Connection", "close"); while (result == 0xBADBAD) { // download the page @@ -73,7 +68,7 @@ bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwn int res = -1; int fh = _open(szDest, _O_BINARY | _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE); if (fh != -1) { - res = _write(fh, nlhrReply->pData, nlhrReply->dataLength); + res = _write(fh, nlhrReply->body, nlhrReply->body.GetLength()); _close(fh); } if (res < 0) @@ -81,28 +76,6 @@ bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwn else result = 0; } - // if the recieved code is 302 Moved, Found, etc - // workaround for url forwarding - else if (nlhrReply->resultCode == 302 || nlhrReply->resultCode == 301 || nlhrReply->resultCode == 307) { // page moved - // get the url for the new location and save it to szInfo - // look for the reply header "Location" - if (auto *pszUrl = Netlib_GetHeader(nlhrReply, "Location")) { - size_t rlen = 0; - if (pszUrl[0] == '/') { - const char *szPref = strstr(szUrl, "://"); - szPref = szPref ? szPref + 3 : szUrl; - const char *szPath = strchr(szPref, '/'); - rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl); - } - - szRedirUrl = (char *)mir_realloc(szRedirUrl, rlen + mir_strlen(pszUrl) * 3 + 1); - - strncpy(szRedirUrl, szUrl, rlen); - mir_strcpy(szRedirUrl + rlen, pszUrl); - - nlhr.szUrl = szRedirUrl; - } - } else result = 1; } else { @@ -111,8 +84,6 @@ bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwn } } - mir_free(szRedirUrl); - return result == 0; } -- cgit v1.2.3