From 3d76a2ece9aa689080389efdf5986d868823ac6c Mon Sep 17 00:00:00 2001 From: Donald Pillou Date: Mon, 30 Dec 2013 13:22:41 +0100 Subject: [PATCH] Added optional origin header field --- easywsclient.cpp | 17 ++++++++++++----- easywsclient.hpp | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/easywsclient.cpp b/easywsclient.cpp index b358721..f510af9 100644 --- a/easywsclient.cpp +++ b/easywsclient.cpp @@ -381,7 +381,7 @@ class _RealWebSocket : public easywsclient::WebSocket }; -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) { char host[128]; int port; char path[128]; @@ -389,6 +389,10 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask) fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str()); return NULL; } + if (origin.size() >= 200) { + fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str()); + return NULL; + } if (false) { } else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) { } @@ -426,6 +430,9 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask) } snprintf(line, 256, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0); snprintf(line, 256, "Connection: Upgrade\r\n"); ::send(sockfd, line, strlen(line), 0); + if (!origin.empty()) { + snprintf(line, 256, "Origin: %s\r\n", origin.c_str()); ::send(sockfd, line, strlen(line), 0); + } snprintf(line, 256, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0); snprintf(line, 256, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0); snprintf(line, 256, "\r\n"); ::send(sockfd, line, strlen(line), 0); @@ -463,12 +470,12 @@ WebSocket::pointer WebSocket::create_dummy() { } -WebSocket::pointer WebSocket::from_url(const std::string& url) { - return ::from_url(url, true); +WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) { + return ::from_url(url, true, origin); } -WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url) { - return ::from_url(url, false); +WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) { + return ::from_url(url, false, origin); } diff --git a/easywsclient.hpp b/easywsclient.hpp index 7165ecc..0daabdb 100644 --- a/easywsclient.hpp +++ b/easywsclient.hpp @@ -19,8 +19,8 @@ class WebSocket { // Factories: static pointer create_dummy(); - static pointer from_url(const std::string& url); - static pointer from_url_no_mask(const std::string& url); + 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()); // Interfaces: virtual ~WebSocket() { }