-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmk_http_parser.h
221 lines (187 loc) · 5.64 KB
/
mk_http_parser.h
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Monkey HTTP Server
* ==================
* Copyright 2001-2014 Monkey Software LLC <eduardo@monkey.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#ifndef MK_HTTP_H
#define MK_HTTP_H
typedef struct
{
char *data;
unsigned long len;
} mk_ptr_t;
/* General status */
#define MK_HTTP_PENDING -10 /* cannot complete until more data arrives */
#define MK_HTTP_ERROR -1 /* found an error when parsing the string */
#define MK_HTTP_OK 0
/* Request levels
* ==============
*
* 1. FIRST_LINE : Method, URI (+ QS) + Protocol version + CRLF
* 2. HEADERS (optional) : KEY, SEP, VALUE + CRLF
* 3. BODY (option) : data based on Content-Length or Chunked transfer encoding
*/
enum {
REQ_LEVEL_FIRST = 1,
REQ_LEVEL_CONTINUE ,
REQ_LEVEL_HEADERS ,
REQ_LEVEL_END ,
REQ_LEVEL_BODY
};
/* Statuses per levels */
enum {
/* REQ_LEVEL_FIRST */
MK_ST_REQ_METHOD = 1,
MK_ST_REQ_URI ,
MK_ST_REQ_QUERY_STRING ,
MK_ST_REQ_PROT_VERSION ,
MK_ST_FIRST_CONTINUE ,
MK_ST_FIRST_FINALIZING , /* LEVEL_FIRST finalize the request */
MK_ST_FIRST_COMPLETE ,
/* REQ_HEADERS */
MK_ST_HEADER_KEY ,
MK_ST_HEADER_SEP ,
MK_ST_HEADER_VAL_STARTS ,
MK_ST_HEADER_VALUE ,
MK_ST_HEADER_END ,
MK_ST_BLOCK_END
};
/*
* Define a list of known headers, they are used to perform headers
* lookups in the parser and further Monkey core.
*/
enum {
MK_HEADER_ACCEPT = 0,
MK_HEADER_ACCEPT_CHARSET ,
MK_HEADER_ACCEPT_ENCODING ,
MK_HEADER_ACCEPT_LANGUAGE ,
MK_HEADER_AUTHORIZATION ,
MK_HEADER_COOKIE ,
MK_HEADER_CONNECTION ,
MK_HEADER_CONTENT_LENGTH ,
MK_HEADER_CONTENT_RANGE ,
MK_HEADER_CONTENT_TYPE ,
MK_HEADER_IF_MODIFIED_SINCE ,
MK_HEADER_HOST ,
MK_HEADER_LAST_MODIFIED ,
MK_HEADER_LAST_MODIFIED_SINCE ,
MK_HEADER_REFERER ,
MK_HEADER_RANGE ,
MK_HEADER_USER_AGENT ,
MK_HEADER_SIZEOF
};
struct mk_http_header {
int type;
mk_ptr_t key;
mk_ptr_t val;
};
/* This structure is the 'Parser Context' */
struct mk_http_parser {
int i;
int level; /* request level */
int status; /* level status */
int next; /* something next after status ? */
int length;
/* lookup fields */
int start;
int end;
int chars;
/* it stores the numeric value of Content-Length header */
long body_received;
long header_content_length;
/* probable current header, fly parsing */
int header_key;
int header_sep;
int header_val;
int header_min;
int header_max;
struct mk_http_header headers[MK_HEADER_SIZEOF];
};
struct mk_http_parser *mk_http_parser_new();
int mk_http_parser(struct mk_http_parser *req, char *buffer, int len);
#ifdef HTTP_STANDALONE
/* ANSI Colors */
#define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m"
#define ANSI_CYAN "\033[36m"
#define ANSI_BOLD_CYAN ANSI_BOLD ANSI_CYAN
#define ANSI_MAGENTA "\033[35m"
#define ANSI_BOLD_MAGENTA ANSI_BOLD ANSI_MAGENTA
#define ANSI_RED "\033[31m"
#define ANSI_BOLD_RED ANSI_BOLD ANSI_RED
#define ANSI_YELLOW "\033[33m"
#define ANSI_BOLD_YELLOW ANSI_BOLD ANSI_YELLOW
#define ANSI_BLUE "\033[34m"
#define ANSI_BOLD_BLUE ANSI_BOLD ANSI_BLUE
#define ANSI_GREEN "\033[32m"
#define ANSI_BOLD_GREEN ANSI_BOLD ANSI_GREEN
#define ANSI_WHITE "\033[37m"
#define ANSI_BOLD_WHITE ANSI_BOLD ANSI_WHITE
#define TEST_OK 0
#define TEST_FAIL 1
static inline void p_field(struct mk_http_parser *req, char *buffer)
{
int i;
printf("'");
for (i = req->start; i < req->end; i++) {
printf("%c", buffer[i]);
}
printf("'");
}
static inline int eval_field(struct mk_http_parser *req, char *buffer)
{
if (req->level == REQ_LEVEL_FIRST) {
printf("[ \033[35mfirst level\033[0m ] ");
}
else {
printf("[ \033[36mheaders\033[0m ] ");
}
printf(" ");
switch (req->status) {
case MK_ST_REQ_METHOD:
printf("MK_ST_REQ_METHOD : ");
break;
case MK_ST_REQ_URI:
printf("MK_ST_REQ_URI : ");
break;
case MK_ST_REQ_QUERY_STRING:
printf("MK_ST_REQ_QUERY_STRING : ");
break;
case MK_ST_REQ_PROT_VERSION:
printf("MK_ST_REQ_PROT_VERSION : ");
break;
case MK_ST_HEADER_KEY:
printf("MK_ST_HEADER_KEY : ");
break;
case MK_ST_HEADER_VAL_STARTS:
printf("MK_ST_HEADER_VAL_STARTS: ");
break;
case MK_ST_HEADER_VALUE:
printf("MK_ST_HEADER_VALUE : ");
break;
case MK_ST_HEADER_END:
printf("MK_ST_HEADER_END : ");
break;
default:
printf("\033[31mUNKNOWN STATUS (%i)\033[0m : ", req->status);
break;
};
p_field(req, buffer);
printf("\n");
return 0;
}
#endif /* HTTP_STANDALONE */
#endif /* MK_HTTP_H */