-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttputils.nim
1964 lines (1833 loc) · 59 KB
/
httputils.nim
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
#
# HTTP Utilities
# (c) Copyright 2018
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
{.push raises: [].}
import std/[times, strutils, algorithm, sequtils]
import results
export results
const
ALPHA* = {'a'..'z', 'A'..'Z'}
NUM* = {'0'..'9'}
TOKEND* = {'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`',
'|', '~'}
# HTTP token delimeters
URITOKEND* = {'-', '.', '_', '~', ':', '/', '?', '#', '[', ']', '@', '!',
'$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '%'}
# URI token delimeters
SPACE* = {' ', '\t'}
COLON* = {':'}
SLASH* = {'/'}
DOT* = {'.'}
CR* = char(0x0D)
LF* = char(0x0A)
ALPHANUM = ALPHA + NUM
TOKENURI = TOKEND * URITOKEND - DOT
TOKENONLY = TOKEND - URITOKEND - DOT
URIONLY = URITOKEND - TOKEND - COLON - SLASH - DOT
HEADERNAME* = ALPHANUM + TOKENURI + TOKENONLY + DOT
BSLASH = {'\\'}
FSLASH = {'/'}
COMMA = {','}
DQUOTE = {'"'}
EQUALS = {'='}
SEMCOL = {';'}
SPACEO = {'\x20'}
HOTAB = {'\x09'}
CHAR = {'\x00' .. '\x7E'}
CTL = {'\x00' .. '\x1F', '\x7F'}
SEPARATORS = {'(', ')', '<', '>', '@', ',', ';', ':',
'\\', '"', '/', '[', ']', '?', '=', '{',
'}'} + SPACEO + HOTAB
LTOKEN = CHAR - CTL - SEPARATORS
LSEP = SEPARATORS - BSLASH - DQUOTE - EQUALS - SEMCOL -
SPACEO - HOTAB
LSEP2 = LSEP - COMMA - FSLASH
LSEP3 = LSEP - FSLASH
CTL2 = CTL - HOTAB
# Legend:
# [0x81, 0x8D] - markers
# 0x81 - start HTTP request method
# 0x82 - end of HTTP request method
# 0x83 - start of HTTP request URI
# 0x84 - end of HTTP request URI
# 0x85 - start of HTTP version
# 0x86 - end of HTTP version
# 0x87 - LF
# 0x88 - start of header name
# 0x89 - end of header name
# 0x8A - start of header value
# 0x8B - end of header value
# 0x8C - last header LF
# 0x8D - header's finish
# [0xC0, 0xCF] - errors
# * ALPHA NUM TO^UR TOON URON CR LF COLON SLASH DOT SPACE PAD PAD PAD PAD
requestSM = [
0xC0, 0x81, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xCF, 0xCF, 0xCF, 0xCF, # s00: first method char
0xC1, 0x01, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0x82, 0xCF, 0xCF, 0xCF, 0xCF, # s01: method
0xC2, 0x83, 0x83, 0x83, 0xC2, 0x83, 0xC2, 0xC2, 0x83, 0x83, 0x83, 0xC2, 0xCF, 0xCF, 0xCF, 0xCF, # s02: first uri char
0xC2, 0x03, 0x03, 0x03, 0xC2, 0x03, 0xC2, 0xC2, 0x03, 0x03, 0x03, 0x84, 0xCF, 0xCF, 0xCF, 0xCF, # s03: uri
0xC3, 0x85, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xCF, 0xCF, 0xCF, 0xCF, # s04: first version char
0xC3, 0x05, 0x05, 0xC3, 0xC3, 0xC3, 0x86, 0xC3, 0xC3, 0x05, 0x05, 0xC3, 0xCF, 0xCF, 0xCF, 0xCF, # s05: version
0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0x87, 0xC4, 0xC4, 0xC4, 0xC4, 0xCF, 0xCF, 0xCF, 0xCF, # s06: LF
0xC5, 0x88, 0x88, 0x88, 0x88, 0xC5, 0x8D, 0xC5, 0xC5, 0xC5, 0x88, 0xC5, 0xCF, 0xCF, 0xCF, 0xCF, # s07: first token char
0xC5, 0x08, 0x08, 0x08, 0x08, 0xC5, 0xC5, 0xC5, 0x89, 0xC5, 0x08, 0xC5, 0xCF, 0xCF, 0xCF, 0xCF, # s08: header name
0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8C, 0xC6, 0x8B, 0x8B, 0x8B, 0x8A, 0xCF, 0xCF, 0xCF, 0xCF, # s09: first header char
0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8C, 0xC6, 0x8B, 0x8B, 0x8B, 0x0A, 0xCF, 0xCF, 0xCF, 0xCF, # s0a: 1st space
0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x8C, 0xC7, 0x0B, 0x0B, 0x0B, 0x0B, 0xCF, 0xCF, 0xCF, 0xCF, # s0b: header value
0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0x87, 0xC7, 0xC7, 0xC7, 0xC7, 0xCF, 0xCF, 0xCF, 0xCF, # s0c: header LF
0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0x8E, 0xC8, 0xC8, 0xC8, 0xC8, 0xCF, 0xCF, 0xCF, 0xCF # s0d: last LF
]
# Legend:
# [0x81, 0x8D] - markers
# 0x81 - start HTTP version
# 0x82 - end of HTTP version
# 0x83 - start of HTTP response code
# 0x84 - end of HTTP response code
# 0x85 - start of HTTP reason string
# 0x86 - end of HTTP reason string
# 0x87 - LF
# 0x88 - start of header name
# 0x89 - end of header name
# 0x8A - start of header value
# 0x8B - end of header value
# 0x8C - last header LF
# 0x8D - header's finish
# [0xC0, 0xCF] - errors
# * ALPHA NUM TO^UR TOON URON CR LF COLON SLASH DOT SPACE PAD PAD PAD PAD
responseSM = [
0xC0, 0x81, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xCF, 0xCF, 0xCF, 0xCF, # s00: first version char
0xC1, 0x01, 0x01, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0x01, 0x01, 0x82, 0xCF, 0xCF, 0xCF, 0xCF, # s01: version
0xC2, 0xC2, 0x83, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xCF, 0xCF, 0xCF, 0xCF, # s02: first code char
0xC2, 0xC2, 0x03, 0xC2, 0xC2, 0xC2, 0x84, 0xC2, 0xC2, 0xC2, 0xC2, 0x85, 0xCF, 0xCF, 0xCF, 0xCF, # s03: code
0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0x88, 0xC2, 0xC2, 0xC2, 0xC2, 0xCF, 0xCF, 0xCF, 0xCF, # s04: no reason LF
0xC3, 0x86, 0x86, 0x86, 0x86, 0x86, 0x87, 0xC3, 0x86, 0x86, 0x86, 0x86, 0xCF, 0xCF, 0xCF, 0xCF, # s05: first reason char
0xC3, 0x06, 0x06, 0x06, 0x06, 0x06, 0x87, 0xC3, 0x06, 0x06, 0x06, 0x06, 0xCF, 0xCF, 0xCF, 0xCF, # s06: reason
0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x88, 0xC3, 0xC3, 0xC3, 0xC3, 0xCF, 0xCF, 0xCF, 0xCF, # s07: LF
0xC3, 0x8A, 0x8A, 0x8A, 0x8A, 0xC3, 0x8F, 0xC3, 0xC3, 0xC3, 0x8A, 0xC4, 0xCF, 0xCF, 0xCF, 0xCF, # s08: no headers CR
0xC4, 0x8A, 0x8A, 0x8A, 0x8A, 0xC4, 0x8F, 0xC4, 0xC4, 0xC4, 0x8A, 0xC4, 0xCF, 0xCF, 0xCF, 0xCF, # s09: first token char
0xC4, 0x0A, 0x0A, 0x0A, 0x0A, 0xC4, 0xC4, 0xC4, 0x8B, 0xC4, 0x0A, 0xC4, 0xCF, 0xCF, 0xCF, 0xCF, # s0a: header name
0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8E, 0xC5, 0x8D, 0x8D, 0x8D, 0x8C, 0xCF, 0xCF, 0xCF, 0xCF, # s0b: first header char
0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0xC5, 0x8E, 0xC5, 0x8D, 0x8D, 0x8D, 0x0C, 0xCF, 0xCF, 0xCF, 0xCF, # s0c: 1st space
0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x8E, 0xC5, 0x0D, 0x0D, 0x0D, 0x0D, 0xCF, 0xCF, 0xCF, 0xCF, # s0d: header value
0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0x89, 0xC7, 0xC7, 0xC7, 0xC7, 0xCF, 0xCF, 0xCF, 0xCF, # s0e: header LF
0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0x9F, 0xC8, 0xC8, 0xC8, 0xC8, 0xCF, 0xCF, 0xCF, 0xCF, # s0f: last LF
]
# Legend:
# 0x81 - start of disposition type
# 0x82 - end of disposition type or end of token value.
# 0x83 - start of parameter name
# 0x84 - end of parameter name
# 0x85 - start of tokenized parameter value
# 0x86 - end of tokenized parameter value
# 0x87 - double quote
# 0x88 - start of quoted parameter value
# 0x89 - 0x8A - escaped quote
# 0x8B - end of quoted parameter value
# 0x8C - semicolon and spaces
# [0xC0 - 0xCC] error values
# * LTOK LSEP BSLA DQUO EQUA SEMC SPAC HOTA
contdispSM = [
0xC0, 0x81, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, # s0: start of disposition-type
0xC1, 0x01, 0xC1, 0xC1, 0xC1, 0xC1, 0x82, 0xC1, 0xC1, # s1: disposition-type
0xC2, 0x83, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0x02, 0x02, # s2: semicolon and spaces
0xC3, 0x03, 0xC1, 0xC1, 0xC1, 0x84, 0xC1, 0xC1, 0xC1, # s3: parm-name
0xC4, 0x85, 0xC4, 0xC4, 0x87, 0xC4, 0x8D, 0xC4, 0xC4, # s4: =
0xC5, 0x05, 0xC5, 0xC5, 0xC5, 0xC5, 0x86, 0xC5, 0xC5, # s5: parm-value as token start
0xC6, 0x83, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x06, 0x06, # s6: parm-value as token finish
0xC7, 0x88, 0x88, 0x88, 0x8B, 0x88, 0x88, 0x88, 0x88, # s7: double quote
0xC8, 0x08, 0x08, 0x89, 0x8B, 0x08, 0x08, 0x08, 0x08, # s8: quoted parm-value
0xC9, 0x08, 0x08, 0x08, 0x8A, 0x08, 0x08, 0x08, 0x08, # s9: middle double quote
0xCA, 0x08, 0x08, 0x89, 0x8B, 0x08, 0x08, 0x08, 0x08, # sA: quoted parm-value
0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0x8C, 0xCB, 0xCB, # sB: finish double quote
0xCC, 0x83, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x0C, 0x0C, # sC: semicolon and spaces
0xCD, 0x83, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0x0D, 0x0D, # sD: semicolon and spaces
]
# * CTL2 LTOK LSEP3 / \ ; " = SPACE
contentTypeSM = [
0xE00, 0xE00, 0x801, 0xE00, 0xE00, 0xE00, 0xE00, 0xE00, 0xE00, 0xE00, # s00: start of media-type
0xE01, 0xE01, 0x001, 0xE01, 0x802, 0xE01, 0xE01, 0xE01, 0xE01, 0xE01, # s01: media-type and forward slash
0xE02, 0xE02, 0x803, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, # s02: forward slash
0xE03, 0xE03, 0x003, 0xE03, 0xE03, 0xE03, 0x805, 0xE03, 0xE03, 0x804, # s03: media-subtype
0xE04, 0xE04, 0xE04, 0xE04, 0xE04, 0xE04, 0x005, 0xE04, 0xE04, 0x004, # s04: spaces
0xE05, 0xE05, 0x806, 0xE05, 0xE05, 0xE05, 0xE05, 0xE05, 0xE05, 0x005, # s05: semicolon and spaces
0xE06, 0xE06, 0x006, 0xE06, 0xE06, 0xE06, 0xE06, 0xE06, 0x807, 0xE06, # s06: param-name
0xE07, 0xE07, 0x808, 0xE07, 0xE07, 0xE07, 0x811, 0x80B, 0xE07, 0xE07, # s07: =
0xE08, 0xE08, 0x008, 0xE08, 0xE08, 0xE08, 0x80A, 0xE08, 0xE08, 0x809, # s08: parm-value-token
0xE09, 0xE09, 0x806, 0xE09, 0xE09, 0xE09, 0x805, 0xE09, 0xE09, 0x009, # s09: spaces after parm-value-token
0xE0A, 0xE0A, 0x806, 0xE0A, 0xE0A, 0xE0A, 0xE0A, 0xE0A, 0xE0A, 0x00A, # s0a: semicolon and spaces
0x80C, 0xE0B, 0x80C, 0x80C, 0x80C, 0x80D, 0x80C, 0x80F, 0x80C, 0x80C, # s0b: starting double quote
0x00C, 0xE0C, 0x00C, 0x00C, 0x00C, 0x80D, 0x00C, 0x80F, 0x00C, 0x00C, # s0c: parm-value-quote
0x80E, 0xE0D, 0x80E, 0x80E, 0x80E, 0x80E, 0x80E, 0x80E, 0x80E, 0x80E, # s0d: escaped character
0x00C, 0xE0E, 0x00C, 0x00C, 0x00C, 0x80D, 0x00C, 0x80F, 0x00C, 0x00C, # s0e: parm-value-quote
0xE0F, 0xE0F, 0xE0F, 0xE0F, 0xE0F, 0xE0F, 0x810, 0xE0F, 0xE0F, 0x00F, # s0f: spaces after parm-value-quote
0xE10, 0xE10, 0x806, 0xE10, 0xE10, 0xE10, 0xE10, 0xE10, 0xE10, 0x010, # s10: semicolon and spaces
0xE11, 0xE11, 0x806, 0xE11, 0xE11, 0xE11, 0xE11, 0xE11, 0xE11, 0x011 # s11: empty value, semicolon and spaces
]
# * LTOKN LSEP2 / \ , ; " = SPACE
acceptSM = [
0xE00, 0x801, 0xE00, 0xE00, 0xE00, 0x000, 0xE00, 0xE00, 0xE00, 0xE00, # s00: start of media-type
0xE01, 0x001, 0xE01, 0x802, 0xE01, 0xE01, 0xE01, 0xE01, 0xE01, 0xE01, # s01: media-type and forward slash
0xE02, 0x803, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, 0xE02, # s02: forward slash
0xE03, 0x003, 0xE03, 0xE03, 0xE03, 0x805, 0x806, 0xE03, 0xE03, 0x804, # s03: media-subtype
0xE04, 0xE04, 0xE04, 0xE04, 0xE04, 0x805, 0x806, 0xE04, 0xE04, 0x004, # s04: spaces
0xE05, 0x801, 0xE05, 0xE05, 0xE05, 0xE05, 0xE05, 0xE05, 0xE05, 0x005, # s05: comma and spaces
0xE06, 0x807, 0xE06, 0xE06, 0xE06, 0xE06, 0xE06, 0xE06, 0xE06, 0x006, # s06: semicolon and spaces
0xE07, 0x007, 0xE07, 0xE07, 0xE07, 0xE07, 0xE07, 0xE07, 0x808, 0xE07, # s07: param-name
0xE08, 0x80A, 0xE08, 0xE08, 0xE08, 0x809, 0x80D, 0x80E, 0xE08, 0xE08, # s08: =
0xE09, 0x801, 0xE09, 0xE09, 0xE09, 0xE09, 0xE09, 0xE09, 0xE09, 0x009, # s09: comma and spaces after <param_name>=,
0xE0A, 0x00A, 0xE0A, 0xE0A, 0xE0A, 0x80C, 0x80B, 0xE0A, 0xE0A, 0xE0A, # s0A: param value as token
0xE0B, 0x807, 0xE0B, 0xE0B, 0xE0B, 0xE0B, 0xE0B, 0xE0B, 0xE0B, 0x00B, # s0B: spaces after ;
0xE0C, 0x801, 0xE0C, 0xE0C, 0xE0C, 0xE0C, 0xE0C, 0xE0C, 0xE0C, 0x00C, # s0C: spaces after ,
0xE0D, 0x807, 0xE0D, 0xE0D, 0xE0D, 0xE0D, 0xE0D, 0xE0D, 0xE0D, 0x00D, # s0D: spaces after =;
0xE0E, 0x80F, 0x80F, 0x80F, 0x810, 0x80F, 0x80F, 0x812, 0x80F, 0x80F, # s0E: starting double quote
0xE0F, 0x00F, 0x00F, 0x00F, 0x810, 0x00F, 0x00F, 0x812, 0x00F, 0x00F, # s0F: quoted param-value
0xE10, 0x00F, 0x00F, 0x00F, 0x00F, 0x00F, 0x00F, 0x811, 0x00F, 0x00F, # s10: backslash
0xE11, 0x00F, 0x00F, 0x00F, 0x810, 0x00F, 0x00F, 0x812, 0x00F, 0x00F, # s11: double quote after backslash
0xE12, 0xE12, 0xE12, 0xE12, 0xE12, 0x814, 0x813, 0xE12, 0xE12, 0xE12, # s12: finish double quote
0xE13, 0x807, 0xE13, 0xE13, 0xE13, 0xE13, 0xE13, 0xE13, 0xE13, 0x013, # s13: [";] and spaces
0xE14, 0x801, 0xE14, 0xE14, 0xE14, 0xE14, 0xE14, 0xE14, 0xE14, 0x014 # s14: [",] and spaces
]
type
HttpCode* = enum
## HTTP error codes
Http100 = "100 Continue",
Http101 = "101 Switching Protocols",
Http200 = "200 OK",
Http201 = "201 Created",
Http202 = "202 Accepted",
Http203 = "203 Non-Authoritative Information",
Http204 = "204 No Content",
Http205 = "205 Reset Content",
Http206 = "206 Partial Content",
Http207 = "207 Multi-Status",
Http300 = "300 Multiple Choices",
Http301 = "301 Moved Permanently",
Http302 = "302 Found",
Http303 = "303 See Other",
Http304 = "304 Not Modified",
Http305 = "305 Use Proxy",
Http307 = "307 Temporary Redirect",
Http400 = "400 Bad Request",
Http401 = "401 Unauthorized",
Http403 = "403 Forbidden",
Http404 = "404 Not Found",
Http405 = "405 Method Not Allowed",
Http406 = "406 Not Acceptable",
Http407 = "407 Proxy Authentication Required",
Http408 = "408 Request Timeout",
Http409 = "409 Conflict",
Http410 = "410 Gone",
Http411 = "411 Length Required",
Http412 = "412 Precondition Failed",
Http413 = "413 Request Entity Too Large",
Http414 = "414 Request-URI Too Long",
Http415 = "415 Unsupported Media Type",
Http416 = "416 Requested Range Not Satisfiable",
Http417 = "417 Expectation Failed",
Http418 = "418 I'm a teapot",
Http421 = "421 Misdirected Request",
Http422 = "422 Unprocessable Entity",
Http426 = "426 Upgrade Required",
Http428 = "428 Precondition Required",
Http429 = "429 Too Many Requests",
Http431 = "431 Request Header Fields Too Large",
Http451 = "451 Unavailable For Legal Reasons",
Http500 = "500 Internal Server Error",
Http501 = "501 Not Implemented",
Http502 = "502 Bad Gateway",
Http503 = "503 Service Unavailable",
Http504 = "504 Gateway Timeout",
Http505 = "505 HTTP Version Not Supported"
HttpVersion* = enum
## HTTP version
HttpVersion09
HttpVersion11,
HttpVersion10,
HttpVersion20,
HttpVersionError
HttpMethod* = enum
## HTTP methods
MethodGet,
MethodPost,
MethodHead,
MethodPut,
MethodDelete,
MethodTrace,
MethodOptions,
MethodConnect,
MethodPatch,
MethodError
HttpStatus* = enum
## HTTP parser status type
Success, Failure
HttpHeaderPart* = object
## HTTP offset representation object
s*: int ## Start offset
e*: int ## End offset
HttpHeader* = object
## HTTP header representation object
name*: HttpHeaderPart ## Header name
value*: HttpHeaderPart ## Header value
HttpRequestHeader* = object
## HTTP request header
data: seq[byte] ## Data blob
meth*: HttpMethod ## HTTP request method
version*: HttpVersion ## HTTP version
status*: HttpStatus ## HTTP headers processing status
url: HttpHeaderPart
state*: int
hdrs: seq[HttpHeader]
length*: int ## HTTP headers length
HttpHeadersList* = object
## HTTP request headers list
data: seq[byte]
state*: int
status*: HttpStatus
hdrs: seq[HttpHeader]
length*: int
HttpResponseHeader* = object
## HTTP response header
data: seq[byte] ## Data blob
version*: HttpVersion ## HTTP version
code*: int ## HTTP result code
status*: HttpStatus ## HTTP headers processing status
rsn: HttpHeaderPart
state*: int
hdrs: seq[HttpHeader]
length*: int ## HTTP headers length
ContentDispositionHeader* = object
## `Content-Disposition` header
data: seq[byte]
state*: int
status*: HttpStatus
disptype: HttpHeaderPart
flds: seq[HttpHeader]
AcceptMediaType* = object
mtype*: HttpHeaderPart
stype*: HttpHeaderPart
params*: seq[HttpHeader]
AcceptHeader* = object
data: seq[byte]
state*: int
status*: HttpStatus
mediaTypes*: seq[AcceptMediaType]
MediaType* = object
media*: string
subtype*: string
AcceptMediaItem* = object
mediaType*: MediaType
qvalue*: float
params*: seq[tuple[name: string, value: string]]
AcceptInfo* = object
data*: seq[AcceptMediaItem]
ContentTypeData* = object
## `Content-Type` header data
state*: int
status*: HttpStatus
mediaType*: MediaType
params*: seq[tuple[name: string, value: string]]
HttpReqRespHeader* = HttpRequestHeader | HttpResponseHeader | HttpHeadersList
BChar* = byte | char
proc toString[T: BChar](data: openArray[T], start, stop: int): string
template processHeaders(sm: untyped, state: var int, ch: char): int =
let code =
case ch
of ALPHA:
1
of NUM:
2
of TOKENURI:
3
of TOKENONLY:
4
of URIONLY:
5
of CR:
6
of LF:
7
of COLON:
8
of SLASH:
9
of DOT:
10
of SPACE:
11
else:
0
let newstate = sm[(state shl 4) + code]
state = newstate and 0x0F
newstate
template processDisposition(sm: untyped, state: var int, ch: char): int =
let code =
case ch
of LTOKEN:
1
of LSEP:
2
of BSLASH:
3
of DQUOTE:
4
of EQUALS:
5
of SEMCOL:
6
of SPACEO:
7
of HOTAB:
8
else:
0
let newstate = sm[(state shl 3) + state + code]
state = newstate and 0x0F
newstate
template processAcceptHeader(sm: untyped, state: var int, ch: char): int =
let code =
case ch
of LTOKEN:
1
of LSEP2:
2
of FSLASH:
3
of BSLASH:
4
of COMMA:
5
of SEMCOL:
6
of DQUOTE:
7
of EQUALS:
8
of SPACE:
9
else:
0
let newstate = sm[(state shl 3) + (state shl 1) + code]
state = newstate and 0xFF
newstate
template processContentType(sm: untyped, state: var int, ch: char): int =
let code =
case ch
of CTL2:
1
of LTOKEN:
2
of LSEP3:
3
of FSLASH:
4
of BSLASH:
5
of SEMCOL:
6
of DQUOTE:
7
of EQUALS:
8
of SPACEO:
9
else:
0
let newstate = sm[(state shl 3) + (state shl 1) + code]
state = newstate and 0xFF
newstate
proc processMethod[T: BChar](data: openArray[T], s, e: int): HttpMethod =
let length = e - s + 1
case char(data[s])
of 'G':
if length == 3:
if char(data[s + 1]) == 'E' and char(data[s + 2]) == 'T':
return MethodGet
of 'H':
if length == 4:
if char(data[s + 1]) == 'E' and char(data[s + 2]) == 'A' and
char(data[s + 3]) == 'D':
return MethodHead
of 'P':
if length == 3:
if char(data[s + 1]) == 'U' and char(data[s + 2]) == 'T':
return MethodPut
elif length == 4:
if char(data[s + 1]) == 'O' and char(data[s + 2]) == 'S' and
char(data[s + 3]) == 'T':
return MethodPost
elif length == 5:
if char(data[s + 1]) == 'A' and char(data[s + 2]) == 'T' and
char(data[s + 3]) == 'C' and char(data[s + 4]) == 'H':
return MethodPatch
of 'D':
if length == 6:
if char(data[s + 1]) == 'E' and char(data[s + 2]) == 'L' and
char(data[s + 3]) == 'E' and char(data[s + 4]) == 'T' and
char(data[s + 5]) == 'E':
return MethodDelete
of 'T':
if length == 5:
if char(data[s + 1]) == 'R' and char(data[s + 2]) == 'A' and
char(data[s + 3]) == 'C' and char(data[s + 4]) == 'E':
return MethodTrace
of 'O':
if length == 7:
if char(data[s + 1]) == 'P' and char(data[s + 2]) == 'T' and
char(data[s + 3]) == 'I' and char(data[s + 4]) == 'O' and
char(data[s + 5]) == 'N' and char(data[s + 6]) == 'S':
return MethodOptions
of 'C':
if length == 7:
if char(data[s + 1]) == 'O' and char(data[s + 2]) == 'N' and
char(data[s + 3]) == 'N' and char(data[s + 4]) == 'E' and
char(data[s + 5]) == 'C' and char(data[s + 6]) == 'T':
return MethodConnect
else:
discard
return HttpMethod.MethodError
proc processVersion[T: BChar](data: openArray[T], s, e: int): HttpVersion =
let length = e - s + 1
if length == 8:
if char(data[s]) == 'H' and char(data[s + 1]) == 'T' and
char(data[s + 2]) == 'T' and char(data[s + 3]) == 'P' and
char(data[s + 4]) == '/':
if char(data[s + 5]) == '1' and char(data[s + 6]) == '.':
if char(data[s + 7]) == '0':
return HttpVersion10
elif char(data[s + 7]) == '1':
return HttpVersion11
elif char(data[s + 5]) == '0' and char(data[s + 6]) == '.':
if char(data[s + 7]) == '9':
return HttpVersion09
elif char(data[s + 5]) == '2' and char(data[s + 6]) == '.':
if char(data[s + 7]) == '0':
return HttpVersion20
return HttpVersionError
proc processCode[T: BChar](data: openArray[T], s, e: int): int =
var res = -1
let length = e - s + 1
if length == 3:
res = (ord(data[s]) - ord('0')) * 100 +
(ord(data[s + 1]) - ord('0')) * 10 +
ord(data[s + 2]) - ord('0')
res
proc parseRequest*[T: BChar](data: openArray[T],
makeCopy: bool): HttpRequestHeader =
## Parse sequence of characters or bytes ``data`` as HTTP request header.
##
## If `makeCopy` flag is ``true``, procedure will create a copy of ``data``
## in result.
##
## Returns `HttpRequestHeader` instance.
var
index = 0
state = 0
start = -1
finish = 0
hdr: HttpHeader
var res = HttpRequestHeader(
status: HttpStatus.Failure,
version: HttpVersionError,
meth: MethodError,
hdrs: newSeq[HttpHeader]()
)
if len(data) == 0:
return res
if makeCopy:
# Make copy of ``data`` sequence in our result object.
res.data = newSeq[byte](len(data))
copyMem(addr res.data[0], unsafeAddr data[0], len(data))
while index < len(data):
let ps = requestSM.processHeaders(state, char(data[index]))
res.state = ps
case ps
of 0x81:
start = index
of 0x82:
if start == -1:
break
finish = index - 1
let m = processMethod(data, start, finish)
if m == HttpMethod.MethodError:
break
res.meth = m
start = -1
of 0x83:
start = index
of 0x84:
if start == -1:
break
finish = index - 1
res.url = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x85:
start = index
of 0x86:
if start == -1:
break
finish = index - 1
let m = processVersion(data, start, finish)
if m == HttpVersion.HttpVersionError:
break
res.version = m
start = -1
of 0x87, 0x8A, 0x8D:
discard
of 0x88:
start = index
of 0x89:
if start == -1:
break
finish = index - 1
hdr.name = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x8B:
start = index
of 0x8C:
if start == -1:
# empty header
hdr.value = HttpHeaderPart(s: -1, e: -1)
else:
finish = index - 1
hdr.value = HttpHeaderPart(s: start, e: finish)
res.hdrs.add(hdr)
start = -1
of 0x8E:
res.length = index + 1
res.status = HttpStatus.Success
break
of 0xC0..0xCF:
# error
break
of 0x00..0x0F:
# data processing
discard
else:
# must not be happened
break
inc(index)
res
proc parseRequest*[T: BChar](data: sink seq[T]): HttpRequestHeader =
## Parse sequence of characters or bytes as HTTP request header.
##
## Note: to prevent unnecessary allocations source array ``data`` will be
## be shallow copied to result and all parsed fields will have references to
## this buffer. If you plan to change contents of ``data`` while parsing
## request and/or processing headers, please make a real copy of ``data`` and
## pass copy to ``parseRequest(data)``.
##
## Returns `HttpRequestHeader` instance.
var res = parseRequest(data, false)
when declared(shallowCopy):
shallowCopy(res.data, cast[seq[byte]](data))
else:
res.data = cast[seq[byte]](data)
res
proc parseHeaders*[T: BChar](data: openArray[T],
makeCopy: bool): HttpHeadersList =
## Parse sequence of characters or bytes ``data`` as HTTP headers list.
##
## If `makeCopy` flag is ``true``, procedure will create a copy of ``data``
## in result.
##
## Returns `HttpHeadersList` instance.
var
index = 0
state = 8
start = 0
finish = 0
hdr: HttpHeader
var res = HttpHeadersList(
status: HttpStatus.Failure,
hdrs: newSeq[HttpHeader]()
)
if len(data) == 0:
return res
if makeCopy:
# Make copy of ``data`` sequence in our result object.
res.data = newSeq[byte](len(data))
copyMem(addr res.data[0], unsafeAddr data[0], len(data))
while index < len(data):
let ps = requestSM.processHeaders(state, char(data[index]))
res.state = ps
case ps
of 0x87, 0x8A, 0x8D:
discard
of 0x88:
start = index
of 0x89:
if start == -1:
break
finish = index - 1
hdr.name = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x8B:
start = index
of 0x8C:
if start == -1:
# empty header
hdr.value = HttpHeaderPart(s: -1, e: -1)
else:
finish = index - 1
hdr.value = HttpHeaderPart(s: start, e: finish)
res.hdrs.add(hdr)
start = -1
of 0x8E:
res.length = index + 1
res.status = HttpStatus.Success
break
of 0xC0..0xCF:
# error
break
of 0x00..0x0F:
# data processing
discard
else:
# must not be happened
break
inc(index)
res
proc parseHeaders*[T: BChar](data: sink seq[T]): HttpHeadersList =
## Parse sequence of characters or bytes as HTTP headers list.
##
## Note: to prevent unnecessary allocations source array ``data`` will be
## be shallow copied to result and all parsed fields will have references to
## this buffer. If you plan to change contents of ``data`` while parsing
## request and/or processing headers, please make a real copy of ``data`` and
## pass copy to ``parseHeaders(data)``.
##
## Returns `HttpHeadersList` instance.
var res = parseHeaders(data, false)
when declared(shallowCopy):
shallowCopy(res.data, cast[seq[byte]](data))
else:
res.data = cast[seq[byte]](data)
res
proc parseResponse*[T: BChar](data: openArray[T],
makeCopy: bool): HttpResponseHeader =
## Parse sequence of characters or bytes as HTTP response header.
##
## If `makeCopy` flag is ``true``, procedure will create a copy of ``data``
## in result.
##
## Returns `HttpResponseHeader` instance.
var
index = 0
state = 0
start = -1
finish = 0
hdr: HttpHeader
var res = HttpResponseHeader(
status: HttpStatus.Failure,
version: HttpVersionError,
code: -1,
hdrs: newSeq[HttpHeader]()
)
if len(data) == 0:
return res
if makeCopy:
# Make copy of ``data`` sequence in our result object.
res.data = newSeq[byte](len(data))
copyMem(addr res.data[0], unsafeAddr data[0], len(data))
while index < len(data):
let ps = responseSM.processHeaders(state, char(data[index]))
res.state = ps
case ps
of 0x81:
start = index
of 0x82:
if start == -1:
break
finish = index - 1
let m = processVersion(data, start, finish)
if m == HttpVersion.HttpVersionError:
break
res.version = m
start = -1
of 0x83:
start = index
of 0x84, 0x85:
if start == -1:
break
finish = index - 1
let m = processCode(data, start, finish)
if m == -1:
break
res.code = m
if ps == 0x84:
res.rsn = HttpHeaderPart(s: -1, e: -1)
start = -1
of 0x86:
start = index
of 0x87:
if start == -1:
res.rsn = HttpHeaderPart(s: -1, e: -1)
else:
finish = index - 1
res.rsn = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x88, 0x89, 0x8C, 0x8F:
discard
of 0x8A:
start = index
of 0x8B:
if start == -1:
break
finish = index - 1
hdr.name = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x8D:
start = index
of 0x8E:
if start == -1:
# empty header
hdr.value = HttpHeaderPart(s: -1, e: -1)
else:
finish = index - 1
hdr.value = HttpHeaderPart(s: start, e: finish)
res.hdrs.add(hdr)
start = -1
of 0x9F:
res.length = index + 1
res.status = HttpStatus.Success
break
of 0xC0..0xCF:
# error
break
of 0x00..0x0F:
# data processing
discard
else:
# must not be happened
break
inc(index)
res
proc parseResponse*[T: BChar](data: sink seq[T]): HttpResponseHeader =
## Parse sequence of characters or bytes as HTTP response header.
##
## Note: to prevent unnecessary allocations source array ``data`` will be
## be shallow copied to result and all parsed fields will have references to
## this buffer. If you plan to change contents of ``data`` while parsing
## request and/or processing headers, please make a real copy of ``data`` and
## pass copy to ``parseResponse(data)``.
##
## Returns `HttpResponseHeader` instance.
var res = parseResponse(data, false)
when declared(shallowCopy):
shallowCopy(res.data, cast[seq[byte]](data))
else:
res.data = cast[seq[byte]](data)
res
proc parseDisposition*[T: BChar](data: openArray[T],
makeCopy: bool): ContentDispositionHeader =
## Parse sequence of characters or bytes of HTTP ``Content-Disposition``
## header according to RFC6266.
##
## TODO: Support extended `<fieldname>*` values.
##
## If `makeCopy` flag is ``true``, procedure will create a copy of ``data``
## in result.
##
## Returns `ContentDispositionHeader` instance.
var
index = 0
state = 0
start = -1
finish = 0
hdr: HttpHeader
var res = ContentDispositionHeader(
status: HttpStatus.Failure,
disptype: HttpHeaderPart(),
flds: newSeq[HttpHeader]()
)
if len(data) == 0:
return res
if makeCopy:
# Make copy of ``data`` sequence in our result object.
res.data = newSeq[byte](len(data))
copyMem(addr res.data[0], unsafeAddr data[0], len(data))
while index < len(data):
let ps = contdispSM.processDisposition(state, char(data[index]))
res.state = ps
case ps
of 0x81:
start = index
of 0x82:
if start == -1:
break
finish = index - 1
res.disptype = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x83:
start = index
of 0x84:
if start == -1:
break
finish = index - 1
hdr.name = HttpHeaderPart(s: start, e: finish)
start = -1
of 0x85:
start = index
of 0x86:
if start == -1:
break
finish = index - 1
hdr.value = HttpHeaderPart(s: start, e: finish)
res.flds.add(hdr)
start = -1
of 0x87:
start = index + 1
of 0x88:
start = index
of 0x89, 0x8A:
discard
of 0x8B:
if start == -1:
break
finish = index - 1
hdr.value = HttpHeaderPart(s: start, e: finish)
res.flds.add(hdr)
of 0x8C:
discard
of 0x8D:
hdr.value = HttpHeaderPart(s: index, e: index - 1)
res.flds.add(hdr)
of 0x00..0x0D:
discard
of 0xC0..0xCD:
break
else:
break
inc(index)
res.status =
case res.state
of 0x01, 0x81:
if start == -1:
HttpStatus.Failure
else:
res.disptype = HttpHeaderPart(s: start, e: index - 1)
HttpStatus.Success
of 0x84:
hdr.value = HttpHeaderPart(s: index, e: index - 1)
res.flds.add(hdr)
HttpStatus.Success
of 0x05, 0x85:
if start == -1:
HttpStatus.Failure
else:
hdr.value = HttpHeaderPart(s: start, e: index - 1)
res.flds.add(hdr)
HttpStatus.Success
of 0x0B, 0x8B:
HttpStatus.Success
else:
HttpStatus.Failure
if res.status == HttpStatus.Success:
res
else:
ContentDispositionHeader(status: HttpStatus.Failure)
proc parseDisposition*[T: BChar](data: sink seq[T]): ContentDispositionHeader =