adding in optional ability to add headers

This commit is contained in:
talksik
2024-01-30 10:58:11 -08:00
parent afc1d8cfc5
commit f7659c5764
2 changed files with 33 additions and 9 deletions
+20 -5
View File
@@ -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
@@ -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
View File
@@ -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() { }