Skip to content

Commit 80d5c25

Browse files
committed
Merge pull request dhbaird#24 from donpillou/httpOrigin
Support for http origin header field
2 parents 368682d + 3d76a2e commit 80d5c25

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

easywsclient.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
namespace { // private module-only namespace
7171

72-
socket_t hostname_connect(std::string hostname, int port) {
72+
socket_t hostname_connect(const std::string& hostname, int port) {
7373
struct addrinfo hints;
7474
struct addrinfo *result;
7575
struct addrinfo *p;
@@ -108,10 +108,10 @@ class _DummyWebSocket : public easywsclient::WebSocket
108108
{
109109
public:
110110
void poll(int timeout) { }
111-
void send(std::string message) { }
111+
void send(const std::string& message) { }
112112
void close() { }
113113
void _dispatch(Callback & callable) { }
114-
readyStateValues getReadyState() { return CLOSED; }
114+
readyStateValues getReadyState() const { return CLOSED; }
115115
};
116116

117117

@@ -174,7 +174,7 @@ class _RealWebSocket : public easywsclient::WebSocket
174174
_RealWebSocket(socket_t sockfd, bool useMask) : sockfd(sockfd), readyState(OPEN), useMask(useMask) {
175175
}
176176

177-
readyStateValues getReadyState() {
177+
readyStateValues getReadyState() const {
178178
return readyState;
179179
}
180180

@@ -312,7 +312,7 @@ class _RealWebSocket : public easywsclient::WebSocket
312312
}
313313
}
314314

315-
void send(std::string message) {
315+
void send(const std::string& message) {
316316
// TODO:
317317
// Masking key should (must) be derived from a high quality random
318318
// number generator, to mitigate attacks on non-WebSocket friendly
@@ -381,14 +381,18 @@ class _RealWebSocket : public easywsclient::WebSocket
381381
};
382382

383383

384-
easywsclient::WebSocket::pointer from_url(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(/service/std::string url, bool useMask) {
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(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(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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ class WebSocket {
1919

2020
// Factories:
2121
static pointer create_dummy();
22-
static pointer from_url(std::string url);
23-
static pointer from_url_no_mask(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() { }
2727
virtual void poll(int timeout = 0) = 0; // timeout in milliseconds
28-
virtual void send(std::string message) = 0;
28+
virtual void send(const std::string& message) = 0;
2929
virtual void close() = 0;
30-
virtual readyStateValues getReadyState() = 0;
30+
virtual readyStateValues getReadyState() const = 0;
3131
template<class Callable>
3232
void dispatch(Callable callable) { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
3333
struct _Callback : public Callback {
34-
Callable & callable;
35-
_Callback(Callable & callable) : callable(callable) { }
36-
void operator()(const std::string & message) { callable(message); }
34+
Callable& callable;
35+
_Callback(Callable& callable) : callable(callable) { }
36+
void operator()(const std::string& message) { callable(message); }
3737
};
3838
_Callback callback(callable);
3939
_dispatch(callback);
4040
}
4141

4242
protected:
43-
struct Callback { virtual void operator()(const std::string & message) = 0; };
44-
virtual void _dispatch(Callback & callable) = 0;
43+
struct Callback { virtual void operator()(const std::string& message) = 0; };
44+
virtual void _dispatch(Callback& callable) = 0;
4545
};
4646

4747
} // namespace easywsclient

0 commit comments

Comments
 (0)