/
websockets.d
1081 lines (935 loc) · 30.3 KB
/
websockets.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Implements WebSocket support and fallbacks for older browsers.
Standards: $(LINK2 https://tools.ietf.org/html/rfc6455, RFC6455)
Copyright: © 2012-2014 RejectedSoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Jan Krüger
*/
module vibe.http.websockets;
///
@safe unittest {
void handleConn(scope WebSocket sock)
{
// simple echo server
while (sock.connected) {
auto msg = sock.receiveText();
sock.send(msg);
}
}
void startServer()
{
import vibe.http.router;
auto router = new URLRouter;
router.get("/ws", handleWebSockets(&handleConn));
// Start HTTP server using listenHTTP()...
}
}
import vibe.core.core;
import vibe.core.log;
import vibe.core.net;
import vibe.core.sync;
import vibe.stream.operations;
import vibe.http.server;
import vibe.http.client;
import vibe.core.connectionpool;
import vibe.utils.array;
import core.time;
import std.array;
import std.base64;
import std.conv;
import std.exception;
import std.bitmanip;
import std.digest.sha;
import std.string;
import std.functional;
import std.uuid;
import std.base64;
import std.digest.sha;
import vibe.crypto.cryptorand;
@safe:
alias WebSocketHandshakeDelegate = void delegate(scope WebSocket);
/// Exception thrown by $(D vibe.http.websockets).
class WebSocketException: Exception
{
@safe pure nothrow:
///
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
{
super(msg, file, line, next);
}
///
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
{
super(msg, next, file, line);
}
}
/**
Returns a WebSocket client object that is connected to the specified host.
*/
WebSocket connectWebSocket(URL url, const(HTTPClientSettings) settings = defaultSettings)
@safe {
import std.typecons : Tuple, tuple;
auto host = url.host;
auto port = url.port;
bool use_tls = (url.schema == "wss") ? true : false;
if (port == 0)
port = (use_tls) ? 443 : 80;
static struct ConnInfo { string host; ushort port; bool useTLS; string proxyIP; ushort proxyPort; }
static vibe.utils.array.FixedRingBuffer!(Tuple!(ConnInfo, ConnectionPool!HTTPClient), 16) s_connections;
auto ckey = ConnInfo(host, port, use_tls, settings ? settings.proxyURL.host : null, settings ? settings.proxyURL.port : 0);
ConnectionPool!HTTPClient pool;
foreach (c; s_connections)
if (c[0].host == host && c[0].port == port && c[0].useTLS == use_tls && (settings is null || (c[0].proxyIP == settings.proxyURL.host && c[0].proxyPort == settings.proxyURL.port)))
pool = c[1];
if (!pool)
{
logDebug("Create HTTP client pool %s:%s %s proxy %s:%d", host, port, use_tls, (settings) ? settings.proxyURL.host : string.init, (settings) ? settings.proxyURL.port : 0);
pool = new ConnectionPool!HTTPClient({
auto ret = new HTTPClient;
ret.connect(host, port, use_tls, settings);
return ret;
});
if (s_connections.full)
s_connections.popFront();
s_connections.put(tuple(ckey, pool));
}
auto rng = secureRNG();
auto challengeKey = generateChallengeKey(rng);
auto answerKey = computeAcceptKey(challengeKey);
auto cl = pool.lockConnection();
auto res = cl.request((scope req){
req.requestURL = (url.localURI == "") ? "/" : url.localURI;
req.method = HTTPMethod.GET;
req.headers["Upgrade"] = "websocket";
req.headers["Connection"] = "Upgrade";
req.headers["Sec-WebSocket-Version"] = "13";
req.headers["Sec-WebSocket-Key"] = challengeKey;
});
enforce(res.statusCode == HTTPStatus.switchingProtocols, "Server didn't accept the protocol upgrade request.");
auto key = "sec-websocket-accept" in res.headers;
enforce(key !is null, "Response is missing the Sec-WebSocket-Accept header.");
enforce(*key == answerKey, "Response has wrong accept key");
auto conn = res.switchProtocol("websocket");
auto ws = new WebSocket(conn, null, rng);
return ws;
}
/// ditto
void connectWebSocket(URL url, scope WebSocketHandshakeDelegate del, const(HTTPClientSettings) settings = defaultSettings)
@safe {
bool use_tls = (url.schema == "wss") ? true : false;
url.schema = use_tls ? "https" : "http";
/*scope*/auto rng = secureRNG();
auto challengeKey = generateChallengeKey(rng);
auto answerKey = computeAcceptKey(challengeKey);
requestHTTP(url,
(scope req) {
req.method = HTTPMethod.GET;
req.headers["Upgrade"] = "websocket";
req.headers["Connection"] = "Upgrade";
req.headers["Sec-WebSocket-Version"] = "13";
req.headers["Sec-WebSocket-Key"] = challengeKey;
},
(scope res) {
enforce(res.statusCode == HTTPStatus.switchingProtocols, "Server didn't accept the protocol upgrade request.");
auto key = "sec-websocket-accept" in res.headers;
enforce(key !is null, "Response is missing the Sec-WebSocket-Accept header.");
enforce(*key == answerKey, "Response has wrong accept key");
res.switchProtocol("websocket", (scope conn) @trusted {
scope ws = new WebSocket(conn, null, rng);
del(ws);
});
}
);
}
/// Scheduled for deprecation - use a `@safe` callback instead.
void connectWebSocket(URL url, scope void delegate(scope WebSocket) @system del, const(HTTPClientSettings) settings = defaultSettings)
@system {
connectWebSocket(url, (scope ws) @trusted => del(ws), settings);
}
/**
Establishes a web socket conection and passes it to the $(D on_handshake) delegate.
*/
void handleWebSocket(scope WebSocketHandshakeDelegate on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res)
@safe {
auto pUpgrade = "Upgrade" in req.headers;
auto pConnection = "Connection" in req.headers;
auto pKey = "Sec-WebSocket-Key" in req.headers;
//auto pProtocol = "Sec-WebSocket-Protocol" in req.headers;
auto pVersion = "Sec-WebSocket-Version" in req.headers;
auto isUpgrade = false;
if( pConnection ) {
auto connectionTypes = split(*pConnection, ",");
foreach( t ; connectionTypes ) {
if( t.strip().toLower() == "upgrade" ) {
isUpgrade = true;
break;
}
}
}
string req_error;
if (!isUpgrade) req_error = "WebSocket endpoint only accepts \"Connection: upgrade\" requests.";
else if (!pUpgrade || icmp(*pUpgrade, "websocket") != 0) req_error = "WebSocket endpoint requires \"Upgrade: websocket\" header.";
else if (!pVersion || *pVersion != "13") req_error = "Only version 13 of the WebSocket protocol is supported.";
else if (!pKey) req_error = "Missing \"Sec-WebSocket-Key\" header.";
if (req_error.length) {
logDebug("Browser sent invalid WebSocket request: %s", req_error);
res.statusCode = HTTPStatus.badRequest;
res.writeBody(req_error);
return;
}
auto accept = () @trusted { return cast(string)Base64.encode(sha1Of(*pKey ~ s_webSocketGuid)); } ();
res.headers["Sec-WebSocket-Accept"] = accept;
res.headers["Connection"] = "Upgrade";
ConnectionStream conn = res.switchProtocol("websocket");
WebSocket socket = new WebSocket(conn, req, null);
try {
on_handshake(socket);
} catch (Exception e) {
logDiagnostic("WebSocket handler failed: %s", e.msg);
}
socket.close();
}
/// Scheduled for deprecation - use a `@safe` callback instead.
void handleWebSocket(scope void delegate(scope WebSocket) @system on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res)
@system {
handleWebSocket((scope ws) @trusted => on_handshake(ws), req, res);
}
/**
Returns a HTTP request handler that establishes web socket conections.
*/
HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @safe on_handshake)
@safe {
return handleWebSockets(() @trusted { return toDelegate(on_handshake); } ());
}
/// ditto
HTTPServerRequestDelegateS handleWebSockets(WebSocketHandshakeDelegate on_handshake)
@safe {
void callback(scope HTTPServerRequest req, scope HTTPServerResponse res)
@safe {
auto pUpgrade = "Upgrade" in req.headers;
auto pConnection = "Connection" in req.headers;
auto pKey = "Sec-WebSocket-Key" in req.headers;
//auto pProtocol = "Sec-WebSocket-Protocol" in req.headers;
auto pVersion = "Sec-WebSocket-Version" in req.headers;
auto isUpgrade = false;
if( pConnection ) {
auto connectionTypes = split(*pConnection, ",");
foreach( t ; connectionTypes ) {
if( t.strip().toLower() == "upgrade" ) {
isUpgrade = true;
break;
}
}
}
if( !(isUpgrade &&
pUpgrade && icmp(*pUpgrade, "websocket") == 0 &&
pKey &&
pVersion && *pVersion == "13") )
{
logDebug("Browser sent invalid WebSocket request.");
res.statusCode = HTTPStatus.badRequest;
res.writeVoidBody();
return;
}
auto accept = () @trusted { return cast(string)Base64.encode(sha1Of(*pKey ~ s_webSocketGuid)); } ();
res.headers["Sec-WebSocket-Accept"] = accept;
res.headers["Connection"] = "Upgrade";
res.switchProtocol("websocket", (scope conn) {
// TODO: put back 'scope' once it is actually enforced by DMD
/*scope*/ auto socket = new WebSocket(conn, req, null);
try on_handshake(socket);
catch (Exception e) {
logDiagnostic("WebSocket handler failed: %s", e.msg);
}
socket.close();
});
}
return &callback;
}
/// Scheduled for deprecation - use a `@safe` callback instead.
HTTPServerRequestDelegateS handleWebSockets(void delegate(scope WebSocket) @system on_handshake)
@system {
return handleWebSockets(delegate (scope ws) @trusted => on_handshake(ws));
}
/// Scheduled for deprecation - use a `@safe` callback instead.
HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @system on_handshake)
@system {
return handleWebSockets(delegate (scope ws) @trusted => on_handshake(ws));
}
/**
* Represents a single _WebSocket connection.
*
* ---
* shared static this ()
* {
* runTask(() => connectToWS());
* }
*
* void connectToWS ()
* {
* auto ws_url = URL("wss://websockets.example.com/websocket/auth_token");
* auto ws = connectWebSocket(ws_url);
* logInfo("WebSocket connected");
*
* while (ws.waitForData())
* {
* auto txt = ws.receiveText;
* logInfo("Received: %s", txt);
* }
* logFatal("Connection lost!");
* }
* ---
*/
final class WebSocket {
@safe:
private {
ConnectionStream m_conn;
bool m_sentCloseFrame = false;
IncomingWebSocketMessage m_nextMessage = null;
const HTTPServerRequest m_request;
Task m_reader;
InterruptibleTaskMutex m_readMutex, m_writeMutex;
InterruptibleTaskCondition m_readCondition;
Timer m_pingTimer;
uint m_lastPingIndex;
bool m_pongReceived;
bool m_pongSkipped;
short m_closeCode;
const(char)[] m_closeReason;
/// The entropy generator to use
/// If not null, it means this is a server socket.
RandomNumberStream m_rng;
}
/**
* Private constructor, called from `connectWebSocket`.
*
* Params:
* conn = Underlying connection string
* request = HTTP request used to establish the connection
* rng = Source of entropy to use. If null, assume we're a server socket
*/
private this(ConnectionStream conn, in HTTPServerRequest request,
RandomNumberStream rng)
{
m_conn = conn;
m_request = request;
assert(m_conn);
m_rng = rng;
m_writeMutex = new InterruptibleTaskMutex;
m_readMutex = new InterruptibleTaskMutex;
m_readCondition = new InterruptibleTaskCondition(m_readMutex);
m_readMutex.performLocked!({
m_reader = runTask(&startReader);
if (request !is null && request.serverSettings.webSocketPingInterval != Duration.zero) {
m_pongReceived = true;
m_pingTimer = setTimer(request.serverSettings.webSocketPingInterval, &sendPing, true);
}
});
}
/**
Determines if the WebSocket connection is still alive and ready for sending.
Note that for determining the ready state for $(EM reading), you need
to use $(D waitForData) instead, because both methods can return
different values while a disconnect is in proress.
See_also: $(D waitForData)
*/
@property bool connected() { return m_conn.connected && !m_sentCloseFrame; }
/**
Returns the close code sent by the remote end.
Note if the connection was never opened, is still alive, or was closed
locally this value will be 0. If no close code was given by the remote
end in the close frame, the value will be 1005. If the connection was
not closed cleanly by the remote end, this value will be 1006.
*/
@property short closeCode() { return m_closeCode; }
/**
Returns the close reason sent by the remote end.
Note if the connection was never opened, is still alive, or was closed
locally this value will be an empty string.
*/
@property const(char)[] closeReason() { return m_closeReason; }
/**
The HTTP request that established the web socket connection.
*/
@property const(HTTPServerRequest) request() const { return m_request; }
/**
Checks if data is readily available for read.
*/
@property bool dataAvailableForRead() { return m_conn.dataAvailableForRead || m_nextMessage !is null; }
/** Waits until either a message arrives or until the connection is closed.
This function can be used in a read loop to cleanly determine when to stop reading.
*/
bool waitForData()
{
if (m_nextMessage) return true;
m_readMutex.performLocked!({
while (connected && m_nextMessage is null)
m_readCondition.wait();
});
return m_nextMessage !is null;
}
/// ditto
bool waitForData(Duration timeout)
{
import std.datetime;
if (m_nextMessage) return true;
immutable limit_time = Clock.currTime(UTC()) + timeout;
m_readMutex.performLocked!({
while (connected && m_nextMessage is null && timeout > 0.seconds) {
m_readCondition.wait(timeout);
timeout = limit_time - Clock.currTime(UTC());
}
});
return m_nextMessage !is null;
}
/**
Sends a text message.
On the JavaScript side, the text will be available as message.data (type string).
Throws:
A `WebSocketException` is thrown if the connection gets closed
before or during the transfer of the message.
*/
void send(scope const(char)[] data)
{
send(
(scope message) { message.write(cast(const ubyte[])data); },
FrameOpcode.text);
}
/**
Sends a binary message.
On the JavaScript side, the text will be available as message.data (type Blob).
Throws:
A `WebSocketException` is thrown if the connection gets closed
before or during the transfer of the message.
*/
void send(in ubyte[] data)
{
send((scope message){ message.write(data); }, FrameOpcode.binary);
}
/**
Sends a message using an output stream.
Throws:
A `WebSocketException` is thrown if the connection gets closed
before or during the transfer of the message.
*/
void send(scope void delegate(scope OutgoingWebSocketMessage) @safe sender, FrameOpcode frameOpcode)
{
m_writeMutex.performLocked!({
enforceEx!WebSocketException(!m_sentCloseFrame, "WebSocket connection already actively closed.");
/*scope*/auto message = new OutgoingWebSocketMessage(m_conn, frameOpcode, m_rng);
scope(exit) message.finalize();
sender(message);
});
}
/// Compatibility overload - will be removed soon.
deprecated("Call the overload which requires an explicit FrameOpcode.")
void send(scope void delegate(scope OutgoingWebSocketMessage) @safe sender)
{
send(sender, FrameOpcode.text);
}
/**
Actively closes the connection.
Params:
code = Numeric code indicating a termination reason.
reason = Message describing why the connection was terminated.
*/
void close(short code = 0, scope const(char)[] reason = "")
{
//control frame payloads are limited to 125 bytes
assert(reason.length <= 123);
if (connected) {
m_writeMutex.performLocked!({
m_sentCloseFrame = true;
Frame frame;
frame.opcode = FrameOpcode.close;
if(code != 0)
frame.payload = std.bitmanip.nativeToBigEndian(code) ~ cast(const ubyte[])reason;
frame.fin = true;
frame.writeFrame(m_conn, m_rng);
});
}
if (m_pingTimer) m_pingTimer.stop();
if (Task.getThis() != m_reader) m_reader.join();
}
/**
Receives a new message and returns its contents as a newly allocated data array.
Params:
strict = If set, ensures the exact frame type (text/binary) is received and throws an execption otherwise.
Throws: WebSocketException if the connection is closed or
if $(D strict == true) and the frame received is not the right type
*/
ubyte[] receiveBinary(bool strict = true)
{
ubyte[] ret;
receive((scope message){
enforceEx!WebSocketException(!strict || message.frameOpcode == FrameOpcode.binary,
"Expected a binary message, got "~message.frameOpcode.to!string());
ret = message.readAll();
});
return ret;
}
/// ditto
string receiveText(bool strict = true)
{
string ret;
receive((scope message){
enforceEx!WebSocketException(!strict || message.frameOpcode == FrameOpcode.text,
"Expected a text message, got "~message.frameOpcode.to!string());
ret = message.readAllUTF8();
});
return ret;
}
/**
Receives a new message using an InputStream.
Throws: WebSocketException if the connection is closed.
*/
void receive(scope void delegate(scope IncomingWebSocketMessage) @safe receiver)
{
m_readMutex.performLocked!({
while (!m_nextMessage) {
enforceEx!WebSocketException(connected, "Connection closed while reading message.");
m_readCondition.wait();
}
receiver(m_nextMessage);
m_nextMessage = null;
m_readCondition.notifyAll();
});
}
private void startReader()
{
m_readMutex.performLocked!({}); //Wait until initialization
scope (exit) m_readCondition.notifyAll();
try {
while (!m_conn.empty) {
assert(!m_nextMessage);
if (m_pingTimer) {
if (m_pongSkipped) {
logDebug("Pong not received, closing connection");
m_writeMutex.performLocked!({
m_conn.close();
});
return;
}
if (!m_conn.waitForData(request.serverSettings.webSocketPingInterval))
continue;
}
/*scope*/auto msg = new IncomingWebSocketMessage(m_conn, m_rng);
if (msg.frameOpcode == FrameOpcode.pong) {
enforce(msg.peek().length == uint.sizeof, "Pong payload has wrong length");
enforce(m_lastPingIndex == littleEndianToNative!uint(msg.peek()[0..uint.sizeof]), "Pong payload has wrong value");
m_pongReceived = true;
continue;
}
if(msg.frameOpcode == FrameOpcode.close) {
logDebug("Got closing frame (%s)", m_sentCloseFrame);
// If no close code was passed, we default to 1005
this.m_closeCode = 1005;
// If provided in the frame, attempt to parse the close code/reason
if (msg.peek().length >= short.sizeof) {
this.m_closeCode = bigEndianToNative!short(msg.peek()[0..short.sizeof]);
if (msg.peek().length > short.sizeof) {
this.m_closeReason = cast(const(char) [])msg.peek()[short.sizeof..$];
}
}
if(!m_sentCloseFrame) close();
logDebug("Terminating connection (%s)", m_sentCloseFrame);
m_conn.close();
return;
}
m_readMutex.performLocked!({
m_nextMessage = msg;
m_readCondition.notifyAll();
while (m_nextMessage) m_readCondition.wait();
});
}
} catch (Exception e) {
logDiagnostic("Error while reading websocket message: %s", e.msg);
logDiagnostic("Closing connection.");
}
// If no close code was passed, e.g. this was an unclean termination
// of our websocket connection, set the close code to 1006.
if (this.m_closeCode == 0) this.m_closeCode = 1006;
m_conn.close();
}
private void sendPing()
nothrow {
if (!m_pongReceived) {
logDebug("Pong skipped");
m_pongSkipped = true;
m_pingTimer.stop();
return;
}
try m_writeMutex.performLocked!({
m_pongReceived = false;
Frame ping;
ping.opcode = FrameOpcode.ping;
ping.fin = true;
ping.payload = nativeToLittleEndian(++m_lastPingIndex);
ping.writeFrame(m_conn, m_rng);
logDebug("Ping sent");
});
catch (Exception e) {
logError("Failed to acquire write mutex for sending a WebSocket ping frame: %s", e.msg);
}
}
}
/**
Represents a single outgoing _WebSocket message as an OutputStream.
*/
final class OutgoingWebSocketMessage : OutputStream {
@safe:
private {
RandomNumberStream m_rng;
Stream m_conn;
FrameOpcode m_frameOpcode;
Appender!(ubyte[]) m_buffer;
bool m_finalized = false;
}
this(Stream conn, FrameOpcode frameOpcode, RandomNumberStream rng)
{
assert(conn !is null);
m_conn = conn;
m_frameOpcode = frameOpcode;
m_rng = rng;
}
size_t write(in ubyte[] bytes, IOMode mode)
{
assert(!m_finalized);
if (!m_buffer.data.length) {
ubyte[Frame.maxHeaderSize] header_padding;
m_buffer.put(header_padding[]);
}
m_buffer.put(bytes);
return bytes.length;
}
void flush()
{
assert(!m_finalized);
if (m_buffer.data.length > 0)
sendFrame(false);
}
void finalize()
{
if (m_finalized) return;
m_finalized = true;
sendFrame(true);
}
private void sendFrame(bool fin)
{
if (!m_buffer.data.length)
write(null, IOMode.once);
assert(m_buffer.data.length >= Frame.maxHeaderSize);
Frame frame;
frame.fin = fin;
frame.opcode = m_frameOpcode;
frame.payload = m_buffer.data[Frame.maxHeaderSize .. $];
auto hsize = frame.getHeaderSize(m_rng !is null);
auto msg = m_buffer.data[Frame.maxHeaderSize-hsize .. $];
frame.writeHeader(msg[0 .. hsize], m_rng);
m_conn.write(msg);
m_conn.flush();
m_buffer.clear();
}
alias write = OutputStream.write;
}
/**
Represents a single incoming _WebSocket message as an InputStream.
*/
final class IncomingWebSocketMessage : InputStream {
@safe:
private {
RandomNumberStream m_rng;
Stream m_conn;
Frame m_currentFrame;
}
this(Stream conn, RandomNumberStream rng)
{
assert(conn !is null);
m_conn = conn;
m_rng = rng;
readFrame();
}
@property bool empty() const { return m_currentFrame.payload.length == 0; }
@property ulong leastSize() const { return m_currentFrame.payload.length; }
@property bool dataAvailableForRead() { return true; }
/// The frame type for this nessage;
@property FrameOpcode frameOpcode() const { return m_currentFrame.opcode; }
const(ubyte)[] peek() { return m_currentFrame.payload; }
/**
* Retrieve the next websocket frame of the stream and discard the current
* one
*
* This function is helpful if one wish to process frames by frames,
* or minimize memory allocation, as `peek` will only return the current
* frame data, and read requires a pre-allocated buffer.
*
* Returns:
* `false` if the current frame is the final one, `true` if a new frame
* was read.
*/
bool skipFrame()
{
if (m_currentFrame.fin)
return false;
m_currentFrame = Frame.readFrame(m_conn);
return true;
}
size_t read(scope ubyte[] dst, IOMode mode)
{
size_t nread = 0;
while (dst.length > 0) {
enforceEx!WebSocketException(!empty , "cannot read from empty stream");
enforceEx!WebSocketException(leastSize > 0, "no data available" );
import std.algorithm : min;
auto sz = cast(size_t)min(leastSize, dst.length);
dst[0 .. sz] = m_currentFrame.payload[0 .. sz];
dst = dst[sz .. $];
m_currentFrame.payload = m_currentFrame.payload[sz .. $];
nread += sz;
if (leastSize == 0) {
if (mode == IOMode.immediate || mode == IOMode.once && nread > 0)
break;
this.skipFrame();
}
}
return nread;
}
alias read = InputStream.read;
private void readFrame() {
Frame frame;
do {
frame = Frame.readFrame(m_conn);
switch(frame.opcode) {
case FrameOpcode.continuation:
case FrameOpcode.text:
case FrameOpcode.binary:
case FrameOpcode.close:
case FrameOpcode.pong:
m_currentFrame = frame;
break;
case FrameOpcode.ping:
Frame pong;
pong.opcode = FrameOpcode.pong;
pong.fin = true;
pong.payload = frame.payload;
pong.writeFrame(m_conn, m_rng);
break;
default:
throw new WebSocketException("unknown frame opcode");
}
} while( frame.opcode == FrameOpcode.ping );
}
}
/// Magic string defined by the RFC for challenging the server during upgrade
private static immutable s_webSocketGuid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
/**
* The Opcode is 4 bits, as defined in Section 5.2
*
* Values are defined in section 11.8
* Currently only 6 values are defined, however the opcode is defined as
* taking 4 bits.
*/
private enum FrameOpcode : ubyte {
continuation = 0x0,
text = 0x1,
binary = 0x2,
close = 0x8,
ping = 0x9,
pong = 0xA
}
static assert(FrameOpcode.max < 0b1111, "FrameOpcode is only 4 bits");
private struct Frame {
@safe:
enum maxHeaderSize = 14;
bool fin;
FrameOpcode opcode;
ubyte[] payload;
/**
* Return the header length encoded with the expected amount of bits
*
* The WebSocket RFC define a variable-length payload length.
* In short, it means that:
* - If the length is <= 125, it is stored as the 7 least significant
* bits of the second header byte. The first bit is reserved for MASK.
* - If the length is <= 65_536 (so it fits in 2 bytes), a magic value of
* 126 is stored in the aforementioned 7 bits, and the actual length
* is stored in the next two bytes, resulting in a 4 bytes header
* ( + masking key, if any).
* - If the length is > 65_536, a magic value of 127 will be used for
* the 7-bit field, and the next 8 bytes are expected to be the length,
* resulting in a 10 bytes header ( + masking key, if any).
*
* Those functions encapsulate all this logic and allow to just get the
* length with the desired size.
*
* Return:
* - For `ubyte`, the value to store in the 7 bits field, either the
* length or a magic value (126 or 127).
* - For `ushort`, a value in the range [126; 65_536].
* If payload.length is not in this bound, an assertion will be triggered.
* - For `ulong`, a value in the range [65_537; size_t.max].
* If payload.length is not in this bound, an assertion will be triggered.
*/
size_t getHeaderSize(bool mask)
{
size_t ret = 1;
if (payload.length < 126) ret += 1;
else if (payload.length < 65536) ret += 3;
else ret += 9;
if (mask) ret += 4;
return ret;
}
void writeHeader(ubyte[] dst, RandomNumberStream sys_rng)
{
ubyte[4] buff;
ubyte firstByte = cast(ubyte)opcode;
if (fin) firstByte |= 0x80;
dst[0] = firstByte;
dst = dst[1 .. $];
auto b1 = sys_rng ? 0x80 : 0x00;
if (payload.length < 126) {
dst[0] = cast(ubyte)(b1 | payload.length);
dst = dst[1 .. $];
} else if (payload.length < 65536) {
dst[0] = cast(ubyte) (b1 | 126);
dst[1 .. 3] = std.bitmanip.nativeToBigEndian(cast(ushort)payload.length);
dst = dst[3 .. $];
} else {
dst[0] = cast(ubyte) (b1 | 127);
dst[1 .. 9] = std.bitmanip.nativeToBigEndian(cast(ulong)payload.length);
dst = dst[9 .. $];
}
if (sys_rng) {
sys_rng.read(dst[0 .. 4]);
for (size_t i = 0; i < payload.length; i++)
payload[i] ^= dst[i % 4];
}
}
void writeFrame(OutputStream stream, RandomNumberStream sys_rng)
{
import vibe.stream.wrapper;
auto rng = StreamOutputRange(stream);
ubyte[maxHeaderSize] hdr;
writeHeader(hdr[], sys_rng);
rng.put(hdr[0 .. getHeaderSize(sys_rng !is null)]);
rng.flush();
stream.flush();
}
static Frame readFrame(InputStream stream)
{
Frame frame;
ubyte[8] data;
stream.read(data[0 .. 2]);
frame.fin = (data[0] & 0x80) != 0;
frame.opcode = cast(FrameOpcode)(data[0] & 0x0F);
bool masked = !!(data[1] & 0b1000_0000);
//parsing length
ulong length = data[1] & 0b0111_1111;
if (length == 126) {
stream.read(data[0 .. 2]);
length = bigEndianToNative!ushort(data[0 .. 2]);
} else if (length == 127) {
stream.read(data);
length = bigEndianToNative!ulong(data);
// RFC 6455, 5.2, 'Payload length': If 127, the following 8 bytes
// interpreted as a 64-bit unsigned integer (the most significant
// bit MUST be 0)
enforceEx!WebSocketException(!(length >> 63),
"Received length has a non-zero most significant bit");
}
logDebug("Read frame: %s %s %s length=%d",
frame.opcode,
frame.fin ? "final frame" : "continuation",
masked ? "masked" : "not masked",
length);
// Masking key is 32 bits / uint
if (masked)
stream.read(data[0 .. 4]);
// Read payload
// TODO: Provide a way to limit the size read, easy
// DOS for server code here (rejectedsoftware/vibe.d#1496).
enforceEx!WebSocketException(length <= size_t.max);
frame.payload = new ubyte[](cast(size_t)length);
stream.read(frame.payload);
//de-masking
if (masked)
foreach (size_t i; 0 .. cast(size_t)length)
frame.payload[i] = frame.payload[i] ^ data[i % 4];
return frame;
}
}
unittest {
import std.algorithm.searching : all;
final class DummyRNG : RandomNumberStream {
@safe:
@property bool empty() { return false; }
@property ulong leastSize() { return ulong.max; }