-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebSocket old.cpn
19758 lines (19751 loc) · 647 KB
/
WebSocket old.cpn
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE workspaceElements PUBLIC "-//CPN//DTD CPNXML 1.0//EN" "http://cpntools.org/DTD/6/cpn.dtd">
<workspaceElements>
<generator tool="CPN Tools"
version="3.2.2"
format="6"/>
<cpnet>
<globbox>
<block id="ID1412310166">
<id>Standard priorities</id>
<ml id="ID1412310255">val P_HIGH = 100;
<layout>val P_HIGH = 100;</layout>
</ml>
<ml id="ID1412310292">val P_NORMAL = 1000;
<layout>val P_NORMAL = 1000;</layout>
</ml>
<ml id="ID1412310322">val P_LOW = 10000;
<layout>val P_LOW = 10000;</layout>
</ml>
</block>
<block id="ID1">
<id>Standard declarations</id>
<color id="ID85042">
<id>UNIT</id>
<unit/>
<layout>colset UNIT = unit;</layout>
</color>
<color id="ID3">
<id>INT</id>
<int/>
</color>
<color id="ID4">
<id>BOOL</id>
<bool/>
</color>
<color id="ID5">
<id>STRING</id>
<string/>
</color>
<color id="ID1412604261">
<id>STRINGLIST</id>
<list>
<id>STRING</id>
</list>
<layout>colset STRINGLIST = list STRING;</layout>
</color>
<var id="ID1412347868">
<type>
<id>UNIT</id>
</type>
<id>u</id>
<id>u1</id>
<id>u2</id>
<id>u3</id>
<layout>var u, u1, u2, u3: UNIT;</layout>
</var>
<var id="ID1412346888">
<type>
<id>BOOL</id>
</type>
<id>b</id>
<id>b1</id>
<id>b2</id>
<id>b3</id>
<layout>var b, b1, b2, b3: BOOL;</layout>
</var>
<var id="ID1412347465">
<type>
<id>INT</id>
</type>
<id>i</id>
<id>j</id>
<id>k</id>
<layout>var i, j, k: INT;</layout>
</var>
<var id="ID1412346333">
<type>
<id>STRING</id>
</type>
<id>s</id>
<id>s1</id>
<id>s2</id>
<id>s3</id>
<layout>var s, s1, s2, s3: STRING;</layout>
</var>
<var id="ID1412612087">
<type>
<id>STRINGLIST</id>
</type>
<id>ss</id>
<id>ss1</id>
<id>ss2</id>
<layout>var ss, ss1, ss2: STRINGLIST;</layout>
</var>
</block>
<color id="ID1417689610">
<id>BYTE</id>
<index>
<ml>0x00</ml>
<ml>0xFF</ml>
<id>Byte</id>
</index>
<layout>colset BYTE = index Byte with 0x00..0xFF;</layout>
</color>
<color id="ID1417736058">
<id>BIT</id>
<bool>
<with>
<id>clear</id>
<id>set</id>
</with>
</bool>
<layout>colset BIT= bool with (clear, set);</layout>
</color>
<block id="ID1421456676">
<id>Parameters</id>
<ml id="ID1421470602">val fragSize = 20;
<layout>val fragSize = 20;</layout>
</ml>
</block>
<block id="ID1415544646">
<id>Constants</id>
<ml id="ID1413249567">val httpVersion = "HTTP/1.1";
<layout>val httpVersion = "HTTP/1.1";</layout>
</ml>
<ml id="ID1415560287">val nonce = "nonce";
<layout>val nonce = "nonce";</layout>
</ml>
<ml id="ID1416539554">val uuid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
<layout>val uuid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";</layout>
</ml>
</block>
<block id="ID1415525155">
<id>Functions</id>
<ml id="ID1415536841">fun B64 str =
"B64("^str^")";
<layout>fun B64 str =
"B64("^str^")";</layout>
</ml>
<ml id="ID1416508324">fun SHA1 str =
"SHA1("^str^")";
<layout>fun SHA1 str =
"SHA1("^str^")";</layout>
</ml>
</block>
<block id="ID1412651705">
<id>URL declarations</id>
<color id="ID1412345812">
<id>URL</id>
<record>
<recordfield>
<id>Protocol</id>
<id>STRING</id>
</recordfield>
<recordfield>
<id>Host</id>
<id>STRING</id>
</recordfield>
<recordfield>
<id>Port</id>
<id>INT</id>
</recordfield>
<recordfield>
<id>Path</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset URL = record
Protocol: STRING *
Host: STRING *
Port: INT *
Path: STRING;</layout>
</color>
<var id="ID1412827859">
<type>
<id>URL</id>
</type>
<id>url</id>
<layout>var url: URL;</layout>
</var>
<ml id="ID1412366299">fun split2 (s, t, i) =
(* Recursively scan for character t in string s starting as position i, split
if match *)
let
val ss = String.extract(s, i, NONE)
in
if String.isPrefix t ss then
[substring(s, 0, i),
String.extract(s, i + String.size t, NONE)]
else split2(s, t, i+1)
end
<layout>fun split2 (s, t, i) =
(* Recursively scan for character t in string s starting as position i, split
if match *)
let
val ss = String.extract(s, i, NONE)
in
if String.isPrefix t ss then
[substring(s, 0, i),
String.extract(s, i + String.size t, NONE)]
else split2(s, t, i+1)
end</layout>
</ml>
<ml id="ID1412365221">fun split (s, t) =
(* Split string s on character c *)
if String.isPrefix t s then
[String.extract(s, String.size t, NONE)]
else if String.isSubstring t s then
split2 (s, t, 1)
else [s]
<layout>fun split (s, t) =
(* Split string s on character c *)
if String.isPrefix t s then
[String.extract(s, String.size t, NONE)]
else if String.isSubstring t s then
split2 (s, t, 1)
else [s]</layout>
</ml>
<ml id="ID1412351951">fun parseUrl (s) =
let
val proto'rest = split (s, "://")
val proto'rest =
if length proto'rest = 1
then "ws" :: proto'rest
else proto'rest
val pro = List.hd(proto'rest)
val host'path = split (List.nth(proto'rest, 1), "/")
val pat = if length host'path = 2
then "/" ^ List.nth(host'path, 1)
else "/"
val host'port = split (List.hd(host'path), ":")
val hos = List.hd(host'port)
val port'default = case pro of
"wss" => 443
| _ => 80
val por = if length host'port = 1
then port'default
else let
val port'str = List.nth(host'port, 1)
val port'int'opt = Int.fromString port'str
in
Option.getOpt(port'int'opt, port'default)
end
in
{Protocol=pro,
Host=hos,
Port=por,
Path=pat}
end;
<layout>fun parseUrl (s) =
let
val proto'rest = split (s, "://")
val proto'rest =
if length proto'rest = 1
then "ws" :: proto'rest
else proto'rest
val pro = List.hd(proto'rest)
val host'path = split (List.nth(proto'rest, 1), "/")
val pat = if length host'path = 2
then "/" ^ List.nth(host'path, 1)
else "/"
val host'port = split (List.hd(host'path), ":")
val hos = List.hd(host'port)
val port'default = case pro of
"wss" => 443
| _ => 80
val por = if length host'port = 1
then port'default
else let
val port'str = List.nth(host'port, 1)
val port'int'opt = Int.fromString port'str
in
Option.getOpt(port'int'opt, port'default)
end
in
{Protocol=pro,
Host=hos,
Port=por,
Path=pat}
end;</layout>
</ml>
</block>
<block id="ID1418275477">
<id>Interface declarations</id>
<color id="ID1414429371">
<id>OPERATION</id>
<enum>
<id>TEXT</id>
<id>BINARY</id>
<id>PING</id>
<id>PONG</id>
<id>CLOSE</id>
</enum>
<layout>colset OPERATION = with
TEXT | BINARY |
PING | PONG | CLOSE;</layout>
</color>
<color id="ID1412718173">
<id>MESSAGE</id>
<record>
<recordfield>
<id>Op</id>
<id>OPERATION</id>
</recordfield>
<recordfield>
<id>Message</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset MESSAGE = record
Op: OPERATION *
Message: STRING;</layout>
</color>
<color id="ID1416942917">
<id>MESSAGES</id>
<list>
<id>MESSAGE</id>
</list>
<layout>colset MESSAGES = list MESSAGE;</layout>
</color>
<var id="ID1414597729">
<type>
<id>MESSAGE</id>
</type>
<id>msg</id>
<layout>var msg: MESSAGE;</layout>
</var>
<var id="ID1416952324">
<type>
<id>MESSAGES</id>
</type>
<id>msgs</id>
<id>msgs2</id>
<layout>var msgs, msgs2: MESSAGES;</layout>
</var>
<ml id="ID1423281020">fun isData (msg:MESSAGE) =
(#Op msg = TEXT) orelse
(#Op msg = BINARY);
<layout>fun isData (msg:MESSAGE) =
(#Op msg = TEXT) orelse
(#Op msg = BINARY);</layout>
</ml>
<color id="ID1415723367">
<id>CONN_RESULT</id>
<bool>
<with>
<id>fail</id>
<id>success</id>
</with>
</bool>
<layout>colset CONN_RESULT= bool with
(fail, success);</layout>
</color>
<color id="ID1420664468">
<id>CONN_STATUS</id>
<union>
<unionfield>
<id>CONN_OPEN</id>
</unionfield>
<unionfield>
<id>CONN_CLOSING</id>
</unionfield>
<unionfield>
<id>CONN_CLOSED</id>
</unionfield>
</union>
<layout>colset CONN_STATUS = union
CONN_OPEN +
CONN_CLOSING +
CONN_CLOSED;</layout>
</color>
<block id="ID1430235555">
<id>Client-side</id>
<color id="ID1418966764">
<id>CLIENT_EVENT</id>
<union>
<unionfield>
<id>CliGetMsg</id>
<type>
<id>MESSAGE</id>
</type>
</unionfield>
<unionfield>
<id>ConnResult</id>
<type>
<id>CONN_RESULT</id>
</type>
</unionfield>
</union>
<layout>colset CLIENT_EVENT = union
CliGetMsg:MESSAGE +
ConnResult:CONN_RESULT;</layout>
</color>
<color id="ID1428308302">
<id>CLIENT_EVENTS</id>
<list>
<id>CLIENT_EVENT</id>
</list>
<layout>colset CLIENT_EVENTS = list CLIENT_EVENT;</layout>
</color>
<var id="ID1428326462">
<type>
<id>CLIENT_EVENTS</id>
</type>
<id>cEvents</id>
<layout>var cEvents: CLIENT_EVENTS;</layout>
</var>
<color id="ID1428877257">
<id>CLIENT_CALL</id>
<union>
<unionfield>
<id>Connect</id>
<type>
<id>URL</id>
</type>
</unionfield>
<unionfield>
<id>CliSendMsg</id>
<type>
<id>MESSAGE</id>
</type>
</unionfield>
</union>
<layout>colset CLIENT_CALL = union
Connect:URL +
CliSendMsg:MESSAGE;</layout>
</color>
<color id="ID1428886244">
<id>CLIENT_CALLS</id>
<list>
<id>CLIENT_CALL</id>
</list>
<layout>colset CLIENT_CALLS
= list CLIENT_CALL;</layout>
</color>
<var id="ID1428887598">
<type>
<id>CLIENT_CALL</id>
</type>
<id>cCall</id>
<layout>var cCall: CLIENT_CALL;</layout>
</var>
<var id="ID1428888084">
<type>
<id>CLIENT_CALLS</id>
</type>
<id>cCalls</id>
<layout>var cCalls: CLIENT_CALLS;</layout>
</var>
</block>
<block id="ID1430242857">
<id>Server-side</id>
<color id="ID1430256771">
<id>SERVER_EVENT</id>
<union>
<unionfield>
<id>SerGetMsg</id>
<type>
<id>MESSAGE</id>
</type>
</unionfield>
<unionfield>
<id>ConnRequest</id>
<type>
<id>UNIT</id>
</type>
</unionfield>
</union>
<layout>colset SERVER_EVENT = union
SerGetMsg:MESSAGE +
ConnRequest:UNIT;</layout>
</color>
<color id="ID1430263391">
<id>SERVER_EVENTS</id>
<list>
<id>SERVER_EVENT</id>
</list>
<layout>colset SERVER_EVENTS =
list SERVER_EVENT;</layout>
</color>
<var id="ID1430281549">
<type>
<id>SERVER_EVENTS</id>
</type>
<id>sEvents</id>
<layout>var sEvents: SERVER_EVENTS;</layout>
</var>
<color id="ID1428028302">
<id>CONN_REPLY</id>
<bool>
<with>
<id>reject</id>
<id>accept</id>
</with>
</bool>
<layout>colset CONN_REPLY = bool with
(reject, accept);</layout>
</color>
<var id="ID1428038536">
<type>
<id>CONN_REPLY</id>
</type>
<id>conn_reply</id>
<layout>var conn_reply: CONN_REPLY;</layout>
</var>
<color id="ID1428017863">
<id>SERVER_CALL</id>
<union>
<unionfield>
<id>Msg'</id>
<type>
<id>MESSAGE</id>
</type>
</unionfield>
<unionfield>
<id>ConnReply</id>
<type>
<id>CONN_REPLY</id>
</type>
</unionfield>
</union>
<layout>colset SERVER_CALL = union
Msg':MESSAGE +
ConnReply:CONN_REPLY;</layout>
</color>
<color id="ID1428310405">
<id>SERVER_CALLS</id>
<list>
<id>SERVER_CALL</id>
</list>
<layout>colset SERVER_CALLS
= list SERVER_CALL;</layout>
</color>
<var id="ID1428431863">
<type>
<id>SERVER_CALLS</id>
</type>
<id>sCalls</id>
<layout>var sCalls: SERVER_CALLS;</layout>
</var>
</block>
</block>
<block id="ID1413015241">
<id>WebSocket frame declarations</id>
<color id="ID1417705065">
<id>MASK</id>
<list>
<with>
<ml>4</ml>
<ml>4</ml>
</with>
<id>BYTE</id>
</list>
<layout>colset MASK = list BYTE with 4..4;</layout>
</color>
<color id="ID1417975474">
<id>MASKING</id>
<union>
<unionfield>
<id>Nomask</id>
</unionfield>
<unionfield>
<id>Mask</id>
<type>
<id>MASK</id>
</type>
</unionfield>
</union>
<layout>colset MASKING = union
Nomask +
Mask:MASK;</layout>
</color>
<ml id="ID1420933783">fun randMask() = Mask([
Byte(0),Byte(0),Byte(0),Byte(0)
(*
BYTE.ran(),
BYTE.ran(),
BYTE.ran(),
BYTE.ran()
*)
]);
<layout>fun randMask() = Mask([
Byte(0),Byte(0),Byte(0),Byte(0)
(*
BYTE.ran(),
BYTE.ran(),
BYTE.ran(),
BYTE.ran()
*)
]);</layout>
</ml>
<ml id="ID1418650267">val opContinuation = 0x0;
val opText = 0x1;
val opBinary = 0x2
val opConnectionClose = 0x8;
val opPing = 0x9;
val opPong = 0xA;
<layout>val opContinuation = 0x0;
val opText = 0x1;
val opBinary = 0x2
val opConnectionClose = 0x8;
val opPing = 0x9;
val opPong = 0xA;</layout>
</ml>
<color id="ID1413023483">
<id>WSFRAME</id>
<record>
<recordfield>
<id>Fin</id>
<id>BIT</id>
</recordfield>
<recordfield>
<id>Rsv1</id>
<id>BIT</id>
</recordfield>
<recordfield>
<id>Rsv2</id>
<id>BIT</id>
</recordfield>
<recordfield>
<id>Rsv3</id>
<id>BIT</id>
</recordfield>
<recordfield>
<id>Opcode</id>
<id>INT</id>
</recordfield>
<recordfield>
<id>Masked</id>
<id>BIT</id>
</recordfield>
<recordfield>
<id>Payload_length</id>
<id>INT</id>
</recordfield>
<recordfield>
<id>Masking_key</id>
<id>MASKING</id>
</recordfield>
<recordfield>
<id>Payload</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset WSFRAME = record
Fin: BIT *
Rsv1: BIT *
Rsv2: BIT *
Rsv3: BIT *
Opcode: INT *
Masked: BIT *
Payload_length: INT *
Masking_key: MASKING *
Payload: STRING;</layout>
</color>
<color id="ID1424508837">
<id>WSFRAMES</id>
<list>
<id>WSFRAME</id>
</list>
<layout>colset WSFRAMES = list WSFRAME;</layout>
</color>
<var id="ID1417863155">
<type>
<id>WSFRAME</id>
</type>
<id>wsframe</id>
<layout>var wsframe: WSFRAME;</layout>
</var>
<var id="ID1424497188">
<type>
<id>WSFRAMES</id>
</type>
<id>wsframes</id>
<id>wsframes2</id>
<layout>var wsframes, wsframes2: WSFRAMES;</layout>
</var>
</block>
<block id="ID1415671438">
<id>HTTP</id>
<color id="ID1413309696">
<id>HEADER</id>
<record>
<recordfield>
<id>Key</id>
<id>STRING</id>
</recordfield>
<recordfield>
<id>Value</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset HEADER = record
Key: STRING *
Value: STRING;</layout>
</color>
<color id="ID1413313357">
<id>HEADERS</id>
<list>
<id>HEADER</id>
</list>
<layout>colset HEADERS = list HEADER;</layout>
</color>
<ml id="ID1416597873">fun getHeader (_, []) = ""
| getHeader (key, (h:HEADER)::hs) =
if key = #Key h
then #Value h
else getHeader (key, hs);
<layout>fun getHeader (_, []) = ""
| getHeader (key, (h:HEADER)::hs) =
if key = #Key h
then #Value h
else getHeader (key, hs);</layout>
</ml>
<block id="ID1412941143">
<id>HTTP request declarations</id>
<color id="ID1413229090">
<id>HTTP_VERB</id>
<union>
<unionfield>
<id>GET</id>
</unionfield>
<unionfield>
<id>POST</id>
</unionfield>
<unionfield>
<id>PUT</id>
</unionfield>
<unionfield>
<id>DELETE</id>
</unionfield>
<unionfield>
<id>HEAD</id>
</unionfield>
</union>
<layout>colset HTTP_VERB = union
GET + POST + PUT + DELETE +
HEAD;</layout>
</color>
<color id="ID1413225712">
<id>REQUEST_LINE</id>
<record>
<recordfield>
<id>Verb</id>
<id>HTTP_VERB</id>
</recordfield>
<recordfield>
<id>Path</id>
<id>STRING</id>
</recordfield>
<recordfield>
<id>Version</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset REQUEST_LINE = record
Verb: HTTP_VERB *
Path: STRING *
Version: STRING;</layout>
</color>
<color id="ID1412944834">
<id>HTTPREQ</id>
<record>
<recordfield>
<id>RequestLine</id>
<id>REQUEST_LINE</id>
</recordfield>
<recordfield>
<id>Headers</id>
<id>HEADERS</id>
</recordfield>
</record>
<layout>colset HTTPREQ = record
RequestLine: REQUEST_LINE *
Headers: HEADERS;</layout>
</color>
<var id="ID1417752500">
<type>
<id>HTTPREQ</id>
</type>
<id>httpreq</id>
<layout>var httpreq: HTTPREQ;</layout>
</var>
<ml id="ID1413296999">fun httpReqFromUrl (url:URL) =
{
RequestLine={
Verb=GET,
Path=(#Path url),
Version=httpVersion
},
Headers=[
{Key="Host", Value=(#Host url)},
{Key="Upgrade", Value="websocket"},
{Key="Connection", Value="Upgrade"},
{Key="Sec-WebSocket-Key", Value=(B64 nonce)},
{Key="Sec-WebSocket-Version", Value="13"}
]
};
<layout>fun httpReqFromUrl (url:URL) =
{
RequestLine={
Verb=GET,
Path=(#Path url),
Version=httpVersion
},
Headers=[
{Key="Host", Value=(#Host url)},
{Key="Upgrade", Value="websocket"},
{Key="Connection", Value="Upgrade"},
{Key="Sec-WebSocket-Key", Value=(B64 nonce)},
{Key="Sec-WebSocket-Version", Value="13"}
]
};</layout>
</ml>
</block>
<block id="ID1415639717">
<id>HTTP response declarations</id>
<color id="ID1415663493">
<id>RESPONSE_LINE</id>
<record>
<recordfield>
<id>Version</id>
<id>STRING</id>
</recordfield>
<recordfield>
<id>Status</id>
<id>INT</id>
</recordfield>
<recordfield>
<id>Message</id>
<id>STRING</id>
</recordfield>
</record>
<layout>colset RESPONSE_LINE = record
Version: STRING *
Status: INT *
Message: STRING;</layout>
</color>
<color id="ID1415655550">
<id>HTTPRES</id>
<record>
<recordfield>
<id>ResponseLine</id>
<id>RESPONSE_LINE</id>
</recordfield>
<recordfield>
<id>Headers</id>
<id>HEADERS</id>
</recordfield>
</record>
<layout>colset HTTPRES = record
ResponseLine: RESPONSE_LINE *
Headers: HEADERS;</layout>
</color>
<var id="ID1417782108">
<type>
<id>HTTPRES</id>
</type>
<id>httpres</id>
<layout>var httpres: HTTPRES;</layout>
</var>
<ml id="ID1416526151">fun generateAccept str =
B64(SHA1(str^uuid));
<layout>fun generateAccept str =
B64(SHA1(str^uuid));</layout>
</ml>
<ml id="ID1416013432">fun isResponseValid (res:HTTPRES, req:HTTPREQ) = let
val rline = #ResponseLine res
val headers = #Headers res
val accepttoken = generateAccept(
getHeader("Sec-WebSocket-Key",
(#Headers req)))
in
#Status rline = 101 andalso
getHeader("Upgrade", headers)
= "websocket" andalso
getHeader("Connection", headers)
= "Upgrade" andalso
getHeader("Sec-WebSocket-Accept",headers)
= accepttoken
end
<layout>fun isResponseValid (res:HTTPRES, req:HTTPREQ) = let
val rline = #ResponseLine res
val headers = #Headers res
val accepttoken = generateAccept(
getHeader("Sec-WebSocket-Key",
(#Headers req)))
in
#Status rline = 101 andalso
getHeader("Upgrade", headers)
= "websocket" andalso
getHeader("Connection", headers)
= "Upgrade" andalso
getHeader("Sec-WebSocket-Accept",headers)
= accepttoken
end</layout>
</ml>
</block>
</block>
<ml id="ID1419619584">fun opHex2Sym(hex) = case hex of
0x1 => TEXT
| 0x2 => BINARY
| 0x8 => CLOSE
| 0x9 => PING
| 0xA => PONG;
<layout>fun opHex2Sym(hex) = case hex of
0x1 => TEXT
| 0x2 => BINARY
| 0x8 => CLOSE
| 0x9 => PING
| 0xA => PONG;</layout>
</ml>
<ml id="ID1421364557">fun opSym2Hex(sym) = case sym of
TEXT => opText
| BINARY => opBinary
| CLOSE=> opConnectionClose
| PING => opPing
| PONG => opPong;
<layout>fun opSym2Hex(sym) = case sym of
TEXT => opText
| BINARY => opBinary
| CLOSE=> opConnectionClose
| PING => opPing
| PONG => opPong;</layout>
</ml>
<block id="ID1428073498">
<id>More functions</id>
<ml id="ID1427803702">fun mask (ws:WSFRAME) = let
val ws1 = WSFRAME.set_Masked ws set
val ws2 = WSFRAME.set_Masking_key ws1 (randMask())
in
ws2
end;
<layout>fun mask (ws:WSFRAME) = let
val ws1 = WSFRAME.set_Masked ws set
val ws2 = WSFRAME.set_Masking_key ws1 (randMask())
in
ws2
end;</layout>
</ml>
<ml id="ID1428015533">fun unmask (ws:WSFRAME) = let
val ws1 = WSFRAME.set_Masked ws clear
val ws2 = WSFRAME.set_Masking_key ws1 Nomask
in
ws2
end;
<layout>fun unmask (ws:WSFRAME) = let
val ws1 = WSFRAME.set_Masked ws clear
val ws2 = WSFRAME.set_Masking_key ws1 Nomask
in
ws2
end;</layout>
</ml>
<ml id="ID1424522774">fun wrap (opc,payload,fin) = {
Fin=fin,
Rsv1=clear,
Rsv2=clear,
Rsv3=clear,
Opcode=opc,
Masked=clear,
Payload_length=(String.size payload),
Masking_key= Nomask,
Payload=payload
}
<layout>fun wrap (opc,payload,fin) = {
Fin=fin,
Rsv1=clear,
Rsv2=clear,
Rsv3=clear,
Opcode=opc,
Masked=clear,
Payload_length=(String.size payload),
Masking_key= Nomask,
Payload=payload
}</layout>
</ml>
<ml id="ID1424606596">fun wrapmsg (msg:MESSAGE,fin) =
wrap(opSym2Hex(#Op msg),
(#Message msg), fin);
<layout>fun wrapmsg (msg:MESSAGE,fin) =
wrap(opSym2Hex(#Op msg),
(#Message msg), fin);</layout>
</ml>
<ml id="ID1424503628">fun fragment (msg:MESSAGE) = let
fun loop (opc, s, acc) =
if (String.size s) > fragSize
then loop(
opContinuation,
String.extract(s,fragSize,NONE),
acc^^[wrap(opc,
String.substring(s,0,fragSize),
clear
)]