Skip to content

Commit 3d76a2e

Browse files
committed
Added optional origin header field
1 parent 4dfa56b commit 3d76a2e

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

easywsclient.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,18 @@ class _RealWebSocket : public easywsclient::WebSocket
381381
};
382382

383383

384-
easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask) {
384+
easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin) {
385385
char host[128];
386386
int port;
387387
char path[128];
388388
if (url.size() >= 128) {
389389
fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str());
390390
return NULL;
391391
}
392+
if (origin.size() >= 200) {
393+
fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str());
394+
return NULL;
395+
}
392396
if (false) { }
393397
else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) {
394398
}
@@ -426,6 +430,9 @@ easywsclient::WebSocket::pointer from_url(http://github.com/const%20std::string&%20url,%20bool%20useMask)
426430
}
427431
snprintf(line, 256, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0);
428432
snprintf(line, 256, "Connection: Upgrade\r\n"); ::send(sockfd, line, strlen(line), 0);
433+
if (!origin.empty()) {
434+
snprintf(line, 256, "Origin: %s\r\n", origin.c_str()); ::send(sockfd, line, strlen(line), 0);
435+
}
429436
snprintf(line, 256, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0);
430437
snprintf(line, 256, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0);
431438
snprintf(line, 256, "\r\n"); ::send(sockfd, line, strlen(line), 0);
@@ -463,12 +470,12 @@ WebSocket::pointer WebSocket::create_dummy() {
463470
}
464471

465472

466-
WebSocket::pointer WebSocket::from_url(const std::string& url) {
467-
return ::from_url(url, true);
473+
WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) {
474+
return ::from_url(url, true, origin);
468475
}
469476

470-
WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url) {
471-
return ::from_url(url, false);
477+
WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) {
478+
return ::from_url(url, false, origin);
472479
}
473480

474481

easywsclient.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class WebSocket {
1919

2020
// Factories:
2121
static pointer create_dummy();
22-
static pointer from_url(const std::string& url);
23-
static pointer from_url_no_mask(const std::string& url);
22+
static pointer from_url(const std::string& url, const std::string& origin = std::string());
23+
static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string());
2424

2525
// Interfaces:
2626
virtual ~WebSocket() { }

0 commit comments

Comments
 (0)