forked from HISONA/https_client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
278 lines (237 loc) · 6.96 KB
/
Copy pathmain.c
File metadata and controls
278 lines (237 loc) · 6.96 KB
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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "https.h"
#include "netio_netsocket.h"
#include "esp_bridge.h"
#include "netio_esp.h"
#include "bridge_config.h"
#include "esp_utils.h"
#include "json_parser.h"
#define NETIO_DEVICE_SOCKETS 1
#define NETIO_DEVICE_ESP_BRIDGE 2
#define NETIO_DEVICE NETIO_DEVICE_SOCKETS
static int https_image_download(HTTP_INFO *hi, netio_t *io, const char *url);
int main(int argc, char *argv[])
{
char *url;
char data[1024], response[4096];
int i, ret, size;
HTTP_INFO hi;
#if NETIO_DEVICE == NETIO_DEVICE_SOCKETS
netio_t *io = netio_netsocket_create();
#elif NETIO_DEVICE == NETIO_DEVICE_ESP_BRIDGE
esp_debug_set_level(ESP_DBG_ERROR);
netio_t *io = netio_esp_create();
if (netio_esp_establish_bridge(io, ESP_BRIDGE_ADDRESS, ESP_BRIDGE_PORT)) {
printf("Failed to establish bridge\n");
}
else {
printf("Connected to remote ESP\n");
}
#else
#error "Invalid NETIO_DEVICE"
#endif
#define HTTP_LIB_USE
#ifdef HTTP_LIB_USE
// Init http session. verify: check the server CA cert.
http_init(&hi, TRUE, io);
const char *main_url = "https://xkcd.com/info.0.json";
int status = 0;
ret = http_get(&hi, main_url, response, sizeof(response), io);
printf("return code: %d \n", ret);
if (ret != 200)
{
printf("HTTPS request failed\n");
status = -1;
goto https_end;
}
int num_comics = -1;
if (json_parser_get_number(response, "num", &num_comics)) {
printf("Failed to parse json\n");
status = -1;
goto https_end;
}
if (num_comics <= 0) {
printf("Invalid num_comics %d\n", num_comics);
status = -1;
goto https_end;
}
printf("Num comics: %d\n", num_comics);
srand(time(NULL));
int selected = rand() % num_comics + 1;
printf("Selected: %d\n", selected);
char tmp[256];
sprintf(tmp, "https://xkcd.com/%d/info.0.json", selected);
ret = http_get(&hi, tmp, response, sizeof(response), io);
printf("return code: %d \n", ret);
if (ret != 200)
{
printf("HTTPS request failed\n");
status = -1;
goto https_end;
}
if (json_parser_get_string(response, "img", tmp, 256)) {
printf("Failed to get img URL from json\n");
status = -1;
goto https_end;
}
const char *image_url = tmp;
printf("Image URL: %s\n", image_url);
memset(response, 0, sizeof(response));
ret = http_get_header(&hi, image_url, response, sizeof(response), io);
printf("return code: %d \n", ret);
if (ret != 200)
{
printf("HTTPS request failed\n");
status = -1;
goto https_end;
}
int content_len = hi.response.content_length;
printf("Image size: %d\n", content_len);
printf("Response size: %d\n", strlen(response));
if (content_len > 150000) {
printf("Image too big\n");
status = -1;
goto https_end;
}
https_image_download(&hi, io, image_url);
https_end:
http_close(&hi);
io->disconnect(io);
if (status < 0) {
return status;
}
#endif
//#define HTTPS_IMG_DOWNLOAD
#ifdef HTTPS_IMG_DOWNLOAD
http_init(&hi, TRUE, io);
https_image_download(&hi, io);
http_close(&hi);
io->disconnect(io);
#endif
#ifdef RAW_ESP_REQUEST
if (io->connect(io, "httpbin.org", "80")) {
printf("Failed to connect to TCP server\n");
}
else {
printf("Connected to TCP server\n");
}
const char *request = "GET /ip HTTP/1.1\r\nHost:httpbin.org\r\n";
if (io->send(io, request, strlen(request)) <= 0) {
printf("Failed to send request\n");
}
else {
printf("HTTP request sent\n");
}
printf("Received HTTP:\n");
char buf[2048];
int res;
while(1) {
memset(buf, 0, 2048);
printf("HTTP resp read:\n");
if ((res = io->recv_timeout(io, buf, 400, 2000)) <= 0)
break;
printf("HTTP: %d:\n", res, buf);
int found_zero = 0;
for(int i=0;i<res;i++) {
putc(buf[i], stdout);
if (!found_zero && buf[i] == 0) {
found_zero = 1;
printf("0");
}
}
printf("\nHTTP DONE\n");
}
printf("Done\n");
#endif
#if NETIO_DEVICE == NETIO_DEVICE_SOCKETS
netio_netsocket_free(io);
#elif NETIO_DEVICE == NETIO_DEVICE_ESP_BRIDGE
netio_esp_free(io);
#endif
return 0;
}
#define HTTPS_RANGED_SIZE 1024
#define HTTPS_PRINT_VERBOSE 0
#define HTTPS_PRINT_PROGRESS 1
#define HTTPS_PRINT_PROGRESS_BARS 30
static int https_download(HTTP_INFO *hi, netio_t *io, int fd, const char *url)
{
char buf[HTTPS_RANGED_SIZE+1];
size_t content_len = 0;
size_t pos = 0;
#if HTTPS_PRINT_PROGRESS
int curr_progress = 0;
int progress_it;
#endif
while (content_len == 0 || pos < content_len) {
size_t range_end = pos + HTTPS_RANGED_SIZE - 1;
size_t len = 0;
if (content_len && range_end >= content_len)
range_end = content_len - 1;
#if HTTPS_PRINT_VERBOSE
printf("Get ranged %d to %d from %d\n", pos, range_end, content_len);
#endif
int ret = http_get_ranged(hi, url, buf, pos, range_end, &len, io);
if ( len == 0) {
printf("Failed to get ranged HTTPS\n");
return -1;
}
if (content_len == 0)
content_len = len;
else if (content_len != len) {
printf("Invalid content len %d\n", len);
return -1;
}
#if HTTPS_PRINT_PROGRESS
int progress = (int)((float)(range_end + 1) / (float)content_len * HTTPS_PRINT_PROGRESS_BARS);
if (curr_progress == 0) {
printf(" [");
for (progress_it = 0; progress_it < HTTPS_PRINT_PROGRESS_BARS - 1; progress_it++) {
printf("-");
}
printf("]\n=> ");
curr_progress = 1;
}
if (progress > curr_progress) {
for (progress_it = curr_progress; progress_it < progress; progress_it++) {
printf(".");
}
curr_progress = progress;
}
if (progress == HTTPS_PRINT_PROGRESS_BARS) {
printf(" Done!\n");
}
fflush(stdout);
#endif
size_t write_len = range_end - pos + 1;
ssize_t res = write(fd, buf, write_len);
if (write_len != res) {
printf("Write failed\n");
return -1;
}
pos = range_end + 1;
}
return 0;
}
static int https_image_download(HTTP_INFO *hi, netio_t *io, const char *url)
{
int fd = open("out.png", O_CREAT | O_RDWR | O_TRUNC, 0666);
int ret;
if (fd <= 0) {
printf("Failed to open out file\n");
return -1;
}
printf("Download image from %s\n", url);
ret = https_download(hi, io, fd, url);
if (!ret)
printf("Download complete\n");
else
printf("Download failed\n");
close(fd);
return ret;
}