-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_http_post.c
136 lines (120 loc) · 3.4 KB
/
backend_http_post.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <espressif/esp_common.h>
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "shared.h"
#include "backend_http_post.h"
#define TIMEOUT 5000
static bool send_str(int sock, const char *str) {
if (write(sock, str, strlen(str)) < 0) {
printf("... socket send failed\r\n");
close(sock);
return false;
}
return true;
}
void http_post(const struct output_config *config, const struct sensordata *values) {
char *host = get_config(config, "host");
char *port_str = get_config(config, "port");
if (!port_str) {
port_str = "80";
}
char *path = get_config(config, "path");
if (!host || !path) {
printf("Invalid http_post configuration!\n");
return;
}
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
int err = getaddrinfo(host, port_str, &hints, &res);
if(err != 0 || res == NULL) {
printf("DNS lookup of %s:%s failed err=%d res=%p\r\n", host, port_str, err, res);
if (res) {
freeaddrinfo(res);
}
return;
}
int s = socket(res->ai_family, res->ai_socktype, 0);
if(s < 0) {
printf("... Failed to allocate socket.\r\n");
freeaddrinfo(res);
return;
}
int opt = TIMEOUT;
if (lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *)&opt, sizeof(opt)) != 0) {
close(s);
printf("Failed to set recv timeout\r\n");
return;
}
opt = TIMEOUT;
if (lwip_setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (void *)&opt, sizeof(opt)) != 0) {
close(s);
printf("Failed to set send timeout\r\n");
return;
}
if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
close(s);
freeaddrinfo(res);
printf("... socket connect failed.\r\n");
return;
}
freeaddrinfo(res);
char body[256];
snprintf(body, sizeof(body), "{"
"\"softwareversion\":\"Luftikus/%s\","
"\"sensordatavalues\":[",
VERSION);
int bodylen;
for(const struct sensordata *v = values; v->name; v++) {
bodylen = strlen(body);
snprintf(body + bodylen, sizeof(body) - bodylen,
"%s{"
"\"value_type\":\"%s\","
"\"value\":\"%s\""
"}",
(v == values ? "" : ","),
v->name,
v->value
);
}
bodylen = strlen(body);
snprintf(body + bodylen, sizeof(body) - bodylen, "]}");
char buf[128];
snprintf(buf, sizeof(buf),
"POST %s HTTP/1.1\r\n", path);
if (!send_str(s, buf)) return;
snprintf(buf, sizeof(buf),
"Host: %s\r\n", host);
if (!send_str(s, buf)) return;
if (!send_str(s, "Connection: close\r\n")) return;
if (!send_str(s, "Content-Type: application/json\r\n")) return;
snprintf(buf, sizeof(buf),
"Content-Length: %i\r\n", strlen(body));
if (!send_str(s, buf)) return;
snprintf(buf, sizeof(buf),
"X-Sensor: esp8266-%u\r\n", sdk_system_get_chip_id());
if (!send_str(s, buf)) return;
char *x_pin = get_config(config, "x-pin");
if (x_pin) {
snprintf(buf, sizeof(buf),
"X-PIN: %s\r\n", x_pin);
if (!send_str(s, buf)) return;
}
if (!send_str(s, "\r\n")) return;
if (!send_str(s, body)) return;
printf("POST http://%s:%s%s\n", host, port_str, path);
int r;
do {
bzero(buf, sizeof(buf));
r = read(s, buf, sizeof(buf) - 1);
} while(r > 0);
close(s);
}