summaryrefslogtreecommitdiff
path: root/src/http_resp.c
blob: 23b2e58cd505cb596a7bd21771881fe5d676c262 (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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "log.h"
#include "http_resp.h"
#include "http_resp_parser.h"

typedef struct {
	chunkqueue *cq;

	chunk *c; /* current chunk in the chunkqueue */
	size_t offset; /* current offset in current chunk */

	chunk *lookup_c;
	size_t lookup_offset;

	int is_key;
	int is_statusline;
} http_resp_tokenizer_t;

http_resp *http_response_init(void) {
	http_resp *resp = calloc(1, sizeof(*resp));

	resp->reason = buffer_init();
	resp->headers = array_init();
	resp->status = -1;

	return resp;
}

void http_response_reset(http_resp *resp) {
	if (!resp) return;

	buffer_reset(resp->reason);
	array_reset(resp->headers);
	resp->status = -1;

}

void http_response_free(http_resp *resp) {
	if (!resp) return;

	buffer_free(resp->reason);
	array_free(resp->headers);

	free(resp);
}

static int http_resp_get_next_char(http_resp_tokenizer_t *t, unsigned char *c) {
	if (t->offset == t->c->mem->used - 1) {
		/* end of chunk, open next chunk */

		if (!t->c->next) return -1;

		t->c = t->c->next;
		t->offset = 0;
	}

	*c = t->c->mem->ptr[t->offset++];

	t->lookup_offset = t->offset;
	t->lookup_c = t->c;

#if 0
	fprintf(stderr, "%s.%d: get: %c (%d) at offset: %d\r\n", __FILE__, __LINE__, *c > 31 ? *c : ' ', *c, t->offset - 1);
#endif

	return 0;
}

static int http_resp_lookup_next_char(http_resp_tokenizer_t *t, unsigned char *c) {
	if (t->lookup_offset == t->lookup_c->mem->used - 1) {
		/* end of chunk, open next chunk */

		if (!t->lookup_c->next) return -1;

		t->lookup_c = t->lookup_c->next;
		t->lookup_offset = 0;
	}

	*c = t->lookup_c->mem->ptr[t->lookup_offset++];
#if 0
	fprintf(stderr, "%s.%d: lookup: %c (%d) at offset: %d\r\n", __FILE__, __LINE__, *c > 31 ? *c : ' ', *c, t->lookup_offset - 1);
#endif

	return 0;
}

typedef enum {
	PARSER_UNSET,
	PARSER_OK,
	PARSER_ERROR,
	PARSER_EOF
} http_resp_parser_t;

static http_resp_parser_t http_resp_tokenizer(
	http_resp_tokenizer_t *t,
	int *token_id,
	buffer *token
) {
	unsigned char c;
	int tid = 0;

	/* push the token to the parser */

	while (tid == 0 && 0 == http_resp_get_next_char(t, &c)) {
		switch (c) {
		case ':':
			tid = TK_COLON;

			t->is_key = 0;

			break;
		case ' ':
		case '\t':
			/* ignore WS */

			break;
		case '\r':
			if (0 != http_resp_lookup_next_char(t, &c)) return -1;

			if (c == '\n') {
				tid = TK_CRLF;

				t->c = t->lookup_c;
				t->offset = t->lookup_offset;

				t->is_statusline = 0;
				t->is_key = 1;
			} else {
				ERROR("CR with out LF at pos: %zu", t->offset);
				return PARSER_ERROR;
			}
			break;
		case '\n':
			tid = TK_CRLF;

			t->is_statusline = 0;
			t->is_key = 1;

			break;
		default:
			while (c >= 32 && c != 127 && c != 255) {
				if (t->is_statusline) {
					if (t->is_key && c == ':') { t->is_statusline = 0; break; } /* this is not a status line by a real header */
					if (c == 32) { t->is_key = 0; break; } /* the space is a splitter in the statusline */
				} else {
					if (t->is_key) {
						if (c == ':') break; /* the : is the splitter between key and value */
					}
				}
				if (0 != http_resp_lookup_next_char(t, &c)) return PARSER_EOF;
			}

			if (t->c == t->lookup_c &&
				t->offset == t->lookup_offset + 1) {

				ERROR("invalid char (%d) at pos: %zu", c, t->offset);
				return PARSER_ERROR;
			}

			tid = TK_STRING;

			/* the lookup points to the first invalid char */
			t->lookup_offset--;

			/* no overlapping string */
			if (t->c == t->lookup_c) {
				buffer_copy_string_len(token, t->c->mem->ptr + t->offset - 1, t->lookup_offset - t->offset + 1);
			} else {
				/* first chunk */
				buffer_copy_string_len(token, t->c->mem->ptr + t->offset - 1, t->c->mem->used - t->offset);

				/* chunks in the middle */
				for (t->c = t->c->next; t->c != t->lookup_c; t->c = t->c->next) {
					buffer_append_string_buffer(token, t->c->mem);
					t->offset = t->c->mem->used - 1;
				}

				/* last chunk */
				buffer_append_string_len(token, t->c->mem->ptr, t->lookup_offset);
			}

			t->offset = t->lookup_offset;

			break;
		}
	}

	if (tid) {
		*token_id = tid;

		return PARSER_OK;
	}

	return PARSER_EOF;
}

parse_status_t http_response_parse_cq(chunkqueue *cq, http_resp *resp) {
	http_resp_tokenizer_t t;
	void *pParser = NULL;
	int token_id = 0;
	buffer *token = NULL;
	http_resp_ctx_t context;
	parse_status_t ret = PARSE_UNSET;
	http_resp_parser_t parser_ret;
	int last_token_id = 0;

	if(!cq->first) return PARSE_NEED_MORE;
	t.cq = cq;
	t.c = cq->first;
	t.offset = t.c->offset;
	t.is_key = 1;
	t.is_statusline = 1;

	context.ok = 1;
	context.errmsg = buffer_init();
	context.resp = resp;
	context.unused_buffers = buffer_pool_init();

	array_reset(resp->headers);
	resp->status = 0;

	pParser = http_resp_parserAlloc( malloc );
	token = buffer_pool_get(context.unused_buffers);
#if 0
	http_resp_parserTrace(stderr, "http-response: ");
#endif

	while((PARSER_OK == (parser_ret = http_resp_tokenizer(&t, &token_id, token))) && context.ok) {
		http_resp_parser(pParser, token_id, token, &context);

		token = buffer_pool_get(context.unused_buffers);

		/* CRLF CRLF ... the header end sequence */
		if (last_token_id == TK_CRLF &&
		    token_id == TK_CRLF) break;

		last_token_id = token_id;
	}

	// Tokenizer failed
	if (parser_ret == PARSER_ERROR) {
		ret = PARSE_ERROR;
	}

	/* oops, the parser failed */
	if (context.ok == 0) {
		ret = PARSE_ERROR;

		if (!buffer_is_empty(context.errmsg)) {
			TRACE("parsing failed: %s", SAFE_BUF_STR(context.errmsg));
		} else {
			TRACE("%s", "parsing failed ...");
		}
	}

	http_resp_parser(pParser, 0, token, &context);
	http_resp_parserFree(pParser, free);

	if (!buffer_is_empty(context.errmsg)) {
		TRACE("parsing failed: %s", SAFE_BUF_STR(context.errmsg));
	}
	if (context.ok == 0) {
		/* we are missing the some tokens */

		if (!buffer_is_empty(context.errmsg)) {
			TRACE("parsing failed: %s", SAFE_BUF_STR(context.errmsg));
		}

		if (ret == PARSE_UNSET) {
			ret = buffer_is_empty(context.errmsg) ? PARSE_NEED_MORE : PARSE_ERROR;
		}
	} else if (parser_ret == PARSER_EOF) { // didn't see CRLF CRLF, no other error till now
		ret = PARSE_NEED_MORE;
	} else {
		chunk *c;

		for (c = cq->first; c != t.c; c = c->next) {
			c->offset = c->mem->used - 1;
			cq->bytes_out += c->mem->used - 1;
		}

		c->offset = t.offset;
		cq->bytes_out += t.offset;

		ret = PARSE_SUCCESS;
	}

	buffer_pool_append(context.unused_buffers, token);
	buffer_pool_free(context.unused_buffers);
	buffer_free(context.errmsg);

	if (resp->status && (resp->status < 100 || resp->status > 999)) {
		ERROR("invalid status code %i", resp->status);
		return PARSE_ERROR;
	}

	return ret;
}