adding in optional ability to add headers
This commit is contained in:
+22
-7
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
#include <map>
|
||||||
#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
|
||||||
@@ -119,7 +120,7 @@ class _DummyWebSocket : public easywsclient::WebSocket
|
|||||||
void sendBinary(const std::string& message) { }
|
void sendBinary(const std::string& message) { }
|
||||||
void sendBinary(const std::vector<uint8_t>& message) { }
|
void sendBinary(const std::vector<uint8_t>& message) { }
|
||||||
void sendPing() { }
|
void sendPing() { }
|
||||||
void close() { }
|
void close() { }
|
||||||
readyStateValues getReadyState() const { return CLOSED; }
|
readyStateValues getReadyState() const { return CLOSED; }
|
||||||
void _dispatch(Callback_Imp & callable) { }
|
void _dispatch(Callback_Imp & callable) { }
|
||||||
void _dispatchBinary(BytesCallback_Imp& callable) { }
|
void _dispatchBinary(BytesCallback_Imp& callable) { }
|
||||||
@@ -340,7 +341,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
// 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
|
||||||
) {
|
) {
|
||||||
@@ -454,7 +455,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin) {
|
easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin, const std::map<std::string, std::string>& extraHeaders) {
|
||||||
char host[512];
|
char host[512];
|
||||||
int port;
|
int port;
|
||||||
char path[512];
|
char path[512];
|
||||||
@@ -506,6 +507,12 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask,
|
|||||||
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 (!extraHeaders.empty()) {
|
||||||
|
for (auto header : extraHeaders) {
|
||||||
|
snprintf(line, 1024, "%s: %s\r\n", header.first.c_str(), header.second.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-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, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0);
|
||||||
snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0);
|
snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0);
|
||||||
@@ -543,12 +550,20 @@ WebSocket::pointer WebSocket::create_dummy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) {
|
WebSocket::pointer WebSocket::from_url(
|
||||||
return ::from_url(url, true, origin);
|
const std::string& url,
|
||||||
|
const std::string& origin,
|
||||||
|
const std::map<std::string, std::string>& extraHeaders
|
||||||
|
) {
|
||||||
|
return ::from_url(url, true, origin, extraHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) {
|
WebSocket::pointer WebSocket::from_url_no_mask(
|
||||||
return ::from_url(url, false, origin);
|
const std::string& url,
|
||||||
|
const std::string& origin,
|
||||||
|
const std::map<std::string, std::string>& extraHeaders
|
||||||
|
) {
|
||||||
|
return ::from_url(url, false, origin, extraHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+11
-2
@@ -8,6 +8,7 @@
|
|||||||
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp
|
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp
|
||||||
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp
|
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -23,8 +24,16 @@ class WebSocket {
|
|||||||
|
|
||||||
// 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(
|
||||||
static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string());
|
const std::string& url,
|
||||||
|
const std::string& origin = std::string(),
|
||||||
|
const std::map<std::string, std::string>& extraHeaders = std::map<std::string, std::string>()
|
||||||
|
);
|
||||||
|
static pointer from_url_no_mask(
|
||||||
|
const std::string& url,
|
||||||
|
const std::string& origin = std::string(),
|
||||||
|
const std::map<std::string, std::string>& extraHeaders = std::map<std::string, std::string>()
|
||||||
|
);
|
||||||
|
|
||||||
// Interfaces:
|
// Interfaces:
|
||||||
virtual ~WebSocket() { }
|
virtual ~WebSocket() { }
|
||||||
|
|||||||
Reference in New Issue
Block a user