diff options
author | George Hazan <ghazan@miranda.im> | 2020-01-10 22:39:33 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-01-10 22:39:33 +0300 |
commit | dcb62e4830223c1f5233d90b855e74006fd0942b (patch) | |
tree | 3b9c5be79f43958d8b9b2f691f0f6801d2aa25d0 /src/mir_app | |
parent | cb462baced051c43598acf767058514a9aefd73c (diff) |
WebSocket_Connect to return the reply to a HTTP request instead of a connection
Diffstat (limited to 'src/mir_app')
-rw-r--r-- | src/mir_app/src/netlib_websocket.cpp | 73 |
1 files changed, 21 insertions, 52 deletions
diff --git a/src/mir_app/src/netlib_websocket.cpp b/src/mir_app/src/netlib_websocket.cpp index 72979bc5e0..8fe51ca62a 100644 --- a/src/mir_app/src/netlib_websocket.cpp +++ b/src/mir_app/src/netlib_websocket.cpp @@ -27,7 +27,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../../libs/zlib/src/zlib.h" -MIR_APP_DLL(HNETLIBCONN) WebSocket_Connect(HNETLIBUSER nlu, const char *szHost, NETLIBHTTPHEADER *pHeaders) +MIR_APP_DLL(NETLIBHTTPREQUEST*) WebSocket_Connect(HNETLIBUSER nlu, const char *szHost, NETLIBHTTPHEADER *pHeaders) { CMStringA tmpHost(szHost); @@ -35,69 +35,38 @@ MIR_APP_DLL(HNETLIBCONN) WebSocket_Connect(HNETLIBUSER nlu, const char *szHost, if (!mir_strncmp(tmpHost, "wss://", 6)) tmpHost.Delete(0, 6); - NETLIBOPENCONNECTION conn = {}; - conn.flags = NLOCF_V2 | NLOCF_SSL; - conn.timeout = 5; - - int pos = tmpHost.Find(':'); - if (pos != -1) { - conn.wPort = atoi(tmpHost.GetBuffer() + pos + 1); - tmpHost.Truncate(pos); - } - else conn.wPort = 443; - - CMStringA args; - if ((pos = tmpHost.Find('/')) != -1) { - args = tmpHost.Mid(pos); - tmpHost.Truncate(pos); - } - - conn.szHost = tmpHost; - - HNETLIBCONN res = Netlib_OpenConnection(nlu, &conn); - if (res == nullptr) { - Netlib_Logf(nlu, "WebSocket connection failed to connect to %s:%d, exiting", tmpHost.c_str(), conn.wPort); - return false; - } - - CMStringA szBuf; - szBuf.AppendFormat("GET https://%s%s HTTP/1.1\r\n", tmpHost.c_str(), args.c_str()); - szBuf.AppendFormat("Host: %s\r\n", tmpHost.c_str()); - szBuf.AppendFormat("Upgrade: websocket\r\n"); - szBuf.AppendFormat("Pragma: no-cache\r\n"); - szBuf.AppendFormat("Cache-Control: no-cache\r\n"); - szBuf.AppendFormat("Connection: Upgrade\r\n"); - szBuf.AppendFormat("Sec-WebSocket-Version: 13\r\n"); - szBuf.AppendFormat("Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"); + auto *nlr = new MHttpRequest; + nlr->flags = NLHRF_PERSISTENT | NLHRF_HTTP11 | NLHRF_SSL; + nlr->szUrl = tmpHost.GetBuffer(); + nlr->AddHeader("Upgrade", "websocket"); + nlr->AddHeader("Pragma", "no-cache"); + nlr->AddHeader("Cache-Control", "no-cache"); + nlr->AddHeader("Connection", "Upgrade"); + nlr->AddHeader("Sec-WebSocket-Version", "13"); + nlr->AddHeader("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"); + if (pHeaders) { while (pHeaders->szName != nullptr) { - szBuf.AppendFormat("%s: %s\r\n", pHeaders->szName, pHeaders->szValue); + nlr->AddHeader(pHeaders->szName, pHeaders->szValue); pHeaders++; } } - szBuf.AppendFormat("\r\n"); - if (Netlib_Send(res, szBuf, szBuf.GetLength(), MSG_DUMPASTEXT) == SOCKET_ERROR) { - Netlib_Logf(nlu, "Error establishing WebSocket connection to %s:%d, send failed", tmpHost.c_str(), conn.wPort); - Netlib_CloseHandle(res); - return nullptr; - } - char buf[1024]; - int bufSize = Netlib_Recv(res, buf, _countof(buf), MSG_DUMPASTEXT); - if (bufSize <= 0) { - Netlib_Logf(nlu, "Error establishing WebSocket connection to %s:%d, read failed", tmpHost.c_str(), conn.wPort); - Netlib_CloseHandle(res); + auto *pReply = Netlib_HttpTransaction(nlu, nlr); + delete nlr; + + if (pReply == nullptr) { + Netlib_Logf(nlu, "Error establishing WebSocket connection to %s, send failed", tmpHost.c_str()); return nullptr; } - int status = 0; - if (sscanf(buf, "HTTP/1.1 %d", &status) != 1 || status != 101) { - Netlib_Logf(nlu, "Error establishing WebSocket connection to %s:%d, status %d", tmpHost.c_str(), conn.wPort, status); - Netlib_CloseHandle(res); + if (pReply->resultCode != 101) { + Netlib_Logf(nlu, "Error establishing WebSocket connection to %s, status %d", tmpHost.c_str(), pReply->resultCode); + Netlib_FreeHttpRequest(pReply); return nullptr; } - return res; + return pReply; } MIR_APP_DLL(bool) WebSocket_InitHeader(WSHeader &hdr, const void *pData, size_t bufSize) |