blob: 69add1a6e9599b441ecfca50786c701237e6ca93 [file] [log] [blame]
William Lallemand83614a92021-08-13 14:47:57 +02001/*
2 * HTTP Client
3 *
4 * Copyright (C) 2021 HAProxy Technologies, William Lallemand <wlallemand@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * This file implements an HTTP Client API.
12 *
13 */
William Lallemand83614a92021-08-13 14:47:57 +020014
William Lallemand2a8fe8b2021-08-20 14:25:15 +020015#include <haproxy/api.h>
William Lallemand33b0d092021-08-13 16:05:53 +020016#include <haproxy/applet.h>
17#include <haproxy/cli.h>
William Lallemandcf5cb0b2022-04-22 14:48:45 +020018#include <haproxy/ssl_ckch.h>
William Lallemand33b0d092021-08-13 16:05:53 +020019#include <haproxy/dynbuf.h>
William Lallemand83614a92021-08-13 14:47:57 +020020#include <haproxy/cfgparse.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010021#include <haproxy/conn_stream.h>
22#include <haproxy/cs_utils.h>
William Lallemand83614a92021-08-13 14:47:57 +020023#include <haproxy/global.h>
William Lallemand0da616e2021-10-28 15:34:26 +020024#include <haproxy/istbuf.h>
William Lallemand33b0d092021-08-13 16:05:53 +020025#include <haproxy/h1_htx.h>
26#include <haproxy/http.h>
William Lallemand2b7dc4e2022-02-24 16:55:41 +010027#include <haproxy/http_ana-t.h>
William Lallemand33b0d092021-08-13 16:05:53 +020028#include <haproxy/http_client.h>
29#include <haproxy/http_htx.h>
30#include <haproxy/htx.h>
William Lallemand83614a92021-08-13 14:47:57 +020031#include <haproxy/log.h>
32#include <haproxy/proxy.h>
William Lallemand2a8fe8b2021-08-20 14:25:15 +020033#include <haproxy/server.h>
Willy Tarreau1df20422021-10-06 11:28:24 +020034#include <haproxy/ssl_sock-t.h>
William Lallemand83614a92021-08-13 14:47:57 +020035#include <haproxy/tools.h>
36
37#include <string.h>
38
39
40static struct proxy *httpclient_proxy;
41static struct server *httpclient_srv_raw;
William Lallemand957ab132021-08-24 18:33:28 +020042#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +020043static struct server *httpclient_srv_ssl;
William Lallemandf1344b32022-04-26 12:00:06 +020044static int httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED;
William Lallemand957ab132021-08-24 18:33:28 +020045#endif
William Lallemand33b0d092021-08-13 16:05:53 +020046static struct applet httpclient_applet;
47
William Lallemandeaa703e2022-04-22 17:52:33 +020048
William Lallemand03a4eb12021-08-18 16:46:21 +020049/* --- This part of the file implement an HTTP client over the CLI ---
50 * The functions will be starting by "hc_cli" for "httpclient cli"
51 */
52
William Lallemand03a4eb12021-08-18 16:46:21 +020053/* What kind of data we need to read */
54#define HC_CLI_F_RES_STLINE 0x01
55#define HC_CLI_F_RES_HDR 0x02
56#define HC_CLI_F_RES_BODY 0x04
57#define HC_CLI_F_RES_END 0x08
58
59
60/* These are the callback used by the HTTP Client when it needs to notify new
61 * data, we only sets a flag in the IO handler */
62
63void hc_cli_res_stline_cb(struct httpclient *hc)
64{
65 struct appctx *appctx = hc->caller;
66
William Lallemanddfc3f892021-08-20 11:35:29 +020067 if (!appctx)
68 return;
69
William Lallemand03a4eb12021-08-18 16:46:21 +020070 appctx->ctx.cli.i0 |= HC_CLI_F_RES_STLINE;
William Lallemanddfc3f892021-08-20 11:35:29 +020071 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020072}
73
74void hc_cli_res_headers_cb(struct httpclient *hc)
75{
76 struct appctx *appctx = hc->caller;
77
William Lallemanddfc3f892021-08-20 11:35:29 +020078 if (!appctx)
79 return;
80
William Lallemand03a4eb12021-08-18 16:46:21 +020081 appctx->ctx.cli.i0 |= HC_CLI_F_RES_HDR;
William Lallemanddfc3f892021-08-20 11:35:29 +020082 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020083}
84
85void hc_cli_res_body_cb(struct httpclient *hc)
86{
87 struct appctx *appctx = hc->caller;
88
William Lallemanddfc3f892021-08-20 11:35:29 +020089 if (!appctx)
90 return;
91
William Lallemand03a4eb12021-08-18 16:46:21 +020092 appctx->ctx.cli.i0 |= HC_CLI_F_RES_BODY;
William Lallemanddfc3f892021-08-20 11:35:29 +020093 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020094}
95
96void hc_cli_res_end_cb(struct httpclient *hc)
97{
98 struct appctx *appctx = hc->caller;
99
William Lallemanddfc3f892021-08-20 11:35:29 +0200100 if (!appctx)
101 return;
102
William Lallemand03a4eb12021-08-18 16:46:21 +0200103 appctx->ctx.cli.i0 |= HC_CLI_F_RES_END;
William Lallemanddfc3f892021-08-20 11:35:29 +0200104 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200105}
106
107/*
108 * Parse an httpclient keyword on the cli:
109 * httpclient <ID> <method> <URI>
110 */
111static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private)
112{
113 struct httpclient *hc;
114 char *err = NULL;
115 enum http_meth_t meth;
116 char *meth_str;
117 struct ist uri;
William Lallemanddec25c32021-10-25 19:48:37 +0200118 struct ist body = IST_NULL;
William Lallemand03a4eb12021-08-18 16:46:21 +0200119
120 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
121 return 1;
122
123 if (!*args[1] || !*args[2]) {
124 memprintf(&err, ": not enough parameters");
125 goto err;
126 }
127
128 meth_str = args[1];
129 uri = ist(args[2]);
130
William Lallemanddec25c32021-10-25 19:48:37 +0200131 if (payload)
132 body = ist(payload);
133
William Lallemand03a4eb12021-08-18 16:46:21 +0200134 meth = find_http_meth(meth_str, strlen(meth_str));
135
136 hc = httpclient_new(appctx, meth, uri);
137 if (!hc) {
138 goto err;
139 }
140
141 /* update the httpclient callbacks */
142 hc->ops.res_stline = hc_cli_res_stline_cb;
143 hc->ops.res_headers = hc_cli_res_headers_cb;
144 hc->ops.res_payload = hc_cli_res_body_cb;
145 hc->ops.res_end = hc_cli_res_end_cb;
146
147 appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */
148 appctx->ctx.cli.i0 = 0;
149
William Lallemandbad9c8c2022-01-14 14:10:33 +0100150 if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE)
William Lallemand03a4eb12021-08-18 16:46:21 +0200151 goto err;
152
153
154 if (!httpclient_start(hc))
155 goto err;
156
157 return 0;
158
159err:
160 memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : "");
161 return cli_err(appctx, err);
162}
163
164/* This function dumps the content of the httpclient receive buffer
165 * on the CLI output
166 *
167 * Return 1 when the processing is finished
168 * return 0 if it needs to be called again
169 */
170static int hc_cli_io_handler(struct appctx *appctx)
171{
Christopher Faulet908628c2022-03-25 16:43:49 +0100172 struct conn_stream *cs = appctx->owner;
William Lallemand03a4eb12021-08-18 16:46:21 +0200173 struct buffer *trash = alloc_trash_chunk();
174 struct httpclient *hc = appctx->ctx.cli.p0;
175 struct http_hdr *hdrs, *hdr;
176
177 if (!trash)
178 goto out;
179 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_STLINE) {
William Lallemandde6ecc32022-02-16 11:37:02 +0100180 chunk_appendf(trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn),
181 hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason));
Christopher Faulet908628c2022-03-25 16:43:49 +0100182 if (ci_putchk(cs_ic(cs), trash) == -1)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200183 cs_rx_room_blk(cs);
William Lallemand03a4eb12021-08-18 16:46:21 +0200184 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_STLINE;
185 goto out;
186 }
187
188 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_HDR) {
189 hdrs = hc->res.hdrs;
190 for (hdr = hdrs; isttest(hdr->v); hdr++) {
191 if (!h1_format_htx_hdr(hdr->n, hdr->v, trash))
192 goto out;
193 }
194 if (!chunk_memcat(trash, "\r\n", 2))
195 goto out;
Christopher Faulet908628c2022-03-25 16:43:49 +0100196 if (ci_putchk(cs_ic(cs), trash) == -1)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200197 cs_rx_room_blk(cs);
William Lallemand03a4eb12021-08-18 16:46:21 +0200198 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_HDR;
199 goto out;
200 }
201
202 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_BODY) {
203 int ret;
204
Christopher Faulet908628c2022-03-25 16:43:49 +0100205 ret = httpclient_res_xfer(hc, cs_ib(cs));
206 channel_add_input(cs_ic(cs), ret); /* forward what we put in the buffer channel */
William Lallemand03a4eb12021-08-18 16:46:21 +0200207
William Lallemand518878e2021-09-21 10:45:34 +0200208 if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */
William Lallemand03a4eb12021-08-18 16:46:21 +0200209 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_BODY;
210 }
211 goto out;
212 }
213
214 /* we must close only if F_END is the last flag */
215 if (appctx->ctx.cli.i0 == HC_CLI_F_RES_END) {
Christopher Fauletda098e62022-03-31 17:44:45 +0200216 cs_shutw(cs);
217 cs_shutr(cs);
William Lallemand03a4eb12021-08-18 16:46:21 +0200218 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_END;
219 goto out;
220 }
221
222out:
223 /* we didn't clear every flags, we should come back to finish things */
224 if (appctx->ctx.cli.i0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200225 cs_rx_room_blk(cs);
William Lallemand03a4eb12021-08-18 16:46:21 +0200226
227 free_trash_chunk(trash);
228 return 0;
229}
230
231static void hc_cli_release(struct appctx *appctx)
232{
233 struct httpclient *hc = appctx->ctx.cli.p0;
234
235 /* Everything possible was printed on the CLI, we can destroy the client */
William Lallemandecb83e12021-09-28 11:00:43 +0200236 httpclient_stop_and_destroy(hc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200237
238 return;
239}
240
241/* register cli keywords */
242static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2c8f9842022-02-18 16:26:36 +0100243 { { "httpclient", NULL }, "httpclient <method> <URI> : launch an HTTP request", hc_cli_parse, hc_cli_io_handler, hc_cli_release, NULL, ACCESS_EXPERT},
William Lallemand03a4eb12021-08-18 16:46:21 +0200244 { { NULL }, NULL, NULL, NULL }
245}};
246
247INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
248
249
250/* --- This part of the file implements the actual HTTP client API --- */
251
William Lallemand33b0d092021-08-13 16:05:53 +0200252/*
253 * Generate a simple request and fill the httpclient request buffer with it.
254 * The request contains a request line generated from the absolute <url> and
255 * <meth> as well as list of headers <hdrs>.
256 *
257 * If the buffer was filled correctly the function returns 0, if not it returns
258 * an error_code but there is no guarantee that the buffer wasn't modified.
259 */
William Lallemanddec25c32021-10-25 19:48:37 +0200260int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_meth_t meth, const struct http_hdr *hdrs, const struct ist payload)
William Lallemand33b0d092021-08-13 16:05:53 +0200261{
262 struct htx_sl *sl;
263 struct htx *htx;
264 int err_code = 0;
265 struct ist meth_ist, vsn;
William Lallemanddec25c32021-10-25 19:48:37 +0200266 unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM;
William Lallemandf03b53c2021-11-24 15:38:17 +0100267 int i;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100268 int foundhost = 0, foundaccept = 0, foundua = 0;
William Lallemand33b0d092021-08-13 16:05:53 +0200269
Christopher Faulet600985d2022-01-12 11:14:08 +0100270 if (!b_alloc(&hc->req.buf))
271 goto error;
272
William Lallemand33b0d092021-08-13 16:05:53 +0200273 if (meth >= HTTP_METH_OTHER)
274 goto error;
275
276 meth_ist = http_known_methods[meth];
277
278 vsn = ist("HTTP/1.1");
279
280 htx = htx_from_buf(&hc->req.buf);
281 if (!htx)
282 goto error;
William Lallemande1e045f2022-01-14 14:08:34 +0100283
284 if (!hc->ops.req_payload && !isttest(payload))
285 flags |= HTX_SL_F_BODYLESS;
286
William Lallemand33b0d092021-08-13 16:05:53 +0200287 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn);
288 if (!sl) {
289 goto error;
290 }
291 sl->info.req.meth = meth;
292
William Lallemandf03b53c2021-11-24 15:38:17 +0100293 for (i = 0; hdrs && hdrs[i].n.len; i++) {
294 /* Don't check the value length because a header value may be empty */
295 if (isttest(hdrs[i].v) == 0)
296 continue;
297
298 if (isteqi(hdrs[i].n, ist("host")))
299 foundhost = 1;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100300 else if (isteqi(hdrs[i].n, ist("accept")))
301 foundaccept = 1;
302 else if (isteqi(hdrs[i].n, ist("user-agent")))
303 foundua = 1;
William Lallemandf03b53c2021-11-24 15:38:17 +0100304
305 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
306 goto error;
307 }
William Lallemand33b0d092021-08-13 16:05:53 +0200308
William Lallemandf03b53c2021-11-24 15:38:17 +0100309 if (!foundhost) {
310 /* Add Host Header from URL */
311 if (!htx_add_header(htx, ist("Host"), ist("h")))
William Lallemand79a34782021-09-20 16:19:15 +0200312 goto error;
William Lallemandf03b53c2021-11-24 15:38:17 +0100313 if (!http_update_host(htx, sl, url))
William Lallemand79a34782021-09-20 16:19:15 +0200314 goto error;
315 }
William Lallemand33b0d092021-08-13 16:05:53 +0200316
William Lallemandbad9c8c2022-01-14 14:10:33 +0100317 if (!foundaccept) {
318 if (!htx_add_header(htx, ist("Accept"), ist("*/*")))
319 goto error;
320 }
321
322 if (!foundua) {
323 if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT)))
324 goto error;
325 }
326
327
William Lallemandf03b53c2021-11-24 15:38:17 +0100328 if (!htx_add_endof(htx, HTX_BLK_EOH))
329 goto error;
330
William Lallemanddec25c32021-10-25 19:48:37 +0200331 if (isttest(payload)) {
332 /* add the payload if it can feat in the buffer, no need to set
333 * the Content-Length, the data will be sent chunked */
334 if (!htx_add_data_atonce(htx, payload))
335 goto error;
336 }
337
William Lallemand0da616e2021-10-28 15:34:26 +0200338 /* If req.payload was set, does not set the end of stream which *MUST*
339 * be set in the callback */
340 if (!hc->ops.req_payload)
341 htx->flags |= HTX_FL_EOM;
William Lallemand33b0d092021-08-13 16:05:53 +0200342
343 htx_to_buf(htx, &hc->req.buf);
344
345 return 0;
346error:
347 err_code |= ERR_ALERT | ERR_ABORT;
348 return err_code;
349}
350
351/*
352 * transfer the response to the destination buffer and wakeup the HTTP client
353 * applet so it could fill again its buffer.
354 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500355 * Return the number of bytes transferred.
William Lallemand33b0d092021-08-13 16:05:53 +0200356 */
357int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst)
358{
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100359 size_t room = b_room(dst);
William Lallemand33b0d092021-08-13 16:05:53 +0200360 int ret;
361
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100362 ret = b_force_xfer(dst, &hc->res.buf, MIN(room, b_data(&hc->res.buf)));
William Lallemand33b0d092021-08-13 16:05:53 +0200363 /* call the client once we consumed all data */
Christopher Faulet600985d2022-01-12 11:14:08 +0100364 if (!b_data(&hc->res.buf)) {
365 b_free(&hc->res.buf);
366 if (hc->appctx)
367 appctx_wakeup(hc->appctx);
368 }
William Lallemand33b0d092021-08-13 16:05:53 +0200369 return ret;
370}
371
372/*
William Lallemand0da616e2021-10-28 15:34:26 +0200373 * Transfer raw HTTP payload from src, and insert it into HTX format in the
374 * httpclient.
375 *
376 * Must be used to transfer the request body.
377 * Then wakeup the httpclient so it can transfer it.
378 *
379 * <end> tries to add the ending data flag if it succeed to copy all data.
380 *
381 * Return the number of bytes copied from src.
382 */
383int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end)
384{
385 int ret = 0;
386 struct htx *htx;
387
Christopher Faulet600985d2022-01-12 11:14:08 +0100388 if (!b_alloc(&hc->req.buf))
389 goto error;
390
William Lallemand0da616e2021-10-28 15:34:26 +0200391 htx = htx_from_buf(&hc->req.buf);
392 if (!htx)
393 goto error;
394
395 if (hc->appctx)
396 appctx_wakeup(hc->appctx);
397
398 ret += htx_add_data(htx, src);
399
400
401 /* if we copied all the data and the end flag is set */
402 if ((istlen(src) == ret) && end) {
403 htx->flags |= HTX_FL_EOM;
404 }
405 htx_to_buf(htx, &hc->req.buf);
406
407error:
408
409 return ret;
410}
411
William Lallemandb4a4ef62022-02-23 14:18:16 +0100412/* Set the 'timeout server' in ms for the next httpclient request */
413void httpclient_set_timeout(struct httpclient *hc, int timeout)
414{
415 hc->timeout_server = timeout;
416}
417
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100418/*
419 * Sets a destination for the httpclient from an HAProxy addr format
420 * This will prevent to determine the destination from the URL
421 * Return 0 in case of success or -1 otherwise.
422 */
423int httpclient_set_dst(struct httpclient *hc, const char *dst)
424{
425 struct sockaddr_storage *sk;
426 char *errmsg = NULL;
427
428 sockaddr_free(&hc->dst);
429 /* 'sk' is statically allocated (no need to be freed). */
430 sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL,
431 &errmsg, NULL, NULL,
432 PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
433 if (!sk) {
434 ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg);
435 free(errmsg);
436 return -1;
437 }
438
439 if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) {
440 ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__);
441 return -1;
442 }
443
444 return 0;
445}
William Lallemand0da616e2021-10-28 15:34:26 +0200446
447/*
William Lallemand33b0d092021-08-13 16:05:53 +0200448 * Start the HTTP client
449 * Create the appctx, session, stream and wakeup the applet
450 *
451 * FIXME: It also fill the sockaddr with the IP address, but currently only IP
452 * in the URL are supported, it lacks a resolver.
453 *
454 * Return the <appctx> or NULL if it failed
455 */
456struct appctx *httpclient_start(struct httpclient *hc)
457{
458 struct applet *applet = &httpclient_applet;
459 struct appctx *appctx;
460 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100461 struct conn_stream *cs;
William Lallemand33b0d092021-08-13 16:05:53 +0200462 struct stream *s;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100463 struct sockaddr_storage *addr = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200464 int len;
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100465 struct sockaddr_storage ss_url;
466 struct sockaddr_storage* ss_dst;
William Lallemand33b0d092021-08-13 16:05:53 +0200467 struct split_url out;
William Lallemand4006b0f2022-04-25 18:23:35 +0200468 enum obj_type *target = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200469
William Lallemand5085bc32022-02-17 12:52:09 +0100470 /* if the client was started and not ended, an applet is already
471 * running, we shouldn't try anything */
472 if (httpclient_started(hc) && !httpclient_ended(hc))
473 return NULL;
474
475 hc->flags = 0;
476
William Lallemand33b0d092021-08-13 16:05:53 +0200477 /* parse URI and fill sockaddr_storage */
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100478 len = url2sa(istptr(hc->req.url), istlen(hc->req.url), &ss_url, &out);
William Lallemand33b0d092021-08-13 16:05:53 +0200479 if (len == -1) {
William Lallemand614e6832021-09-26 18:12:43 +0200480 ha_alert("httpclient: cannot parse uri '%s'.\n", istptr(hc->req.url));
William Lallemand33b0d092021-08-13 16:05:53 +0200481 goto out;
482 }
483
484 /* The HTTP client will be created in the same thread as the caller,
485 * avoiding threading issues */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100486 appctx = appctx_new(applet, NULL);
William Lallemand33b0d092021-08-13 16:05:53 +0200487 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100488 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200489
490 sess = session_new(httpclient_proxy, NULL, &appctx->obj_type);
491 if (!sess) {
492 ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__);
493 goto out_free_appctx;
494 }
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100495
William Lallemand4006b0f2022-04-25 18:23:35 +0200496 /* choose the SSL server or not */
497 switch (out.scheme) {
498 case SCH_HTTP:
499 target = &httpclient_srv_raw->obj_type;
500 break;
501 case SCH_HTTPS:
502#ifdef USE_OPENSSL
503 if (httpclient_srv_ssl) {
504 target = &httpclient_srv_ssl->obj_type;
505 } else {
506 ha_alert("httpclient: SSL was disabled (wrong verify/ca-file)!\n");
507 goto out_free_sess;
508 }
509#else
510 ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__);
511 goto out_free_sess;
512#endif
513 break;
514 }
515
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100516 /* if httpclient_set_dst() was used, sets the alternative address */
517 if (hc->dst)
518 ss_dst = hc->dst;
519 else
520 ss_dst = &ss_url;
521
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100522 if (!sockaddr_alloc(&addr, ss_dst, sizeof(*hc->dst)))
523 goto out_free_sess;
524
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100525 cs = cs_new_from_applet(appctx->endp, sess, &hc->req.buf);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100526 if (!cs) {
527 ha_alert("httpclient: Failed to initialize stream %s:%d.\n", __FUNCTION__, __LINE__);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100528 goto out_free_addr;
William Lallemand33b0d092021-08-13 16:05:53 +0200529 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100530 s = DISGUISE(cs_strm(cs));
531
William Lallemand4006b0f2022-04-25 18:23:35 +0200532 s->target = target;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100533 /* set the "timeout server" */
534 s->req.wto = hc->timeout_server;
535 s->res.rto = hc->timeout_server;
William Lallemand33b0d092021-08-13 16:05:53 +0200536
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200537 s->csb->dst = addr;
Christopher Faulet8abe7122022-03-30 15:10:18 +0200538 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100539 s->flags |= SF_ASSIGNED|SF_ADDR_SET;
William Lallemand33b0d092021-08-13 16:05:53 +0200540 s->res.flags |= CF_READ_DONTWAIT;
541
542 /* applet is waiting for data */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200543 cs_cant_get(s->csf);
William Lallemand33b0d092021-08-13 16:05:53 +0200544 appctx_wakeup(appctx);
545
William Lallemand33b0d092021-08-13 16:05:53 +0200546 hc->appctx = appctx;
William Lallemandb8b13702021-09-28 12:15:37 +0200547 hc->flags |= HTTPCLIENT_FS_STARTED;
William Lallemand33b0d092021-08-13 16:05:53 +0200548 appctx->ctx.httpclient.ptr = hc;
Christopher Faulet6ced61d2022-01-12 15:27:41 +0100549
550 /* The request was transferred when the stream was created. So switch
551 * directly to REQ_BODY or RES_STLINE state
552 */
553 appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE);
William Lallemand33b0d092021-08-13 16:05:53 +0200554
555 return appctx;
556
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100557out_free_addr:
558 sockaddr_free(&addr);
William Lallemand33b0d092021-08-13 16:05:53 +0200559out_free_sess:
560 session_free(sess);
561out_free_appctx:
562 appctx_free(appctx);
563out:
564
565 return NULL;
566}
567
William Lallemandecb83e12021-09-28 11:00:43 +0200568/*
569 * This function tries to destroy the httpclient if it wasn't running.
570 * If it was running, stop the client and ask it to autodestroy itself.
571 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500572 * Once this function is used, all pointer sto the client must be removed
William Lallemandecb83e12021-09-28 11:00:43 +0200573 *
574 */
575void httpclient_stop_and_destroy(struct httpclient *hc)
576{
577
William Lallemandb8b13702021-09-28 12:15:37 +0200578 /* The httpclient was already stopped or never started, we can safely destroy it */
579 if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) {
William Lallemandecb83e12021-09-28 11:00:43 +0200580 httpclient_destroy(hc);
581 } else {
582 /* if the client wasn't stopped, ask for a stop and destroy */
583 hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP);
584 if (hc->appctx)
585 appctx_wakeup(hc->appctx);
586 }
587}
588
William Lallemand33b0d092021-08-13 16:05:53 +0200589/* Free the httpclient */
590void httpclient_destroy(struct httpclient *hc)
591{
William Lallemand03f5a1c2021-09-27 15:17:47 +0200592 struct http_hdr *hdrs;
593
594
William Lallemand33b0d092021-08-13 16:05:53 +0200595 if (!hc)
596 return;
William Lallemandecb83e12021-09-28 11:00:43 +0200597
William Lallemand2a879002021-10-05 15:50:45 +0200598 /* we should never destroy a client which was started but not stopped */
599 BUG_ON(httpclient_started(hc) && !httpclient_ended(hc));
William Lallemandecb83e12021-09-28 11:00:43 +0200600
William Lallemand03f5a1c2021-09-27 15:17:47 +0200601 /* request */
602 istfree(&hc->req.url);
William Lallemand33b0d092021-08-13 16:05:53 +0200603 b_free(&hc->req.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200604 /* response */
605 istfree(&hc->res.vsn);
606 istfree(&hc->res.reason);
607 hdrs = hc->res.hdrs;
608 while (hdrs && isttest(hdrs->n)) {
609 istfree(&hdrs->n);
610 istfree(&hdrs->v);
611 hdrs++;
612 }
613 ha_free(&hc->res.hdrs);
William Lallemand33b0d092021-08-13 16:05:53 +0200614 b_free(&hc->res.buf);
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100615 sockaddr_free(&hc->dst);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200616
William Lallemand33b0d092021-08-13 16:05:53 +0200617 free(hc);
618
619 return;
620}
621
622/* Allocate an httpclient and its buffers
623 * Return NULL on failure */
624struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url)
625{
626 struct httpclient *hc;
William Lallemand33b0d092021-08-13 16:05:53 +0200627
628 hc = calloc(1, sizeof(*hc));
629 if (!hc)
630 goto err;
631
Christopher Faulet600985d2022-01-12 11:14:08 +0100632 hc->req.buf = BUF_NULL;
633 hc->res.buf = BUF_NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200634 hc->caller = caller;
William Lallemand67b77842021-11-10 16:57:25 +0100635 hc->req.url = istdup(url);
William Lallemand33b0d092021-08-13 16:05:53 +0200636 hc->req.meth = meth;
637
638 return hc;
639
640err:
641 httpclient_destroy(hc);
642 return NULL;
643}
644
645static void httpclient_applet_io_handler(struct appctx *appctx)
646{
647 struct httpclient *hc = appctx->ctx.httpclient.ptr;
Christopher Faulet908628c2022-03-25 16:43:49 +0100648 struct conn_stream *cs = appctx->owner;
649 struct stream *s = __cs_strm(cs);
William Lallemand33b0d092021-08-13 16:05:53 +0200650 struct channel *req = &s->req;
651 struct channel *res = &s->res;
652 struct htx_blk *blk = NULL;
653 struct htx *htx;
William Lallemandb7020302021-08-20 11:24:13 +0200654 struct htx_sl *sl = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200655 int32_t pos;
656 uint32_t hdr_num;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100657 uint32_t sz;
William Lallemand933fe392021-11-04 09:45:58 +0100658 int ret;
William Lallemand33b0d092021-08-13 16:05:53 +0200659
660
661 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200662
663 /* required to stop */
664 if (hc->flags & HTTPCLIENT_FA_STOP)
665 goto end;
666
William Lallemand33b0d092021-08-13 16:05:53 +0200667 switch(appctx->st0) {
668
669 case HTTPCLIENT_S_REQ:
William Lallemanddb8a1f32021-11-08 16:55:14 +0100670 /* we know that the buffer is empty here, since
671 * it's the first call, we can freely copy the
672 * request from the httpclient buffer */
William Lallemand933fe392021-11-04 09:45:58 +0100673 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
William Lallemanddb8a1f32021-11-08 16:55:14 +0100674 if (!ret)
675 goto more;
William Lallemand933fe392021-11-04 09:45:58 +0100676
Christopher Faulet600985d2022-01-12 11:14:08 +0100677 if (!b_data(&hc->req.buf))
678 b_free(&hc->req.buf);
679
William Lallemanddb8a1f32021-11-08 16:55:14 +0100680 htx = htx_from_buf(&req->buf);
William Lallemand933fe392021-11-04 09:45:58 +0100681 if (!htx)
682 goto more;
683
William Lallemanddb8a1f32021-11-08 16:55:14 +0100684 channel_add_input(req, htx->data);
685
William Lallemand933fe392021-11-04 09:45:58 +0100686 if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */
687 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
688 else
689 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
690
William Lallemand33b0d092021-08-13 16:05:53 +0200691 goto more; /* we need to leave the IO handler once we wrote the request */
692 break;
William Lallemand0da616e2021-10-28 15:34:26 +0200693 case HTTPCLIENT_S_REQ_BODY:
694 /* call the payload callback */
695 {
696 if (hc->ops.req_payload) {
William Lallemandccc7ee42022-03-18 17:57:15 +0100697 struct htx *hc_htx;
William Lallemand0da616e2021-10-28 15:34:26 +0200698
William Lallemand0da616e2021-10-28 15:34:26 +0200699 /* call the request callback */
700 hc->ops.req_payload(hc);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100701
William Lallemandccc7ee42022-03-18 17:57:15 +0100702 hc_htx = htx_from_buf(&hc->req.buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100703 htx = htx_from_buf(&req->buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100704
William Lallemandccc7ee42022-03-18 17:57:15 +0100705 if (htx_is_empty(hc_htx))
William Lallemanddb8a1f32021-11-08 16:55:14 +0100706 goto more;
Christopher Faulet600985d2022-01-12 11:14:08 +0100707
William Lallemandccc7ee42022-03-18 17:57:15 +0100708 if (htx_is_empty(htx)) {
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200709 size_t data = hc_htx->data;
710
William Lallemandccc7ee42022-03-18 17:57:15 +0100711 /* Here htx_to_buf() will set buffer data to 0 because
712 * the HTX is empty, and allow us to do an xfer.
713 */
714 htx_to_buf(hc_htx, &hc->req.buf);
715 htx_to_buf(htx, &req->buf);
William Lallemandccc7ee42022-03-18 17:57:15 +0100716 b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200717 channel_add_input(req, data);
William Lallemandccc7ee42022-03-18 17:57:15 +0100718 } else {
719 struct htx_ret ret;
Christopher Faulet600985d2022-01-12 11:14:08 +0100720
William Lallemandccc7ee42022-03-18 17:57:15 +0100721 ret = htx_xfer_blks(htx, hc_htx, hc_htx->data, HTX_BLK_UNUSED);
722 channel_add_input(req, ret.ret);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100723
William Lallemandccc7ee42022-03-18 17:57:15 +0100724 /* we must copy the EOM if we empty the buffer */
725 if (htx_is_empty(hc_htx)) {
726 htx->flags |= (hc_htx->flags & HTX_FL_EOM);
727 }
728 htx_to_buf(htx, &req->buf);
729 htx_to_buf(hc_htx, &hc->req.buf);
730 }
731
732
733 if (!b_data(&hc->req.buf))
734 b_free(&hc->req.buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200735 }
736
William Lallemanddb8a1f32021-11-08 16:55:14 +0100737 htx = htx_from_buf(&req->buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200738 if (!htx)
739 goto more;
740
741 /* if the request contains the HTX_FL_EOM, we finished the request part. */
Christopher Faulet3d433242022-03-03 15:38:39 +0100742 if (htx->flags & HTX_FL_EOM) {
Christopher Faulet908628c2022-03-25 16:43:49 +0100743 cs->endp->flags |= CS_EP_EOI;
Christopher Faulet3d433242022-03-03 15:38:39 +0100744 req->flags |= CF_EOI;
William Lallemand0da616e2021-10-28 15:34:26 +0200745 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
Christopher Faulet3d433242022-03-03 15:38:39 +0100746 }
William Lallemand0da616e2021-10-28 15:34:26 +0200747
William Lallemand1eca8942022-03-17 14:57:23 +0100748 goto process_data; /* we need to leave the IO handler once we wrote the request */
William Lallemand0da616e2021-10-28 15:34:26 +0200749 }
750 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200751
752 case HTTPCLIENT_S_RES_STLINE:
753 /* copy the start line in the hc structure,then remove the htx block */
William Lallemanda625b032022-03-17 14:45:46 +0100754 if (!co_data(res))
William Lallemand33b0d092021-08-13 16:05:53 +0200755 goto more;
756 htx = htxbuf(&res->buf);
757 if (!htx)
758 goto more;
William Lallemand97f69c62022-03-10 17:23:40 +0100759 blk = htx_get_head_blk(htx);
William Lallemand33b0d092021-08-13 16:05:53 +0200760 if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL))
761 sl = htx_get_blk_ptr(htx, blk);
762 if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP)))
763 goto more;
764
765 /* copy the status line in the httpclient */
766 hc->res.status = sl->info.res.status;
767 hc->res.vsn = istdup(htx_sl_res_vsn(sl));
768 hc->res.reason = istdup(htx_sl_res_reason(sl));
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100769 sz = htx_get_blksz(blk);
770 co_set_data(res, co_data(res) - sz);
771 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200772 /* caller callback */
773 if (hc->ops.res_stline)
774 hc->ops.res_stline(hc);
775
776 /* if there is no HTX data anymore and the EOM flag is
777 * set, leave (no body) */
778 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM)
779 appctx->st0 = HTTPCLIENT_S_RES_END;
780 else
781 appctx->st0 = HTTPCLIENT_S_RES_HDR;
782 break;
783
784 case HTTPCLIENT_S_RES_HDR:
785 /* first copy the headers in a local hdrs
786 * structure, once we the total numbers of the
787 * header we allocate the right size and copy
788 * them. The htx block of the headers are
789 * removed each time one is read */
790 {
791 struct http_hdr hdrs[global.tune.max_http_hdr];
792
William Lallemanda625b032022-03-17 14:45:46 +0100793 if (!co_data(res))
William Lallemand33b0d092021-08-13 16:05:53 +0200794 goto more;
795 htx = htxbuf(&res->buf);
796 if (!htx)
797 goto more;
798
799 hdr_num = 0;
800
William Lallemand97f69c62022-03-10 17:23:40 +0100801 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
William Lallemand33b0d092021-08-13 16:05:53 +0200802 struct htx_blk *blk = htx_get_blk(htx, pos);
803 enum htx_blk_type type = htx_get_blk_type(blk);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100804 uint32_t sz = htx_get_blksz(blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200805
William Lallemandc020b252022-03-09 18:56:02 +0100806 if (type == HTX_BLK_UNUSED) {
807 c_rew(res, sz);
808 htx_remove_blk(htx, blk);
809 }
810
811 if (type == HTX_BLK_HDR) {
812 hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk));
813 hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk));
814 if (!isttest(hdrs[hdr_num].v) || !isttest(hdrs[hdr_num].n))
815 goto end;
816 c_rew(res, sz);
817 htx_remove_blk(htx, blk);
818 hdr_num++;
819 }
820
821 /* create a NULL end of array and leave the loop */
William Lallemand33b0d092021-08-13 16:05:53 +0200822 if (type == HTX_BLK_EOH) {
823 hdrs[hdr_num].n = IST_NULL;
824 hdrs[hdr_num].v = IST_NULL;
William Lallemandc020b252022-03-09 18:56:02 +0100825 c_rew(res, sz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100826 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200827 break;
828 }
William Lallemand33b0d092021-08-13 16:05:53 +0200829 }
830
William Lallemand0d6f7792021-08-20 11:59:49 +0200831 if (hdr_num) {
832 /* alloc and copy the headers in the httpclient struct */
833 hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs));
834 if (!hc->res.hdrs)
835 goto end;
836 memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
William Lallemand33b0d092021-08-13 16:05:53 +0200837
William Lallemand0d6f7792021-08-20 11:59:49 +0200838 /* caller callback */
839 if (hc->ops.res_headers)
840 hc->ops.res_headers(hc);
841 }
William Lallemand33b0d092021-08-13 16:05:53 +0200842
843 /* if there is no HTX data anymore and the EOM flag is
844 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200845 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200846 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200847 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200848 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200849 }
William Lallemand33b0d092021-08-13 16:05:53 +0200850 }
851 break;
852
853 case HTTPCLIENT_S_RES_BODY:
854 /*
855 * The IO handler removes the htx blocks in the response buffer and
856 * push them in the hc->res.buf buffer in a raw format.
857 */
William Lallemanda625b032022-03-17 14:45:46 +0100858 if (!co_data(res))
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100859 goto more;
860
William Lallemand33b0d092021-08-13 16:05:53 +0200861 htx = htxbuf(&res->buf);
862 if (!htx || htx_is_empty(htx))
863 goto more;
864
Christopher Faulet600985d2022-01-12 11:14:08 +0100865 if (!b_alloc(&hc->res.buf))
866 goto more;
867
William Lallemand33b0d092021-08-13 16:05:53 +0200868 if (b_full(&hc->res.buf))
Christopher Faulet600985d2022-01-12 11:14:08 +0100869 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200870
871 /* decapsule the htx data to raw data */
William Lallemand97f69c62022-03-10 17:23:40 +0100872 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100873 struct htx_blk *blk = htx_get_blk(htx, pos);
874 enum htx_blk_type type = htx_get_blk_type(blk);
875 size_t count = co_data(res);
876 uint32_t blksz = htx_get_blksz(blk);
877 uint32_t room = b_room(&hc->res.buf);
878 uint32_t vlen;
William Lallemand33b0d092021-08-13 16:05:53 +0200879
William Lallemandc8f1eb92022-03-09 11:58:51 +0100880 /* we should try to copy the maximum output data in a block, which fit
881 * the destination buffer */
882 vlen = MIN(count, blksz);
883 vlen = MIN(vlen, room);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100884
William Lallemandc8f1eb92022-03-09 11:58:51 +0100885 if (vlen == 0)
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100886 goto process_data;
887
William Lallemand33b0d092021-08-13 16:05:53 +0200888 if (type == HTX_BLK_DATA) {
889 struct ist v = htx_get_blk_value(htx, blk);
890
William Lallemandc8f1eb92022-03-09 11:58:51 +0100891 __b_putblk(&hc->res.buf, v.ptr, vlen);
892 c_rew(res, vlen);
William Lallemand33b0d092021-08-13 16:05:53 +0200893
William Lallemandc8f1eb92022-03-09 11:58:51 +0100894 if (vlen == blksz)
895 htx_remove_blk(htx, blk);
896 else
897 htx_cut_data_blk(htx, blk, vlen);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100898
William Lallemand33b0d092021-08-13 16:05:53 +0200899 /* the data must be processed by the caller in the receive phase */
900 if (hc->ops.res_payload)
901 hc->ops.res_payload(hc);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100902
903 /* cannot copy everything, need to processs */
904 if (vlen != blksz)
905 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200906 } else {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100907 if (vlen != blksz)
908 goto process_data;
909
William Lallemand33b0d092021-08-13 16:05:53 +0200910 /* remove any block which is not a data block */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100911 c_rew(res, blksz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100912 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200913 }
914 }
William Lallemandc8f1eb92022-03-09 11:58:51 +0100915
William Lallemand33b0d092021-08-13 16:05:53 +0200916 /* if not finished, should be called again */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100917 if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)))
William Lallemand33b0d092021-08-13 16:05:53 +0200918 goto more;
919
William Lallemandc8f1eb92022-03-09 11:58:51 +0100920
William Lallemand33b0d092021-08-13 16:05:53 +0200921 /* end of message, we should quit */
922 appctx->st0 = HTTPCLIENT_S_RES_END;
923 break;
924
925 case HTTPCLIENT_S_RES_END:
926 goto end;
927 break;
928 }
929 }
930
931process_data:
932
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200933 cs_rx_chan_rdy(cs);
William Lallemand33b0d092021-08-13 16:05:53 +0200934
935 return;
936more:
937 /* There was not enough data in the response channel */
938
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200939 cs_rx_room_blk(cs);
William Lallemand33b0d092021-08-13 16:05:53 +0200940
941 if (appctx->st0 == HTTPCLIENT_S_RES_END)
942 goto end;
943
944 /* The state machine tries to handle as much data as possible, if there
945 * isn't any data to handle and a shutdown is detected, let's stop
946 * everything */
947 if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) ||
William Lallemand58a81ae2022-03-17 15:14:15 +0100948 (res->flags & CF_SHUTW) ||
949 ((res->flags & CF_SHUTW_NOW) && channel_is_empty(res))) {
William Lallemand33b0d092021-08-13 16:05:53 +0200950 goto end;
951 }
952 return;
953
954end:
Christopher Fauletda098e62022-03-31 17:44:45 +0200955 cs_shutw(cs);
956 cs_shutr(cs);
William Lallemand33b0d092021-08-13 16:05:53 +0200957 return;
958}
959
960static void httpclient_applet_release(struct appctx *appctx)
961{
962 struct httpclient *hc = appctx->ctx.httpclient.ptr;
963
William Lallemand1123dde2021-09-21 10:58:10 +0200964 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +0200965 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +0200966 /* the applet is leaving, remove the ptr so we don't try to call it
967 * again from the caller */
968 hc->appctx = NULL;
969
William Lallemandeb0d4c42022-04-06 14:12:37 +0200970 if (hc->ops.res_end)
971 hc->ops.res_end(hc);
William Lallemandecb83e12021-09-28 11:00:43 +0200972
973 /* destroy the httpclient when set to autotokill */
974 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
975 httpclient_destroy(hc);
976 }
977
William Lallemand33b0d092021-08-13 16:05:53 +0200978 return;
979}
980
981/* HTTP client applet */
982static struct applet httpclient_applet = {
983 .obj_type = OBJ_TYPE_APPLET,
984 .name = "<HTTPCLIENT>",
985 .fct = httpclient_applet_io_handler,
986 .release = httpclient_applet_release,
987};
988
William Lallemand83614a92021-08-13 14:47:57 +0200989/*
990 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
991 * the other for HTTPS.
992 */
William Lallemand2c8b0842022-04-22 15:16:09 +0200993static int httpclient_precheck()
William Lallemand83614a92021-08-13 14:47:57 +0200994{
995 int err_code = 0;
996 char *errmsg = NULL;
997
William Lallemandc6ceba32022-04-22 16:49:53 +0200998 if (global.mode & MODE_MWORKER_WAIT)
999 return 0;
1000
William Lallemand83614a92021-08-13 14:47:57 +02001001 httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg);
1002 if (!httpclient_proxy) {
1003 err_code |= ERR_ALERT | ERR_FATAL;
1004 goto err;
1005 }
1006
Willy Tarreau0e72e402021-08-20 10:23:12 +02001007 proxy_preset_defaults(httpclient_proxy);
1008
William Lallemandccc7ee42022-03-18 17:57:15 +01001009 httpclient_proxy->options |= PR_O_WREQ_BODY;
William Lallemand71abad02022-03-17 15:24:28 +01001010 httpclient_proxy->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED | PR_RE_TIMEOUT;
William Lallemand83614a92021-08-13 14:47:57 +02001011 httpclient_proxy->options2 |= PR_O2_INDEPSTR;
1012 httpclient_proxy->mode = PR_MODE_HTTP;
1013 httpclient_proxy->maxconn = 0;
1014 httpclient_proxy->accept = NULL;
William Lallemand71abad02022-03-17 15:24:28 +01001015 httpclient_proxy->conn_retries = CONN_RETRIES;
William Lallemand83614a92021-08-13 14:47:57 +02001016 httpclient_proxy->timeout.client = TICK_ETERNITY;
1017 /* The HTTP Client use the "option httplog" with the global log server */
1018 httpclient_proxy->conf.logformat_string = default_http_log_format;
1019 httpclient_proxy->http_needed = 1;
1020
1021 /* clear HTTP server */
1022 httpclient_srv_raw = new_server(httpclient_proxy);
1023 if (!httpclient_srv_raw) {
1024 err_code |= ERR_ALERT | ERR_FATAL;
1025 memprintf(&errmsg, "out of memory.");
1026 goto err;
1027 }
1028
1029 httpclient_srv_raw->iweight = 0;
1030 httpclient_srv_raw->uweight = 0;
1031 httpclient_srv_raw->xprt = xprt_get(XPRT_RAW);
1032 httpclient_srv_raw->id = strdup("<HTTPCLIENT>");
1033 if (!httpclient_srv_raw->id)
1034 goto err;
1035
William Lallemand957ab132021-08-24 18:33:28 +02001036#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +02001037 /* SSL HTTP server */
1038 httpclient_srv_ssl = new_server(httpclient_proxy);
1039 if (!httpclient_srv_ssl) {
1040 memprintf(&errmsg, "out of memory.");
1041 err_code |= ERR_ALERT | ERR_FATAL;
1042 goto err;
1043 }
1044 httpclient_srv_ssl->iweight = 0;
1045 httpclient_srv_ssl->uweight = 0;
1046 httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL);
1047 httpclient_srv_ssl->use_ssl = 1;
William Lallemand211c9672021-08-24 17:18:13 +02001048 httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>");
William Lallemand83614a92021-08-13 14:47:57 +02001049 if (!httpclient_srv_ssl->id)
1050 goto err;
1051
William Lallemandeaa703e2022-04-22 17:52:33 +02001052 httpclient_srv_ssl->ssl_ctx.verify = httpclient_ssl_verify;
William Lallemand4006b0f2022-04-25 18:23:35 +02001053 /* if the verify is required, try to load the system CA */
William Lallemandeaa703e2022-04-22 17:52:33 +02001054 if (httpclient_ssl_verify == SSL_SOCK_VERIFY_REQUIRED) {
1055 httpclient_srv_ssl->ssl_ctx.ca_file = strdup("@system-ca");
William Lallemand4006b0f2022-04-25 18:23:35 +02001056 if (!ssl_store_load_locations_file(httpclient_srv_ssl->ssl_ctx.ca_file, 1, CAFILE_CERT)) {
1057 ha_warning("httpclient: cannot initialize SSL verify with 'ca-file \"%s\"'. Disabling SSL.\n", httpclient_srv_ssl->ssl_ctx.ca_file);
1058 ha_free(&httpclient_srv_ssl->ssl_ctx.ca_file);
1059 srv_drop(httpclient_srv_ssl);
1060 httpclient_srv_ssl = NULL;
1061 }
William Lallemandeaa703e2022-04-22 17:52:33 +02001062 }
William Lallemandcf5cb0b2022-04-22 14:48:45 +02001063
William Lallemand957ab132021-08-24 18:33:28 +02001064#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +02001065
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001066 /* add the proxy in the proxy list only if everything is successful */
William Lallemand83614a92021-08-13 14:47:57 +02001067 httpclient_proxy->next = proxies_list;
1068 proxies_list = httpclient_proxy;
1069
William Lallemand211c9672021-08-24 17:18:13 +02001070 /* link the 2 servers in the proxy */
1071 httpclient_srv_raw->next = httpclient_proxy->srv;
William Lallemand957ab132021-08-24 18:33:28 +02001072 httpclient_proxy->srv = httpclient_srv_raw;
1073
1074#ifdef USE_OPENSSL
William Lallemand4006b0f2022-04-25 18:23:35 +02001075 if (httpclient_srv_ssl) {
1076 httpclient_srv_ssl->next = httpclient_proxy->srv;
1077 httpclient_proxy->srv = httpclient_srv_ssl;
1078 }
William Lallemand957ab132021-08-24 18:33:28 +02001079#endif
1080
William Lallemand211c9672021-08-24 17:18:13 +02001081
William Lallemand83614a92021-08-13 14:47:57 +02001082 return 0;
1083
1084err:
1085 ha_alert("httpclient: cannot initialize.\n");
1086 free(errmsg);
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001087 srv_drop(httpclient_srv_raw);
William Lallemand957ab132021-08-24 18:33:28 +02001088#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001089 srv_drop(httpclient_srv_ssl);
William Lallemand957ab132021-08-24 18:33:28 +02001090#endif
William Lallemand83614a92021-08-13 14:47:57 +02001091 free_proxy(httpclient_proxy);
1092 return err_code;
1093}
1094
William Lallemand2c8b0842022-04-22 15:16:09 +02001095static int httpclient_postcheck()
William Lallemand83614a92021-08-13 14:47:57 +02001096{
1097 struct logsrv *logsrv;
1098 struct proxy *curproxy = httpclient_proxy;
William Lallemand71e31582022-03-16 15:47:47 +01001099 char *errmsg = NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001100
William Lallemandc6ceba32022-04-22 16:49:53 +02001101 if (global.mode & MODE_MWORKER_WAIT)
1102 return 0;
1103
William Lallemand83614a92021-08-13 14:47:57 +02001104 /* copy logs from "global" log list */
1105 list_for_each_entry(logsrv, &global.logsrvs, list) {
1106 struct logsrv *node = malloc(sizeof(*node));
1107
1108 if (!node) {
1109 ha_alert("httpclient: cannot allocate memory.\n");
1110 goto err;
1111 }
1112
1113 memcpy(node, logsrv, sizeof(*node));
1114 LIST_INIT(&node->list);
1115 LIST_APPEND(&curproxy->logsrvs, &node->list);
Willy Tarreaue1e9f6b2022-04-21 14:14:28 +02001116 node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
1117 node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001118 }
1119 if (curproxy->conf.logformat_string) {
William Lallemand83614a92021-08-13 14:47:57 +02001120 curproxy->conf.args.ctx = ARGC_LOG;
1121 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
1122 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
William Lallemand715c1012022-03-16 16:39:23 +01001123 SMP_VAL_FE_LOG_END, &errmsg)) {
1124 ha_alert("httpclient: failed to parse log-format : %s.\n", errmsg);
1125 free(errmsg);
William Lallemand83614a92021-08-13 14:47:57 +02001126 goto err;
1127 }
1128 curproxy->conf.args.file = NULL;
1129 curproxy->conf.args.line = 0;
1130 }
William Lallemand71e31582022-03-16 15:47:47 +01001131
1132#ifdef USE_OPENSSL
William Lallemand4006b0f2022-04-25 18:23:35 +02001133 if (httpclient_srv_ssl) {
William Lallemand715c1012022-03-16 16:39:23 +01001134 int err_code = 0;
1135
1136 /* init the SNI expression */
1137 /* always use the host header as SNI, without the port */
1138 httpclient_srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)");
1139 err_code |= server_parse_sni_expr(httpclient_srv_ssl, httpclient_proxy, &errmsg);
1140 if (err_code & ERR_CODE) {
1141 ha_alert("httpclient: failed to configure sni: %s.\n", errmsg);
1142 free(errmsg);
1143 goto err;
1144 }
William Lallemand71e31582022-03-16 15:47:47 +01001145 }
1146#endif
1147
William Lallemand83614a92021-08-13 14:47:57 +02001148 return 0;
1149err:
1150 return 1;
1151}
1152
William Lallemand83614a92021-08-13 14:47:57 +02001153/* initialize the proxy and servers for the HTTP client */
1154
William Lallemand2c8b0842022-04-22 15:16:09 +02001155REGISTER_PRE_CHECK(httpclient_precheck);
1156REGISTER_POST_CHECK(httpclient_postcheck);
William Lallemandeaa703e2022-04-22 17:52:33 +02001157
1158#ifdef USE_OPENSSL
1159static int httpclient_parse_global_verify(char **args, int section_type, struct proxy *curpx,
1160 const struct proxy *defpx, const char *file, int line,
1161 char **err)
1162{
1163 if (too_many_args(1, args, err, NULL))
1164 return -1;
1165
1166 if (strcmp(args[1],"none") == 0)
1167 httpclient_ssl_verify = SSL_SERVER_VERIFY_NONE;
1168 else if (strcmp(args[1],"required") == 0)
1169 httpclient_ssl_verify = SSL_SERVER_VERIFY_REQUIRED;
1170 else {
1171 ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, line, args[0]);
1172 return -1;
1173 }
1174
1175 return 0;
1176}
1177
1178static struct cfg_kw_list cfg_kws = {ILH, {
1179 { CFG_GLOBAL, "httpclient-ssl-verify", httpclient_parse_global_verify },
1180 { 0, NULL, NULL },
1181}};
1182
1183INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1184#endif