|
69 | 69 |
|
70 | 70 | namespace { // private module-only namespace
|
71 | 71 |
|
72 |
| -socket_t hostname_connect(std::string hostname, int port) { |
| 72 | +socket_t hostname_connect(const std::string& hostname, int port) { |
73 | 73 | struct addrinfo hints;
|
74 | 74 | struct addrinfo *result;
|
75 | 75 | struct addrinfo *p;
|
@@ -108,10 +108,10 @@ class _DummyWebSocket : public easywsclient::WebSocket
|
108 | 108 | {
|
109 | 109 | public:
|
110 | 110 | void poll(int timeout) { }
|
111 |
| - void send(std::string message) { } |
| 111 | + void send(const std::string& message) { } |
112 | 112 | void close() { }
|
113 | 113 | void _dispatch(Callback & callable) { }
|
114 |
| - readyStateValues getReadyState() { return CLOSED; } |
| 114 | + readyStateValues getReadyState() const { return CLOSED; } |
115 | 115 | };
|
116 | 116 |
|
117 | 117 |
|
@@ -174,7 +174,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
174 | 174 | _RealWebSocket(socket_t sockfd, bool useMask) : sockfd(sockfd), readyState(OPEN), useMask(useMask) {
|
175 | 175 | }
|
176 | 176 |
|
177 |
| - readyStateValues getReadyState() { |
| 177 | + readyStateValues getReadyState() const { |
178 | 178 | return readyState;
|
179 | 179 | }
|
180 | 180 |
|
@@ -312,7 +312,7 @@ class _RealWebSocket : public easywsclient::WebSocket
|
312 | 312 | }
|
313 | 313 | }
|
314 | 314 |
|
315 |
| - void send(std::string message) { |
| 315 | + void send(const std::string& message) { |
316 | 316 | // TODO:
|
317 | 317 | // Masking key should (must) be derived from a high quality random
|
318 | 318 | // number generator, to mitigate attacks on non-WebSocket friendly
|
@@ -381,14 +381,18 @@ class _RealWebSocket : public easywsclient::WebSocket
|
381 | 381 | };
|
382 | 382 |
|
383 | 383 |
|
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) { |
385 | 385 | char host[128];
|
386 | 386 | int port;
|
387 | 387 | char path[128];
|
388 | 388 | if (url.size() >= 128) {
|
389 | 389 | fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str());
|
390 | 390 | return NULL;
|
391 | 391 | }
|
| 392 | + if (origin.size() >= 200) { |
| 393 | + fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str()); |
| 394 | + return NULL; |
| 395 | + } |
392 | 396 | if (false) { }
|
393 | 397 | else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) {
|
394 | 398 | }
|
@@ -426,6 +430,9 @@ easywsclient::WebSocket::pointer from_url(/service/std::string url, bool useMask) {
|
426 | 430 | }
|
427 | 431 | snprintf(line, 256, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0);
|
428 | 432 | 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 | + } |
429 | 436 | snprintf(line, 256, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0);
|
430 | 437 | snprintf(line, 256, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0);
|
431 | 438 | snprintf(line, 256, "\r\n"); ::send(sockfd, line, strlen(line), 0);
|
@@ -463,12 +470,12 @@ WebSocket::pointer WebSocket::create_dummy() {
|
463 | 470 | }
|
464 | 471 |
|
465 | 472 |
|
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); |
468 | 475 | }
|
469 | 476 |
|
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); |
472 | 479 | }
|
473 | 480 |
|
474 | 481 |
|
|
0 commit comments