summaryrefslogtreecommitdiff
path: root/fhp.c
blob: 44c20e486b93171622b54d30035f355ccb099641 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
#include <string.h>
#include "fhp/fhp.h"

#define UNUSED(a) ((void) (a))

#define CASE_ALNUM_CHARS \
  case '0': \
  case '1': \
  case '2': \
  case '3': \
  case '4': \
  case '5': \
  case '6': \
  case '7': \
  case '8': \
  case '9': \
  case 'a': \
  case 'b': \
  case 'c': \
  case 'd': \
  case 'e': \
  case 'f': \
  case 'g': \
  case 'h': \
  case 'i': \
  case 'j': \
  case 'k': \
  case 'l': \
  case 'm': \
  case 'n': \
  case 'o': \
  case 'p': \
  case 'q': \
  case 'r': \
  case 's': \
  case 't': \
  case 'u': \
  case 'v': \
  case 'w': \
  case 'x': \
  case 'y': \
  case 'z': \
  case 'A': \
  case 'B': \
  case 'C': \
  case 'D': \
  case 'E': \
  case 'F': \
  case 'G': \
  case 'H': \
  case 'I': \
  case 'J': \
  case 'K': \
  case 'L': \
  case 'M': \
  case 'N': \
  case 'O': \
  case 'P': \
  case 'Q': \
  case 'R': \
  case 'S': \
  case 'T': \
  case 'U': \
  case 'V': \
  case 'W': \
  case 'X': \
  case 'Y': \
  case 'Z':

#define CASE_DIGIT_CHARS \
  case '0': \
  case '1': \
  case '2': \
  case '3': \
  case '4': \
  case '5': \
  case '6': \
  case '7': \
  case '8': \
  case '9':

#define CASE_HEX_ALPHA_CHARS \
  case 'a': \
  case 'b': \
  case 'c': \
  case 'd': \
  case 'e': \
  case 'f': \
  case 'A': \
  case 'B': \
  case 'C': \
  case 'D': \
  case 'E': \
  case 'F':

//
// rfc7230, Appendix B
// https://tools.ietf.org/html/rfc7230
//
//   tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
//           "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
//   token = 1*tchar
//
#define CASE_TOKEN_CHARS \
  CASE_ALNUM_CHARS \
  case '!': \
  case '#': \
  case '$': \
  case '%': \
  case '&': \
  case '\'': \
  case '*': \
  case '+': \
  case '-': \
  case '.': \
  case '^': \
  case '_': \
  case '|': \
  case '~': \

#define CASE_URL_CHARS \
  CASE_ALNUM_CHARS \
  case '!': \
  case '"': \
  case '#': \
  case '$': \
  case '&': \
  case '\'': \
  case '(': \
  case ')': \
  case '*': \
  case '+': \
  case ',': \
  case '-': \
  case '.': \
  case '/': \
  case ':': \
  case ';': \
  case '<': \
  case '=': \
  case '>': \
  case '?': \
  case '@': \
  case '[': \
  case '\\': \
  case ']': \
  case '^': \
  case '_': \
  case '`': \
  case '{': \
  case '|': \
  case '}': \
  case '~':

#define CASE_VISUAL_CHARS \
  CASE_URL_CHARS \
  case '%':

#define CASE_VERSION_CHARS \
  CASE_TOKEN_CHARS \
  case '/':

//
// rfc7230, Appendix B
// https://tools.ietf.org/html/rfc7230
//
//   OWS = *( SP / HTAB )
//
#define CASE_OWS_CHARS \
  case ' ': \
  case '\t':

//
// hash functions (djb2)
// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/)
//

uint32_t
fhp_hash_init(void) {
  return 5381;
}

uint32_t
fhp_hash_push(uint32_t hash, uint8_t * const buf, size_t len) {
  for (size_t i = 0; i < len; i++)
    hash = ((hash << 5) + hash) + buf[len];

  return hash;
}

uint32_t
fhp_hash_string(char * const str) {
  uint32_t r = fhp_hash_init();
  return fhp_hash_push(r, (uint8_t*) str, strlen(str));
}

uint32_t
fhp_lc_hash_push(
  uint32_t hash,
  uint8_t * const buf,
  size_t len
) {
  for (size_t i = 0; i < len; i++) {
    uint8_t c = buf[len];

    if (c >= 'A' && c <= 'Z')
      c = (c - 'A') + 'a';

    hash = ((hash << 5) + hash) + c;
  }

  return hash;
}

uint32_t
fhp_lc_hash_string(char * const str) {
  uint32_t r = fhp_hash_init();
  return fhp_lc_hash_push(r, (uint8_t*) str, strlen(str));
}

//
// error functions
//

static const char *
fhp_errors[] = {
  "OK",
  "callback error",
  "bad state",
  "invalid character",
  "invalid character in HTTP method",
  "invalid character in HTTP URL",
  "invalid percent-encoded character in HTTP URL",
  "invalid character in HTTP version",
  "invalid character after carriage return",
  "invalid character in HTTP header name",
  "invalid error code",
  "invalid body type",
  "buffer too small",
};

fhp_err_t
fhp_strerror(
  fhp_err_t err,
  char * const buf,
  size_t len
) {
  // check error code
  if (err >= FHP_ERR_LAST)
    return FHP_ERR_INVALID_ERROR;

  // check buffer size
  size_t err_len = strlen(fhp_errors[err]) + 1;
  if (len < err_len)
    return FHP_ERR_BUFFER_TOO_SMALL;

  // copy string
  memcpy(buf, fhp_errors[err], err_len);

  // return success
  return FHP_OK;
}

//
// token functions
//

static const char *
fhp_tokens[] = {
  "METHOD_START",
  "METHOD_FRAGMENT",
  "METHOD_END",

  "METHOD_GET",
  "METHOD_POST",
  "METHOD_HEAD",
  "METHOD_PUT",
  "METHOD_DELETE",
  "METHOD_OPTIONS",
  "METHOD_OTHER",

  "URL_START",
  "URL_FRAGMENT",
  "URL_END",

  "VERSION_START",
  "VERSION_FRAGMENT",
  "VERSION_END",

  "VERSION_HTTP_10",
  "VERSION_HTTP_11",
  "VERSION_OTHER",

  "HEADER_NAME_START",
  "HEADER_NAME_FRAGMENT",
  "HEADER_NAME_END",

  "HEADER_VALUE_START",
  "HEADER_VALUE_FRAGMENT",
  "HEADER_VALUE_END",

  "LAST"
};

fhp_err_t
fhp_strtoken(
  fhp_token_t token,
  char * const buf,
  size_t len
) {
  // check token code
  if (token >= FHP_TOKEN_LAST)
    return FHP_ERR_INVALID_ERROR;

  // check buffer size
  size_t str_len = strlen(fhp_tokens[token]) + 1;
  if (len < str_len)
    return FHP_ERR_BUFFER_TOO_SMALL;

  // copy string
  memcpy(buf, fhp_tokens[token], str_len);

  // return success
  return FHP_OK;
}

//
// string functions
//

static char * const
fhp_strings[] = {
  "GET",
  "POST",
  "HEAD",
  "PUT",
  "DELETE",
  "OPTIONS",
  "HTTP/1.0",
  "HTTP/1.1",
  "content-length",
  "transfer-encoding",
  "gzip",
  "x-gzip",
  "deflate",
  "x-deflate",
  "compress",
  "x-compress",
  "chunked",
  NULL
};

typedef enum {
  FHP_STR_GET,
  FHP_STR_POST,
  FHP_STR_HEAD,
  FHP_STR_PUT,
  FHP_STR_DELETE,
  FHP_STR_OPTIONS,
  FHP_STR_HTTP_10,
  FHP_STR_HTTP_11,
  FHP_STR_CONTENT_LENGTH,
  FHP_STR_TRANSFER_ENCODING,
  FHP_STR_GZIP,
  FHP_STR_X_GZIP,
  FHP_STR_DEFLATE,
  FHP_STR_X_DEFLATE,
  FHP_STR_COMPRESS,
  FHP_STR_X_COMPRESS,
  FHP_STR_CHUNKED,
  FHP_STR_LAST
} fhp_str_t;

void
fhp_env_init(fhp_env_t * const env) {
  for (size_t i = 0; i < FHP_STR_LAST; i++)
    env->hashes[i] = fhp_lc_hash_string(fhp_strings[i]);
}

static fhp_env_t fhp_default_env;

fhp_env_t *
fhp_get_default_env(void) {
  static fhp_env_t *r = NULL;

  if (!r) {
    r = &fhp_default_env;
    fhp_env_init(r);
  }

  return r;
}

//
// context functions
//

static const fhp_t DEFAULT_CONTEXT = {
  .state = FHP_STATE_INIT,
  .user_data = NULL,
  .cb = NULL,
  .err = FHP_OK,
  .ofs = 0,
  .buf_len = 0,
  .is_hashing = false,
  .header_name_hash = 0,
  .body_type = FHP_BODY_TYPE_NONE,
  .content_length = 0,
  .num_tes = 0,
};

fhp_err_t
fhp_init(
  fhp_t * const fhp,
  fhp_env_t * const env,
  fhp_cb_t cb,
  void * const user_data
) {
  *fhp = DEFAULT_CONTEXT;
  fhp->env = env ? env : fhp_get_default_env();
  fhp->user_data = user_data;
  fhp->cb = cb;

  /* return success */
  return FHP_OK;
}

static void
fhp_buf_clear(fhp_t * const fhp) {
  // clear buffer
  fhp->buf_len = 0;
}

static bool
fhp_buf_flush(
  fhp_t * const fhp,
  fhp_token_t token
) {
  if (fhp->buf_len > 0) {
    // push data
    if (!fhp->cb(fhp, token, fhp->buf, fhp->buf_len))
      return false;

    // update buffer hash
    if (fhp->is_hashing)
      fhp->buf_hash = fhp_hash_push(fhp->buf_hash, fhp->buf, fhp->buf_len);

    // clear buffer
    fhp_buf_clear(fhp);
  }

  // return success
  return true;
}

static bool
fhp_buf_push(
  fhp_t * const fhp,
  fhp_token_t token,
  uint8_t byte
) {
  // flush buffer
  if (fhp->buf_len + 1 >= FHP_MAX_BUF_SIZE) {
    if (!fhp_buf_flush(fhp, token))
      return false;
  }

  // append to buffer
  fhp->buf[fhp->buf_len] = byte;
  fhp->buf_len++;

  // return success
  return true;
}

static fhp_err_t
fhp_handle_method(fhp_t * const fhp) {
  // get method token
  fhp->http_method = FHP_TOKEN_METHOD_OTHER;
  if (fhp->buf_hash == fhp->env->hashes[FHP_STR_GET]) {
    fhp->http_method = FHP_TOKEN_METHOD_GET;
  } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_POST]) {
    fhp->http_method = FHP_TOKEN_METHOD_POST;
  } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HEAD]) {
    fhp->http_method = FHP_TOKEN_METHOD_HEAD;
  } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_PUT]) {
    fhp->http_method = FHP_TOKEN_METHOD_PUT;
  } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_DELETE]) {
    fhp->http_method = FHP_TOKEN_METHOD_DELETE;
  } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_OPTIONS]) {
    fhp->http_method = FHP_TOKEN_METHOD_OPTIONS;
  }

  // send method token
  if (!fhp->cb(fhp, fhp->http_method, 0, 0))
    return FHP_ERR_CB;

  // return success
  return FHP_OK;
}

// FIXME: follow RFC7230 3.3.3
// https://tools.ietf.org/html/rfc7230#section-3.3.3
static fhp_err_t
fhp_handle_header_name(fhp_t * const fhp) {
  uint32_t hash = fhp->header_name_hash;

  if (hash == fhp->env->hashes[FHP_STR_CONTENT_LENGTH]) {
    switch (fhp->body_type) {
    case FHP_BODY_TYPE_CONTENT_LENGTH:
      // duplicate content-length, ignore first one
      // FIXME: should print warning here
    case FHP_BODY_TYPE_NONE:
      fhp->body_type = FHP_BODY_TYPE_CONTENT_LENGTH;
      break;
    case FHP_BODY_TYPE_TRANSFER_ENCODING:
      // ignore content-length
      // FIXME: should print warnings
      break;
    default:
      // never reached
      return FHP_ERR_INVALID_BODY_TYPE;
    }
  } else if (hash == fhp->env->hashes[FHP_STR_TRANSFER_ENCODING]) {
    switch (fhp->body_type) {
    case FHP_BODY_TYPE_CONTENT_LENGTH:
      // duplicate content-length, ignore first one
      // FIXME: should print warning here
    case FHP_BODY_TYPE_NONE:
      fhp->body_type = FHP_BODY_TYPE_TRANSFER_ENCODING;
      break;
    case FHP_BODY_TYPE_TRANSFER_ENCODING:
      // ignore content-length
      // FIXME: should print warnings
      break;
    default:
      // never reached
      return FHP_ERR_INVALID_BODY_TYPE;
    }
  }

  // return success
  return FHP_OK;
}


static fhp_err_t
fhp_push_byte(
  fhp_t * const fhp,
  uint8_t byte
) {
retry:
  switch (fhp->state) {
  case FHP_STATE_INIT:
    switch (byte) {
    CASE_TOKEN_CHARS
      // send start token
      if (!fhp->cb(fhp, FHP_TOKEN_METHOD_START, 0, 0))
        return FHP_ERR_CB;

      // enable buffer hashing
      fhp->is_hashing = true;
      fhp->buf_hash = fhp_hash_init();

      // set state
      fhp->state = FHP_STATE_METHOD;
      goto retry;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_METHOD;
    }

    break;
  case FHP_STATE_METHOD:
    switch (byte) {
    CASE_TOKEN_CHARS
      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_METHOD_FRAGMENT, byte))
        return FHP_ERR_CB;

      break;
    case ' ':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_METHOD_FRAGMENT))
        return FHP_ERR_CB;

      // disable buffer hashing
      fhp->is_hashing = false;

      // send end token
      if (!fhp->cb(fhp, FHP_TOKEN_METHOD_END, 0, 0))
        return FHP_ERR_CB;

      // handle method
      fhp_err_t err = fhp_handle_method(fhp);
      if (err != FHP_OK)
        return err;

      // set state
      fhp->state = FHP_STATE_METHOD_END;
      goto retry;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_METHOD;
    }

    break;
  case FHP_STATE_METHOD_END:
    switch (byte) {
    case ' ':
      // FIXME: do we want to allow more than one whitespace?
      // ignore
      break;
    CASE_URL_CHARS
      if (!fhp->cb(fhp, FHP_TOKEN_URL_START, 0, 0))
        return FHP_ERR_CB;

      fhp->state = FHP_STATE_URL;
      goto retry;
    default:
      return FHP_ERR_INVALID_CHAR_IN_URL;
    }

    break;
  case FHP_STATE_URL:
    switch (byte) {
    case ' ':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_URL_FRAGMENT))
        return FHP_ERR_CB;

      // send end token
      if (!fhp->cb(fhp, FHP_TOKEN_URL_END, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_URL_END;
      goto retry;

      break;
    case '%':
      // set state
      fhp->state = FHP_STATE_URL_PERCENT;

      break;
    CASE_URL_CHARS
      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_URL_FRAGMENT, byte))
        return FHP_ERR_CB;
    }

    break;
  case FHP_STATE_URL_PERCENT:
    switch (byte) {
    CASE_DIGIT_CHARS
      fhp->hex = byte - '0';
      fhp->state = FHP_STATE_URL_PERCENT_LAST;

      break;
    CASE_HEX_ALPHA_CHARS
      fhp->hex = 10 + byte - ((byte >= 'a') ? 'a' : 'A');
      fhp->state = FHP_STATE_URL_PERCENT_LAST;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_URL_PERCENT;
    }

    break;
  case FHP_STATE_URL_PERCENT_LAST:
    switch (byte) {
    CASE_DIGIT_CHARS
      fhp->hex = (fhp->hex << 4) + (byte - '0');
      break;
    CASE_HEX_ALPHA_CHARS
      fhp->hex = (fhp->hex << 4) + (10 + byte - ((byte >= 'a') ? 'a' : 'A'));
      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_URL_PERCENT;
    }

    // add to buffer
    if (!fhp_buf_push(fhp, FHP_TOKEN_URL_FRAGMENT, fhp->hex))
      return FHP_ERR_CB;

    // set state
    fhp->state = FHP_STATE_URL;

    break;
  case FHP_STATE_URL_END:
    switch (byte) {
    case ' ':
      // ignore
      break;
    default:
      if (!fhp->cb(fhp, FHP_TOKEN_VERSION_START, 0, 0))
        return FHP_ERR_CB;

      // enable buffer hashing
      fhp->is_hashing = true;
      fhp->buf_hash = fhp_hash_init();

      // set state
      fhp->state = FHP_STATE_VERSION;
      goto retry;
    }

    break;
  case FHP_STATE_VERSION:
    switch (byte) {
    case '\r':
    case '\n':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_VERSION_FRAGMENT))
        return FHP_ERR_CB;

      // send end token
      if (!fhp->cb(fhp, FHP_TOKEN_VERSION_END, 0, 0))
        return FHP_ERR_CB;

      // disable buffer hashing
      fhp->is_hashing = false;

      // get version token
      fhp->http_version = FHP_TOKEN_VERSION_OTHER;
      if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HTTP_10]) {
        fhp->http_version = FHP_TOKEN_VERSION_HTTP_10;
      } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HTTP_11]) {
        fhp->http_version = FHP_TOKEN_VERSION_HTTP_11;
      }

      // send version token
      if (!fhp->cb(fhp, fhp->http_version, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_VERSION_END;
      goto retry;

      break;
    CASE_VERSION_CHARS
      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_VERSION_FRAGMENT, byte))
        return FHP_ERR_CB;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_VERSION;
    }

    break;
  case FHP_STATE_VERSION_END:
    switch (byte) {
    case '\r':
      fhp->state = FHP_STATE_VERSION_END_CR;

      break;
    case '\n':
      fhp->state = FHP_STATE_STATUS_END;

      break;
    default:
      // invalid character
      // (should never be reached)
      return FHP_ERR_INVALID_CHAR;
    };

    break;
  case FHP_STATE_VERSION_END_CR:
    switch (byte) {
    case '\n':
      fhp->state = FHP_STATE_STATUS_END;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_AFTER_CR;
    };

    break;
  case FHP_STATE_STATUS_END:
    switch (byte) {
    CASE_TOKEN_CHARS
      // send start token
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_START, 0, 0))
        return FHP_ERR_CB;

      // enable buffer hashing
      fhp->is_hashing = true;
      fhp->buf_hash = fhp_hash_init();

      // set state
      fhp->state = FHP_STATE_HEADER_NAME;
      goto retry;

      break;
    case '\r':
      // set state
      fhp->state = FHP_STATE_STATUS_END_CR;

      break;
    case '\n':
      // set state
      fhp->state = FHP_STATE_HEADERS_END;

      break;
    }

    break;
  case FHP_STATE_STATUS_END_CR:
    switch (byte) {
    case '\n':
      // set state
      fhp->state = FHP_STATE_HEADERS_END;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_AFTER_CR;
    }

    break;
  case FHP_STATE_HEADER_NAME:
    switch (byte) {
    CASE_TOKEN_CHARS
      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte))
        return FHP_ERR_CB;

      break;
    case ':':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT))
        return FHP_ERR_CB;

      // disable buffer hashing and cache header name hash
      fhp->is_hashing = false;
      fhp->header_name_hash = fhp->buf_hash;

      // handle header name
      fhp_err_t err = fhp_handle_header_name(fhp);
      if (err != FHP_OK)
        return err;

      // send end token
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_END, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADER_NAME_END;

      break;
    default:
      return FHP_ERR_INVALID_CHAR_IN_HEADER_NAME;
    }

    break;
  case FHP_STATE_HEADER_NAME_END:
    switch (byte) {
    CASE_OWS_CHARS
      // ignore leading spaces

      break;
    default:
      // send start token
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_START, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADER_VALUE;
      goto retry;

      break;
    }

    break;
  case FHP_STATE_HEADER_VALUE:
    switch (byte) {
    case '\r':
      fhp->state = FHP_STATE_HEADER_VALUE_END_CR;

      break;
    case '\n':
      fhp->state = FHP_STATE_HEADER_VALUE_END;
      break;
    default:
      // FIXME: need more limits on valid octets

      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT, byte))
        return FHP_ERR_CB;

      break;
    }

    break;
  case FHP_STATE_HEADER_VALUE_END_CR:
    switch (byte) {
    case '\n':
      fhp->state = FHP_STATE_HEADER_VALUE_END;
      break;
    default:
      return FHP_ERR_INVALID_CHAR;
    }

    break;
  case FHP_STATE_HEADER_VALUE_END:
    switch (byte) {
    CASE_OWS_CHARS
      // ows-fold

      // add space to buffer
      // folding to ' ', as per RFC7230 3.2.4
      // https://tools.ietf.org/html/rfc7230#section-3.2.4
      if (!fhp_buf_push(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT, ' '))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADER_VALUE;

      break;
    CASE_TOKEN_CHARS
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT))
        return FHP_ERR_CB;

      // end header value
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0))
        return FHP_ERR_CB;

      // send start token
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_START, 0, 0))
        return FHP_ERR_CB;

      // add to buffer
      if (!fhp_buf_push(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADER_NAME;

      break;
    case '\r':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT))
        return FHP_ERR_CB;

      // end header value
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADERS_END_CR;

      break;
    case '\n':
      // flush buffer
      if (!fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT))
        return FHP_ERR_CB;

      // end header value
      if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0))
        return FHP_ERR_CB;

      // set state
      fhp->state = FHP_STATE_HEADERS_END;

      break;
    default:
      return FHP_ERR_INVALID_CHAR;
    }

    break;
  case FHP_STATE_HEADERS_END_CR:
    switch (byte) {
    case '\n':
      fhp->state = FHP_STATE_HEADERS_END;

      break;
    default:
      return FHP_ERR_INVALID_CHAR;
    }

    break;
  case FHP_STATE_HEADERS_END:
    // TODO

    break;
  default:
    // invalid state
    // (should never be reached)
    return FHP_ERR_BAD_STATE;
  }

  // increment byte offset
  fhp->ofs++;

  /* return success */
  return FHP_OK;
}

fhp_err_t
fhp_push(
  fhp_t * const fhp,
  uint8_t * const buf,
  size_t len
) {
  switch (fhp->state) {
  case FHP_STATE_ERROR:
    return fhp->err;

    break;
  default:
    for (size_t i = 0; i < len; i++) {
      // push byte
      fhp_err_t err = fhp_push_byte(fhp, buf[i]);

      // check result
      if (err != FHP_OK) {
        fhp->state = FHP_STATE_ERROR;
        fhp->err = err;
        return err;
      }
    }
  }

  // return success
  return FHP_OK;
}

fhp_env_t *
fhp_get_env(fhp_t * const fhp) {
  return fhp->env;
}

void *
fhp_get_user_data(fhp_t * const fhp) {
  return fhp->user_data;
}