adding in use of username
This commit is contained in:
+166
-118
@@ -1,20 +1,21 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
|
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
#define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express
|
#define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in
|
||||||
|
// MSVC2013 Express
|
||||||
#endif
|
#endif
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#endif
|
#endif
|
||||||
#include <fcntl.h>
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <WS2tcpip.h>
|
#include <WS2tcpip.h>
|
||||||
|
#include <WinSock2.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#pragma comment(lib, "ws2_32")
|
#pragma comment(lib, "ws2_32")
|
||||||
|
#include <io.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <io.h>
|
|
||||||
#ifndef _SSIZE_T_DEFINED
|
#ifndef _SSIZE_T_DEFINED
|
||||||
typedef int ssize_t;
|
typedef int ssize_t;
|
||||||
#define _SSIZE_T_DEFINED
|
#define _SSIZE_T_DEFINED
|
||||||
@@ -45,6 +46,7 @@
|
|||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -52,7 +54,6 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdint.h>
|
|
||||||
#ifndef _SOCKET_T_DEFINED
|
#ifndef _SOCKET_T_DEFINED
|
||||||
typedef int socket_t;
|
typedef int socket_t;
|
||||||
#define _SOCKET_T_DEFINED
|
#define _SOCKET_T_DEFINED
|
||||||
@@ -70,13 +71,13 @@
|
|||||||
#define SOCKET_EWOULDBLOCK EWOULDBLOCK
|
#define SOCKET_EWOULDBLOCK EWOULDBLOCK
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "easywsclient.hpp"
|
#include "easywsclient.hpp"
|
||||||
|
|
||||||
using easywsclient::Callback_Imp;
|
|
||||||
using easywsclient::BytesCallback_Imp;
|
using easywsclient::BytesCallback_Imp;
|
||||||
|
using easywsclient::Callback_Imp;
|
||||||
|
|
||||||
namespace { // private module-only namespace
|
namespace { // private module-only namespace
|
||||||
|
|
||||||
@@ -91,15 +92,15 @@ socket_t hostname_connect(const std::string& hostname, int port) {
|
|||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = AF_UNSPEC;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
snprintf(sport, 16, "%d", port);
|
snprintf(sport, 16, "%d", port);
|
||||||
if ((ret = getaddrinfo(hostname.c_str(), sport, &hints, &result)) != 0)
|
if ((ret = getaddrinfo(hostname.c_str(), sport, &hints, &result)) != 0) {
|
||||||
{
|
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
for(p = result; p != NULL; p = p->ai_next)
|
for (p = result; p != NULL; p = p->ai_next) {
|
||||||
{
|
|
||||||
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||||
if (sockfd == INVALID_SOCKET) { continue; }
|
if (sockfd == INVALID_SOCKET) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) {
|
if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -110,9 +111,7 @@ socket_t hostname_connect(const std::string& hostname, int port) {
|
|||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _DummyWebSocket : public easywsclient::WebSocket {
|
||||||
class _DummyWebSocket : public easywsclient::WebSocket
|
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
void poll(int timeout) {}
|
void poll(int timeout) {}
|
||||||
void send(const std::string &message) {}
|
void send(const std::string &message) {}
|
||||||
@@ -125,9 +124,7 @@ class _DummyWebSocket : public easywsclient::WebSocket
|
|||||||
void _dispatchBinary(BytesCallback_Imp &callable) {}
|
void _dispatchBinary(BytesCallback_Imp &callable) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class _RealWebSocket : public easywsclient::WebSocket {
|
||||||
class _RealWebSocket : public easywsclient::WebSocket
|
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
// http://tools.ietf.org/html/rfc6455#section-5.2 Base Framing Protocol
|
// http://tools.ietf.org/html/rfc6455#section-5.2 Base Framing Protocol
|
||||||
//
|
//
|
||||||
@@ -176,15 +173,9 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
bool isRxBad;
|
bool isRxBad;
|
||||||
|
|
||||||
_RealWebSocket(socket_t sockfd, bool useMask)
|
_RealWebSocket(socket_t sockfd, bool useMask)
|
||||||
: sockfd(sockfd)
|
: sockfd(sockfd), readyState(OPEN), useMask(useMask), isRxBad(false) {}
|
||||||
, readyState(OPEN)
|
|
||||||
, useMask(useMask)
|
|
||||||
, isRxBad(false) {
|
|
||||||
}
|
|
||||||
|
|
||||||
readyStateValues getReadyState() const {
|
readyStateValues getReadyState() const { return readyState; }
|
||||||
return readyState;
|
|
||||||
}
|
|
||||||
|
|
||||||
void poll(int timeout) { // timeout in milliseconds
|
void poll(int timeout) { // timeout in milliseconds
|
||||||
if (readyState == CLOSED) {
|
if (readyState == CLOSED) {
|
||||||
@@ -201,7 +192,9 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
FD_ZERO(&wfds);
|
FD_ZERO(&wfds);
|
||||||
FD_SET(sockfd, &rfds);
|
FD_SET(sockfd, &rfds);
|
||||||
if (txbuf.size()) { FD_SET(sockfd, &wfds); }
|
if (txbuf.size()) {
|
||||||
|
FD_SET(sockfd, &wfds);
|
||||||
|
}
|
||||||
select(sockfd + 1, &rfds, &wfds, 0, timeout > 0 ? &tv : 0);
|
select(sockfd + 1, &rfds, &wfds, 0, timeout > 0 ? &tv : 0);
|
||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -210,35 +203,34 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
rxbuf.resize(N + 1500);
|
rxbuf.resize(N + 1500);
|
||||||
ret = recv(sockfd, (char *)&rxbuf[0] + N, 1500, 0);
|
ret = recv(sockfd, (char *)&rxbuf[0] + N, 1500, 0);
|
||||||
if (false) { }
|
if (false) {
|
||||||
else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) {
|
} else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK ||
|
||||||
|
socketerrno == SOCKET_EAGAIN_EINPROGRESS)) {
|
||||||
rxbuf.resize(N);
|
rxbuf.resize(N);
|
||||||
break;
|
break;
|
||||||
}
|
} else if (ret <= 0) {
|
||||||
else if (ret <= 0) {
|
|
||||||
rxbuf.resize(N);
|
rxbuf.resize(N);
|
||||||
closesocket(sockfd);
|
closesocket(sockfd);
|
||||||
readyState = CLOSED;
|
readyState = CLOSED;
|
||||||
fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr);
|
fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr);
|
||||||
break;
|
break;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
rxbuf.resize(N + ret);
|
rxbuf.resize(N + ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (txbuf.size()) {
|
while (txbuf.size()) {
|
||||||
int ret = ::send(sockfd, (char *)&txbuf[0], txbuf.size(), 0);
|
int ret = ::send(sockfd, (char *)&txbuf[0], txbuf.size(), 0);
|
||||||
if (false) { } // ??
|
if (false) {
|
||||||
else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) {
|
} // ??
|
||||||
|
else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK ||
|
||||||
|
socketerrno == SOCKET_EAGAIN_EINPROGRESS)) {
|
||||||
break;
|
break;
|
||||||
}
|
} else if (ret <= 0) {
|
||||||
else if (ret <= 0) {
|
|
||||||
closesocket(sockfd);
|
closesocket(sockfd);
|
||||||
readyState = CLOSED;
|
readyState = CLOSED;
|
||||||
fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr);
|
fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr);
|
||||||
break;
|
break;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
txbuf.erase(txbuf.begin(), txbuf.begin() + ret);
|
txbuf.erase(txbuf.begin(), txbuf.begin() + ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -275,26 +267,29 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
wsheader_type ws;
|
wsheader_type ws;
|
||||||
if (rxbuf.size() < 2) { return; /* Need at least 2 */ }
|
if (rxbuf.size() < 2) {
|
||||||
|
return; /* Need at least 2 */
|
||||||
|
}
|
||||||
const uint8_t *data = (uint8_t *)&rxbuf[0]; // peek, but don't consume
|
const uint8_t *data = (uint8_t *)&rxbuf[0]; // peek, but don't consume
|
||||||
ws.fin = (data[0] & 0x80) == 0x80;
|
ws.fin = (data[0] & 0x80) == 0x80;
|
||||||
ws.opcode = (wsheader_type::opcode_type)(data[0] & 0x0f);
|
ws.opcode = (wsheader_type::opcode_type)(data[0] & 0x0f);
|
||||||
ws.mask = (data[1] & 0x80) == 0x80;
|
ws.mask = (data[1] & 0x80) == 0x80;
|
||||||
ws.N0 = (data[1] & 0x7f);
|
ws.N0 = (data[1] & 0x7f);
|
||||||
ws.header_size = 2 + (ws.N0 == 126? 2 : 0) + (ws.N0 == 127? 8 : 0) + (ws.mask? 4 : 0);
|
ws.header_size = 2 + (ws.N0 == 126 ? 2 : 0) + (ws.N0 == 127 ? 8 : 0) +
|
||||||
if (rxbuf.size() < ws.header_size) { return; /* Need: ws.header_size - rxbuf.size() */ }
|
(ws.mask ? 4 : 0);
|
||||||
|
if (rxbuf.size() < ws.header_size) {
|
||||||
|
return; /* Need: ws.header_size - rxbuf.size() */
|
||||||
|
}
|
||||||
int i = 0;
|
int i = 0;
|
||||||
if (ws.N0 < 126) {
|
if (ws.N0 < 126) {
|
||||||
ws.N = ws.N0;
|
ws.N = ws.N0;
|
||||||
i = 2;
|
i = 2;
|
||||||
}
|
} else if (ws.N0 == 126) {
|
||||||
else if (ws.N0 == 126) {
|
|
||||||
ws.N = 0;
|
ws.N = 0;
|
||||||
ws.N |= ((uint64_t)data[2]) << 8;
|
ws.N |= ((uint64_t)data[2]) << 8;
|
||||||
ws.N |= ((uint64_t)data[3]) << 0;
|
ws.N |= ((uint64_t)data[3]) << 0;
|
||||||
i = 4;
|
i = 4;
|
||||||
}
|
} else if (ws.N0 == 127) {
|
||||||
else if (ws.N0 == 127) {
|
|
||||||
ws.N = 0;
|
ws.N = 0;
|
||||||
ws.N |= ((uint64_t)data[2]) << 56;
|
ws.N |= ((uint64_t)data[2]) << 56;
|
||||||
ws.N |= ((uint64_t)data[3]) << 48;
|
ws.N |= ((uint64_t)data[3]) << 48;
|
||||||
@@ -325,8 +320,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
ws.masking_key[1] = ((uint8_t)data[i + 1]) << 0;
|
ws.masking_key[1] = ((uint8_t)data[i + 1]) << 0;
|
||||||
ws.masking_key[2] = ((uint8_t)data[i + 2]) << 0;
|
ws.masking_key[2] = ((uint8_t)data[i + 2]) << 0;
|
||||||
ws.masking_key[3] = ((uint8_t)data[i + 3]) << 0;
|
ws.masking_key[3] = ((uint8_t)data[i + 3]) << 0;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ws.masking_key[0] = 0;
|
ws.masking_key[0] = 0;
|
||||||
ws.masking_key[1] = 0;
|
ws.masking_key[1] = 0;
|
||||||
ws.masking_key[2] = 0;
|
ws.masking_key[2] = 0;
|
||||||
@@ -335,31 +329,44 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
|
|
||||||
// Note: The checks above should hopefully ensure this addition
|
// Note: The checks above should hopefully ensure this addition
|
||||||
// cannot overflow:
|
// cannot overflow:
|
||||||
if (rxbuf.size() < ws.header_size+ws.N) { return; /* Need: ws.header_size+ws.N - rxbuf.size() */ }
|
if (rxbuf.size() < ws.header_size + ws.N) {
|
||||||
|
return; /* Need: ws.header_size+ws.N - rxbuf.size() */
|
||||||
|
}
|
||||||
|
|
||||||
// We got a whole message, now do something with it:
|
// We got a whole message, now do something with it:
|
||||||
if (false) { }
|
if (false) {
|
||||||
else if (
|
} else if (ws.opcode == wsheader_type::TEXT_FRAME ||
|
||||||
ws.opcode == wsheader_type::TEXT_FRAME
|
ws.opcode == wsheader_type::BINARY_FRAME ||
|
||||||
|| ws.opcode == wsheader_type::BINARY_FRAME
|
ws.opcode == wsheader_type::CONTINUATION) {
|
||||||
|| ws.opcode == wsheader_type::CONTINUATION
|
if (ws.mask) {
|
||||||
) {
|
for (size_t i = 0; i != ws.N; ++i) {
|
||||||
if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } }
|
rxbuf[i + ws.header_size] ^= ws.masking_key[i & 0x3];
|
||||||
receivedData.insert(receivedData.end(), rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N);// just feed
|
}
|
||||||
|
}
|
||||||
|
receivedData.insert(receivedData.end(), rxbuf.begin() + ws.header_size,
|
||||||
|
rxbuf.begin() + ws.header_size +
|
||||||
|
(size_t)ws.N); // just feed
|
||||||
if (ws.fin) {
|
if (ws.fin) {
|
||||||
callable((const std::vector<uint8_t>)receivedData);
|
callable((const std::vector<uint8_t>)receivedData);
|
||||||
receivedData.erase(receivedData.begin(), receivedData.end());
|
receivedData.erase(receivedData.begin(), receivedData.end());
|
||||||
std::vector<uint8_t>().swap(receivedData); // free memory
|
std::vector<uint8_t>().swap(receivedData); // free memory
|
||||||
}
|
}
|
||||||
|
} else if (ws.opcode == wsheader_type::PING) {
|
||||||
|
if (ws.mask) {
|
||||||
|
for (size_t i = 0; i != ws.N; ++i) {
|
||||||
|
rxbuf[i + ws.header_size] ^= ws.masking_key[i & 0x3];
|
||||||
}
|
}
|
||||||
else if (ws.opcode == wsheader_type::PING) {
|
}
|
||||||
if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } }
|
std::string data(rxbuf.begin() + ws.header_size,
|
||||||
std::string data(rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N);
|
rxbuf.begin() + ws.header_size + (size_t)ws.N);
|
||||||
sendData(wsheader_type::PONG, data.size(), data.begin(), data.end());
|
sendData(wsheader_type::PONG, data.size(), data.begin(), data.end());
|
||||||
|
} else if (ws.opcode == wsheader_type::PONG) {
|
||||||
|
} else if (ws.opcode == wsheader_type::CLOSE) {
|
||||||
|
close();
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n");
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
else if (ws.opcode == wsheader_type::PONG) { }
|
|
||||||
else if (ws.opcode == wsheader_type::CLOSE) { close(); }
|
|
||||||
else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); }
|
|
||||||
|
|
||||||
rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size + (size_t)ws.N);
|
rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size + (size_t)ws.N);
|
||||||
}
|
}
|
||||||
@@ -371,31 +378,39 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
}
|
}
|
||||||
|
|
||||||
void send(const std::string &message) {
|
void send(const std::string &message) {
|
||||||
sendData(wsheader_type::TEXT_FRAME, message.size(), message.begin(), message.end());
|
sendData(wsheader_type::TEXT_FRAME, message.size(), message.begin(),
|
||||||
|
message.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendBinary(const std::string &message) {
|
void sendBinary(const std::string &message) {
|
||||||
sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end());
|
sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(),
|
||||||
|
message.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendBinary(const std::vector<uint8_t> &message) {
|
void sendBinary(const std::vector<uint8_t> &message) {
|
||||||
sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end());
|
sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(),
|
||||||
|
message.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Iterator>
|
template <class Iterator>
|
||||||
void sendData(wsheader_type::opcode_type type, uint64_t message_size, Iterator message_begin, Iterator message_end) {
|
void sendData(wsheader_type::opcode_type type, uint64_t message_size,
|
||||||
|
Iterator message_begin, Iterator message_end) {
|
||||||
// TODO:
|
// TODO:
|
||||||
// Masking key should (must) be derived from a high quality random
|
// Masking key should (must) be derived from a high quality random
|
||||||
// number generator, to mitigate attacks on non-WebSocket friendly
|
// number generator, to mitigate attacks on non-WebSocket friendly
|
||||||
// middleware:
|
// middleware:
|
||||||
const uint8_t masking_key[4] = {0x12, 0x34, 0x56, 0x78};
|
const uint8_t masking_key[4] = {0x12, 0x34, 0x56, 0x78};
|
||||||
// TODO: consider acquiring a lock on txbuf...
|
// TODO: consider acquiring a lock on txbuf...
|
||||||
if (readyState == CLOSING || readyState == CLOSED) { return; }
|
if (readyState == CLOSING || readyState == CLOSED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
std::vector<uint8_t> header;
|
std::vector<uint8_t> header;
|
||||||
header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0);
|
header.assign(2 + (message_size >= 126 ? 2 : 0) +
|
||||||
|
(message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0),
|
||||||
|
0);
|
||||||
header[0] = 0x80 | type;
|
header[0] = 0x80 | type;
|
||||||
if (false) { }
|
if (false) {
|
||||||
else if (message_size < 126) {
|
} else if (message_size < 126) {
|
||||||
header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0);
|
header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0);
|
||||||
if (useMask) {
|
if (useMask) {
|
||||||
header[2] = masking_key[0];
|
header[2] = masking_key[0];
|
||||||
@@ -403,8 +418,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
header[4] = masking_key[2];
|
header[4] = masking_key[2];
|
||||||
header[5] = masking_key[3];
|
header[5] = masking_key[3];
|
||||||
}
|
}
|
||||||
}
|
} else if (message_size < 65536) {
|
||||||
else if (message_size < 65536) {
|
|
||||||
header[1] = 126 | (useMask ? 0x80 : 0);
|
header[1] = 126 | (useMask ? 0x80 : 0);
|
||||||
header[2] = (message_size >> 8) & 0xff;
|
header[2] = (message_size >> 8) & 0xff;
|
||||||
header[3] = (message_size >> 0) & 0xff;
|
header[3] = (message_size >> 0) & 0xff;
|
||||||
@@ -414,8 +428,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
header[6] = masking_key[2];
|
header[6] = masking_key[2];
|
||||||
header[7] = masking_key[3];
|
header[7] = masking_key[3];
|
||||||
}
|
}
|
||||||
}
|
} else { // TODO: run coverage testing here
|
||||||
else { // TODO: run coverage testing here
|
|
||||||
header[1] = 127 | (useMask ? 0x80 : 0);
|
header[1] = 127 | (useMask ? 0x80 : 0);
|
||||||
header[2] = (message_size >> 56) & 0xff;
|
header[2] = (message_size >> 56) & 0xff;
|
||||||
header[3] = (message_size >> 48) & 0xff;
|
header[3] = (message_size >> 48) & 0xff;
|
||||||
@@ -432,7 +445,8 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
header[13] = masking_key[3];
|
header[13] = masking_key[3];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// N.B. - txbuf will keep growing until it can be transmitted over the socket:
|
// N.B. - txbuf will keep growing until it can be transmitted over the
|
||||||
|
// socket:
|
||||||
txbuf.insert(txbuf.end(), header.begin(), header.end());
|
txbuf.insert(txbuf.end(), header.begin(), header.end());
|
||||||
txbuf.insert(txbuf.end(), message_begin, message_end);
|
txbuf.insert(txbuf.end(), message_begin, message_end);
|
||||||
if (useMask) {
|
if (useMask) {
|
||||||
@@ -444,17 +458,20 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
if(readyState == CLOSING || readyState == CLOSED) { return; }
|
if (readyState == CLOSING || readyState == CLOSED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
readyState = CLOSING;
|
readyState = CLOSING;
|
||||||
uint8_t closeFrame[6] = {0x88, 0x80, 0x00, 0x00, 0x00, 0x00}; // last 4 bytes are a masking key
|
uint8_t closeFrame[6] = {0x88, 0x80, 0x00, 0x00,
|
||||||
|
0x00, 0x00}; // last 4 bytes are a masking key
|
||||||
std::vector<uint8_t> header(closeFrame, closeFrame + 6);
|
std::vector<uint8_t> header(closeFrame, closeFrame + 6);
|
||||||
txbuf.insert(txbuf.end(), header.begin(), header.end());
|
txbuf.insert(txbuf.end(), header.begin(), header.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
easywsclient::WebSocket::pointer from_url(const std::string &url, bool useMask,
|
||||||
easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin) {
|
const std::string &origin,
|
||||||
|
const std::string &username) {
|
||||||
char host[512];
|
char host[512];
|
||||||
int port;
|
int port;
|
||||||
char path[512];
|
char path[512];
|
||||||
@@ -466,24 +483,21 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask,
|
|||||||
fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str());
|
fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (false) { }
|
if (false) {
|
||||||
else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) {
|
} else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) {
|
||||||
}
|
} else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) {
|
||||||
else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) {
|
|
||||||
port = 80;
|
port = 80;
|
||||||
}
|
} else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) {
|
||||||
else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) {
|
|
||||||
path[0] = '\0';
|
path[0] = '\0';
|
||||||
}
|
} else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) {
|
||||||
else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) {
|
|
||||||
port = 80;
|
port = 80;
|
||||||
path[0] = '\0';
|
path[0] = '\0';
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str());
|
fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
//fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n", host, port, path);
|
// fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n",
|
||||||
|
// host, port, path);
|
||||||
socket_t sockfd = hostname_connect(host, port);
|
socket_t sockfd = hostname_connect(host, port);
|
||||||
if (sockfd == INVALID_SOCKET) {
|
if (sockfd == INVALID_SOCKET) {
|
||||||
fprintf(stderr, "Unable to connect to %s:%d\n", host, port);
|
fprintf(stderr, "Unable to connect to %s:%d\n", host, port);
|
||||||
@@ -494,33 +508,68 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask,
|
|||||||
char line[1024];
|
char line[1024];
|
||||||
int status;
|
int status;
|
||||||
int i;
|
int i;
|
||||||
snprintf(line, 1024, "GET /%s HTTP/1.1\r\n", path); ::send(sockfd, line, strlen(line), 0);
|
snprintf(line, 1024, "GET /%s HTTP/1.1\r\n", path);
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
if (port == 80) {
|
if (port == 80) {
|
||||||
snprintf(line, 1024, "Host: %s\r\n", host); ::send(sockfd, line, strlen(line), 0);
|
snprintf(line, 1024, "Host: %s\r\n", host);
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
} else {
|
||||||
|
snprintf(line, 1024, "Host: %s:%d\r\n", host, port);
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
}
|
}
|
||||||
else {
|
snprintf(line, 1024, "Upgrade: websocket\r\n");
|
||||||
snprintf(line, 1024, "Host: %s:%d\r\n", host, port); ::send(sockfd, line, strlen(line), 0);
|
::send(sockfd, line, strlen(line), 0);
|
||||||
}
|
snprintf(line, 1024, "Connection: Upgrade\r\n");
|
||||||
snprintf(line, 1024, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0);
|
::send(sockfd, line, strlen(line), 0);
|
||||||
snprintf(line, 1024, "Connection: Upgrade\r\n"); ::send(sockfd, line, strlen(line), 0);
|
|
||||||
if (!origin.empty()) {
|
if (!origin.empty()) {
|
||||||
snprintf(line, 1024, "Origin: %s\r\n", origin.c_str()); ::send(sockfd, line, strlen(line), 0);
|
snprintf(line, 1024, "Origin: %s\r\n", origin.c_str());
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
}
|
||||||
|
if (!username.empty()) {
|
||||||
|
snprintf(line, 1024, "username: %s\r\n", username.c_str());
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
}
|
||||||
|
snprintf(line, 1024, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n");
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
snprintf(line, 1024, "Sec-WebSocket-Version: 13\r\n");
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
snprintf(line, 1024, "\r\n");
|
||||||
|
::send(sockfd, line, strlen(line), 0);
|
||||||
|
for (i = 0;
|
||||||
|
i < 2 || (i < 1023 && line[i - 2] != '\r' && line[i - 1] != '\n');
|
||||||
|
++i) {
|
||||||
|
if (recv(sockfd, line + i, 1, 0) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
snprintf(line, 1024, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0);
|
|
||||||
snprintf(line, 1024, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0);
|
|
||||||
snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0);
|
|
||||||
for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } }
|
|
||||||
line[i] = 0;
|
line[i] = 0;
|
||||||
if (i == 1023) { fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n", url.c_str()); return NULL; }
|
if (i == 1023) {
|
||||||
if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) { fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(), line); return NULL; }
|
fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n",
|
||||||
|
url.c_str());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) {
|
||||||
|
fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(),
|
||||||
|
line);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
// TODO: verify response headers,
|
// TODO: verify response headers,
|
||||||
while (true) {
|
while (true) {
|
||||||
for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } }
|
for (i = 0;
|
||||||
if (line[0] == '\r' && line[1] == '\n') { break; }
|
i < 2 || (i < 1023 && line[i - 2] != '\r' && line[i - 1] != '\n');
|
||||||
|
++i) {
|
||||||
|
if (recv(sockfd, line + i, 1, 0) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (line[0] == '\r' && line[1] == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int flag = 1;
|
int flag = 1;
|
||||||
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm
|
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
|
||||||
|
sizeof(flag)); // Disable Nagle's algorithm
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
u_long on = 1;
|
u_long on = 1;
|
||||||
ioctlsocket(sockfd, FIONBIO, &on);
|
ioctlsocket(sockfd, FIONBIO, &on);
|
||||||
@@ -531,9 +580,7 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask,
|
|||||||
return easywsclient::WebSocket::pointer(new _RealWebSocket(sockfd, useMask));
|
return easywsclient::WebSocket::pointer(new _RealWebSocket(sockfd, useMask));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end of module-only namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace easywsclient {
|
namespace easywsclient {
|
||||||
|
|
||||||
@@ -542,14 +589,15 @@ WebSocket::pointer WebSocket::create_dummy() {
|
|||||||
return dummy;
|
return dummy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebSocket::pointer WebSocket::from_url(const std::string &url,
|
||||||
WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) {
|
const std::string &origin,
|
||||||
return ::from_url(url, true, origin);
|
const std::string &username) {
|
||||||
|
return ::from_url(url, true, origin, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) {
|
WebSocket::pointer WebSocket::from_url_no_mask(const std::string &url,
|
||||||
return ::from_url(url, false, origin);
|
const std::string &origin) {
|
||||||
|
return ::from_url(url, false, origin, "Anonymous");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace easywsclient
|
} // namespace easywsclient
|
||||||
|
|||||||
+24
-8
@@ -13,18 +13,30 @@
|
|||||||
|
|
||||||
namespace easywsclient {
|
namespace easywsclient {
|
||||||
|
|
||||||
struct Callback_Imp { virtual void operator()(const std::string& message) = 0; };
|
struct Callback_Imp {
|
||||||
struct BytesCallback_Imp { virtual void operator()(const std::vector<uint8_t>& message) = 0; };
|
virtual void operator()(const std::string &message) = 0;
|
||||||
|
};
|
||||||
|
struct BytesCallback_Imp {
|
||||||
|
virtual void operator()(const std::vector<uint8_t> &message) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class WebSocket {
|
class WebSocket {
|
||||||
public:
|
public:
|
||||||
typedef WebSocket *pointer;
|
typedef WebSocket *pointer;
|
||||||
typedef enum readyStateValues { CLOSING, CLOSED, CONNECTING, OPEN } readyStateValues;
|
typedef enum readyStateValues {
|
||||||
|
CLOSING,
|
||||||
|
CLOSED,
|
||||||
|
CONNECTING,
|
||||||
|
OPEN
|
||||||
|
} readyStateValues;
|
||||||
|
|
||||||
// Factories:
|
// Factories:
|
||||||
static pointer create_dummy();
|
static pointer create_dummy();
|
||||||
static pointer from_url(const std::string& url, const std::string& origin = std::string());
|
static pointer from_url(const std::string &url,
|
||||||
static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string());
|
const std::string &origin = std::string(),
|
||||||
|
const std::string &username = "Anonymous");
|
||||||
|
static pointer from_url_no_mask(const std::string &url,
|
||||||
|
const std::string &origin = std::string());
|
||||||
|
|
||||||
// Interfaces:
|
// Interfaces:
|
||||||
virtual ~WebSocket() {}
|
virtual ~WebSocket() {}
|
||||||
@@ -39,7 +51,8 @@ class WebSocket {
|
|||||||
template <class Callable>
|
template <class Callable>
|
||||||
void dispatch(Callable callable)
|
void dispatch(Callable callable)
|
||||||
// For callbacks that accept a string argument.
|
// For callbacks that accept a string argument.
|
||||||
{ // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
|
{ // N.B. this is compatible with both C++11 lambdas, functors and C function
|
||||||
|
// pointers
|
||||||
struct _Callback : public Callback_Imp {
|
struct _Callback : public Callback_Imp {
|
||||||
Callable &callable;
|
Callable &callable;
|
||||||
_Callback(Callable &callable) : callable(callable) {}
|
_Callback(Callable &callable) : callable(callable) {}
|
||||||
@@ -52,11 +65,14 @@ class WebSocket {
|
|||||||
template <class Callable>
|
template <class Callable>
|
||||||
void dispatchBinary(Callable callable)
|
void dispatchBinary(Callable callable)
|
||||||
// For callbacks that accept a std::vector<uint8_t> argument.
|
// For callbacks that accept a std::vector<uint8_t> argument.
|
||||||
{ // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
|
{ // N.B. this is compatible with both C++11 lambdas, functors and C function
|
||||||
|
// pointers
|
||||||
struct _Callback : public BytesCallback_Imp {
|
struct _Callback : public BytesCallback_Imp {
|
||||||
Callable &callable;
|
Callable &callable;
|
||||||
_Callback(Callable &callable) : callable(callable) {}
|
_Callback(Callable &callable) : callable(callable) {}
|
||||||
void operator()(const std::vector<uint8_t>& message) { callable(message); }
|
void operator()(const std::vector<uint8_t> &message) {
|
||||||
|
callable(message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
_Callback callback(callable);
|
_Callback callback(callable);
|
||||||
_dispatchBinary(callback);
|
_dispatchBinary(callback);
|
||||||
|
|||||||
Reference in New Issue
Block a user