summaryrefslogtreecommitdiff
path: root/samples/net/http_server/src/http_server.c
blob: 3355a4ce037a49dd41306dc8795ac59a5b598581 (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
/*
 * Copyright (c) 2017 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "http_types.h"
#include "http_server.h"
#include "http_utils.h"
#include "http_write_utils.h"
#include "config.h"

#include <net/http_parser.h>
#include <net/nbuf.h>
#include <stdio.h>

#define URL_DEFAULT_HANDLER_INDEX 0

#define HTTP_BUF_CTR	HTTP_MAX_NUMBER_SERVER_CTX
#define HTTP_BUF_SIZE	1024

NET_BUF_POOL_DEFINE(http_msg_pool, HTTP_BUF_CTR, HTTP_BUF_SIZE, 0, NULL);

/**
 * @brief http_ctx_release	Releases an HTTP context
 * @return			0, future versions may return error codes
 */
static
int http_ctx_release(struct http_server_ctx *http_ctx);

/**
 * @brief parser_init	Initializes some parser-related fields at the
 *			http_server_ctx structure
 * @param ctx		HTTP server context
 * @param net_ctx	Network context
 * @return		0 on success
 * @return		-EINVAL on error
 */
static
int parser_init(struct http_server_ctx *ctx);

/**
 * @brief parser_parse_request	Parses an HTTP REQUEST
 * @param ctx		HTTP server context
 * @param rx		Input buffer
 * @return		0 on success
 * @return		-EINVAL on error
 */
static
int parser_parse_request(struct http_server_ctx *ctx, struct net_buf *rx);

static struct http_root_url *http_url_find(struct http_server_ctx *http_ctx);

static int http_url_cmp(const char *url, uint16_t url_len,
			const char *root_url, uint16_t root_url_len);

static void http_tx(struct http_server_ctx *http_ctx);

void http_rx_tx(struct net_context *net_ctx, struct net_buf *rx, int status,
		void *user_data)
{
	struct http_server_ctx *http_ctx = NULL;
	struct net_buf *data = NULL;
	uint16_t rcv_len;
	uint16_t offset;
	int parsed_len;
	int rc;

	if (status) {
		printf("[%s:%d] Status code: %d, <%s>\n",
			__func__, __LINE__, status, RC_STR(status));
		goto lb_exit;
	}

	if (!user_data) {
		printf("[%s:%d] User data is null\n", __func__, __LINE__);
		goto lb_exit;
	}

	http_ctx = (struct http_server_ctx *)user_data;
	if (http_ctx->net_ctx != net_ctx) {
		printf("[%s:%d] Wrong network context received\n",
		       __func__, __LINE__);
		goto lb_exit;
	}

	if (!rx) {
		printf("[%s:%d] Connection closed by peer\n",
		       __func__, __LINE__);
		http_ctx_release(http_ctx);
		goto lb_exit;
	}

	rcv_len = net_nbuf_appdatalen(rx);
	if (rcv_len == 0) {
		/* don't print info about zero-length app data buffers */
		goto lb_exit;
	}

	data = net_buf_alloc(&http_msg_pool, APP_SLEEP_MSECS);
	if (data == NULL) {
		printf("[%s:%d] Data buffer alloc error\n", __func__, __LINE__);
		goto lb_exit;
	}

	offset = net_buf_frags_len(rx) - rcv_len;
	rc = net_nbuf_linear_copy(data, rx, offset, rcv_len);
	if (rc != 0) {
		printf("[%s:%d] Linear copy error\n", __func__, __LINE__);
		goto lb_exit;
	}

	parser_init(http_ctx);
	parsed_len = parser_parse_request(http_ctx, data);
	if (parsed_len <= 0) {
		printf("[%s:%d] Received: %u bytes, only parsed: %d bytes\n",
		       __func__, __LINE__, rcv_len, parsed_len);
	}

	if (http_ctx->parser.http_errno != HPE_OK) {
		http_write_400_bad_request(http_ctx);
	} else {
		http_tx(http_ctx);
	}

lb_exit:
	net_buf_unref(data);
	net_buf_unref(rx);
}

/**
 * @brief on_header_field	HTTP Parser callback for header fields
 * @param parser		HTTP Parser
 * @param at			Points to where the field begins
 * @param length		Field's length
 * @return			0 (always)
 */
static
int on_header_field(struct http_parser *parser, const char *at, size_t length)
{
	struct http_server_ctx *ctx = (struct http_server_ctx *)parser->data;

	if (ctx->field_values_ctr >= HTTP_PARSER_MAX_FIELD_VALUES) {
		return 0;
	}

	ctx->field_values[ctx->field_values_ctr].key = at;
	ctx->field_values[ctx->field_values_ctr].key_len = length;

	return 0;
}

/**
 * @brief on_header_value	HTTP Parser callback for header values
 * @param parser		HTTP Parser
 * @param at			Points to where the value begins
 * @param length		Value's length
 * @return			0 (always)
 */
static
int on_header_value(struct http_parser *parser, const char *at, size_t length)
{
	struct http_server_ctx *ctx = (struct http_server_ctx *)parser->data;

	if (ctx->field_values_ctr >= HTTP_PARSER_MAX_FIELD_VALUES) {
		return 0;
	}

	ctx->field_values[ctx->field_values_ctr].value = at;
	ctx->field_values[ctx->field_values_ctr].value_len = length;

	ctx->field_values_ctr++;

	return 0;
}

/**
 * @brief on_url	HTTP Parser callback for URLs
 * @param parser	HTTP Parser
 * @param at		Points to where the value begins
 * @param length	Value's length
 * @return		0 (always)
 */
static
int on_url(struct http_parser *parser, const char *at, size_t length)
{
	struct http_server_ctx *ctx = (struct http_server_ctx *)parser->data;

	ctx->url = at;
	ctx->url_len = length;

	return 0;
}

static
int parser_init(struct http_server_ctx *ctx)
{
	memset(ctx->field_values, 0x00, sizeof(ctx->field_values));

	ctx->parser_settings.on_header_field = on_header_field;
	ctx->parser_settings.on_header_value = on_header_value;
	ctx->parser_settings.on_url = on_url;

	http_parser_init(&ctx->parser, HTTP_REQUEST);

	/* This circular reference is useful when some parser callbacks
	 * want to access some internal data structures
	 */
	ctx->parser.data = ctx;

	return 0;
}

static
int parser_parse_request(struct http_server_ctx *ctx, struct net_buf *rx)
{
	int rc;

	ctx->field_values_ctr = 0;
	rc = http_parser_execute(&ctx->parser, &ctx->parser_settings,
				 rx->data, rx->len);
	if (rc < 0) {
		printf("[%s:%d] http_parser_execute: %s\n\t%s\n",
		       __func__, __LINE__,
		       http_errno_name(ctx->parser.http_errno),
		       http_errno_description(ctx->parser.http_errno));

		rc = -EINVAL;
		goto exit_routine;
	}

exit_routine:
	return rc;
}

/**
 * @brief server_collection	This is a collection of server ctx structs
 */
static
struct http_server_ctx server_collection[HTTP_MAX_NUMBER_SERVER_CTX];

/**
 * @brief http_url_ctx	Just one URL context per application
 */
static struct http_url_ctx url_ctx;

int http_ctx_init(void)
{
	memset(server_collection, 0x00, sizeof(server_collection));

	memset(&url_ctx, 0x00, sizeof(url_ctx));

	/* 0 is reserved for the default handler */
	url_ctx.urls_ctr = 1;

	return 0;
}

struct http_server_ctx *http_ctx_get(void)
{
	int i;

	for (i = 0; i < HTTP_MAX_NUMBER_SERVER_CTX; i++) {

		if (server_collection[i].used == HTTP_CTX_FREE) {

			printf("[%s:%d] Free ctx found, index: %d\n",
			       __func__, __LINE__, i);

			memset(server_collection + i, 0x00,
			       sizeof(struct http_server_ctx));

			return server_collection + i;
		}
	}

	return NULL;
}

int http_ctx_set(struct http_server_ctx *http_ctx, struct net_context *net_ctx)
{
	if (http_ctx == NULL || net_ctx == NULL) {
		return -EINVAL;
	}

	http_ctx->used = HTTP_CTX_IN_USE;
	http_ctx->net_ctx = net_ctx;

	return 0;
}

static
int http_ctx_release(struct http_server_ctx *http_ctx)
{
	if (http_ctx == NULL) {
		return 0;
	}

	http_ctx->used = HTTP_CTX_FREE;

	return 0;
}

int http_url_default_handler(int (*write_cb)(struct http_server_ctx *))
{
	if (write_cb == NULL) {
		return -EINVAL;
	}

	url_ctx.urls[URL_DEFAULT_HANDLER_INDEX].flags = 0x00;
	url_ctx.urls[URL_DEFAULT_HANDLER_INDEX].root = NULL;
	url_ctx.urls[URL_DEFAULT_HANDLER_INDEX].root_len = 0;
	url_ctx.urls[URL_DEFAULT_HANDLER_INDEX].write_cb = write_cb;

	return 0;
}

int http_url_add(const char *url, uint8_t flags,
		 int (*write_cb)(struct http_server_ctx *http_ctx))
{
	struct http_root_url *root = NULL;

	if (url_ctx.urls_ctr >= HTTP_MAX_NUMBER_URL) {
		return -ENOMEM;
	}

	root = &url_ctx.urls[url_ctx.urls_ctr];

	root->root = url;
	/* this will speed-up some future operations */
	root->root_len = strlen(url);
	root->flags = flags;
	root->write_cb = write_cb;

	url_ctx.urls_ctr++;

	return 0;
}

static int http_url_cmp(const char *url, uint16_t url_len,
			const char *root_url, uint16_t root_url_len)
{
	if (url_len < root_url_len) {
		return -EINVAL;
	}

	if (memcmp(url, root_url, root_url_len) == 0) {
		if (url_len == root_url_len) {
			return 0;
		}

		/* Here we evlaute the following conditions:
		 * root_url = /images, url = /images/ -> OK
		 * root_url = /images/, url = /images/img.png -> OK
		 * root_url = /images/, url = /images_and_docs -> ERROR
		 */
		if (url_len > root_url_len) {
			if (root_url[root_url_len - 1] == '/') {
				return 0;
			}

			if (url[root_url_len] == '/') {
				return 0;
			}
		}
	}

	return -EINVAL;
}

static struct http_root_url *http_url_find(struct http_server_ctx *http_ctx)
{
	uint16_t url_len = http_ctx->url_len;
	const char *url = http_ctx->url;
	struct http_root_url *root_url;
	uint8_t i;
	int rc;

	/* at some point we must come up with something better */
	for (i = 1; i < url_ctx.urls_ctr; i++) {
		root_url = &url_ctx.urls[i];

		rc = http_url_cmp(url, url_len,
				  root_url->root, root_url->root_len);
		if (rc == 0) {
			return root_url;
		}
	}

	return NULL;
}

static void http_tx(struct http_server_ctx *http_ctx)
{
	struct http_root_url *root_url;

	root_url = http_url_find(http_ctx);
	if (!root_url) {
		root_url = &url_ctx.urls[URL_DEFAULT_HANDLER_INDEX];
	}

	if (root_url->write_cb) {
		root_url->write_cb(http_ctx);
	} else {
		printf("[%s:%d] No default handler for %.*s\n",
		       __func__, __LINE__,
		       http_ctx->url_len, http_ctx->url);
	}
}