summaryrefslogtreecommitdiff
path: root/token.c
blob: d061692f8ac00ed0d783b827c2c332bd1059e5e0 (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
#include "internal.h"

//
// 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",

  "HEADER_TRANSFER_ENCODING",
  "HEADER_CONTENT_LENGTH",
  "HEADER_CONTENT_TYPE",

  "HEADERS_END",

  "BODY_NONE",
  "BODY_START",
  "BODY_FRAGMENT",
  "BODY_END",

  "CHUNK_START",
  "CHUNK_END",
  "CHUNK_LAST",

  "REQUEST_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;
}