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

#include <zephyr.h>
#include <errno.h>
#include <misc/printk.h>

#include "http_client_types.h"
#include "http_client.h"
#include "config.h"

#define POST_CONTENT_TYPE "application/x-www-form-urlencoded"
#define POST_PAYLOAD "os=ZephyrRTOS&arch="CONFIG_ARCH

#define MAX_ITERATIONS	100

static struct http_client_ctx http_ctx;

static void send_http_method(enum http_method method, const char *url,
			     const char *content_type, const char *payload);

void main(void)
{
	int i = MAX_ITERATIONS;
	int rc;

	http_init(&http_ctx);
	http_ctx.tcp_ctx.receive_cb = http_receive_cb;
	http_ctx.tcp_ctx.timeout = HTTP_NETWORK_TIMEOUT;

	rc = tcp_set_local_addr(&http_ctx.tcp_ctx, LOCAL_ADDR);
	if (rc) {
		printk("tcp_set_local_addr error\n");
		goto lb_exit;
	}

	while (i-- > 0) {
		send_http_method(HTTP_GET, "/index.html", NULL, NULL);
		k_sleep(APP_NAP_TIME);

		send_http_method(HTTP_HEAD, "/", NULL, NULL);
		k_sleep(APP_NAP_TIME);

		send_http_method(HTTP_OPTIONS, "/index.html", NULL, NULL);
		k_sleep(APP_NAP_TIME);

		send_http_method(HTTP_POST, "/post_test.php",
				 POST_CONTENT_TYPE, POST_PAYLOAD);
		k_sleep(APP_NAP_TIME);
	}

lb_exit:
	printk("\nBye!\n");
}

void print_banner(enum http_method method)
{
	printk("\n*******************************************\n"
	       "HTTP Client: %s\nConnecting to: %s port %d\n"
	       "Hostname: %s\nHTTP Request: %s\n",
	       LOCAL_ADDR, SERVER_ADDR, SERVER_PORT,
	       HOST_NAME, http_method_str(method));
}

static
void send_http_method(enum http_method method, const char *url,
		      const char *content_type, const char *payload)
{
	int rc;

	print_banner(method);

	http_reset_ctx(&http_ctx);

	rc = tcp_connect(&http_ctx.tcp_ctx, SERVER_ADDR, SERVER_PORT);
	if (rc) {
		printk("tcp_connect error\n");
		return;
	}

	switch (method) {
	case HTTP_GET:
		rc = http_send_get(&http_ctx, url);
		break;
	case HTTP_POST:
		rc = http_send_post(&http_ctx, url, content_type, payload);
		break;
	case HTTP_HEAD:
		rc = http_send_head(&http_ctx, url);
		break;
	case HTTP_OPTIONS:
		rc = http_send_options(&http_ctx, url, NULL, NULL);
		break;
	default:
		printk("Not yet implemented\n");
		goto lb_exit;
	}

	if (rc) {
		printk("Send error\n");
		goto lb_exit;
	}

	/* this is async, so we wait until the reception callback
	 * processes the server's response (if any)
	 */
	k_sleep(APP_NAP_TIME);

	printk("\nHTTP server response status: %s\n", http_ctx.http_status);

	printk("HTTP parser status: %s\n",
	       http_errno_description(http_ctx.parser.http_errno));

	if (method == HTTP_GET) {
		if (http_ctx.body_found) {
			printk("HTTP body: %u bytes, expected: %u bytes\n",
			       http_ctx.processed, http_ctx.content_length);
		} else {
			printk("Error detected during HTTP msg processing\n");
		}
	}

lb_exit:
	tcp_disconnect(&http_ctx.tcp_ctx);
}