Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
09aac56ab4 | |||
26897b2425 | |||
e3c98a03cc | |||
97fedf9482 | |||
ae187c0e98 | |||
0f21a20fe3 | |||
54db6ec8bb | |||
0e0a748037 | |||
3b19b0eeca | |||
dbfe3104e8 | |||
68fd8c20d6 | |||
d932af8568 | |||
3add6d4c2e | |||
0d7fb05567 | |||
bf1747ef18 | |||
5c9c05caff | |||
2573ca151b | |||
c5b5fa82be | |||
80dff08304 | |||
24c2eae3d7 |
2
.github/workflows/mkdocs.yml
vendored
2
.github/workflows/mkdocs.yml
vendored
@ -21,5 +21,7 @@ jobs:
|
|||||||
pip install pygments
|
pip install pygments
|
||||||
- name: Build doc
|
- name: Build doc
|
||||||
run: |
|
run: |
|
||||||
|
git clean -dfx .
|
||||||
|
git fetch
|
||||||
git pull
|
git pull
|
||||||
mkdocs gh-deploy
|
mkdocs gh-deploy
|
||||||
|
3
.github/workflows/unittest_windows_gcc.yml
vendored
3
.github/workflows/unittest_windows_gcc.yml
vendored
@ -20,7 +20,8 @@ jobs:
|
|||||||
ninja
|
ninja
|
||||||
- run: |
|
- run: |
|
||||||
cd build
|
cd build
|
||||||
ninja test
|
ctest -V
|
||||||
|
# ninja test
|
||||||
|
|
||||||
#- run: ../build/test/ixwebsocket_unittest.exe
|
#- run: ../build/test/ixwebsocket_unittest.exe
|
||||||
# working-directory: test
|
# working-directory: test
|
||||||
|
@ -88,6 +88,7 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXSocketTLSOptions.h
|
ixwebsocket/IXSocketTLSOptions.h
|
||||||
ixwebsocket/IXStrCaseCompare.h
|
ixwebsocket/IXStrCaseCompare.h
|
||||||
ixwebsocket/IXUdpSocket.h
|
ixwebsocket/IXUdpSocket.h
|
||||||
|
ixwebsocket/IXUniquePtr.h
|
||||||
ixwebsocket/IXUrlParser.h
|
ixwebsocket/IXUrlParser.h
|
||||||
ixwebsocket/IXUuid.h
|
ixwebsocket/IXUuid.h
|
||||||
ixwebsocket/IXUtf8Validator.h
|
ixwebsocket/IXUtf8Validator.h
|
||||||
|
11
README.md
11
README.md
@ -24,6 +24,7 @@ A bad security bug affecting users compiling with SSL enabled and OpenSSL as the
|
|||||||
|
|
||||||
#include <ixwebsocket/IXNetSystem.h>
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include <ixwebsocket/IXUserAgent.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -34,6 +35,8 @@ int main()
|
|||||||
// Our websocket object
|
// Our websocket object
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
|
|
||||||
|
// Connect to a server with encryption
|
||||||
|
// See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration
|
||||||
std::string url("wss://echo.websocket.org");
|
std::string url("wss://echo.websocket.org");
|
||||||
webSocket.setUrl(url);
|
webSocket.setUrl(url);
|
||||||
|
|
||||||
@ -53,6 +56,12 @@ int main()
|
|||||||
std::cout << "Connection established" << std::endl;
|
std::cout << "Connection established" << std::endl;
|
||||||
std::cout << "> " << std::flush;
|
std::cout << "> " << std::flush;
|
||||||
}
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
||||||
|
{
|
||||||
|
// Maybe SSL is not configured properly
|
||||||
|
std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
|
||||||
|
std::cout << "> " << std::flush;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -123,6 +132,7 @@ To check the performance of a websocket library, you can look at the [autoroute]
|
|||||||
| Windows | Disabled | None | [![Build2][5]][0] |
|
| Windows | Disabled | None | [![Build2][5]][0] |
|
||||||
| UWP | Disabled | None | [![Build2][6]][0] |
|
| UWP | Disabled | None | [![Build2][6]][0] |
|
||||||
| Linux | OpenSSL | Address Sanitizer | [![Build2][7]][0] |
|
| Linux | OpenSSL | Address Sanitizer | [![Build2][7]][0] |
|
||||||
|
| Mingw | Disabled | None | [![Build2][8]][0] |
|
||||||
|
|
||||||
* ASAN fails on Linux because of a known problem, we need a
|
* ASAN fails on Linux because of a known problem, we need a
|
||||||
* Some tests are disabled on Windows/UWP because of a pathing problem
|
* Some tests are disabled on Windows/UWP because of a pathing problem
|
||||||
@ -136,4 +146,5 @@ To check the performance of a websocket library, you can look at the [autoroute]
|
|||||||
[5]: https://github.com/machinezone/IXWebSocket/workflows/windows/badge.svg
|
[5]: https://github.com/machinezone/IXWebSocket/workflows/windows/badge.svg
|
||||||
[6]: https://github.com/machinezone/IXWebSocket/workflows/uwp/badge.svg
|
[6]: https://github.com/machinezone/IXWebSocket/workflows/uwp/badge.svg
|
||||||
[7]: https://github.com/machinezone/IXWebSocket/workflows/linux_asan/badge.svg
|
[7]: https://github.com/machinezone/IXWebSocket/workflows/linux_asan/badge.svg
|
||||||
|
[8]: https://github.com/machinezone/IXWebSocket/workflows/unittest_windows_gcc/badge.svg
|
||||||
|
|
||||||
|
@ -2,6 +2,42 @@
|
|||||||
|
|
||||||
All changes to this project will be documented in this file.
|
All changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [11.2.8] - 2021-06-03
|
||||||
|
|
||||||
|
(websocket client + server) WebSocketMessage class tweak to fix unsafe patterns
|
||||||
|
|
||||||
|
## [11.2.7] - 2021-05-27
|
||||||
|
|
||||||
|
(websocket server) Handle and accept firefox browser special upgrade value (keep-alive, Upgrade)
|
||||||
|
|
||||||
|
## [11.2.6] - 2021-05-18
|
||||||
|
|
||||||
|
(Windows) move EINVAL (re)definition from IXSocket.h to IXNetSystem.h (fix #289)
|
||||||
|
|
||||||
|
## [11.2.5] - 2021-04-04
|
||||||
|
|
||||||
|
(http client) DEL is not an HTTP method name, but DELETE is
|
||||||
|
|
||||||
|
## [11.2.4] - 2021-03-25
|
||||||
|
|
||||||
|
(cmake) install IXUniquePtr.h
|
||||||
|
|
||||||
|
## [11.2.3] - 2021-03-24
|
||||||
|
|
||||||
|
(ssl + windows) missing include for CertOpenStore function
|
||||||
|
|
||||||
|
## [11.2.2] - 2021-03-23
|
||||||
|
|
||||||
|
(ixwebsocket) version bump
|
||||||
|
|
||||||
|
## [11.2.1] - 2021-03-23
|
||||||
|
|
||||||
|
(ixwebsocket) version bump
|
||||||
|
|
||||||
|
## [11.2.0] - 2021-03-23
|
||||||
|
|
||||||
|
(ixwebsocket) correct mingw support (gcc on windows)
|
||||||
|
|
||||||
## [11.1.4] - 2021-03-23
|
## [11.1.4] - 2021-03-23
|
||||||
|
|
||||||
(ixwebsocket) add getMinWaitBetweenReconnectionRetries
|
(ixwebsocket) add getMinWaitBetweenReconnectionRetries
|
||||||
|
@ -374,13 +374,10 @@ The webSocket reference is guaranteed to be always valid ; by design the callbac
|
|||||||
// Bound host name, max connections and listen backlog can also be passed in as parameters.
|
// Bound host name, max connections and listen backlog can also be passed in as parameters.
|
||||||
ix::WebSocketServer server(port);
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionState,
|
server.setOnClientMessageCallback([](std::shared_ptr<ix::ConnectionState> connectionState, ix::WebSocket & webSocket, const ix::WebSocketMessagePtr & msg) {
|
||||||
WebSocket& webSocket,
|
|
||||||
const WebSocketMessagePtr& msg)
|
|
||||||
{
|
|
||||||
// The ConnectionState object contains information about the connection,
|
// The ConnectionState object contains information about the connection,
|
||||||
// at this point only the client ip address and the port.
|
// at this point only the client ip address and the port.
|
||||||
std::cout << "Remote ip: " << connectionState->getRemoteIp();
|
std::cout << "Remote ip: " << connectionState->getRemoteIp() << std::endl;
|
||||||
|
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
@ -398,7 +395,7 @@ server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionSta
|
|||||||
std::cout << "Headers:" << std::endl;
|
std::cout << "Headers:" << std::endl;
|
||||||
for (auto it : msg->openInfo.headers)
|
for (auto it : msg->openInfo.headers)
|
||||||
{
|
{
|
||||||
std::cout << it.first << ": " << it.second << std::endl;
|
std::cout << "\t" << it.first << ": " << it.second << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
||||||
@ -407,9 +404,11 @@ server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionSta
|
|||||||
// All connected clients are available in an std::set. See the broadcast cpp example.
|
// All connected clients are available in an std::set. See the broadcast cpp example.
|
||||||
// Second parameter tells whether we are sending the message in binary or text mode.
|
// Second parameter tells whether we are sending the message in binary or text mode.
|
||||||
// Here we send it in the same mode as it was received.
|
// Here we send it in the same mode as it was received.
|
||||||
|
std::cout << "Received: " << msg->str << std::endl;
|
||||||
|
|
||||||
webSocket.send(msg->str, msg->binary);
|
webSocket.send(msg->str, msg->binary);
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
|
|
||||||
auto res = server.listen();
|
auto res = server.listen();
|
||||||
if (!res.first)
|
if (!res.first)
|
||||||
|
@ -137,7 +137,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
contentLength = std::stoi(headers["Content-Length"]);
|
contentLength = std::stoi(headers["Content-Length"]);
|
||||||
}
|
}
|
||||||
catch (std::exception)
|
catch (const std::exception&)
|
||||||
{
|
{
|
||||||
return std::make_tuple(
|
return std::make_tuple(
|
||||||
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
|
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
|
||||||
|
@ -20,10 +20,11 @@
|
|||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
||||||
const std::string HttpClient::kPost = "POST";
|
const std::string HttpClient::kPost = "POST";
|
||||||
const std::string HttpClient::kGet = "GET";
|
const std::string HttpClient::kGet = "GET";
|
||||||
const std::string HttpClient::kHead = "HEAD";
|
const std::string HttpClient::kHead = "HEAD";
|
||||||
const std::string HttpClient::kDel = "DEL";
|
const std::string HttpClient::kDelete = "DELETE";
|
||||||
const std::string HttpClient::kPut = "PUT";
|
const std::string HttpClient::kPut = "PUT";
|
||||||
const std::string HttpClient::kPatch = "PATCH";
|
const std::string HttpClient::kPatch = "PATCH";
|
||||||
|
|
||||||
@ -557,9 +558,9 @@ namespace ix
|
|||||||
return request(url, kHead, std::string(), args);
|
return request(url, kHead, std::string(), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::del(const std::string& url, HttpRequestArgsPtr args)
|
HttpResponsePtr HttpClient::Delete(const std::string& url, HttpRequestArgsPtr args)
|
||||||
{
|
{
|
||||||
return request(url, kDel, std::string(), args);
|
return request(url, kDelete, std::string(), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::request(const std::string& url,
|
HttpResponsePtr HttpClient::request(const std::string& url,
|
||||||
|
@ -30,7 +30,7 @@ namespace ix
|
|||||||
|
|
||||||
HttpResponsePtr get(const std::string& url, HttpRequestArgsPtr args);
|
HttpResponsePtr get(const std::string& url, HttpRequestArgsPtr args);
|
||||||
HttpResponsePtr head(const std::string& url, HttpRequestArgsPtr args);
|
HttpResponsePtr head(const std::string& url, HttpRequestArgsPtr args);
|
||||||
HttpResponsePtr del(const std::string& url, HttpRequestArgsPtr args);
|
HttpResponsePtr Delete(const std::string& url, HttpRequestArgsPtr args);
|
||||||
|
|
||||||
HttpResponsePtr post(const std::string& url,
|
HttpResponsePtr post(const std::string& url,
|
||||||
const HttpParameters& httpParameters,
|
const HttpParameters& httpParameters,
|
||||||
@ -94,7 +94,7 @@ namespace ix
|
|||||||
const static std::string kPost;
|
const static std::string kPost;
|
||||||
const static std::string kGet;
|
const static std::string kGet;
|
||||||
const static std::string kHead;
|
const static std::string kHead;
|
||||||
const static std::string kDel;
|
const static std::string kDelete;
|
||||||
const static std::string kPut;
|
const static std::string kPut;
|
||||||
const static std::string kPatch;
|
const static std::string kPatch;
|
||||||
|
|
||||||
|
@ -124,147 +124,158 @@ namespace ix
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ix
|
//
|
||||||
|
// mingw does not have inet_ntop, which were taken as is from the musl C library.
|
||||||
//
|
//
|
||||||
// mingw does not have inet_ntop and inet_pton, which were taken as is from the musl C library.
|
const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
|
||||||
//
|
{
|
||||||
#if defined(_WIN32) && defined(__GNUC__)
|
#if defined(_WIN32) && defined(__GNUC__)
|
||||||
const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
|
const unsigned char* a = (const unsigned char*) a0;
|
||||||
{
|
int i, j, max, best;
|
||||||
const unsigned char* a = (const unsigned char*) a0;
|
char buf[100];
|
||||||
int i, j, max, best;
|
|
||||||
char buf[100];
|
|
||||||
|
|
||||||
switch (af)
|
switch (af)
|
||||||
{
|
|
||||||
case AF_INET:
|
|
||||||
if (snprintf(s, l, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]) < l) return s;
|
|
||||||
break;
|
|
||||||
case AF_INET6:
|
|
||||||
if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12))
|
|
||||||
snprintf(buf,
|
|
||||||
sizeof buf,
|
|
||||||
"%x:%x:%x:%x:%x:%x:%x:%x",
|
|
||||||
256 * a[0] + a[1],
|
|
||||||
256 * a[2] + a[3],
|
|
||||||
256 * a[4] + a[5],
|
|
||||||
256 * a[6] + a[7],
|
|
||||||
256 * a[8] + a[9],
|
|
||||||
256 * a[10] + a[11],
|
|
||||||
256 * a[12] + a[13],
|
|
||||||
256 * a[14] + a[15]);
|
|
||||||
else
|
|
||||||
snprintf(buf,
|
|
||||||
sizeof buf,
|
|
||||||
"%x:%x:%x:%x:%x:%x:%d.%d.%d.%d",
|
|
||||||
256 * a[0] + a[1],
|
|
||||||
256 * a[2] + a[3],
|
|
||||||
256 * a[4] + a[5],
|
|
||||||
256 * a[6] + a[7],
|
|
||||||
256 * a[8] + a[9],
|
|
||||||
256 * a[10] + a[11],
|
|
||||||
a[12],
|
|
||||||
a[13],
|
|
||||||
a[14],
|
|
||||||
a[15]);
|
|
||||||
/* Replace longest /(^0|:)[:0]{2,}/ with "::" */
|
|
||||||
for (i = best = 0, max = 2; buf[i]; i++)
|
|
||||||
{
|
|
||||||
if (i && buf[i] != ':') continue;
|
|
||||||
j = strspn(buf + i, ":0");
|
|
||||||
if (j > max) best = i, max = j;
|
|
||||||
}
|
|
||||||
if (max > 3)
|
|
||||||
{
|
|
||||||
buf[best] = buf[best + 1] = ':';
|
|
||||||
memmove(buf + best + 2, buf + best + max, i - best - max + 1);
|
|
||||||
}
|
|
||||||
if (strlen(buf) < l)
|
|
||||||
{
|
|
||||||
strcpy(s, buf);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default: errno = EAFNOSUPPORT; return 0;
|
|
||||||
}
|
|
||||||
errno = ENOSPC;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int hexval(unsigned c)
|
|
||||||
{
|
|
||||||
if (c - '0' < 10) return c - '0';
|
|
||||||
c |= 32;
|
|
||||||
if (c - 'a' < 6) return c - 'a' + 10;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int inet_pton(int af, const char* s, void* a0)
|
|
||||||
{
|
|
||||||
uint16_t ip[8];
|
|
||||||
unsigned char* a = (unsigned char*) a0;
|
|
||||||
int i, j, v, d, brk = -1, need_v4 = 0;
|
|
||||||
|
|
||||||
if (af == AF_INET)
|
|
||||||
{
|
|
||||||
for (i = 0; i < 4; i++)
|
|
||||||
{
|
{
|
||||||
for (v = j = 0; j < 3 && isdigit(s[j]); j++)
|
case AF_INET:
|
||||||
v = 10 * v + s[j] - '0';
|
if (snprintf(s, l, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]) < l) return s;
|
||||||
if (j == 0 || (j > 1 && s[0] == '0') || v > 255) return 0;
|
break;
|
||||||
a[i] = v;
|
case AF_INET6:
|
||||||
if (s[j] == 0 && i == 3) return 1;
|
if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12))
|
||||||
if (s[j] != '.') return 0;
|
snprintf(buf,
|
||||||
s += j + 1;
|
sizeof buf,
|
||||||
|
"%x:%x:%x:%x:%x:%x:%x:%x",
|
||||||
|
256 * a[0] + a[1],
|
||||||
|
256 * a[2] + a[3],
|
||||||
|
256 * a[4] + a[5],
|
||||||
|
256 * a[6] + a[7],
|
||||||
|
256 * a[8] + a[9],
|
||||||
|
256 * a[10] + a[11],
|
||||||
|
256 * a[12] + a[13],
|
||||||
|
256 * a[14] + a[15]);
|
||||||
|
else
|
||||||
|
snprintf(buf,
|
||||||
|
sizeof buf,
|
||||||
|
"%x:%x:%x:%x:%x:%x:%d.%d.%d.%d",
|
||||||
|
256 * a[0] + a[1],
|
||||||
|
256 * a[2] + a[3],
|
||||||
|
256 * a[4] + a[5],
|
||||||
|
256 * a[6] + a[7],
|
||||||
|
256 * a[8] + a[9],
|
||||||
|
256 * a[10] + a[11],
|
||||||
|
a[12],
|
||||||
|
a[13],
|
||||||
|
a[14],
|
||||||
|
a[15]);
|
||||||
|
/* Replace longest /(^0|:)[:0]{2,}/ with "::" */
|
||||||
|
for (i = best = 0, max = 2; buf[i]; i++)
|
||||||
|
{
|
||||||
|
if (i && buf[i] != ':') continue;
|
||||||
|
j = strspn(buf + i, ":0");
|
||||||
|
if (j > max) best = i, max = j;
|
||||||
|
}
|
||||||
|
if (max > 3)
|
||||||
|
{
|
||||||
|
buf[best] = buf[best + 1] = ':';
|
||||||
|
memmove(buf + best + 2, buf + best + max, i - best - max + 1);
|
||||||
|
}
|
||||||
|
if (strlen(buf) < l)
|
||||||
|
{
|
||||||
|
strcpy(s, buf);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: errno = EAFNOSUPPORT; return 0;
|
||||||
}
|
}
|
||||||
|
errno = ENOSPC;
|
||||||
return 0;
|
return 0;
|
||||||
|
#else
|
||||||
|
return ::inet_ntop(af, a0, s, l);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else if (af != AF_INET6)
|
|
||||||
|
#if defined(_WIN32) && defined(__GNUC__)
|
||||||
|
static int hexval(unsigned c)
|
||||||
{
|
{
|
||||||
errno = EAFNOSUPPORT;
|
if (c - '0' < 10) return c - '0';
|
||||||
|
c |= 32;
|
||||||
|
if (c - 'a' < 6) return c - 'a' + 10;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (*s == ':' && *++s != ':') return 0;
|
//
|
||||||
|
// mingw does not have inet_pton, which were taken as is from the musl C library.
|
||||||
for (i = 0;; i++)
|
//
|
||||||
|
int inet_pton(int af, const char* s, void* a0)
|
||||||
{
|
{
|
||||||
if (s[0] == ':' && brk < 0)
|
#if defined(_WIN32) && defined(__GNUC__)
|
||||||
|
uint16_t ip[8];
|
||||||
|
unsigned char* a = (unsigned char*) a0;
|
||||||
|
int i, j, v, d, brk = -1, need_v4 = 0;
|
||||||
|
|
||||||
|
if (af == AF_INET)
|
||||||
{
|
{
|
||||||
brk = i;
|
for (i = 0; i < 4; i++)
|
||||||
ip[i & 7] = 0;
|
{
|
||||||
if (!*++s) break;
|
for (v = j = 0; j < 3 && isdigit(s[j]); j++)
|
||||||
|
v = 10 * v + s[j] - '0';
|
||||||
|
if (j == 0 || (j > 1 && s[0] == '0') || v > 255) return 0;
|
||||||
|
a[i] = v;
|
||||||
|
if (s[j] == 0 && i == 3) return 1;
|
||||||
|
if (s[j] != '.') return 0;
|
||||||
|
s += j + 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if (af != AF_INET6)
|
||||||
|
{
|
||||||
|
errno = EAFNOSUPPORT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*s == ':' && *++s != ':') return 0;
|
||||||
|
|
||||||
|
for (i = 0;; i++)
|
||||||
|
{
|
||||||
|
if (s[0] == ':' && brk < 0)
|
||||||
|
{
|
||||||
|
brk = i;
|
||||||
|
ip[i & 7] = 0;
|
||||||
|
if (!*++s) break;
|
||||||
|
if (i == 7) return 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (v = j = 0; j < 4 && (d = hexval(s[j])) >= 0; j++)
|
||||||
|
v = 16 * v + d;
|
||||||
|
if (j == 0) return 0;
|
||||||
|
ip[i & 7] = v;
|
||||||
|
if (!s[j] && (brk >= 0 || i == 7)) break;
|
||||||
if (i == 7) return 0;
|
if (i == 7) return 0;
|
||||||
continue;
|
if (s[j] != ':')
|
||||||
|
{
|
||||||
|
if (s[j] != '.' || (i < 6 && brk < 0)) return 0;
|
||||||
|
need_v4 = 1;
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
s += j + 1;
|
||||||
}
|
}
|
||||||
for (v = j = 0; j < 4 && (d = hexval(s[j])) >= 0; j++)
|
if (brk >= 0)
|
||||||
v = 16 * v + d;
|
|
||||||
if (j == 0) return 0;
|
|
||||||
ip[i & 7] = v;
|
|
||||||
if (!s[j] && (brk >= 0 || i == 7)) break;
|
|
||||||
if (i == 7) return 0;
|
|
||||||
if (s[j] != ':')
|
|
||||||
{
|
{
|
||||||
if (s[j] != '.' || (i < 6 && brk < 0)) return 0;
|
memmove(ip + brk + 7 - i, ip + brk, 2 * (i + 1 - brk));
|
||||||
need_v4 = 1;
|
for (j = 0; j < 7 - i; j++)
|
||||||
i++;
|
ip[brk + j] = 0;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
s += j + 1;
|
for (j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
|
*a++ = ip[j] >> 8;
|
||||||
|
*a++ = ip[j];
|
||||||
|
}
|
||||||
|
if (need_v4 && inet_pton(AF_INET, (const char*) s, a - 4) <= 0) return 0;
|
||||||
|
return 1;
|
||||||
|
#else
|
||||||
|
return ::inet_pton(af, s, a0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (brk >= 0)
|
|
||||||
{
|
} // namespace ix
|
||||||
memmove(ip + brk + 7 - i, ip + brk, 2 * (i + 1 - brk));
|
|
||||||
for (j = 0; j < 7 - i; j++)
|
|
||||||
ip[brk + j] = 0;
|
|
||||||
}
|
|
||||||
for (j = 0; j < 8; j++)
|
|
||||||
{
|
|
||||||
*a++ = ip[j] >> 8;
|
|
||||||
*a++ = ip[j];
|
|
||||||
}
|
|
||||||
if (need_v4 && inet_pton(AF_INET, (const char*) s, a - 4) <= 0) return 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#endif // defined(_WIN32) && defined(__GNUC__)
|
|
||||||
|
@ -18,6 +18,19 @@
|
|||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <ws2def.h>
|
#include <ws2def.h>
|
||||||
|
|
||||||
|
#undef EWOULDBLOCK
|
||||||
|
#undef EAGAIN
|
||||||
|
#undef EINPROGRESS
|
||||||
|
#undef EBADF
|
||||||
|
#undef EINVAL
|
||||||
|
|
||||||
|
// map to WSA error codes
|
||||||
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
|
#define EAGAIN WSATRY_AGAIN
|
||||||
|
#define EINPROGRESS WSAEINPROGRESS
|
||||||
|
#define EBADF WSAEBADF
|
||||||
|
#define EINVAL WSAEINVAL
|
||||||
|
|
||||||
// Define our own poll on Windows, as a wrapper on top of select
|
// Define our own poll on Windows, as a wrapper on top of select
|
||||||
typedef unsigned long int nfds_t;
|
typedef unsigned long int nfds_t;
|
||||||
|
|
||||||
@ -53,12 +66,6 @@ struct pollfd
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// mingw does not have those
|
|
||||||
#if defined(_WIN32) && defined(__GNUC__)
|
|
||||||
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
|
|
||||||
int inet_pton(int af, const char* src, void* dst);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -71,4 +78,7 @@ namespace ix
|
|||||||
bool uninitNetSystem();
|
bool uninitNetSystem();
|
||||||
|
|
||||||
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
|
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
|
||||||
|
|
||||||
|
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
|
||||||
|
int inet_pton(int af, const char* src, void* dst);
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -15,20 +15,6 @@
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <BaseTsd.h>
|
#include <BaseTsd.h>
|
||||||
typedef SSIZE_T ssize_t;
|
typedef SSIZE_T ssize_t;
|
||||||
|
|
||||||
#undef EWOULDBLOCK
|
|
||||||
#undef EAGAIN
|
|
||||||
#undef EINPROGRESS
|
|
||||||
#undef EBADF
|
|
||||||
#undef EINVAL
|
|
||||||
|
|
||||||
// map to WSA error codes
|
|
||||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
|
||||||
#define EAGAIN WSATRY_AGAIN
|
|
||||||
#define EINPROGRESS WSAEINPROGRESS
|
|
||||||
#define EBADF WSAEBADF
|
|
||||||
#define EINVAL WSAEINVAL
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
|
@ -16,6 +16,11 @@
|
|||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// For manipulating the certificate store
|
||||||
|
#include <wincrypt.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
SocketMbedTLS::SocketMbedTLS(const SocketTLSOptions& tlsOptions, int fd)
|
SocketMbedTLS::SocketMbedTLS(const SocketTLSOptions& tlsOptions, int fd)
|
||||||
|
@ -24,6 +24,11 @@
|
|||||||
#endif
|
#endif
|
||||||
#define socketerrno errno
|
#define socketerrno errno
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// For manipulating the certificate store
|
||||||
|
#include <wincrypt.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -104,7 +104,7 @@ namespace ix
|
|||||||
server.sin_family = _addressFamily;
|
server.sin_family = _addressFamily;
|
||||||
server.sin_port = htons(_port);
|
server.sin_port = htons(_port);
|
||||||
|
|
||||||
if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
|
if (ix::inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "SocketServer::listen() error calling inet_pton "
|
ss << "SocketServer::listen() error calling inet_pton "
|
||||||
@ -133,7 +133,7 @@ namespace ix
|
|||||||
server.sin6_family = _addressFamily;
|
server.sin6_family = _addressFamily;
|
||||||
server.sin6_port = htons(_port);
|
server.sin6_port = htons(_port);
|
||||||
|
|
||||||
if (inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
|
if (ix::inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "SocketServer::listen() error calling inet_pton "
|
ss << "SocketServer::listen() error calling inet_pton "
|
||||||
@ -338,7 +338,7 @@ namespace ix
|
|||||||
if (_addressFamily == AF_INET)
|
if (_addressFamily == AF_INET)
|
||||||
{
|
{
|
||||||
char remoteIp4[INET_ADDRSTRLEN];
|
char remoteIp4[INET_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
|
if (ix::inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
|
||||||
{
|
{
|
||||||
int err = Socket::getErrno();
|
int err = Socket::getErrno();
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -357,7 +357,8 @@ namespace ix
|
|||||||
else // AF_INET6
|
else // AF_INET6
|
||||||
{
|
{
|
||||||
char remoteIp6[INET6_ADDRSTRLEN];
|
char remoteIp6[INET6_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) == nullptr)
|
if (ix::inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) ==
|
||||||
|
nullptr)
|
||||||
{
|
{
|
||||||
int err = Socket::getErrno();
|
int err = Socket::getErrno();
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -14,7 +14,7 @@ namespace ix
|
|||||||
bool CaseInsensitiveLess::NocaseCompare::operator()(const unsigned char& c1,
|
bool CaseInsensitiveLess::NocaseCompare::operator()(const unsigned char& c1,
|
||||||
const unsigned char& c2) const
|
const unsigned char& c2) const
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#if defined(_WIN32) && !defined(__GNUC__)
|
||||||
return std::tolower(c1, std::locale()) < std::tolower(c2, std::locale());
|
return std::tolower(c1, std::locale()) < std::tolower(c2, std::locale());
|
||||||
#else
|
#else
|
||||||
return std::tolower(c1) < std::tolower(c2);
|
return std::tolower(c1) < std::tolower(c2);
|
||||||
|
@ -15,6 +15,12 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
const std::string emptyMsg;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr;
|
OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr;
|
||||||
@ -38,7 +44,7 @@ namespace ix
|
|||||||
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) {
|
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) {
|
||||||
_onMessageCallback(
|
_onMessageCallback(
|
||||||
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Close,
|
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Close,
|
||||||
"",
|
emptyMsg,
|
||||||
wireSize,
|
wireSize,
|
||||||
WebSocketErrorInfo(),
|
WebSocketErrorInfo(),
|
||||||
WebSocketOpenInfo(),
|
WebSocketOpenInfo(),
|
||||||
@ -217,7 +223,7 @@ namespace ix
|
|||||||
|
|
||||||
_onMessageCallback(ix::make_unique<WebSocketMessage>(
|
_onMessageCallback(ix::make_unique<WebSocketMessage>(
|
||||||
WebSocketMessageType::Open,
|
WebSocketMessageType::Open,
|
||||||
"",
|
emptyMsg,
|
||||||
0,
|
0,
|
||||||
WebSocketErrorInfo(),
|
WebSocketErrorInfo(),
|
||||||
WebSocketOpenInfo(status.uri, status.headers, status.protocol),
|
WebSocketOpenInfo(status.uri, status.headers, status.protocol),
|
||||||
@ -251,7 +257,7 @@ namespace ix
|
|||||||
|
|
||||||
_onMessageCallback(
|
_onMessageCallback(
|
||||||
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Open,
|
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Open,
|
||||||
"",
|
emptyMsg,
|
||||||
0,
|
0,
|
||||||
WebSocketErrorInfo(),
|
WebSocketErrorInfo(),
|
||||||
WebSocketOpenInfo(status.uri, status.headers),
|
WebSocketOpenInfo(status.uri, status.headers),
|
||||||
@ -338,7 +344,7 @@ namespace ix
|
|||||||
connectErr.http_status = status.http_status;
|
connectErr.http_status = status.http_status;
|
||||||
|
|
||||||
_onMessageCallback(ix::make_unique<WebSocketMessage>(WebSocketMessageType::Error,
|
_onMessageCallback(ix::make_unique<WebSocketMessage>(WebSocketMessageType::Error,
|
||||||
"",
|
emptyMsg,
|
||||||
0,
|
0,
|
||||||
connectErr,
|
connectErr,
|
||||||
WebSocketOpenInfo(),
|
WebSocketOpenInfo(),
|
||||||
|
@ -204,6 +204,9 @@ namespace ix
|
|||||||
// Check the value of the connection field
|
// Check the value of the connection field
|
||||||
// Some websocket servers (Go/Gorilla?) send lowercase values for the
|
// Some websocket servers (Go/Gorilla?) send lowercase values for the
|
||||||
// connection header, so do a case insensitive comparison
|
// connection header, so do a case insensitive comparison
|
||||||
|
//
|
||||||
|
// See https://github.com/apache/thrift/commit/7c4bdf9914fcba6c89e0f69ae48b9675578f084a
|
||||||
|
//
|
||||||
if (!insensitiveStringCompare(headers["connection"], "Upgrade"))
|
if (!insensitiveStringCompare(headers["connection"], "Upgrade"))
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -296,7 +299,8 @@ namespace ix
|
|||||||
return sendErrorResponse(400, "Missing Upgrade header");
|
return sendErrorResponse(400, "Missing Upgrade header");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!insensitiveStringCompare(headers["upgrade"], "WebSocket"))
|
if (!insensitiveStringCompare(headers["upgrade"], "WebSocket") &&
|
||||||
|
headers["Upgrade"] != "keep-alive, Upgrade") // special case for firefox
|
||||||
{
|
{
|
||||||
return sendErrorResponse(400,
|
return sendErrorResponse(400,
|
||||||
"Invalid Upgrade header, "
|
"Invalid Upgrade header, "
|
||||||
|
@ -42,6 +42,18 @@ namespace ix
|
|||||||
{
|
{
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deleted overload to prevent binding `str` to a temporary, which would cause
|
||||||
|
* undefined behavior since class members don't extend lifetime beyond the constructor call.
|
||||||
|
*/
|
||||||
|
WebSocketMessage(WebSocketMessageType t,
|
||||||
|
std::string&& s,
|
||||||
|
size_t w,
|
||||||
|
WebSocketErrorInfo e,
|
||||||
|
WebSocketOpenInfo o,
|
||||||
|
WebSocketCloseInfo c,
|
||||||
|
bool b = false) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
using WebSocketMessagePtr = std::unique_ptr<WebSocketMessage>;
|
using WebSocketMessagePtr = std::unique_ptr<WebSocketMessage>;
|
||||||
|
@ -191,7 +191,6 @@ namespace ix
|
|||||||
// Make sure the OS send buffer is flushed before moving on
|
// Make sure the OS send buffer is flushed before moving on
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
size_t bufferedAmount = client->bufferedAmount();
|
|
||||||
std::chrono::duration<double, std::milli> duration(500);
|
std::chrono::duration<double, std::milli> duration(500);
|
||||||
std::this_thread::sleep_for(duration);
|
std::this_thread::sleep_for(duration);
|
||||||
} while (client->bufferedAmount() != 0);
|
} while (client->bufferedAmount() != 0);
|
||||||
|
@ -6,4 +6,4 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define IX_WEBSOCKET_VERSION "11.1.4"
|
#define IX_WEBSOCKET_VERSION "11.2.8"
|
||||||
|
10
main.cpp
10
main.cpp
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <ixwebsocket/IXNetSystem.h>
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include <ixwebsocket/IXUserAgent.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -25,9 +26,12 @@ int main()
|
|||||||
// Our websocket object
|
// Our websocket object
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
|
|
||||||
|
// Connect to a server with encryption
|
||||||
|
// See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration
|
||||||
std::string url("wss://echo.websocket.org");
|
std::string url("wss://echo.websocket.org");
|
||||||
webSocket.setUrl(url);
|
webSocket.setUrl(url);
|
||||||
|
|
||||||
|
std::cout << ix::userAgent() << std::endl;
|
||||||
std::cout << "Connecting to " << url << "..." << std::endl;
|
std::cout << "Connecting to " << url << "..." << std::endl;
|
||||||
|
|
||||||
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
|
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
|
||||||
@ -44,6 +48,12 @@ int main()
|
|||||||
std::cout << "Connection established" << std::endl;
|
std::cout << "Connection established" << std::endl;
|
||||||
std::cout << "> " << std::flush;
|
std::cout << "> " << std::flush;
|
||||||
}
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
||||||
|
{
|
||||||
|
// Maybe SSL is not configured properly
|
||||||
|
std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
|
||||||
|
std::cout << "> " << std::flush;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ test_server:
|
|||||||
test:
|
test:
|
||||||
mkdir -p build && (cd build ; cmake -GNinja -DCMAKE_UNITY_BUILD=ON -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 ..)
|
mkdir -p build && (cd build ; cmake -GNinja -DCMAKE_UNITY_BUILD=ON -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 ..)
|
||||||
(cd build ; ninja)
|
(cd build ; ninja)
|
||||||
(cd build ; ninja test)
|
(cd build ; ninja -v test)
|
||||||
|
|
||||||
test_asan:
|
test_asan:
|
||||||
mkdir -p build && (cd build ; cmake -GNinja -DCMAKE_UNITY_BUILD=ON -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 .. -DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer")
|
mkdir -p build && (cd build ; cmake -GNinja -DCMAKE_UNITY_BUILD=ON -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 .. -DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer")
|
||||||
|
@ -906,7 +906,7 @@ namespace ix
|
|||||||
// code which display correct results
|
// code which display correct results
|
||||||
|
|
||||||
char str[INET_ADDRSTRLEN];
|
char str[INET_ADDRSTRLEN];
|
||||||
inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
|
ix::inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
|
||||||
|
|
||||||
spdlog::info("host: {} ip: {}", hostname, str);
|
spdlog::info("host: {} ip: {}", hostname, str);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user