-
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
// Copyright 2025 Shota FUJI
//
// Licensed under the Zero-Clause BSD License or the Apache License, Version 2.0, at your option.
// You may not use, copy, modify, or distribute this file except according to those terms. You can
// find a copy of the Zero-Clause BSD License at LICENSES/0BSD.txt, and a copy of the Apache License,
// Version 2.0 at LICENSES/Apache-2.0.txt. You may also obtain a copy of the Zero-Clause BSD License
// at <https://opensource.org/license/0bsd> and a copy of the Apache License, Version 2.0 at
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: 0BSD OR Apache-2.0
//
// ===
//
// Zig API.
//! MOO message is a message format used for communicating with Roon Server.
//! Encodes to and decodes from newline-delimited UTF-8 encoded plain text, like HTTP.
//! Newline character is LF (0xa) without CR character.
//!
//! MOO message consists of metadata, header and body: header is a list of key-values
//! that both key and value contains UTF-8 string value, and body is single UTF-8 string.
//! Typical MOO message have "Content-Type" header that describes how the message's body
//! is encoded. (e.g. "application/json")
//! The first line of MOO message is metadata line, which is in "MOO/VERSION VERB SERVICE"
//! format. SERVICE is delimited by slash.
const std = @import("std");
const HeaderParsingContext = struct {
start_position: usize,
};
/// Metadata contains a MOO message's version and semantics.
/// Fields point to the source message bytes: freeing the source message bytes then
/// accessing fields results in use-after-free.
pub const Metadata = struct {
/// Message schema version described in metadata line.
version: u32 = 1,
/// Verb described in metadata line. Verb consists of uppercase alphabets.
/// @example: "REQUEST", "COMPLETE"
verb: []const u8,
/// Service name described in metadata line.
service: []const u8,
context: HeaderParsingContext,
const signature = "MOO/";
pub const ParseError = error{
InvalidSignature,
InvalidVersionNumber,
InvalidVerb,
InvalidService,
NonUtf8ServiceName,
};
pub fn parse(message: []const u8) ParseError!@This() {
var i: usize = 0;
if (!std.mem.startsWith(u8, message, signature)) {
return ParseError.InvalidSignature;
}
i += signature.len;
const version = if (std.mem.indexOfScalarPos(u8, message, i, ' ')) |space_position| blk: {
if (i == space_position) {
return ParseError.InvalidVersionNumber;
}
const parsed = std.fmt.parseInt(u32, message[i..space_position], 10) catch return ParseError.InvalidVersionNumber;
i = space_position + 1;
break :blk parsed;
} else {
return ParseError.InvalidVersionNumber;
};
const verb = if (std.mem.indexOfScalarPos(u8, message, i, ' ')) |space_position| blk: {
if (i == space_position) {
return ParseError.InvalidVerb;
}
const s = message[i..space_position];
for (s) |char| {
if (char < 'A' or char > 'Z') {
return ParseError.InvalidVerb;
}
}
i = space_position + 1;
break :blk s;
} else {
return ParseError.InvalidVerb;
};
const line_delimiter_position = std.mem.indexOfScalarPos(u8, message, i, '\n') orelse message.len;
const service = message[i..line_delimiter_position];
if (service.len == 0) {
return ParseError.InvalidService;
}
if (!std.unicode.utf8ValidateSlice(service)) {
return ParseError.NonUtf8ServiceName;
}
return @This(){
.version = version,
.verb = verb,
.service = service,
.context = HeaderParsingContext{
.start_position = line_delimiter_position + 1,
},
};
}
};
test Metadata {
{
const metadata = try Metadata.parse("MOO/1 REQUEST foo/bar\n");
try std.testing.expectEqual(1, metadata.version);
try std.testing.expectEqualStrings("REQUEST", metadata.verb);
try std.testing.expectEqualStrings("foo/bar", metadata.service);
}
{
const metadata = try Metadata.parse("MOO/20 COMPLETE foo\n");
try std.testing.expectEqual(20, metadata.version);
try std.testing.expectEqualStrings("COMPLETE", metadata.verb);
try std.testing.expectEqualStrings("foo", metadata.service);
}
}
test "Metadata.parse() should reject invalid signature" {
{
const err = Metadata.parse("");
try std.testing.expectError(Metadata.ParseError.InvalidSignature, err);
}
{
const err = Metadata.parse("\n");
try std.testing.expectError(Metadata.ParseError.InvalidSignature, err);
}
{
const err = Metadata.parse("MOO1 REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidSignature, err);
}
{
const err = Metadata.parse("MEOW/1 REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidSignature, err);
}
}
test "Metadata.parse() should reject invalid version" {
{
const err = Metadata.parse("MOO/1.0 REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVersionNumber, err);
}
{
const err = Metadata.parse("MOO/-1 REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVersionNumber, err);
}
{
const err = Metadata.parse("MOO/ REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVersionNumber, err);
}
{
const err = Metadata.parse("MOO/0x1 REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVersionNumber, err);
}
{
const err = Metadata.parse("MOO/one REQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVersionNumber, err);
}
}
test "Metadata.parse() should reject invalid verb" {
{
const err = Metadata.parse("MOO/1 request foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
{
const err = Metadata.parse("MOO/1 RÉQUEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
{
const err = Metadata.parse("MOO/1 REQUEST1 foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
{
const err = Metadata.parse("MOO/1 RE❓️UEST foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
{
const err = Metadata.parse("MOO/1 \n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
{
const err = Metadata.parse("MOO/1 foo/bar\n");
try std.testing.expectError(Metadata.ParseError.InvalidVerb, err);
}
}
test "Metadata.parse() should reject invalid service" {
{
const err = Metadata.parse("MOO/1 REQUEST \n");
try std.testing.expectError(Metadata.ParseError.InvalidService, err);
}
{
const err = Metadata.parse("MOO/1 REQUEST ");
try std.testing.expectError(Metadata.ParseError.InvalidService, err);
}
{
const err = Metadata.parse("MOO/1 REQUEST \xc3\x28\n");
try std.testing.expectError(Metadata.ParseError.NonUtf8ServiceName, err);
}
}
const HeaderIterator = struct {
buffer: []const u8,
pub const IterateError = error{
NoHeaderDelimiterError,
EmptyHeaderKey,
NonUtf8HeaderKey,
NonUtf8HeaderValue,
};
/// Returns bytes read when a header is read, `key` and `value` is populated.
/// Returns `null` when the buffer is exhausted, `key` and `value` is untouched.
pub fn iterate(self: *HeaderIterator, key: *[]const u8, value: *[]const u8) IterateError!?usize {
if (self.buffer.len == 0) {
return null;
}
const line_end = std.mem.indexOfScalar(u8, self.buffer, '\n') orelse self.buffer.len;
const line = self.buffer[0..line_end];
if (line.len == 0) {
return null;
}
const delimiter = std.mem.indexOfScalar(u8, self.buffer, ':') orelse return IterateError.NoHeaderDelimiterError;
const key_slice = self.buffer[0..delimiter];
if (key_slice.len == 0) {
return IterateError.EmptyHeaderKey;
}
if (!std.unicode.utf8ValidateSlice(key_slice)) {
return IterateError.NonUtf8HeaderKey;
}
const value_slice = std.mem.trimLeft(u8, self.buffer[delimiter + 1 .. line_end], " ");
if (!std.unicode.utf8ValidateSlice(value_slice)) {
return IterateError.NonUtf8HeaderKey;
}
key.* = key_slice;
value.* = value_slice;
const read = @min(line_end + 1, self.buffer.len);
self.buffer = self.buffer[read..];
return read;
}
};
test "(HeaderIterator).iterate should parse a header line" {
var iter = HeaderIterator{ .buffer = "Foo: Bar\n" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(9, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("Bar", bar);
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const second_read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, second_read);
}
test "(HeaderIterator).iterate should parse header lines" {
var iter = HeaderIterator{ .buffer = "Foo: foo-value\nbar: bar-value\nbaZ: baz-value\n" };
var foo_key: []const u8 = undefined;
var foo_value: []const u8 = undefined;
const foo_read = try iter.iterate(&foo_key, &foo_value);
try std.testing.expectEqual(15, foo_read);
try std.testing.expectEqualStrings("Foo", foo_key);
try std.testing.expectEqualStrings("foo-value", foo_value);
var bar_key: []const u8 = undefined;
var bar_value: []const u8 = undefined;
const bar_read = try iter.iterate(&bar_key, &bar_value);
try std.testing.expectEqual(15, bar_read);
try std.testing.expectEqualStrings("bar", bar_key);
try std.testing.expectEqualStrings("bar-value", bar_value);
var baz_key: []const u8 = undefined;
var baz_value: []const u8 = undefined;
const baz_read = try iter.iterate(&baz_key, &baz_value);
try std.testing.expectEqual(15, baz_read);
try std.testing.expectEqualStrings("baZ", baz_key);
try std.testing.expectEqualStrings("baz-value", baz_value);
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const final_read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, final_read);
}
test "(HeaderIterator).iterate should parse a header line terminated without newline" {
var iter = HeaderIterator{ .buffer = "Foo: Bar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(8, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("Bar", bar);
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const second_read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, second_read);
}
test "(HeaderIterator).iterate should return false for blank inputs" {
{
var iter = HeaderIterator{ .buffer = "" };
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, read);
}
{
var iter = HeaderIterator{ .buffer = "\n" };
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, read);
}
{
var iter = HeaderIterator{ .buffer = "\n\n\n" };
var key: []const u8 = undefined;
var val: []const u8 = undefined;
const read = try iter.iterate(&key, &val);
try std.testing.expectEqual(null, read);
}
}
test "(HeaderIterator).iterate should return error for empty key" {
// Only zero-length slice: the official Node.js API parses space-only headers...
var iter = HeaderIterator{ .buffer = ": Value" };
var key: []const u8 = undefined;
var val: []const u8 = undefined;
try std.testing.expectError(
HeaderIterator.IterateError.EmptyHeaderKey,
iter.iterate(&key, &val),
);
}
test "(HeaderIterator).iterate should trim starting spaces" {
// Many spaces
{
var iter = HeaderIterator{ .buffer = "Foo: Bar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(19, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("Bar", bar);
}
// Zero space
{
var iter = HeaderIterator{ .buffer = "Foo:Bar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(7, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("Bar", bar);
}
// Tab is not a space
{
var iter = HeaderIterator{ .buffer = "Foo:\tBar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(8, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("\tBar", bar);
}
// Newline is not a space
{
var iter = HeaderIterator{ .buffer = "Foo:\nBar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(5, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("", bar);
}
// NULL character is not a space
{
var iter = HeaderIterator{ .buffer = "Foo:\x00Bar" };
var foo: []const u8 = undefined;
var bar: []const u8 = undefined;
const read = try iter.iterate(&foo, &bar);
try std.testing.expectEqual(8, read);
try std.testing.expectEqualStrings("Foo", foo);
try std.testing.expectEqualStrings("\x00Bar", bar);
}
}
const BodyParsingContext = struct {
start_position: usize,
content_type: []const u8,
content_length: usize,
};
/// WellKnownHeaders stores headers defined in the official node-roon api source code and
/// discards every other header fields.
pub const WellKnownHeaders = struct {
/// MIME type.
/// This slice refers to the source message bytes. Accessing this field after
/// free-ing the source message bytes is use-after-free.
content_type: []const u8,
/// Byte size of the body.
content_length: usize,
/// An ID unique to a connection, to associate corresponding request and response.
request_id: i64,
body_start_position: usize,
pub const ParseError = error{
EmptyContentType,
NonUintContentLength,
NonIntRequestID,
MissingContentType,
MissingContentLength,
MissingRequestID,
} || HeaderIterator.IterateError;
pub fn parse(message: []const u8, context: HeaderParsingContext) ParseError!@This() {
var iter = HeaderIterator{ .buffer = message[context.start_position..] };
var content_type: ?[]const u8 = null;
var content_length: ?usize = null;
var request_id: ?i64 = null;
var i = context.start_position;
var key: []const u8 = undefined;
var val: []const u8 = undefined;
while (try iter.iterate(&key, &val)) |read| {
i += read;
// Unlike HTTP headers, MOO headers are case-sensitive.
if (std.mem.eql(u8, "Content-Type", key)) {
if (val.len == 0) {
return ParseError.EmptyContentType;
}
content_type = val;
continue;
}
if (std.mem.eql(u8, "Content-Length", key)) {
content_length = std.fmt.parseInt(usize, val, 10) catch return ParseError.NonUintContentLength;
continue;
}
if (std.mem.eql(u8, "Request-Id", key)) {
request_id = std.fmt.parseInt(i64, val, 10) catch return ParseError.NonIntRequestID;
continue;
}
}
return WellKnownHeaders{
.content_type = content_type orelse return ParseError.MissingContentType,
.content_length = content_length orelse return ParseError.MissingContentLength,
.request_id = request_id orelse return ParseError.MissingRequestID,
// `i` does not include a final newline.
.body_start_position = i + 1,
};
}
pub fn getContext(self: *const @This()) BodyParsingContext {
return .{
.start_position = self.body_start_position,
.content_type = self.content_type,
.content_length = self.content_length,
};
}
};
test "WellKnownHeaders.parse should parse well-known headers" {
const message = "MOO/1 REQUEST foo\nContent-Type: application/json; charset=utf-8\nContent-Length: 876\nRequest-Id: 1\n\n";
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
try std.testing.expectEqualStrings("application/json; charset=utf-8", header.content_type);
try std.testing.expectEqual(876, header.content_length);
try std.testing.expectEqual(1, header.request_id);
const ctx = header.getContext();
try std.testing.expectEqual(message.len, ctx.start_position);
}
test "WellKnownHeaders.parse should return error when required header is missing" {
{
const message = "MOO/1 REQUEST foo\nContent-Length: 876\nRequest-Id: 1\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.MissingContentType,
WellKnownHeaders.parse(message, meta.context),
);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: application/json; charset=utf-8\nRequest-Id: 1\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.MissingContentLength,
WellKnownHeaders.parse(message, meta.context),
);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: application/json; charset=utf-8\nContent-Length: 876\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.MissingRequestID,
WellKnownHeaders.parse(message, meta.context),
);
}
}
test "WellKnownHeaders.parse should return error on an unexpected format" {
{
const message = "MOO/1 REQUEST foo\nContent-Type:\nContent-Length: 1\nRequest-Id: 1\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.EmptyContentType,
WellKnownHeaders.parse(message, meta.context),
);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: 0x87\nRequest-Id: 1\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.NonUintContentLength,
WellKnownHeaders.parse(message, meta.context),
);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: one\nRequest-Id: 1\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.NonUintContentLength,
WellKnownHeaders.parse(message, meta.context),
);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: 1\nRequest-Id: 0D8C-F91C-22BB\n";
const meta = try Metadata.parse(message);
try std.testing.expectError(
WellKnownHeaders.ParseError.NonIntRequestID,
WellKnownHeaders.parse(message, meta.context),
);
}
}
/// HashMapHeaders
pub const HashMapHeaders = struct {
/// Header key-values.
/// Both key and value points to the source message bytes. Accessing those after
/// free-ing the source message bytes is use-after-free.
map: std.hash_map.StringHashMap([]const u8),
context: BodyParsingContext,
pub const ParseError = error{
MissingContentType,
MissingContentLength,
EmptyContentType,
NonUintContentLength,
} || HeaderIterator.IterateError || std.mem.Allocator.Error;
pub fn parse(allocator: std.mem.Allocator, message: []const u8, context: HeaderParsingContext) ParseError!@This() {
var map = std.hash_map.StringHashMap([]const u8).init(allocator);
var iter = HeaderIterator{ .buffer = message[context.start_position..] };
var i = context.start_position;
var content_type: ?[]const u8 = null;
var content_length: ?usize = null;
var key: []const u8 = undefined;
var val: []const u8 = undefined;
while (try iter.iterate(&key, &val)) |read| {
i += read;
if (std.mem.eql(u8, "Content-Type", key)) {
if (val.len == 0) {
return ParseError.EmptyContentType;
}
content_type = val;
} else if (std.mem.eql(u8, "Content-Length", key)) {
content_length = std.fmt.parseInt(usize, val, 10) catch return ParseError.NonUintContentLength;
}
try map.put(key, val);
}
return HashMapHeaders{
.map = map,
.context = .{
.content_type = content_type orelse return ParseError.MissingContentType,
.content_length = content_length orelse return ParseError.MissingContentLength,
// `i` does not include a final newline.
.start_position = i + 1,
},
};
}
pub fn deinit(self: *@This()) void {
self.map.deinit();
}
};
test "HashMapHeaders.parse should save every headers" {
const message = "MOO/1 REQUEST foo\nContent-Type: application/json; charset=utf-8\nContent-Length: 876\nRequest-Id: 1\nFoo: Bar\n\n";
const meta = try Metadata.parse(message);
var header = try HashMapHeaders.parse(std.testing.allocator, message, meta.context);
defer header.deinit();
try std.testing.expectEqualStrings(header.map.get("Content-Type").?, "application/json; charset=utf-8");
try std.testing.expectEqualStrings(header.map.get("Content-Length").?, "876");
try std.testing.expectEqualStrings(header.map.get("Request-Id").?, "1");
try std.testing.expectEqualStrings(header.map.get("Foo").?, "Bar");
try std.testing.expectEqual(message.len, header.context.start_position);
}
/// RawBody contains a bytes slice of body section in a MOO message.
/// The bytes field points to the source message bytes: freeing the source message bytes
/// then accessing the bytes field results in use-after-free.
pub const RawBody = struct {
bytes: []const u8,
pub const ParseError = error{
ContentLengthMismatch,
};
pub fn parse(message: []const u8, context: BodyParsingContext) ParseError!@This() {
const start = @min(context.start_position, message.len);
const bytes = message[start..];
if (bytes.len != context.content_length) {
return ParseError.ContentLengthMismatch;
}
return .{ .bytes = bytes };
}
};
test "RawBody.parse should save rest of the bytes" {
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: 13\nRequest-Id: 1\n\nHello, World!";
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const body = try RawBody.parse(message, header.getContext());
try std.testing.expectEqualStrings("Hello, World!", body.bytes);
}
test "RawBody.parse should reject mismatching Content-Length" {
{
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: 12\nRequest-Id: 1\n\nHello, World!";
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const err = RawBody.parse(message, header.getContext());
try std.testing.expectError(RawBody.ParseError.ContentLengthMismatch, err);
}
{
const message = "MOO/1 REQUEST foo\nContent-Type: text/plain\nContent-Length: 14\nRequest-Id: 1\n\nHello, World!";
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const err = RawBody.parse(message, header.getContext());
try std.testing.expectError(RawBody.ParseError.ContentLengthMismatch, err);
}
}
/// JsonBody stores MOO message body as JSON value. User has to provided an expected
/// type (schema) as "T".
pub fn JsonBody(comptime T: type) type {
return struct {
allocator: std.mem.Allocator,
value: *const T,
parsed: std.json.Parsed(T),
pub const ParseError = error{
ContentLengthMismatch,
ContentTypeIsNotApplicationJson,
} || std.json.ParseError(std.json.Scanner);
pub fn parse(allocator: std.mem.Allocator, message: []const u8, context: BodyParsingContext) ParseError!@This() {
// Same MIME checking as one in the official Node.js API.
// This does not match to "application/json; charset=utf-8" or such.
// (not doing the strict MIME check because parsing MIME is complex task)
if (!std.mem.eql(u8, "application/json", context.content_type)) {
return ParseError.ContentTypeIsNotApplicationJson;
}
const start = @min(context.start_position, message.len);
const bytes = message[start..];
if (bytes.len != context.content_length) {
return ParseError.ContentLengthMismatch;
}
const parsed = try std.json.parseFromSlice(T, allocator, bytes, .{});
return .{
.allocator = allocator,
.value = &parsed.value,
.parsed = parsed,
};
}
pub fn deinit(self: *const @This()) void {
self.parsed.deinit();
}
};
}
test "JsonBody.parse should deserialize body as JSON" {
const payload = "{\"foo\": 8}";
var message_buffer = std.ArrayList(u8).init(std.testing.allocator);
defer message_buffer.deinit();
try std.fmt.format(
message_buffer.writer(),
"MOO/1 REQUEST foo\nContent-Type: application/json\nContent-Length: {d}\nRequest-Id: 1\n\n{s}",
.{ payload.len, payload },
);
const message = try message_buffer.toOwnedSlice();
defer std.testing.allocator.free(message);
const TestData = struct {
foo: i32,
};
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const body = try JsonBody(TestData).parse(std.testing.allocator, message, header.getContext());
defer body.deinit();
try std.testing.expectEqual(body.value.foo, 8);
}
test "JsonBody.parse should reject invalid JSON text" {
const payload = "{\"foo\": 0x8}";
var message_buffer = std.ArrayList(u8).init(std.testing.allocator);
defer message_buffer.deinit();
try std.fmt.format(
message_buffer.writer(),
"MOO/1 REQUEST foo\nContent-Type: application/json\nContent-Length: {d}\nRequest-Id: 1\n\n{s}",
.{ payload.len, payload },
);
const message = try message_buffer.toOwnedSlice();
defer std.testing.allocator.free(message);
const TestData = struct {
foo: i32,
};
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const err = JsonBody(TestData).parse(std.testing.allocator, message, header.getContext());
try std.testing.expectError(JsonBody(TestData).ParseError.SyntaxError, err);
}
test "JsonBody.parse should reject mismatching Content-Length" {
const payload = "{\"foo\": 8}";
var message_buffer = std.ArrayList(u8).init(std.testing.allocator);
defer message_buffer.deinit();
try std.fmt.format(
message_buffer.writer(),
"MOO/1 REQUEST foo\nContent-Type: application/json\nContent-Length: {d}\nRequest-Id: 1\n\n{s}",
.{ payload.len + 1, payload },
);
const message = try message_buffer.toOwnedSlice();
defer std.testing.allocator.free(message);
const TestData = struct {
foo: i32,
};
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const err = JsonBody(TestData).parse(std.testing.allocator, message, header.getContext());
try std.testing.expectError(JsonBody(TestData).ParseError.ContentLengthMismatch, err);
}
test "JsonBody.parse should reject Content-Type other than application/json" {
const payload = "{\"foo\": 8}";
var message_buffer = std.ArrayList(u8).init(std.testing.allocator);
defer message_buffer.deinit();
try std.fmt.format(
message_buffer.writer(),
"MOO/1 REQUEST foo\nContent-Type: text/json\nContent-Length: {d}\nRequest-Id: 1\n\n{s}",
.{ payload.len, payload },
);
const message = try message_buffer.toOwnedSlice();
defer std.testing.allocator.free(message);
const TestData = struct {
foo: i32,
};
const meta = try Metadata.parse(message);
const header = try WellKnownHeaders.parse(message, meta.context);
const err = JsonBody(TestData).parse(std.testing.allocator, message, header.getContext());
try std.testing.expectError(JsonBody(TestData).ParseError.ContentTypeIsNotApplicationJson, err);
}