Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions easywsclient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <map>
#ifdef _WIN32
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express
Expand Down Expand Up @@ -119,7 +120,7 @@ class _DummyWebSocket : public easywsclient::WebSocket
void sendBinary(const std::string& message) { }
void sendBinary(const std::vector<uint8_t>& message) { }
void sendPing() { }
void close() { }
void close() { }
readyStateValues getReadyState() const { return CLOSED; }
void _dispatch(Callback_Imp & callable) { }
void _dispatchBinary(BytesCallback_Imp& callable) { }
Expand Down Expand Up @@ -340,7 +341,7 @@ class _RealWebSocket : public easywsclient::WebSocket
// We got a whole message, now do something with it:
if (false) { }
else if (
ws.opcode == wsheader_type::TEXT_FRAME
ws.opcode == wsheader_type::TEXT_FRAME
|| ws.opcode == wsheader_type::BINARY_FRAME
|| ws.opcode == wsheader_type::CONTINUATION
) {
Expand Down Expand Up @@ -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];
int port;
char path[512];
Expand Down Expand Up @@ -506,6 +507,12 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask,
if (!origin.empty()) {
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-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0);
snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0);
Expand Down Expand Up @@ -543,12 +550,20 @@ WebSocket::pointer WebSocket::create_dummy() {
}


WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) {
return ::from_url(url, true, origin);
WebSocket::pointer WebSocket::from_url(
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) {
return ::from_url(url, false, origin);
WebSocket::pointer WebSocket::from_url_no_mask(
const std::string& url,
const std::string& origin,
const std::map<std::string, std::string>& extraHeaders
) {
return ::from_url(url, false, origin, extraHeaders);
}


Expand Down
13 changes: 11 additions & 2 deletions easywsclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp

#include <map>
#include <string>
#include <vector>

Expand All @@ -23,8 +24,16 @@ class WebSocket {

// Factories:
static pointer create_dummy();
static pointer from_url(const std::string& url, const std::string& origin = std::string());
static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string());
static pointer from_url(
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:
virtual ~WebSocket() { }
Expand Down
6 changes: 5 additions & 1 deletion example-client-cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ int main()
}
#endif

std::unique_ptr<WebSocket> ws(WebSocket::from_url("ws://localhost:8126/foo"));
std::map<std::string, std::string> headers;
headers.insert(std::make_pair("Authorization", "Bearer 123"));
std::string origin = "https://example.com";

std::unique_ptr<WebSocket> ws(WebSocket::from_url("ws://localhost:8126/foo", origin, headers));
assert(ws);
ws->send("goodbye");
ws->send("hello");
Expand Down
5 changes: 4 additions & 1 deletion example-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ int main()
return 1;
}
#endif
std::map<std::string, std::string> headers;
headers.insert(std::make_pair("Authorization", "Bearer 123"));
std::string origin = "https://example.com";

ws = WebSocket::from_url("ws://localhost:8126/foo");
ws = WebSocket::from_url("ws://localhost:8126/foo", origin, headers);
assert(ws);
ws->send("goodbye");
ws->send("hello");
Expand Down