blob: bf01c1b8553d4a889c4b3f6af096625627b88397 [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>
William Lallemand83614a92021-08-13 14:47:57 +020021#include <haproxy/global.h>
William Lallemand0da616e2021-10-28 15:34:26 +020022#include <haproxy/istbuf.h>
William Lallemand33b0d092021-08-13 16:05:53 +020023#include <haproxy/h1_htx.h>
24#include <haproxy/http.h>
William Lallemand2b7dc4e2022-02-24 16:55:41 +010025#include <haproxy/http_ana-t.h>
William Lallemand33b0d092021-08-13 16:05:53 +020026#include <haproxy/http_client.h>
27#include <haproxy/http_htx.h>
William Lallemand5392ff62022-04-28 16:55:02 +020028#include <haproxy/http_rules.h>
William Lallemand33b0d092021-08-13 16:05:53 +020029#include <haproxy/htx.h>
William Lallemand83614a92021-08-13 14:47:57 +020030#include <haproxy/log.h>
31#include <haproxy/proxy.h>
William Lallemand5392ff62022-04-28 16:55:02 +020032#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020033#include <haproxy/sc_strm.h>
William Lallemand2a8fe8b2021-08-20 14:25:15 +020034#include <haproxy/server.h>
Willy Tarreau1df20422021-10-06 11:28:24 +020035#include <haproxy/ssl_sock-t.h>
William Lallemand7f1df8f2022-04-14 17:50:20 +020036#include <haproxy/sock_inet.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020037#include <haproxy/stconn.h>
William Lallemand83614a92021-08-13 14:47:57 +020038#include <haproxy/tools.h>
39
40#include <string.h>
41
42
43static struct proxy *httpclient_proxy;
44static struct server *httpclient_srv_raw;
William Lallemand6fce46a2022-05-04 14:53:41 +020045
William Lallemand957ab132021-08-24 18:33:28 +020046#ifdef USE_OPENSSL
William Lallemand6fce46a2022-05-04 14:53:41 +020047/* if the httpclient is not configured, error are ignored and features are limited */
48static int hard_error_ssl = 0;
William Lallemand83614a92021-08-13 14:47:57 +020049static struct server *httpclient_srv_ssl;
William Lallemandf1344b32022-04-26 12:00:06 +020050static int httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED;
William Lallemand683fbb82022-05-04 15:43:01 +020051static char *httpclient_ssl_ca_file = NULL;
William Lallemand957ab132021-08-24 18:33:28 +020052#endif
William Lallemand33b0d092021-08-13 16:05:53 +020053static struct applet httpclient_applet;
54
William Lallemand7c5a7ef2022-05-04 15:59:44 +020055/* if the httpclient is not configured, error are ignored and features are limited */
William Lallemand8a734cb2022-05-04 16:10:47 +020056static int hard_error_resolvers = 0;
57static char *resolvers_id = NULL;
William Lallemand7c5a7ef2022-05-04 15:59:44 +020058static char *resolvers_prefer = NULL;
William Lallemandeaa703e2022-04-22 17:52:33 +020059
William Lallemand03a4eb12021-08-18 16:46:21 +020060/* --- This part of the file implement an HTTP client over the CLI ---
61 * The functions will be starting by "hc_cli" for "httpclient cli"
62 */
63
William Lallemand03a4eb12021-08-18 16:46:21 +020064/* What kind of data we need to read */
65#define HC_CLI_F_RES_STLINE 0x01
66#define HC_CLI_F_RES_HDR 0x02
67#define HC_CLI_F_RES_BODY 0x04
68#define HC_CLI_F_RES_END 0x08
69
Willy Tarreau89a7c412022-05-05 19:38:21 +020070/* the CLI context for the httpclient command */
71struct hcli_svc_ctx {
72 struct httpclient *hc; /* the httpclient instance */
73 uint flags; /* flags from HC_CLI_F_* above */
74};
William Lallemand03a4eb12021-08-18 16:46:21 +020075
76/* These are the callback used by the HTTP Client when it needs to notify new
Willy Tarreau89a7c412022-05-05 19:38:21 +020077 * data, we only sets a flag in the IO handler via the svcctx.
78 */
William Lallemand03a4eb12021-08-18 16:46:21 +020079void hc_cli_res_stline_cb(struct httpclient *hc)
80{
81 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +020082 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +020083
William Lallemanddfc3f892021-08-20 11:35:29 +020084 if (!appctx)
85 return;
86
Willy Tarreau89a7c412022-05-05 19:38:21 +020087 ctx = appctx->svcctx;
88 ctx->flags |= HC_CLI_F_RES_STLINE;
William Lallemanddfc3f892021-08-20 11:35:29 +020089 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020090}
91
92void hc_cli_res_headers_cb(struct httpclient *hc)
93{
94 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +020095 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +020096
William Lallemanddfc3f892021-08-20 11:35:29 +020097 if (!appctx)
98 return;
99
Willy Tarreau89a7c412022-05-05 19:38:21 +0200100 ctx = appctx->svcctx;
101 ctx->flags |= HC_CLI_F_RES_HDR;
William Lallemanddfc3f892021-08-20 11:35:29 +0200102 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200103}
104
105void hc_cli_res_body_cb(struct httpclient *hc)
106{
107 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +0200108 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +0200109
William Lallemanddfc3f892021-08-20 11:35:29 +0200110 if (!appctx)
111 return;
112
Willy Tarreau89a7c412022-05-05 19:38:21 +0200113 ctx = appctx->svcctx;
114 ctx->flags |= HC_CLI_F_RES_BODY;
William Lallemanddfc3f892021-08-20 11:35:29 +0200115 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200116}
117
118void hc_cli_res_end_cb(struct httpclient *hc)
119{
120 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +0200121 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +0200122
William Lallemanddfc3f892021-08-20 11:35:29 +0200123 if (!appctx)
124 return;
125
Willy Tarreau89a7c412022-05-05 19:38:21 +0200126 ctx = appctx->svcctx;
127 ctx->flags |= HC_CLI_F_RES_END;
William Lallemanddfc3f892021-08-20 11:35:29 +0200128 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200129}
130
131/*
132 * Parse an httpclient keyword on the cli:
133 * httpclient <ID> <method> <URI>
134 */
135static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private)
136{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200137 struct hcli_svc_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemand03a4eb12021-08-18 16:46:21 +0200138 struct httpclient *hc;
139 char *err = NULL;
140 enum http_meth_t meth;
141 char *meth_str;
142 struct ist uri;
William Lallemanddec25c32021-10-25 19:48:37 +0200143 struct ist body = IST_NULL;
William Lallemand03a4eb12021-08-18 16:46:21 +0200144
145 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
146 return 1;
147
148 if (!*args[1] || !*args[2]) {
149 memprintf(&err, ": not enough parameters");
150 goto err;
151 }
152
153 meth_str = args[1];
154 uri = ist(args[2]);
155
William Lallemanddec25c32021-10-25 19:48:37 +0200156 if (payload)
157 body = ist(payload);
158
William Lallemand03a4eb12021-08-18 16:46:21 +0200159 meth = find_http_meth(meth_str, strlen(meth_str));
160
161 hc = httpclient_new(appctx, meth, uri);
162 if (!hc) {
163 goto err;
164 }
165
166 /* update the httpclient callbacks */
167 hc->ops.res_stline = hc_cli_res_stline_cb;
168 hc->ops.res_headers = hc_cli_res_headers_cb;
169 hc->ops.res_payload = hc_cli_res_body_cb;
170 hc->ops.res_end = hc_cli_res_end_cb;
171
Willy Tarreau89a7c412022-05-05 19:38:21 +0200172 ctx->hc = hc; /* store the httpclient ptr in the applet */
173 ctx->flags = 0;
William Lallemand03a4eb12021-08-18 16:46:21 +0200174
William Lallemandbad9c8c2022-01-14 14:10:33 +0100175 if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE)
William Lallemand03a4eb12021-08-18 16:46:21 +0200176 goto err;
177
178
179 if (!httpclient_start(hc))
180 goto err;
181
182 return 0;
183
184err:
185 memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : "");
186 return cli_err(appctx, err);
187}
188
189/* This function dumps the content of the httpclient receive buffer
190 * on the CLI output
191 *
192 * Return 1 when the processing is finished
193 * return 0 if it needs to be called again
194 */
195static int hc_cli_io_handler(struct appctx *appctx)
196{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200197 struct hcli_svc_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200198 struct stconn *sc = appctx_sc(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200199 struct buffer *trash = alloc_trash_chunk();
Willy Tarreau89a7c412022-05-05 19:38:21 +0200200 struct httpclient *hc = ctx->hc;
William Lallemand03a4eb12021-08-18 16:46:21 +0200201 struct http_hdr *hdrs, *hdr;
202
203 if (!trash)
204 goto out;
Willy Tarreau89a7c412022-05-05 19:38:21 +0200205
206 if (ctx->flags & HC_CLI_F_RES_STLINE) {
William Lallemandde6ecc32022-02-16 11:37:02 +0100207 chunk_appendf(trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn),
208 hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason));
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200209 applet_putchk(appctx, trash);
Willy Tarreau89a7c412022-05-05 19:38:21 +0200210 ctx->flags &= ~HC_CLI_F_RES_STLINE;
William Lallemand03a4eb12021-08-18 16:46:21 +0200211 goto out;
212 }
213
Willy Tarreau89a7c412022-05-05 19:38:21 +0200214 if (ctx->flags & HC_CLI_F_RES_HDR) {
William Lallemand03a4eb12021-08-18 16:46:21 +0200215 hdrs = hc->res.hdrs;
216 for (hdr = hdrs; isttest(hdr->v); hdr++) {
217 if (!h1_format_htx_hdr(hdr->n, hdr->v, trash))
218 goto out;
219 }
220 if (!chunk_memcat(trash, "\r\n", 2))
221 goto out;
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200222 applet_putchk(appctx, trash);
Willy Tarreau89a7c412022-05-05 19:38:21 +0200223 ctx->flags &= ~HC_CLI_F_RES_HDR;
William Lallemand03a4eb12021-08-18 16:46:21 +0200224 goto out;
225 }
226
Willy Tarreau89a7c412022-05-05 19:38:21 +0200227 if (ctx->flags & HC_CLI_F_RES_BODY) {
William Lallemand03a4eb12021-08-18 16:46:21 +0200228 int ret;
229
Willy Tarreau475e4632022-05-27 10:26:46 +0200230 ret = httpclient_res_xfer(hc, sc_ib(sc));
231 channel_add_input(sc_ic(sc), ret); /* forward what we put in the buffer channel */
William Lallemand03a4eb12021-08-18 16:46:21 +0200232
William Lallemand518878e2021-09-21 10:45:34 +0200233 if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */
Willy Tarreau89a7c412022-05-05 19:38:21 +0200234 ctx->flags &= ~HC_CLI_F_RES_BODY;
William Lallemand03a4eb12021-08-18 16:46:21 +0200235 }
236 goto out;
237 }
238
239 /* we must close only if F_END is the last flag */
Willy Tarreau89a7c412022-05-05 19:38:21 +0200240 if (ctx->flags == HC_CLI_F_RES_END) {
Willy Tarreau475e4632022-05-27 10:26:46 +0200241 sc_shutw(sc);
242 sc_shutr(sc);
Willy Tarreau89a7c412022-05-05 19:38:21 +0200243 ctx->flags &= ~HC_CLI_F_RES_END;
William Lallemand03a4eb12021-08-18 16:46:21 +0200244 goto out;
245 }
246
247out:
248 /* we didn't clear every flags, we should come back to finish things */
Willy Tarreau89a7c412022-05-05 19:38:21 +0200249 if (ctx->flags)
Willy Tarreau475e4632022-05-27 10:26:46 +0200250 sc_need_room(sc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200251
252 free_trash_chunk(trash);
253 return 0;
254}
255
256static void hc_cli_release(struct appctx *appctx)
257{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200258 struct hcli_svc_ctx *ctx = appctx->svcctx;
259 struct httpclient *hc = ctx->hc;
William Lallemand03a4eb12021-08-18 16:46:21 +0200260
261 /* Everything possible was printed on the CLI, we can destroy the client */
William Lallemandecb83e12021-09-28 11:00:43 +0200262 httpclient_stop_and_destroy(hc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200263
264 return;
265}
266
267/* register cli keywords */
268static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2c8f9842022-02-18 16:26:36 +0100269 { { "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 +0200270 { { NULL }, NULL, NULL, NULL }
271}};
272
273INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
274
275
276/* --- This part of the file implements the actual HTTP client API --- */
277
William Lallemand33b0d092021-08-13 16:05:53 +0200278/*
279 * Generate a simple request and fill the httpclient request buffer with it.
280 * The request contains a request line generated from the absolute <url> and
281 * <meth> as well as list of headers <hdrs>.
282 *
283 * If the buffer was filled correctly the function returns 0, if not it returns
284 * an error_code but there is no guarantee that the buffer wasn't modified.
285 */
William Lallemanddec25c32021-10-25 19:48:37 +0200286int 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 +0200287{
288 struct htx_sl *sl;
289 struct htx *htx;
290 int err_code = 0;
291 struct ist meth_ist, vsn;
William Lallemanddec25c32021-10-25 19:48:37 +0200292 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 +0100293 int i;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100294 int foundhost = 0, foundaccept = 0, foundua = 0;
William Lallemand33b0d092021-08-13 16:05:53 +0200295
Christopher Faulet600985d2022-01-12 11:14:08 +0100296 if (!b_alloc(&hc->req.buf))
297 goto error;
298
William Lallemand33b0d092021-08-13 16:05:53 +0200299 if (meth >= HTTP_METH_OTHER)
300 goto error;
301
302 meth_ist = http_known_methods[meth];
303
304 vsn = ist("HTTP/1.1");
305
306 htx = htx_from_buf(&hc->req.buf);
307 if (!htx)
308 goto error;
William Lallemande1e045f2022-01-14 14:08:34 +0100309
310 if (!hc->ops.req_payload && !isttest(payload))
311 flags |= HTX_SL_F_BODYLESS;
312
William Lallemand33b0d092021-08-13 16:05:53 +0200313 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn);
314 if (!sl) {
315 goto error;
316 }
317 sl->info.req.meth = meth;
318
William Lallemandf03b53c2021-11-24 15:38:17 +0100319 for (i = 0; hdrs && hdrs[i].n.len; i++) {
320 /* Don't check the value length because a header value may be empty */
321 if (isttest(hdrs[i].v) == 0)
322 continue;
323
324 if (isteqi(hdrs[i].n, ist("host")))
325 foundhost = 1;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100326 else if (isteqi(hdrs[i].n, ist("accept")))
327 foundaccept = 1;
328 else if (isteqi(hdrs[i].n, ist("user-agent")))
329 foundua = 1;
William Lallemandf03b53c2021-11-24 15:38:17 +0100330
331 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
332 goto error;
333 }
William Lallemand33b0d092021-08-13 16:05:53 +0200334
William Lallemandf03b53c2021-11-24 15:38:17 +0100335 if (!foundhost) {
336 /* Add Host Header from URL */
337 if (!htx_add_header(htx, ist("Host"), ist("h")))
William Lallemand79a34782021-09-20 16:19:15 +0200338 goto error;
William Lallemandf03b53c2021-11-24 15:38:17 +0100339 if (!http_update_host(htx, sl, url))
William Lallemand79a34782021-09-20 16:19:15 +0200340 goto error;
341 }
William Lallemand33b0d092021-08-13 16:05:53 +0200342
William Lallemandbad9c8c2022-01-14 14:10:33 +0100343 if (!foundaccept) {
344 if (!htx_add_header(htx, ist("Accept"), ist("*/*")))
345 goto error;
346 }
347
348 if (!foundua) {
349 if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT)))
350 goto error;
351 }
352
353
William Lallemandf03b53c2021-11-24 15:38:17 +0100354 if (!htx_add_endof(htx, HTX_BLK_EOH))
355 goto error;
356
William Lallemanddec25c32021-10-25 19:48:37 +0200357 if (isttest(payload)) {
358 /* add the payload if it can feat in the buffer, no need to set
359 * the Content-Length, the data will be sent chunked */
360 if (!htx_add_data_atonce(htx, payload))
361 goto error;
362 }
363
William Lallemand0da616e2021-10-28 15:34:26 +0200364 /* If req.payload was set, does not set the end of stream which *MUST*
365 * be set in the callback */
366 if (!hc->ops.req_payload)
367 htx->flags |= HTX_FL_EOM;
William Lallemand33b0d092021-08-13 16:05:53 +0200368
369 htx_to_buf(htx, &hc->req.buf);
370
371 return 0;
372error:
373 err_code |= ERR_ALERT | ERR_ABORT;
374 return err_code;
375}
376
377/*
378 * transfer the response to the destination buffer and wakeup the HTTP client
379 * applet so it could fill again its buffer.
380 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500381 * Return the number of bytes transferred.
William Lallemand33b0d092021-08-13 16:05:53 +0200382 */
383int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst)
384{
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100385 size_t room = b_room(dst);
William Lallemand33b0d092021-08-13 16:05:53 +0200386 int ret;
387
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100388 ret = b_force_xfer(dst, &hc->res.buf, MIN(room, b_data(&hc->res.buf)));
William Lallemand33b0d092021-08-13 16:05:53 +0200389 /* call the client once we consumed all data */
Christopher Faulet600985d2022-01-12 11:14:08 +0100390 if (!b_data(&hc->res.buf)) {
391 b_free(&hc->res.buf);
392 if (hc->appctx)
393 appctx_wakeup(hc->appctx);
394 }
William Lallemand33b0d092021-08-13 16:05:53 +0200395 return ret;
396}
397
398/*
William Lallemand0da616e2021-10-28 15:34:26 +0200399 * Transfer raw HTTP payload from src, and insert it into HTX format in the
400 * httpclient.
401 *
402 * Must be used to transfer the request body.
403 * Then wakeup the httpclient so it can transfer it.
404 *
405 * <end> tries to add the ending data flag if it succeed to copy all data.
406 *
407 * Return the number of bytes copied from src.
408 */
409int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end)
410{
411 int ret = 0;
412 struct htx *htx;
413
Christopher Faulet600985d2022-01-12 11:14:08 +0100414 if (!b_alloc(&hc->req.buf))
415 goto error;
416
William Lallemand0da616e2021-10-28 15:34:26 +0200417 htx = htx_from_buf(&hc->req.buf);
418 if (!htx)
419 goto error;
420
421 if (hc->appctx)
422 appctx_wakeup(hc->appctx);
423
424 ret += htx_add_data(htx, src);
425
426
427 /* if we copied all the data and the end flag is set */
428 if ((istlen(src) == ret) && end) {
429 htx->flags |= HTX_FL_EOM;
430 }
431 htx_to_buf(htx, &hc->req.buf);
432
433error:
434
435 return ret;
436}
437
William Lallemandb4a4ef62022-02-23 14:18:16 +0100438/* Set the 'timeout server' in ms for the next httpclient request */
439void httpclient_set_timeout(struct httpclient *hc, int timeout)
440{
441 hc->timeout_server = timeout;
442}
443
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100444/*
445 * Sets a destination for the httpclient from an HAProxy addr format
446 * This will prevent to determine the destination from the URL
447 * Return 0 in case of success or -1 otherwise.
448 */
449int httpclient_set_dst(struct httpclient *hc, const char *dst)
450{
451 struct sockaddr_storage *sk;
452 char *errmsg = NULL;
453
454 sockaddr_free(&hc->dst);
455 /* 'sk' is statically allocated (no need to be freed). */
456 sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL,
457 &errmsg, NULL, NULL,
458 PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
459 if (!sk) {
460 ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg);
461 free(errmsg);
462 return -1;
463 }
464
465 if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) {
466 ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__);
467 return -1;
468 }
469
470 return 0;
471}
William Lallemand0da616e2021-10-28 15:34:26 +0200472
473/*
William Lallemand7f1df8f2022-04-14 17:50:20 +0200474 * Return a splitted URL in <scheme>, <host>, <port>
475 */
476static void httpclient_spliturl(struct ist url, enum http_scheme *scheme,
477 struct ist *host, int *port)
478{
479 enum http_scheme scheme_tmp = SCH_HTTP;
480 int port_tmp = 0;
481 struct ist scheme_ist, authority_ist, host_ist, port_ist;
482 char *p, *end;
483 struct http_uri_parser parser;
484
485 parser = http_uri_parser_init(url);
486 scheme_ist = http_parse_scheme(&parser);
487
488 if (isteqi(scheme_ist, ist("http://"))){
489 scheme_tmp = SCH_HTTP;
490 port_tmp = 80;
491 } else if (isteqi(scheme_ist, ist("https://"))) {
492 scheme_tmp = SCH_HTTPS;
493 port_tmp = 443;
494 }
495
496 authority_ist = http_parse_authority(&parser, 1);
497 p = end = istend(authority_ist);
498
499 /* look for a port at the end of the authority */
500 while (p > istptr(authority_ist) && isdigit((unsigned char)*--p))
501 ;
502
503 if (*p == ':') {
504 host_ist = ist2(istptr(authority_ist), p - istptr(authority_ist));
505 port_ist = istnext(ist2(p, end - p));
506 ist2str(trash.area, port_ist);
507 port_tmp = atoi(trash.area);
508 } else {
509 host_ist = authority_ist;
510 }
511
512 if (scheme)
513 *scheme = scheme_tmp;
514 if (host)
515 *host = host_ist;
516 if (port)
517 *port = port_tmp;
518
519}
520
521/*
William Lallemand33b0d092021-08-13 16:05:53 +0200522 * Start the HTTP client
523 * Create the appctx, session, stream and wakeup the applet
524 *
William Lallemand33b0d092021-08-13 16:05:53 +0200525 * Return the <appctx> or NULL if it failed
526 */
527struct appctx *httpclient_start(struct httpclient *hc)
528{
529 struct applet *applet = &httpclient_applet;
530 struct appctx *appctx;
William Lallemand33b0d092021-08-13 16:05:53 +0200531
William Lallemand5085bc32022-02-17 12:52:09 +0100532 /* if the client was started and not ended, an applet is already
533 * running, we shouldn't try anything */
534 if (httpclient_started(hc) && !httpclient_ended(hc))
535 return NULL;
536
William Lallemand33b0d092021-08-13 16:05:53 +0200537 /* The HTTP client will be created in the same thread as the caller,
538 * avoiding threading issues */
Christopher Faulet6095d572022-05-16 17:09:48 +0200539 appctx = appctx_new_here(applet, NULL);
William Lallemand33b0d092021-08-13 16:05:53 +0200540 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100541 goto out;
Christopher Fauletb1e08362022-05-12 15:33:14 +0200542 appctx->svcctx = hc;
543 hc->flags = 0;
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100544
Christopher Fauletb1e08362022-05-12 15:33:14 +0200545 if (appctx_init(appctx) == -1) {
546 ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__);
Christopher Faulet92202da2022-05-11 12:22:10 +0200547 goto out_free_appctx;
William Lallemand85332732022-05-04 10:59:51 +0200548 }
549
William Lallemand33b0d092021-08-13 16:05:53 +0200550 return appctx;
551
William Lallemand33b0d092021-08-13 16:05:53 +0200552out_free_appctx:
Christopher Fauletb1e08362022-05-12 15:33:14 +0200553 appctx_free_on_early_error(appctx);
William Lallemand33b0d092021-08-13 16:05:53 +0200554out:
555
556 return NULL;
557}
558
William Lallemandecb83e12021-09-28 11:00:43 +0200559/*
560 * This function tries to destroy the httpclient if it wasn't running.
561 * If it was running, stop the client and ask it to autodestroy itself.
562 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500563 * Once this function is used, all pointer sto the client must be removed
William Lallemandecb83e12021-09-28 11:00:43 +0200564 *
565 */
566void httpclient_stop_and_destroy(struct httpclient *hc)
567{
568
William Lallemandb8b13702021-09-28 12:15:37 +0200569 /* The httpclient was already stopped or never started, we can safely destroy it */
570 if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) {
William Lallemandecb83e12021-09-28 11:00:43 +0200571 httpclient_destroy(hc);
572 } else {
573 /* if the client wasn't stopped, ask for a stop and destroy */
574 hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP);
575 if (hc->appctx)
576 appctx_wakeup(hc->appctx);
577 }
578}
579
William Lallemand33b0d092021-08-13 16:05:53 +0200580/* Free the httpclient */
581void httpclient_destroy(struct httpclient *hc)
582{
William Lallemand03f5a1c2021-09-27 15:17:47 +0200583 struct http_hdr *hdrs;
584
585
William Lallemand33b0d092021-08-13 16:05:53 +0200586 if (!hc)
587 return;
William Lallemandecb83e12021-09-28 11:00:43 +0200588
William Lallemand2a879002021-10-05 15:50:45 +0200589 /* we should never destroy a client which was started but not stopped */
590 BUG_ON(httpclient_started(hc) && !httpclient_ended(hc));
William Lallemandecb83e12021-09-28 11:00:43 +0200591
William Lallemand03f5a1c2021-09-27 15:17:47 +0200592 /* request */
593 istfree(&hc->req.url);
William Lallemand33b0d092021-08-13 16:05:53 +0200594 b_free(&hc->req.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200595 /* response */
596 istfree(&hc->res.vsn);
597 istfree(&hc->res.reason);
598 hdrs = hc->res.hdrs;
599 while (hdrs && isttest(hdrs->n)) {
600 istfree(&hdrs->n);
601 istfree(&hdrs->v);
602 hdrs++;
603 }
604 ha_free(&hc->res.hdrs);
William Lallemand33b0d092021-08-13 16:05:53 +0200605 b_free(&hc->res.buf);
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100606 sockaddr_free(&hc->dst);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200607
William Lallemand33b0d092021-08-13 16:05:53 +0200608 free(hc);
609
610 return;
611}
612
613/* Allocate an httpclient and its buffers
614 * Return NULL on failure */
615struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url)
616{
617 struct httpclient *hc;
William Lallemand33b0d092021-08-13 16:05:53 +0200618
619 hc = calloc(1, sizeof(*hc));
620 if (!hc)
621 goto err;
622
Christopher Faulet600985d2022-01-12 11:14:08 +0100623 hc->req.buf = BUF_NULL;
624 hc->res.buf = BUF_NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200625 hc->caller = caller;
William Lallemand67b77842021-11-10 16:57:25 +0100626 hc->req.url = istdup(url);
William Lallemand33b0d092021-08-13 16:05:53 +0200627 hc->req.meth = meth;
628
629 return hc;
630
631err:
632 httpclient_destroy(hc);
633 return NULL;
634}
635
636static void httpclient_applet_io_handler(struct appctx *appctx)
637{
Willy Tarreau1eea6652022-05-05 20:12:01 +0200638 struct httpclient *hc = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200639 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaub89f8722022-05-27 10:37:32 +0200640 struct stream *s = __sc_strm(sc);
William Lallemand33b0d092021-08-13 16:05:53 +0200641 struct channel *req = &s->req;
642 struct channel *res = &s->res;
643 struct htx_blk *blk = NULL;
644 struct htx *htx;
William Lallemandb7020302021-08-20 11:24:13 +0200645 struct htx_sl *sl = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200646 uint32_t hdr_num;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100647 uint32_t sz;
William Lallemand933fe392021-11-04 09:45:58 +0100648 int ret;
William Lallemand33b0d092021-08-13 16:05:53 +0200649
William Lallemand33b0d092021-08-13 16:05:53 +0200650 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200651
652 /* required to stop */
653 if (hc->flags & HTTPCLIENT_FA_STOP)
654 goto end;
655
William Lallemand33b0d092021-08-13 16:05:53 +0200656 switch(appctx->st0) {
657
658 case HTTPCLIENT_S_REQ:
William Lallemanddb8a1f32021-11-08 16:55:14 +0100659 /* we know that the buffer is empty here, since
660 * it's the first call, we can freely copy the
661 * request from the httpclient buffer */
William Lallemand933fe392021-11-04 09:45:58 +0100662 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
William Lallemanddb8a1f32021-11-08 16:55:14 +0100663 if (!ret)
664 goto more;
William Lallemand933fe392021-11-04 09:45:58 +0100665
Christopher Faulet600985d2022-01-12 11:14:08 +0100666 if (!b_data(&hc->req.buf))
667 b_free(&hc->req.buf);
668
William Lallemanddb8a1f32021-11-08 16:55:14 +0100669 htx = htx_from_buf(&req->buf);
William Lallemand933fe392021-11-04 09:45:58 +0100670 if (!htx)
671 goto more;
672
William Lallemanddb8a1f32021-11-08 16:55:14 +0100673 channel_add_input(req, htx->data);
674
William Lallemand933fe392021-11-04 09:45:58 +0100675 if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */
676 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
677 else
678 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
679
William Lallemand33b0d092021-08-13 16:05:53 +0200680 goto more; /* we need to leave the IO handler once we wrote the request */
681 break;
William Lallemand0da616e2021-10-28 15:34:26 +0200682 case HTTPCLIENT_S_REQ_BODY:
683 /* call the payload callback */
684 {
685 if (hc->ops.req_payload) {
William Lallemandccc7ee42022-03-18 17:57:15 +0100686 struct htx *hc_htx;
William Lallemand0da616e2021-10-28 15:34:26 +0200687
William Lallemand0da616e2021-10-28 15:34:26 +0200688 /* call the request callback */
689 hc->ops.req_payload(hc);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100690
William Lallemandccc7ee42022-03-18 17:57:15 +0100691 hc_htx = htx_from_buf(&hc->req.buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100692 htx = htx_from_buf(&req->buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100693
William Lallemandccc7ee42022-03-18 17:57:15 +0100694 if (htx_is_empty(hc_htx))
William Lallemanddb8a1f32021-11-08 16:55:14 +0100695 goto more;
Christopher Faulet600985d2022-01-12 11:14:08 +0100696
William Lallemandccc7ee42022-03-18 17:57:15 +0100697 if (htx_is_empty(htx)) {
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200698 size_t data = hc_htx->data;
699
William Lallemandccc7ee42022-03-18 17:57:15 +0100700 /* Here htx_to_buf() will set buffer data to 0 because
701 * the HTX is empty, and allow us to do an xfer.
702 */
703 htx_to_buf(hc_htx, &hc->req.buf);
704 htx_to_buf(htx, &req->buf);
William Lallemandccc7ee42022-03-18 17:57:15 +0100705 b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200706 channel_add_input(req, data);
William Lallemandccc7ee42022-03-18 17:57:15 +0100707 } else {
708 struct htx_ret ret;
Christopher Faulet600985d2022-01-12 11:14:08 +0100709
Christopher Faulet6b4f1f62022-04-29 13:56:12 +0200710 ret = htx_xfer_blks(htx, hc_htx, htx_used_space(hc_htx), HTX_BLK_UNUSED);
William Lallemandccc7ee42022-03-18 17:57:15 +0100711 channel_add_input(req, ret.ret);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100712
William Lallemandccc7ee42022-03-18 17:57:15 +0100713 /* we must copy the EOM if we empty the buffer */
714 if (htx_is_empty(hc_htx)) {
715 htx->flags |= (hc_htx->flags & HTX_FL_EOM);
716 }
717 htx_to_buf(htx, &req->buf);
718 htx_to_buf(hc_htx, &hc->req.buf);
719 }
720
721
722 if (!b_data(&hc->req.buf))
723 b_free(&hc->req.buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200724 }
725
William Lallemanddb8a1f32021-11-08 16:55:14 +0100726 htx = htx_from_buf(&req->buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200727 if (!htx)
728 goto more;
729
730 /* if the request contains the HTX_FL_EOM, we finished the request part. */
Christopher Faulet3d433242022-03-03 15:38:39 +0100731 if (htx->flags & HTX_FL_EOM) {
Christopher Faulet3d433242022-03-03 15:38:39 +0100732 req->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +0200733 se_fl_set(appctx->sedesc, SE_FL_EOI);
William Lallemand0da616e2021-10-28 15:34:26 +0200734 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
Christopher Faulet3d433242022-03-03 15:38:39 +0100735 }
William Lallemand0da616e2021-10-28 15:34:26 +0200736
William Lallemand1eca8942022-03-17 14:57:23 +0100737 goto process_data; /* we need to leave the IO handler once we wrote the request */
William Lallemand0da616e2021-10-28 15:34:26 +0200738 }
739 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200740
741 case HTTPCLIENT_S_RES_STLINE:
742 /* copy the start line in the hc structure,then remove the htx block */
William Lallemanda625b032022-03-17 14:45:46 +0100743 if (!co_data(res))
William Lallemand33b0d092021-08-13 16:05:53 +0200744 goto more;
745 htx = htxbuf(&res->buf);
746 if (!htx)
747 goto more;
William Lallemand97f69c62022-03-10 17:23:40 +0100748 blk = htx_get_head_blk(htx);
William Lallemand33b0d092021-08-13 16:05:53 +0200749 if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL))
750 sl = htx_get_blk_ptr(htx, blk);
751 if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP)))
752 goto more;
753
754 /* copy the status line in the httpclient */
755 hc->res.status = sl->info.res.status;
756 hc->res.vsn = istdup(htx_sl_res_vsn(sl));
757 hc->res.reason = istdup(htx_sl_res_reason(sl));
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100758 sz = htx_get_blksz(blk);
Christopher Faulet0055d562022-04-29 14:09:03 +0200759 c_rew(res, sz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100760 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200761 /* caller callback */
762 if (hc->ops.res_stline)
763 hc->ops.res_stline(hc);
764
765 /* if there is no HTX data anymore and the EOM flag is
766 * set, leave (no body) */
767 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM)
768 appctx->st0 = HTTPCLIENT_S_RES_END;
769 else
770 appctx->st0 = HTTPCLIENT_S_RES_HDR;
771 break;
772
773 case HTTPCLIENT_S_RES_HDR:
774 /* first copy the headers in a local hdrs
775 * structure, once we the total numbers of the
776 * header we allocate the right size and copy
777 * them. The htx block of the headers are
778 * removed each time one is read */
779 {
780 struct http_hdr hdrs[global.tune.max_http_hdr];
781
William Lallemanda625b032022-03-17 14:45:46 +0100782 if (!co_data(res))
William Lallemand33b0d092021-08-13 16:05:53 +0200783 goto more;
784 htx = htxbuf(&res->buf);
785 if (!htx)
786 goto more;
787
788 hdr_num = 0;
Christopher Faulet534645d2022-04-29 13:44:46 +0200789 blk = htx_get_head_blk(htx);
790 while (blk) {
William Lallemand33b0d092021-08-13 16:05:53 +0200791 enum htx_blk_type type = htx_get_blk_type(blk);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100792 uint32_t sz = htx_get_blksz(blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200793
Christopher Faulet534645d2022-04-29 13:44:46 +0200794 c_rew(res, sz);
William Lallemandc020b252022-03-09 18:56:02 +0100795
Christopher Faulet18de6f22022-06-01 16:37:49 +0200796 if (type == HTX_BLK_HDR) {
William Lallemandc020b252022-03-09 18:56:02 +0100797 hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk));
798 hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk));
William Lallemandc020b252022-03-09 18:56:02 +0100799 hdr_num++;
800 }
Christopher Faulet534645d2022-04-29 13:44:46 +0200801 else if (type == HTX_BLK_EOH) {
802 /* create a NULL end of array and leave the loop */
William Lallemand33b0d092021-08-13 16:05:53 +0200803 hdrs[hdr_num].n = IST_NULL;
804 hdrs[hdr_num].v = IST_NULL;
Christopher Faulet18de6f22022-06-01 16:37:49 +0200805 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200806 break;
807 }
Christopher Faulet18de6f22022-06-01 16:37:49 +0200808 blk = htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200809 }
810
William Lallemand0d6f7792021-08-20 11:59:49 +0200811 if (hdr_num) {
812 /* alloc and copy the headers in the httpclient struct */
813 hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs));
814 if (!hc->res.hdrs)
815 goto end;
816 memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
William Lallemand33b0d092021-08-13 16:05:53 +0200817
William Lallemand0d6f7792021-08-20 11:59:49 +0200818 /* caller callback */
819 if (hc->ops.res_headers)
820 hc->ops.res_headers(hc);
821 }
William Lallemand33b0d092021-08-13 16:05:53 +0200822
823 /* if there is no HTX data anymore and the EOM flag is
824 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200825 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200826 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200827 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200828 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200829 }
William Lallemand33b0d092021-08-13 16:05:53 +0200830 }
831 break;
832
833 case HTTPCLIENT_S_RES_BODY:
834 /*
835 * The IO handler removes the htx blocks in the response buffer and
836 * push them in the hc->res.buf buffer in a raw format.
837 */
William Lallemanda625b032022-03-17 14:45:46 +0100838 if (!co_data(res))
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100839 goto more;
840
William Lallemand33b0d092021-08-13 16:05:53 +0200841 htx = htxbuf(&res->buf);
842 if (!htx || htx_is_empty(htx))
843 goto more;
844
Christopher Faulet600985d2022-01-12 11:14:08 +0100845 if (!b_alloc(&hc->res.buf))
846 goto more;
847
William Lallemand33b0d092021-08-13 16:05:53 +0200848 if (b_full(&hc->res.buf))
Christopher Faulet600985d2022-01-12 11:14:08 +0100849 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200850
851 /* decapsule the htx data to raw data */
Christopher Faulet534645d2022-04-29 13:44:46 +0200852 blk = htx_get_head_blk(htx);
853 while (blk) {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100854 enum htx_blk_type type = htx_get_blk_type(blk);
855 size_t count = co_data(res);
856 uint32_t blksz = htx_get_blksz(blk);
857 uint32_t room = b_room(&hc->res.buf);
858 uint32_t vlen;
William Lallemand33b0d092021-08-13 16:05:53 +0200859
William Lallemandc8f1eb92022-03-09 11:58:51 +0100860 /* we should try to copy the maximum output data in a block, which fit
861 * the destination buffer */
862 vlen = MIN(count, blksz);
863 vlen = MIN(vlen, room);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100864
William Lallemandc8f1eb92022-03-09 11:58:51 +0100865 if (vlen == 0)
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100866 goto process_data;
867
William Lallemand33b0d092021-08-13 16:05:53 +0200868 if (type == HTX_BLK_DATA) {
869 struct ist v = htx_get_blk_value(htx, blk);
870
William Lallemandc8f1eb92022-03-09 11:58:51 +0100871 __b_putblk(&hc->res.buf, v.ptr, vlen);
872 c_rew(res, vlen);
William Lallemand33b0d092021-08-13 16:05:53 +0200873
William Lallemandc8f1eb92022-03-09 11:58:51 +0100874 if (vlen == blksz)
Christopher Faulet534645d2022-04-29 13:44:46 +0200875 blk = htx_remove_blk(htx, blk);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100876 else
877 htx_cut_data_blk(htx, blk, vlen);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100878
William Lallemand33b0d092021-08-13 16:05:53 +0200879 /* the data must be processed by the caller in the receive phase */
880 if (hc->ops.res_payload)
881 hc->ops.res_payload(hc);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100882
883 /* cannot copy everything, need to processs */
884 if (vlen != blksz)
885 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200886 } else {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100887 if (vlen != blksz)
888 goto process_data;
889
William Lallemand33b0d092021-08-13 16:05:53 +0200890 /* remove any block which is not a data block */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100891 c_rew(res, blksz);
Christopher Faulet534645d2022-04-29 13:44:46 +0200892 blk = htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200893 }
894 }
William Lallemandc8f1eb92022-03-09 11:58:51 +0100895
William Lallemand33b0d092021-08-13 16:05:53 +0200896 /* if not finished, should be called again */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100897 if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)))
William Lallemand33b0d092021-08-13 16:05:53 +0200898 goto more;
899
William Lallemandc8f1eb92022-03-09 11:58:51 +0100900
William Lallemand33b0d092021-08-13 16:05:53 +0200901 /* end of message, we should quit */
902 appctx->st0 = HTTPCLIENT_S_RES_END;
903 break;
904
905 case HTTPCLIENT_S_RES_END:
906 goto end;
907 break;
908 }
909 }
910
911process_data:
912
Willy Tarreaub89f8722022-05-27 10:37:32 +0200913 sc_will_read(sc);
William Lallemand33b0d092021-08-13 16:05:53 +0200914
915 return;
916more:
917 /* There was not enough data in the response channel */
918
Willy Tarreaub89f8722022-05-27 10:37:32 +0200919 sc_need_room(sc);
William Lallemand33b0d092021-08-13 16:05:53 +0200920
921 if (appctx->st0 == HTTPCLIENT_S_RES_END)
922 goto end;
923
924 /* The state machine tries to handle as much data as possible, if there
925 * isn't any data to handle and a shutdown is detected, let's stop
926 * everything */
927 if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) ||
William Lallemand58a81ae2022-03-17 15:14:15 +0100928 (res->flags & CF_SHUTW) ||
929 ((res->flags & CF_SHUTW_NOW) && channel_is_empty(res))) {
William Lallemand33b0d092021-08-13 16:05:53 +0200930 goto end;
931 }
932 return;
933
934end:
Willy Tarreaub89f8722022-05-27 10:37:32 +0200935 sc_shutw(sc);
936 sc_shutr(sc);
William Lallemand33b0d092021-08-13 16:05:53 +0200937 return;
938}
939
Christopher Fauletb1e08362022-05-12 15:33:14 +0200940static int httpclient_applet_init(struct appctx *appctx)
941{
942 struct httpclient *hc = appctx->svcctx;
943 struct stream *s;
944 struct sockaddr_storage *addr = NULL;
945 struct sockaddr_storage ss_url = {};
946 struct sockaddr_storage *ss_dst;
947 enum obj_type *target = NULL;
948 struct ist host = IST_NULL;
949 enum http_scheme scheme;
950 int port;
951 int doresolve = 0;
952
953
954 /* parse the URL and */
955 httpclient_spliturl(hc->req.url, &scheme, &host, &port);
956
957 if (hc->dst) {
958 /* if httpclient_set_dst() was used, sets the alternative address */
959 ss_dst = hc->dst;
960 } else {
961 /* set the dst using the host, or 0.0.0.0 to resolve */
962 ist2str(trash.area, host);
963 ss_dst = str2ip2(trash.area, &ss_url, 0);
964 if (!ss_dst) { /* couldn't get an IP from that, try to resolve */
965 doresolve = 1;
966 ss_dst = str2ip2("0.0.0.0", &ss_url, 0);
967 }
968 sock_inet_set_port(ss_dst, port);
969 }
970
Christopher Fauletb1e08362022-05-12 15:33:14 +0200971 if (!sockaddr_alloc(&addr, ss_dst, sizeof(*ss_dst)))
972 goto out_error;
973
974 /* choose the SSL server or not */
975 switch (scheme) {
976 case SCH_HTTP:
977 target = &httpclient_srv_raw->obj_type;
978 break;
979 case SCH_HTTPS:
980#ifdef USE_OPENSSL
981 if (httpclient_srv_ssl) {
982 target = &httpclient_srv_ssl->obj_type;
983 } else {
984 ha_alert("httpclient: SSL was disabled (wrong verify/ca-file)!\n");
985 goto out_free_addr;
986 }
987#else
988 ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__);
989 goto out_free_addr;
990#endif
991 break;
992 }
993
994 if (appctx_finalize_startup(appctx, httpclient_proxy, &hc->req.buf) == -1) {
995 ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__);
996 goto out_free_addr;
997 }
998
999 s = appctx_strm(appctx);
1000 s->target = target;
1001 /* set the "timeout server" */
1002 s->req.wto = hc->timeout_server;
1003 s->res.rto = hc->timeout_server;
1004
1005 if (doresolve) {
1006 /* in order to do the set-dst we need to put the address on the front */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001007 s->scf->dst = addr;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001008 } else {
1009 /* in cases we don't use the resolve we already have the address
1010 * and must put it on the backend side, some of the cases are
1011 * not meant to be used on the frontend (sockpair, unix socket etc.) */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001012 s->scb->dst = addr;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001013 }
1014
Willy Tarreaucb041662022-05-17 19:44:42 +02001015 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001016 s->flags |= SF_ASSIGNED;
1017 s->res.flags |= CF_READ_DONTWAIT;
1018
1019 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001020 applet_need_more_data(appctx);
Christopher Fauletb1e08362022-05-12 15:33:14 +02001021 appctx_wakeup(appctx);
1022
1023 hc->appctx = appctx;
1024 hc->flags |= HTTPCLIENT_FS_STARTED;
1025
1026 /* The request was transferred when the stream was created. So switch
1027 * directly to REQ_BODY or RES_STLINE state
1028 */
1029 appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE);
1030 return 0;
1031
1032 out_free_addr:
1033 sockaddr_free(&addr);
1034 out_error:
1035 return -1;
1036}
1037
William Lallemand33b0d092021-08-13 16:05:53 +02001038static void httpclient_applet_release(struct appctx *appctx)
1039{
Willy Tarreau1eea6652022-05-05 20:12:01 +02001040 struct httpclient *hc = appctx->svcctx;
William Lallemand33b0d092021-08-13 16:05:53 +02001041
William Lallemand1123dde2021-09-21 10:58:10 +02001042 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +02001043 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +02001044 /* the applet is leaving, remove the ptr so we don't try to call it
1045 * again from the caller */
1046 hc->appctx = NULL;
1047
William Lallemandeb0d4c42022-04-06 14:12:37 +02001048 if (hc->ops.res_end)
1049 hc->ops.res_end(hc);
William Lallemandecb83e12021-09-28 11:00:43 +02001050
1051 /* destroy the httpclient when set to autotokill */
1052 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
1053 httpclient_destroy(hc);
1054 }
1055
William Lallemand33b0d092021-08-13 16:05:53 +02001056 return;
1057}
1058
1059/* HTTP client applet */
1060static struct applet httpclient_applet = {
1061 .obj_type = OBJ_TYPE_APPLET,
1062 .name = "<HTTPCLIENT>",
1063 .fct = httpclient_applet_io_handler,
Christopher Fauletb1e08362022-05-12 15:33:14 +02001064 .init = httpclient_applet_init,
William Lallemand33b0d092021-08-13 16:05:53 +02001065 .release = httpclient_applet_release,
1066};
1067
William Lallemand5392ff62022-04-28 16:55:02 +02001068
1069static int httpclient_resolve_init()
1070{
1071 struct act_rule *rule;
1072 int i;
William Lallemand8a734cb2022-05-04 16:10:47 +02001073 char *do_resolve = NULL;
1074 char *http_rules[][11] = {
William Lallemand5392ff62022-04-28 16:55:02 +02001075 { "set-var(txn.hc_ip)", "dst", "" },
William Lallemand8a734cb2022-05-04 16:10:47 +02001076 { do_resolve, "hdr(Host),lower", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" },
William Lallemand5392ff62022-04-28 16:55:02 +02001077 { "return", "status", "503", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" },
1078 { "capture", "var(txn.hc_ip)", "len", "40", "" },
1079 { "set-dst", "var(txn.hc_ip)", "" },
1080 { "" }
1081 };
1082
William Lallemand8a734cb2022-05-04 16:10:47 +02001083 if (!resolvers_id)
1084 resolvers_id = strdup("default");
1085
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001086 memprintf(&do_resolve, "do-resolve(txn.hc_ip,%s%s%s)", resolvers_id, resolvers_prefer ? "," : "", resolvers_prefer ? resolvers_prefer : "");
William Lallemand8a734cb2022-05-04 16:10:47 +02001087 http_rules[1][0] = do_resolve;
1088
William Lallemand7867f632022-05-05 19:02:59 +02001089 /* Try to create the default resolvers section */
1090 resolvers_create_default();
1091
William Lallemand8a734cb2022-05-04 16:10:47 +02001092 /* if the resolver does not exist and no hard_error was set, simply ignore resolving */
1093 if (!find_resolvers_by_id(resolvers_id) && !hard_error_resolvers) {
1094 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001095 return 0;
William Lallemand8a734cb2022-05-04 16:10:47 +02001096 }
William Lallemand5392ff62022-04-28 16:55:02 +02001097
1098
1099 for (i = 0; *http_rules[i][0] != '\0'; i++) {
1100 rule = parse_http_req_cond((const char **)http_rules[i], "httpclient", 0, httpclient_proxy);
1101 if (!rule) {
William Lallemand8a734cb2022-05-04 16:10:47 +02001102 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001103 ha_alert("Couldn't setup the httpclient resolver.\n");
1104 return 1;
1105 }
1106 LIST_APPEND(&httpclient_proxy->http_req_rules, &rule->list);
1107 }
1108
William Lallemand8a734cb2022-05-04 16:10:47 +02001109 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001110 return 0;
1111}
1112
1113
1114
William Lallemand83614a92021-08-13 14:47:57 +02001115/*
1116 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
1117 * the other for HTTPS.
1118 */
William Lallemand2c8b0842022-04-22 15:16:09 +02001119static int httpclient_precheck()
William Lallemand83614a92021-08-13 14:47:57 +02001120{
William Lallemand85af49c2022-05-04 14:33:57 +02001121 int err_code = ERR_NONE;
William Lallemand83614a92021-08-13 14:47:57 +02001122 char *errmsg = NULL;
1123
William Lallemandc6ceba32022-04-22 16:49:53 +02001124 if (global.mode & MODE_MWORKER_WAIT)
William Lallemand85af49c2022-05-04 14:33:57 +02001125 return ERR_NONE;
William Lallemandc6ceba32022-04-22 16:49:53 +02001126
William Lallemand83614a92021-08-13 14:47:57 +02001127 httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg);
1128 if (!httpclient_proxy) {
William Lallemand85af49c2022-05-04 14:33:57 +02001129 memprintf(&errmsg, "couldn't allocate proxy.");
William Lallemand83614a92021-08-13 14:47:57 +02001130 err_code |= ERR_ALERT | ERR_FATAL;
1131 goto err;
1132 }
1133
Willy Tarreau0e72e402021-08-20 10:23:12 +02001134 proxy_preset_defaults(httpclient_proxy);
1135
William Lallemandccc7ee42022-03-18 17:57:15 +01001136 httpclient_proxy->options |= PR_O_WREQ_BODY;
William Lallemand71abad02022-03-17 15:24:28 +01001137 httpclient_proxy->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED | PR_RE_TIMEOUT;
William Lallemand83614a92021-08-13 14:47:57 +02001138 httpclient_proxy->options2 |= PR_O2_INDEPSTR;
1139 httpclient_proxy->mode = PR_MODE_HTTP;
1140 httpclient_proxy->maxconn = 0;
1141 httpclient_proxy->accept = NULL;
William Lallemand71abad02022-03-17 15:24:28 +01001142 httpclient_proxy->conn_retries = CONN_RETRIES;
William Lallemand83614a92021-08-13 14:47:57 +02001143 httpclient_proxy->timeout.client = TICK_ETERNITY;
1144 /* The HTTP Client use the "option httplog" with the global log server */
1145 httpclient_proxy->conf.logformat_string = default_http_log_format;
1146 httpclient_proxy->http_needed = 1;
1147
1148 /* clear HTTP server */
1149 httpclient_srv_raw = new_server(httpclient_proxy);
1150 if (!httpclient_srv_raw) {
William Lallemand83614a92021-08-13 14:47:57 +02001151 memprintf(&errmsg, "out of memory.");
William Lallemand85af49c2022-05-04 14:33:57 +02001152 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001153 goto err;
1154 }
1155
1156 httpclient_srv_raw->iweight = 0;
1157 httpclient_srv_raw->uweight = 0;
1158 httpclient_srv_raw->xprt = xprt_get(XPRT_RAW);
William Lallemand1218d192022-05-03 14:09:06 +02001159 httpclient_srv_raw->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */
William Lallemand83614a92021-08-13 14:47:57 +02001160 httpclient_srv_raw->id = strdup("<HTTPCLIENT>");
William Lallemand85af49c2022-05-04 14:33:57 +02001161 if (!httpclient_srv_raw->id) {
1162 memprintf(&errmsg, "out of memory.");
1163 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001164 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001165 }
William Lallemand83614a92021-08-13 14:47:57 +02001166
William Lallemand957ab132021-08-24 18:33:28 +02001167#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +02001168 /* SSL HTTP server */
1169 httpclient_srv_ssl = new_server(httpclient_proxy);
1170 if (!httpclient_srv_ssl) {
1171 memprintf(&errmsg, "out of memory.");
1172 err_code |= ERR_ALERT | ERR_FATAL;
1173 goto err;
1174 }
1175 httpclient_srv_ssl->iweight = 0;
1176 httpclient_srv_ssl->uweight = 0;
1177 httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL);
1178 httpclient_srv_ssl->use_ssl = 1;
William Lallemand1218d192022-05-03 14:09:06 +02001179 httpclient_srv_ssl->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */
William Lallemand211c9672021-08-24 17:18:13 +02001180 httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>");
William Lallemand85af49c2022-05-04 14:33:57 +02001181 if (!httpclient_srv_ssl->id) {
1182 memprintf(&errmsg, "out of memory.");
1183 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001184 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001185 }
William Lallemand83614a92021-08-13 14:47:57 +02001186
William Lallemandeaa703e2022-04-22 17:52:33 +02001187 httpclient_srv_ssl->ssl_ctx.verify = httpclient_ssl_verify;
William Lallemand4006b0f2022-04-25 18:23:35 +02001188 /* if the verify is required, try to load the system CA */
William Lallemandeaa703e2022-04-22 17:52:33 +02001189 if (httpclient_ssl_verify == SSL_SOCK_VERIFY_REQUIRED) {
William Lallemand683fbb82022-05-04 15:43:01 +02001190
1191 if (!httpclient_ssl_ca_file)
1192 httpclient_ssl_ca_file = strdup("@system-ca");
1193
1194 httpclient_srv_ssl->ssl_ctx.ca_file = httpclient_ssl_ca_file;
William Lallemand4006b0f2022-04-25 18:23:35 +02001195 if (!ssl_store_load_locations_file(httpclient_srv_ssl->ssl_ctx.ca_file, 1, CAFILE_CERT)) {
William Lallemand6fce46a2022-05-04 14:53:41 +02001196 /* if we failed to load the ca-file, only quits in
1197 * error with hard_error, otherwise just disable the
1198 * feature. */
1199 if (hard_error_ssl) {
1200 memprintf(&errmsg, "cannot initialize SSL verify with 'ca-file \"%s\"'.", httpclient_srv_ssl->ssl_ctx.ca_file);
1201 err_code |= ERR_ALERT | ERR_FATAL;
1202 goto err;
1203 } else {
1204 ha_free(&httpclient_srv_ssl->ssl_ctx.ca_file);
1205 srv_drop(httpclient_srv_ssl);
1206 httpclient_srv_ssl = NULL;
1207 }
William Lallemand4006b0f2022-04-25 18:23:35 +02001208 }
William Lallemandeaa703e2022-04-22 17:52:33 +02001209 }
William Lallemandcf5cb0b2022-04-22 14:48:45 +02001210
William Lallemand957ab132021-08-24 18:33:28 +02001211#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +02001212
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001213 /* add the proxy in the proxy list only if everything is successful */
William Lallemand83614a92021-08-13 14:47:57 +02001214 httpclient_proxy->next = proxies_list;
1215 proxies_list = httpclient_proxy;
1216
William Lallemand85af49c2022-05-04 14:33:57 +02001217 if (httpclient_resolve_init() != 0) {
1218 memprintf(&errmsg, "cannot initialize resolvers.");
1219 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand5392ff62022-04-28 16:55:02 +02001220 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001221 }
William Lallemand5392ff62022-04-28 16:55:02 +02001222
William Lallemand211c9672021-08-24 17:18:13 +02001223 /* link the 2 servers in the proxy */
1224 httpclient_srv_raw->next = httpclient_proxy->srv;
William Lallemand957ab132021-08-24 18:33:28 +02001225 httpclient_proxy->srv = httpclient_srv_raw;
1226
1227#ifdef USE_OPENSSL
William Lallemand4006b0f2022-04-25 18:23:35 +02001228 if (httpclient_srv_ssl) {
1229 httpclient_srv_ssl->next = httpclient_proxy->srv;
1230 httpclient_proxy->srv = httpclient_srv_ssl;
1231 }
William Lallemand957ab132021-08-24 18:33:28 +02001232#endif
1233
William Lallemand211c9672021-08-24 17:18:13 +02001234
William Lallemand83614a92021-08-13 14:47:57 +02001235err:
William Lallemand85af49c2022-05-04 14:33:57 +02001236 if (err_code & ERR_CODE) {
1237 ha_alert("httpclient: cannot initialize: %s\n", errmsg);
1238 free(errmsg);
1239 srv_drop(httpclient_srv_raw);
William Lallemand957ab132021-08-24 18:33:28 +02001240#ifdef USE_OPENSSL
William Lallemand85af49c2022-05-04 14:33:57 +02001241 srv_drop(httpclient_srv_ssl);
William Lallemand957ab132021-08-24 18:33:28 +02001242#endif
William Lallemand85af49c2022-05-04 14:33:57 +02001243 free_proxy(httpclient_proxy);
1244 }
William Lallemand83614a92021-08-13 14:47:57 +02001245 return err_code;
1246}
1247
William Lallemand2c8b0842022-04-22 15:16:09 +02001248static int httpclient_postcheck()
William Lallemand83614a92021-08-13 14:47:57 +02001249{
William Lallemand85af49c2022-05-04 14:33:57 +02001250 int err_code = ERR_NONE;
William Lallemand83614a92021-08-13 14:47:57 +02001251 struct logsrv *logsrv;
1252 struct proxy *curproxy = httpclient_proxy;
William Lallemand71e31582022-03-16 15:47:47 +01001253 char *errmsg = NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001254
William Lallemandc6ceba32022-04-22 16:49:53 +02001255 if (global.mode & MODE_MWORKER_WAIT)
William Lallemand85af49c2022-05-04 14:33:57 +02001256 return ERR_NONE;
William Lallemandc6ceba32022-04-22 16:49:53 +02001257
William Lallemand83614a92021-08-13 14:47:57 +02001258 /* copy logs from "global" log list */
1259 list_for_each_entry(logsrv, &global.logsrvs, list) {
1260 struct logsrv *node = malloc(sizeof(*node));
1261
1262 if (!node) {
William Lallemand85af49c2022-05-04 14:33:57 +02001263 memprintf(&errmsg, "out of memory.");
1264 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001265 goto err;
1266 }
1267
1268 memcpy(node, logsrv, sizeof(*node));
1269 LIST_INIT(&node->list);
1270 LIST_APPEND(&curproxy->logsrvs, &node->list);
Willy Tarreaue1e9f6b2022-04-21 14:14:28 +02001271 node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
1272 node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001273 }
1274 if (curproxy->conf.logformat_string) {
William Lallemand83614a92021-08-13 14:47:57 +02001275 curproxy->conf.args.ctx = ARGC_LOG;
1276 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
1277 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
William Lallemand715c1012022-03-16 16:39:23 +01001278 SMP_VAL_FE_LOG_END, &errmsg)) {
William Lallemand85af49c2022-05-04 14:33:57 +02001279 memprintf(&errmsg, "failed to parse log-format : %s.", errmsg);
1280 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001281 goto err;
1282 }
1283 curproxy->conf.args.file = NULL;
1284 curproxy->conf.args.line = 0;
1285 }
William Lallemand71e31582022-03-16 15:47:47 +01001286
1287#ifdef USE_OPENSSL
William Lallemand4006b0f2022-04-25 18:23:35 +02001288 if (httpclient_srv_ssl) {
William Lallemand715c1012022-03-16 16:39:23 +01001289 /* init the SNI expression */
1290 /* always use the host header as SNI, without the port */
1291 httpclient_srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)");
1292 err_code |= server_parse_sni_expr(httpclient_srv_ssl, httpclient_proxy, &errmsg);
1293 if (err_code & ERR_CODE) {
William Lallemand85af49c2022-05-04 14:33:57 +02001294 memprintf(&errmsg, "failed to configure sni: %s.", errmsg);
William Lallemand715c1012022-03-16 16:39:23 +01001295 goto err;
1296 }
William Lallemand71e31582022-03-16 15:47:47 +01001297 }
1298#endif
1299
William Lallemand83614a92021-08-13 14:47:57 +02001300err:
William Lallemand85af49c2022-05-04 14:33:57 +02001301 if (err_code & ERR_CODE) {
1302 ha_alert("httpclient: failed to initialize: %s\n", errmsg);
1303 free(errmsg);
1304
1305 }
1306 return err_code;
William Lallemand83614a92021-08-13 14:47:57 +02001307}
1308
William Lallemand83614a92021-08-13 14:47:57 +02001309/* initialize the proxy and servers for the HTTP client */
1310
William Lallemand2c8b0842022-04-22 15:16:09 +02001311REGISTER_PRE_CHECK(httpclient_precheck);
1312REGISTER_POST_CHECK(httpclient_postcheck);
William Lallemandeaa703e2022-04-22 17:52:33 +02001313
William Lallemand8a734cb2022-05-04 16:10:47 +02001314static int httpclient_parse_global_resolvers(char **args, int section_type, struct proxy *curpx,
1315 const struct proxy *defpx, const char *file, int line,
1316 char **err)
1317{
1318 if (too_many_args(1, args, err, NULL))
1319 return -1;
1320
1321 /* any configuration should set the hard_error flag */
1322 hard_error_resolvers = 1;
1323
1324 free(resolvers_id);
1325 resolvers_id = strdup(args[1]);
1326
1327 return 0;
1328}
1329
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001330static int httpclient_parse_global_prefer(char **args, int section_type, struct proxy *curpx,
1331 const struct proxy *defpx, const char *file, int line,
1332 char **err)
1333{
1334 if (too_many_args(1, args, err, NULL))
1335 return -1;
1336
1337 /* any configuration should set the hard_error flag */
1338 hard_error_resolvers = 1;
1339
1340
1341 if (strcmp(args[1],"ipv4") == 0)
1342 resolvers_prefer = "ipv4";
1343 else if (strcmp(args[1],"ipv6") == 0)
1344 resolvers_prefer = "ipv6";
1345 else {
1346 ha_alert("parsing [%s:%d] : '%s' expects 'ipv4' or 'ipv6' as argument.\n", file, line, args[0]);
1347 return -1;
1348 }
1349
1350 return 0;
1351}
William Lallemand8a734cb2022-05-04 16:10:47 +02001352
1353
William Lallemandeaa703e2022-04-22 17:52:33 +02001354#ifdef USE_OPENSSL
William Lallemand683fbb82022-05-04 15:43:01 +02001355static int httpclient_parse_global_ca_file(char **args, int section_type, struct proxy *curpx,
1356 const struct proxy *defpx, const char *file, int line,
1357 char **err)
1358{
1359 if (too_many_args(1, args, err, NULL))
1360 return -1;
1361
1362 /* any configuration should set the hard_error flag */
1363 hard_error_ssl = 1;
1364
1365 free(httpclient_ssl_ca_file);
1366 httpclient_ssl_ca_file = strdup(args[1]);
1367
1368 return 0;
1369}
1370
William Lallemandeaa703e2022-04-22 17:52:33 +02001371static int httpclient_parse_global_verify(char **args, int section_type, struct proxy *curpx,
1372 const struct proxy *defpx, const char *file, int line,
1373 char **err)
1374{
1375 if (too_many_args(1, args, err, NULL))
1376 return -1;
1377
William Lallemand6fce46a2022-05-04 14:53:41 +02001378 /* any configuration should set the hard_error flag */
1379 hard_error_ssl = 1;
1380
William Lallemandeaa703e2022-04-22 17:52:33 +02001381 if (strcmp(args[1],"none") == 0)
William Lallemand04994de2022-04-28 19:35:21 +02001382 httpclient_ssl_verify = SSL_SOCK_VERIFY_NONE;
William Lallemandeaa703e2022-04-22 17:52:33 +02001383 else if (strcmp(args[1],"required") == 0)
William Lallemand04994de2022-04-28 19:35:21 +02001384 httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED;
William Lallemandeaa703e2022-04-22 17:52:33 +02001385 else {
1386 ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, line, args[0]);
1387 return -1;
1388 }
1389
1390 return 0;
1391}
William Lallemand8a734cb2022-05-04 16:10:47 +02001392#endif /* ! USE_OPENSSL */
William Lallemandeaa703e2022-04-22 17:52:33 +02001393
1394static struct cfg_kw_list cfg_kws = {ILH, {
William Lallemand8a734cb2022-05-04 16:10:47 +02001395 { CFG_GLOBAL, "httpclient.resolvers.id", httpclient_parse_global_resolvers },
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001396 { CFG_GLOBAL, "httpclient.resolvers.prefer", httpclient_parse_global_prefer },
William Lallemand8a734cb2022-05-04 16:10:47 +02001397#ifdef USE_OPENSSL
William Lallemand9ff95e22022-05-04 13:52:29 +02001398 { CFG_GLOBAL, "httpclient.ssl.verify", httpclient_parse_global_verify },
William Lallemand683fbb82022-05-04 15:43:01 +02001399 { CFG_GLOBAL, "httpclient.ssl.ca-file", httpclient_parse_global_ca_file },
William Lallemand8a734cb2022-05-04 16:10:47 +02001400#endif
William Lallemandeaa703e2022-04-22 17:52:33 +02001401 { 0, NULL, NULL },
1402}};
1403
1404INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);