blob: 9c9149bc83b70fb19b4768ed27d6df8b72a3ae54 [file] [log] [blame]
Andy Green9cb4f252019-12-26 02:35:41 +00001/*
2 * lws-minimal-http-client-h2-rxflow
3 *
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * This demonstrates the a minimal http client using lws.
10 *
11 * It visits https://warmcat.com/ and receives the html page there. You
12 * can dump the page data by changing the #if 0 below.
13 */
14
15#include <libwebsockets.h>
16#include <string.h>
17#include <signal.h>
18
19static int interrupted, bad = 1, status, each = 1024;
20static struct lws *client_wsi;
21
22static const lws_retry_bo_t retry = {
23 .secs_since_valid_ping = 3,
24 .secs_since_valid_hangup = 10,
25};
26
27struct pss {
28 lws_sorted_usec_list_t sul;
29 struct lws *wsi;
30};
31
32/*
33 * Once we're established, we ask the server for another 1KB every 250ms
34 * until we have it all.
35 */
36
37static void
38drain_cb(lws_sorted_usec_list_t *sul)
39{
40 struct pss *pss = lws_container_of(sul, struct pss, sul);
41
42 lws_h2_update_peer_txcredit(pss->wsi, LWS_H2_STREAM_SID, each);
43
44 lws_sul_schedule(lws_get_context(pss->wsi), 0, &pss->sul, drain_cb,
45 250 * LWS_US_PER_MS);
46}
47
48
49static int
50callback_http(struct lws *wsi, enum lws_callback_reasons reason,
51 void *user, void *in, size_t len)
52{
53 struct pss *pss = (struct pss *)user;
54
55 switch (reason) {
56
57 /* because we are protocols[0] ... */
58 case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
59 lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
60 in ? (char *)in : "(null)");
61 interrupted = 1;
62 break;
63
64 case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
65 {
66 char buf[128];
67
68 lws_get_peer_simple(wsi, buf, sizeof(buf));
69 status = lws_http_client_http_response(wsi);
70
71 lwsl_user("Connected to %s, http response: %d\n",
72 buf, status);
73 }
74 pss->wsi = wsi;
75 lws_sul_schedule(lws_get_context(wsi), 0, &pss->sul, drain_cb,
76 250 * LWS_US_PER_MS);
77 break;
78
79 /* chunks of chunked content, with header removed */
80 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
81 lwsl_user("RECEIVE_CLIENT_HTTP_READ: read %d\n", (int)len);
82
83#if 0 /* enable to dump the html */
84 {
85 const char *p = in;
86
87 while (len--)
88 if (*p < 0x7f)
89 putchar(*p++);
90 else
91 putchar('.');
92 }
93#endif
94 return 0; /* don't passthru */
95
96 /* uninterpreted http content */
97 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
98 {
99 char buffer[1024 + LWS_PRE];
100 char *px = buffer + LWS_PRE;
101 int lenx = sizeof(buffer) - LWS_PRE;
102
103 if (lws_http_client_read(wsi, &px, &lenx) < 0)
104 return -1;
105 }
106 return 0; /* don't passthru */
107
108 case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
109 lwsl_user("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
110 interrupted = 1;
111 bad = status != 200;
112 lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
113 break;
114
115 case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
116 interrupted = 1;
117 bad = status != 200;
118 lws_sul_schedule(lws_get_context(wsi), 0, &pss->sul, NULL,
119 LWS_SET_TIMER_USEC_CANCEL);
120 lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
121 break;
122
123 default:
124 break;
125 }
126
127 return lws_callback_http_dummy(wsi, reason, user, in, len);
128}
129
130static const struct lws_protocols protocols[] = {
131 {
132 "http",
133 callback_http,
134 sizeof(struct pss),
135 0,
136 },
137 { NULL, NULL, 0, 0 }
138};
139
140static void
141sigint_handler(int sig)
142{
143 interrupted = 1;
144}
145
146struct args {
147 int argc;
148 const char **argv;
149};
150
151static int
152system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
153 int current, int target)
154{
155 struct lws_context *context = mgr->parent;
156 struct lws_client_connect_info i;
157 struct args *a = lws_context_user(context);
158 const char *p;
159
160 if (current != LWS_SYSTATE_OPERATIONAL || target != LWS_SYSTATE_OPERATIONAL)
161 return 0;
162
163 lwsl_info("%s: operational\n", __func__);
164
165 memset(&i, 0, sizeof i); /* otherwise uninitialized garbage */
166 i.context = context;
167 if (!lws_cmdline_option(a->argc, a->argv, "-n"))
168 i.ssl_connection = LCCSCF_USE_SSL;
169
170 if (lws_cmdline_option(a->argc, a->argv, "-l")) {
171 i.port = 7681;
172 i.address = "localhost";
173 i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
174 } else {
175 i.port = 443;
176 i.address = "warmcat.com";
177 }
178
179 if (lws_cmdline_option(a->argc, a->argv, "--nossl"))
180 i.ssl_connection = 0;
181
182 i.ssl_connection |= LCCSCF_H2_QUIRK_OVERFLOWS_TXCR |
183 LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM;
184
185 i.alpn = "h2";
186 if (lws_cmdline_option(a->argc, a->argv, "--h1"))
187 i.alpn = "http/1.1";
188
189 if ((p = lws_cmdline_option(a->argc, a->argv, "-p")))
190 i.port = atoi(p);
191
192 if (lws_cmdline_option(a->argc, a->argv, "-j"))
193 i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
194
195 if (lws_cmdline_option(a->argc, a->argv, "-k"))
196 i.ssl_connection |= LCCSCF_ALLOW_INSECURE;
197
198 if (lws_cmdline_option(a->argc, a->argv, "-m"))
199 i.ssl_connection |= LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
200
201 if (lws_cmdline_option(a->argc, a->argv, "-e"))
202 i.ssl_connection |= LCCSCF_ALLOW_EXPIRED;
203
204 if ((p = lws_cmdline_option(a->argc, a->argv, "-f"))) {
205 i.ssl_connection |= LCCSCF_H2_MANUAL_RXFLOW;
206 i.manual_initial_tx_credit = atoi(p);
207 lwsl_notice("%s: manual peer tx credit %d\n", __func__,
208 i.manual_initial_tx_credit);
209 }
210
211 if ((p = lws_cmdline_option(a->argc, a->argv, "--each")))
212 each = atoi(p);
213
214 /* the default validity check is 5m / 5m10s... -v = 3s / 10s */
215
216 if (lws_cmdline_option(a->argc, a->argv, "-v"))
217 i.retry_and_idle_policy = &retry;
218
219 if ((p = lws_cmdline_option(a->argc, a->argv, "--server")))
220 i.address = p;
221
222 if ((p = lws_cmdline_option(a->argc, a->argv, "--path")))
223 i.path = p;
224 else
225 i.path = "/";
226
227 i.host = i.address;
228 i.origin = i.address;
229 i.method = "GET";
230
231 i.protocol = protocols[0].name;
232 i.pwsi = &client_wsi;
233
234 return !lws_client_connect_via_info(&i);
235}
236
237int main(int argc, const char **argv)
238{
Andy Greend88d4102020-02-29 21:07:28 +0000239 lws_state_notify_link_t notifier = { {0}, system_notify_cb, "app" };
Andy Green9cb4f252019-12-26 02:35:41 +0000240 lws_state_notify_link_t *na[] = { &notifier, NULL };
241 struct lws_context_creation_info info;
242 struct lws_context *context;
243 struct args args;
244 int n = 0;
245 // uint8_t memcert[4096];
246
247 args.argc = argc;
248 args.argv = argv;
249
250 signal(SIGINT, sigint_handler);
251
252 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
253 lws_cmdline_option_handle_builtin(argc, argv, &info);
254
255 lwsl_user("LWS minimal http client [-d<verbosity>] [-l] [--h1]\n");
256
257 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
258 info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
259 info.protocols = protocols;
260 info.user = &args;
261 info.register_notifier_list = na;
262
263 /*
264 * since we know this lws context is only ever going to be used with
265 * one client wsis / fds / sockets at a time, let lws know it doesn't
266 * have to use the default allocations for fd tables up to ulimit -n.
267 * It will just allocate for 1 internal and 1 (+ 1 http2 nwsi) that we
268 * will use.
269 */
270 info.fd_limit_per_thread = 1 + 1 + 1;
271
272#if defined(LWS_WITH_MBEDTLS)
273 /*
274 * OpenSSL uses the system trust store. mbedTLS has to be told which
275 * CA to trust explicitly.
276 */
277 info.client_ssl_ca_filepath = "./warmcat.com.cer";
278#endif
279#if 0
280 n = open("./warmcat.com.cer", O_RDONLY);
281 if (n >= 0) {
282 info.client_ssl_ca_mem_len = read(n, memcert, sizeof(memcert));
283 info.client_ssl_ca_mem = memcert;
284 close(n);
285 n = 0;
286 memcert[info.client_ssl_ca_mem_len++] = '\0';
287 }
288#endif
289 context = lws_create_context(&info);
290 if (!context) {
291 lwsl_err("lws init failed\n");
292 return 1;
293 }
294
295 while (n >= 0 && !interrupted)
296 n = lws_service(context, 0);
297
298 lws_context_destroy(context);
299 lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
300
301 return bad;
302}