Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
/*
 * Copyright (c) 2017 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#if defined(CONFIG_NET_DEBUG_HTTP)
#if defined(CONFIG_HTTPS)
#define SYS_LOG_DOMAIN "https/client"
#else
#define SYS_LOG_DOMAIN "http/client"
#endif
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#include <zephyr.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <stdlib.h>
#include <version.h>

#include <net/net_core.h>
#include <net/net_ip.h>
#include <net/http.h>

#include "../../ip/net_private.h"

#define BUF_ALLOC_TIMEOUT 100

#define RC_STR(rc)	(rc == 0 ? "OK" : "ERROR")

#define HTTP_EOF           "\r\n\r\n"

#define HTTP_HOST          "Host"
#define HTTP_CONTENT_TYPE  "Content-Type"
#define HTTP_CONTENT_LEN   "Content-Length"
#define HTTP_CONT_LEN_SIZE 6

/* Default network activity timeout in seconds */
#define HTTP_NETWORK_TIMEOUT	K_SECONDS(CONFIG_HTTP_CLIENT_NETWORK_TIMEOUT)

int client_reset(struct http_ctx *ctx)
{
	http_parser_init(&ctx->http.parser, HTTP_RESPONSE);

	memset(ctx->http.rsp.http_status, 0,
	       sizeof(ctx->http.rsp.http_status));

	ctx->http.rsp.cl_present = 0;
	ctx->http.rsp.content_length = 0;
	ctx->http.rsp.processed = 0;
	ctx->http.rsp.body_found = 0;
	ctx->http.rsp.message_complete = 0;
	ctx->http.rsp.body_start = NULL;

	memset(ctx->http.rsp.response_buf, 0, ctx->http.rsp.response_buf_len);
	ctx->http.rsp.data_len = 0;

	return 0;
}

int http_request(struct http_ctx *ctx, struct http_request *req, s32_t timeout,
		 void *user_data)
{
	const char *method = http_method_str(req->method);
	int ret;

	if (ctx->pending) {
		net_pkt_unref(ctx->pending);
		ctx->pending = NULL;
	}

	ret = http_add_header(ctx, method, user_data);
	if (ret < 0) {
		goto out;
	}

	ret = http_add_header(ctx, " ", user_data);
	if (ret < 0) {
		goto out;
	}

	ret = http_add_header(ctx, req->url, user_data);
	if (ret < 0) {
		goto out;
	}

	ret = http_add_header(ctx, req->protocol, user_data);
	if (ret < 0) {
		goto out;
	}

	ret = http_add_header(ctx, HTTP_CRLF, user_data);
	if (ret < 0) {
		goto out;
	}

	if (req->host) {
		ret = http_add_header_field(ctx, HTTP_HOST, req->host,
					    user_data);
		if (ret < 0) {
			goto out;
		}
	}

	if (req->header_fields) {
		ret = http_add_header(ctx, req->header_fields, user_data);
		if (ret < 0) {
			goto out;
		}
	}

	if (req->content_type_value) {
		ret = http_add_header_field(ctx, HTTP_CONTENT_TYPE,
					    req->content_type_value,
					    user_data);
		if (ret < 0) {
			goto out;
		}
	}

	if (req->payload && req->payload_size) {
		char content_len_str[HTTP_CONT_LEN_SIZE];

		ret = snprintk(content_len_str, HTTP_CONT_LEN_SIZE,
			       "%u", req->payload_size);
		if (ret <= 0 || ret >= HTTP_CONT_LEN_SIZE) {
			ret = -ENOMEM;
			goto out;
		}

		ret = http_add_header_field(ctx, HTTP_CONTENT_LEN,
					    content_len_str, user_data);
		if (ret < 0) {
			goto out;
		}

		ret = http_add_header(ctx, HTTP_CRLF, user_data);
		if (ret < 0) {
			goto out;
		}

		ret = http_prepare_and_send(ctx, req->payload,
					    req->payload_size, user_data);
		if (ret < 0) {
			goto out;
		}
	} else {
		ret = http_add_header(ctx, HTTP_EOF, user_data);
		if (ret < 0) {
			goto out;
		}
	}

	http_send_flush(ctx, user_data);

out:
	if (ctx->pending) {
		net_pkt_unref(ctx->pending);
		ctx->pending = NULL;
	}

	return ret;
}

#if defined(CONFIG_NET_DEBUG_HTTP)
static void sprint_addr(char *buf, int len,
			sa_family_t family,
			struct sockaddr *addr)
{
	if (family == AF_INET6) {
		net_addr_ntop(AF_INET6, &net_sin6(addr)->sin6_addr, buf, len);
	} else if (family == AF_INET) {
		net_addr_ntop(AF_INET, &net_sin(addr)->sin_addr, buf, len);
	} else {
		NET_DBG("Invalid protocol family");
	}
}
#endif

static inline void print_info(struct http_ctx *ctx,
			      enum http_method method)
{
#if defined(CONFIG_NET_DEBUG_HTTP)
	char local[NET_IPV6_ADDR_LEN];
	char remote[NET_IPV6_ADDR_LEN];

	sprint_addr(local, NET_IPV6_ADDR_LEN,
		    ctx->app_ctx.default_ctx->local.sa_family,
		    &ctx->app_ctx.default_ctx->local);

	sprint_addr(remote, NET_IPV6_ADDR_LEN,
		    ctx->app_ctx.default_ctx->remote.sa_family,
		    &ctx->app_ctx.default_ctx->remote);

	NET_DBG("HTTP %s (%s) %s -> %s port %d",
		http_method_str(method), ctx->http.req.host, local, remote,
		ntohs(net_sin(&ctx->app_ctx.default_ctx->remote)->sin_port));
#endif
}

int http_client_send_req(struct http_ctx *ctx,
			 struct http_request *req,
			 http_response_cb_t cb,
			 u8_t *response_buf,
			 size_t response_buf_len,
			 void *user_data,
			 s32_t timeout)
{
	int ret;

	if (!response_buf || response_buf_len == 0) {
		return -EINVAL;
	}

	ctx->http.rsp.response_buf = response_buf;
	ctx->http.rsp.response_buf_len = response_buf_len;

	client_reset(ctx);

	if (!req->host) {
		req->host = ctx->server;
	}

	ctx->http.req.host = req->host;
	ctx->http.req.method = req->method;
	ctx->http.req.user_data = user_data;

	ctx->http.rsp.cb = cb;

	ret = net_app_connect(&ctx->app_ctx, HTTP_NETWORK_TIMEOUT);
	if (ret < 0) {
		NET_DBG("Cannot connect to server (%d)", ret);
		return ret;
	}

	/* We might wait longer than timeout if the first connection
	 * establishment takes long time (like with HTTPS)
	 */
	if (k_sem_take(&ctx->http.connect_wait, HTTP_NETWORK_TIMEOUT)) {
		NET_DBG("Connection timed out");
		ret = -ETIMEDOUT;
		goto out;
	}

	print_info(ctx, ctx->http.req.method);

	ret = http_request(ctx, req, timeout, user_data);
	if (ret < 0) {
		NET_DBG("Send error (%d)", ret);
		goto out;
	}

	if (timeout != 0 && k_sem_take(&ctx->http.req.wait, timeout)) {
		ret = -ETIMEDOUT;
		goto out;
	}

	if (timeout == 0) {
		return -EINPROGRESS;
	}

	return 0;

out:
	return ret;
}

static void print_header_field(size_t len, const char *str)
{
#if defined(CONFIG_NET_DEBUG_HTTP)
#define MAX_OUTPUT_LEN 128
	char output[MAX_OUTPUT_LEN];

	/* The value of len does not count \0 so we need to increase it
	 * by one.
	 */
	if ((len + 1) > sizeof(output)) {
		len = sizeof(output) - 1;
	}

	snprintk(output, len + 1, "%s", str);

	NET_DBG("[%zd] %s", len, output);
#endif
}

static int on_url(struct http_parser *parser, const char *at, size_t length)
{
	ARG_UNUSED(parser);

	print_header_field(length, at);

	return 0;
}

static int on_status(struct http_parser *parser, const char *at, size_t length)
{
	u16_t len;
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	len = min(length, sizeof(ctx->http.rsp.http_status) - 1);
	memcpy(ctx->http.rsp.http_status, at, len);
	ctx->http.rsp.http_status[len] = 0;

	NET_DBG("HTTP response status %s", ctx->http.rsp.http_status);

	return 0;
}

static int on_header_field(struct http_parser *parser, const char *at,
			   size_t length)
{
	const char *content_len = HTTP_CONTENT_LEN;
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);
	u16_t len;

	len = strlen(content_len);
	if (length >= len && memcmp(at, content_len, len) == 0) {
		ctx->http.rsp.cl_present = true;
	}

	print_header_field(length, at);

	return 0;
}

#define MAX_NUM_DIGITS	16

static int on_header_value(struct http_parser *parser, const char *at,
			   size_t length)
{
	char str[MAX_NUM_DIGITS];
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	if (ctx->http.rsp.cl_present) {
		if (length <= MAX_NUM_DIGITS - 1) {
			long int num;

			memcpy(str, at, length);
			str[length] = 0;

			num = strtol(str, NULL, 10);
			if (num == LONG_MIN || num == LONG_MAX) {
				return -EINVAL;
			}

			ctx->http.rsp.content_length = num;
		}

		ctx->http.rsp.cl_present = false;
	}

	print_header_field(length, at);

	return 0;
}

static int on_body(struct http_parser *parser, const char *at, size_t length)
{
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	ctx->http.rsp.body_found = 1;
	ctx->http.rsp.processed += length;

	NET_DBG("Processed %zd length %zd", ctx->http.rsp.processed, length);

	if (!ctx->http.rsp.body_start &&
	    (u8_t *)at != (u8_t *)ctx->http.rsp.response_buf) {
		ctx->http.rsp.body_start = (u8_t *)at;
	}

	if (ctx->http.rsp.cb) {
		NET_DBG("Calling callback for partitioned %zd len data",
			ctx->http.rsp.data_len);

		ctx->http.rsp.cb(ctx,
				 ctx->http.rsp.response_buf,
				 ctx->http.rsp.response_buf_len,
				 ctx->http.rsp.data_len,
				 HTTP_DATA_MORE,
				 ctx->http.req.user_data);

		/* Re-use the result buffer and start to fill it again */
		ctx->http.rsp.data_len = 0;
		ctx->http.rsp.body_start = NULL;
	}

	return 0;
}

static int on_headers_complete(struct http_parser *parser)
{
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	if (parser->status_code >= 500 && parser->status_code < 600) {
		NET_DBG("Status %d, skipping body", parser->status_code);

		return 1;
	}

	if ((ctx->http.req.method == HTTP_HEAD ||
	     ctx->http.req.method == HTTP_OPTIONS)
	    && ctx->http.rsp.content_length > 0) {
		NET_DBG("No body expected");
		return 1;
	}

	NET_DBG("Headers complete");

	return 0;
}

static int on_message_begin(struct http_parser *parser)
{
#if defined(CONFIG_NET_DEBUG_HTTP) && (CONFIG_SYS_LOG_NET_LEVEL > 2)
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	NET_DBG("-- HTTP %s response (headers) --",
		http_method_str(ctx->http.req.method));
#else
	ARG_UNUSED(parser);
#endif
	return 0;
}

static int on_message_complete(struct http_parser *parser)
{
	struct http_ctx *ctx = CONTAINER_OF(parser,
					    struct http_ctx,
					    http.parser);

	NET_DBG("-- HTTP %s response (complete) --",
		http_method_str(ctx->http.req.method));

	if (ctx->http.rsp.cb) {
		ctx->http.rsp.cb(ctx,
				 ctx->http.rsp.response_buf,
				 ctx->http.rsp.response_buf_len,
				 ctx->http.rsp.data_len,
				 HTTP_DATA_FINAL,
				 ctx->http.req.user_data);
	}

	ctx->http.rsp.message_complete = 1;

	k_sem_give(&ctx->http.req.wait);

	return 0;
}

static int on_chunk_header(struct http_parser *parser)
{
	ARG_UNUSED(parser);

	return 0;
}

static int on_chunk_complete(struct http_parser *parser)
{
	ARG_UNUSED(parser);

	return 0;
}

static void http_received(struct net_app_ctx *app_ctx,
			  struct net_pkt *pkt,
			  int status,
			  void *user_data)
{
	struct http_ctx *ctx = user_data;
	size_t start = ctx->http.rsp.data_len;
	u16_t len = 0;
	struct net_buf *frag, *prev_frag = NULL;
	size_t recv_len;
	size_t pkt_len;

	recv_len = net_pkt_appdatalen(pkt);
	if (recv_len == 0) {
		/* don't print info about zero-length app data buffers */
		goto quit;
	}

	if (status) {
		NET_DBG("[%p] Status %d <%s>", ctx, status, RC_STR(status));
		goto out;
	}

	/* Get rid of possible IP headers in the first fragment. */
	frag = pkt->frags;

	pkt_len = net_pkt_get_len(pkt);

	if (recv_len < pkt_len) {
		net_buf_pull(frag, pkt_len - recv_len);
		net_pkt_set_appdata(pkt, frag->data);
	}

	NET_DBG("[%p] Received %zd bytes http data", ctx, recv_len);

	while (frag) {
		/* If this fragment cannot be copied to result buf,
		 * then parse what we have which will cause the callback to be
		 * called in function on_body(), and continue copying.
		 */
		if ((ctx->http.rsp.data_len + frag->len) >
		    ctx->http.rsp.response_buf_len) {

			/* If the caller has not supplied a callback, then
			 * we cannot really continue if the request buffer
			 * overflows. Set the data_len to mark how many bytes
			 * should be needed in the response_buf.
			 */
			if (!ctx->cb.recv) {
				ctx->http.rsp.data_len = recv_len;
				goto out;
			}

			http_parser_execute(&ctx->http.parser,
					    &ctx->http.parser_settings,
					    ctx->http.rsp.response_buf + start,
					    len);

			ctx->http.rsp.data_len = 0;
			len = 0;
			start = 0;
		}

		memcpy(ctx->http.rsp.response_buf + ctx->http.rsp.data_len,
		       frag->data, frag->len);

		ctx->http.rsp.data_len += frag->len;
		len += frag->len;

		prev_frag = frag;
		frag = frag->frags;
		pkt->frags = frag;

		prev_frag->frags = NULL;
		net_pkt_frag_unref(prev_frag);
	}

out:
	http_parser_execute(&ctx->http.parser,
			    &ctx->http.parser_settings,
			    ctx->http.rsp.response_buf + start,
			    len);

	net_pkt_unref(pkt);
	return;

quit:
	http_parser_init(&ctx->http.parser, HTTP_RESPONSE);
	ctx->http.rsp.data_len = 0;
	net_pkt_unref(pkt);
}

static void http_data_sent(struct net_app_ctx *app_ctx,
			   int status,
			   void *user_data_send,
			   void *user_data)
{
	struct http_ctx *ctx = user_data;

	if (!user_data_send) {
		/* This is the token field in the net_context_send().
		 * If this is not set, then it is TCP ACK messages
		 * that are generated by the stack. We just ignore those.
		 */
		return;
	}

	if (ctx->cb.send) {
		ctx->cb.send(ctx, status, user_data_send, ctx->user_data);
	}
}

static void http_connected(struct net_app_ctx *app_ctx,
			   int status,
			   void *user_data)
{
	struct http_ctx *ctx = user_data;

	if (status < 0) {
		return;
	}

	if (ctx->cb.connect) {
		ctx->cb.connect(ctx, HTTP_CONNECTION, ctx->user_data);
	}

	if (ctx->is_connected) {
		return;
	}

	ctx->is_connected = true;

	k_sem_give(&ctx->http.connect_wait);
}

static void http_closed(struct net_app_ctx *app_ctx,
			int status,
			void *user_data)
{
	struct http_ctx *ctx = user_data;

	ARG_UNUSED(app_ctx);
	ARG_UNUSED(status);

	NET_DBG("[%p] connection closed", ctx);

	ctx->is_connected = false;

	if (ctx->cb.close) {
		ctx->cb.close(ctx, 0, ctx->user_data);
	}
}

int http_client_init(struct http_ctx *ctx,
		     const char *server,
		     u16_t server_port,
		     struct sockaddr *server_addr,
		     s32_t timeout)
{
	int ret;

	memset(ctx, 0, sizeof(*ctx));

	ret = net_app_init_tcp_client(&ctx->app_ctx,
				      NULL,         /* use any local address */
				      server_addr,
				      server,
				      server_port,
				      timeout,
				      ctx);
	if (ret < 0) {
		NET_DBG("Cannot init HTTP client (%d)", ret);
		return ret;
	}

	ret = net_app_set_cb(&ctx->app_ctx, http_connected, http_received,
			     http_data_sent, http_closed);
	if (ret < 0) {
		NET_ERR("Cannot set callbacks (%d)", ret);
		return ret;
	}

	ctx->http.parser_settings.on_body = on_body;
	ctx->http.parser_settings.on_chunk_complete = on_chunk_complete;
	ctx->http.parser_settings.on_chunk_header = on_chunk_header;
	ctx->http.parser_settings.on_headers_complete = on_headers_complete;
	ctx->http.parser_settings.on_header_field = on_header_field;
	ctx->http.parser_settings.on_header_value = on_header_value;
	ctx->http.parser_settings.on_message_begin = on_message_begin;
	ctx->http.parser_settings.on_message_complete = on_message_complete;
	ctx->http.parser_settings.on_status = on_status;
	ctx->http.parser_settings.on_url = on_url;

	k_sem_init(&ctx->http.req.wait, 0, 1);
	k_sem_init(&ctx->http.connect_wait, 0, 1);

	ctx->server = server;
	ctx->is_init = true;
	ctx->is_client = true;

	return 0;
}

int http_request_cancel(struct http_ctx *ctx)
{
	if (!ctx->is_init) {
		return -EINVAL;
	}

	if (!ctx->is_client) {
		return -EINVAL;
	}

	client_reset(ctx);

	return 0;
}

#if defined(CONFIG_HTTPS)
int http_client_set_tls(struct http_ctx *ctx,
			u8_t *request_buf,
			size_t request_buf_len,
			u8_t *personalization_data,
			size_t personalization_data_len,
			net_app_ca_cert_cb_t cert_cb,
			const char *cert_host,
			net_app_entropy_src_cb_t entropy_src_cb,
			struct k_mem_pool *pool,
			k_thread_stack_t *https_stack,
			size_t https_stack_size)
{
	int ret;

	ret = net_app_client_tls(&ctx->app_ctx,
				 request_buf,
				 request_buf_len,
				 personalization_data,
				 personalization_data_len,
				 cert_cb,
				 cert_host,
				 entropy_src_cb,
				 pool,
				 https_stack,
				 https_stack_size);
	if (ret < 0) {
		NET_DBG("Cannot init TLS (%d)", ret);
		return ret;
	}

	ctx->is_tls = true;

	return 0;
}
#endif /* CONFIG_HTTPS */