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

#include "http_client.h"
#include "http_client_rcv.h"
#include "http_client_cb.h"
#include "config.h"

#include <misc/printk.h>
#include <net/nbuf.h>

int http_init(struct http_client_ctx *http_ctx)
{
	memset(http_ctx, 0, sizeof(struct http_client_ctx));

	http_ctx->settings.on_body = on_body;
	http_ctx->settings.on_chunk_complete = on_chunk_complete;
	http_ctx->settings.on_chunk_header = on_chunk_header;
	http_ctx->settings.on_headers_complete = on_headers_complete;
	http_ctx->settings.on_header_field = on_header_field;
	http_ctx->settings.on_header_value = on_header_value;
	http_ctx->settings.on_message_begin = on_message_begin;
	http_ctx->settings.on_message_complete = on_message_complete;
	http_ctx->settings.on_status = on_status;
	http_ctx->settings.on_url = on_url;

	return 0;
}

int http_reset_ctx(struct http_client_ctx *http_ctx)
{
	http_parser_init(&http_ctx->parser, HTTP_RESPONSE);

	memset(http_ctx->http_status, 0, sizeof(http_ctx->http_status));

	http_ctx->cl_present = 0;
	http_ctx->content_length = 0;
	http_ctx->processed = 0;
	http_ctx->body_found = 0;

	return 0;
}

static
int http_send_request(struct http_client_ctx *http_ctx, const char *method,
		      const char *url, const char *protocol,
		      const char *content_type_value, const char *payload)
{
	const char *content_type = "Content-Type: ";
	const char *sep = "\r\n\r\n";
	struct net_buf *tx;
	int rc;

	tx = net_nbuf_get_tx(http_ctx->tcp_ctx.net_ctx, K_FOREVER);
	if (tx == NULL) {
		return -ENOMEM;
	}

	if (!net_nbuf_append(tx, strlen(method), (uint8_t *)method,
			     K_FOREVER)) {
		goto lb_exit;
	}

	if (!net_nbuf_append(tx, strlen(url), (uint8_t *)url, K_FOREVER)) {
		goto lb_exit;
	}

	if (!net_nbuf_append(tx, strlen(protocol), (uint8_t *)protocol,
			     K_FOREVER)) {
		goto lb_exit;
	}

	if (!net_nbuf_append(tx, strlen(HEADER_FIELDS),
			     (uint8_t *)HEADER_FIELDS, K_FOREVER)) {
		goto lb_exit;
	}

	if (content_type_value && payload) {
		char content_len_str[CON_LEN_SIZE];

		if (!net_nbuf_append(tx, strlen(content_type),
				     (uint8_t *)content_type, K_FOREVER)) {
			rc = -ENOMEM;
			goto lb_exit;
		}

		if (!net_nbuf_append(tx, strlen(content_type_value),
				     (uint8_t *)content_type_value,
				     K_FOREVER)) {
			rc = -ENOMEM;
			goto lb_exit;
		}

		rc = snprintk(content_len_str, sizeof(content_len_str),
			      "\r\nContent-Length: %u\r\n\r\n",
			      strlen(payload));
		if (rc <= 0 || rc >= sizeof(content_len_str)) {
			rc = -ENOMEM;
			goto lb_exit;
		}

		if (!net_nbuf_append(tx, strlen(content_len_str),
				     (uint8_t *)content_len_str, K_FOREVER)) {
			rc = -ENOMEM;
			goto lb_exit;
		}

		if (!net_nbuf_append(tx, strlen(payload), (uint8_t *)payload,
				     K_FOREVER)) {
			rc = -ENOMEM;
			goto lb_exit;
		}

	} else {
		if (!net_nbuf_append(tx, strlen(sep), (uint8_t *)sep,
				     K_FOREVER)) {
			rc = -ENOMEM;
			goto lb_exit;
		}
	}

	return net_context_send(tx, NULL, http_ctx->tcp_ctx.timeout,
				NULL, NULL);

lb_exit:
	net_buf_unref(tx);

	return rc;
}

int http_send_get(struct http_client_ctx *http_ctx, const char *url)
{
	return http_send_request(http_ctx, "GET ", url, " HTTP/1.1\r\n",
				 NULL, NULL);
}

int http_send_head(struct http_client_ctx *http_ctx, const char *url)
{
	return http_send_request(http_ctx, "HEAD ", url, " HTTP/1.1\r\n",
				 NULL, NULL);
}

int http_send_options(struct http_client_ctx *http_ctx, const char *url,
		      const char *content_type_value, const char *payload)
{
	return http_send_request(http_ctx, "OPTIONS ", url, " HTTP/1.1\r\n",
				 content_type_value, payload);
}

int http_send_post(struct http_client_ctx *http_ctx, const char *url,
		   const char *content_type_value, const char *payload)
{
	return http_send_request(http_ctx, "POST ", url, " HTTP/1.1\r\n",
				 content_type_value, payload);
}