blob: 2c33c85a27e51a57f9824d5b77cb25dabb9b8353 [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 Tarreaudf3231c2022-09-02 09:02:21 +020035#include <haproxy/ssl_sock.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
William Lallemand83614a92021-08-13 14:47:57 +020042static struct proxy *httpclient_proxy;
William Lallemand6fce46a2022-05-04 14:53:41 +020043
William Lallemand957ab132021-08-24 18:33:28 +020044#ifdef USE_OPENSSL
William Lallemand6fce46a2022-05-04 14:53:41 +020045/* if the httpclient is not configured, error are ignored and features are limited */
46static int hard_error_ssl = 0;
William Lallemandf1344b32022-04-26 12:00:06 +020047static int httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED;
William Lallemand683fbb82022-05-04 15:43:01 +020048static char *httpclient_ssl_ca_file = NULL;
William Lallemand957ab132021-08-24 18:33:28 +020049#endif
William Lallemand33b0d092021-08-13 16:05:53 +020050static struct applet httpclient_applet;
51
William Lallemand7c5a7ef2022-05-04 15:59:44 +020052/* if the httpclient is not configured, error are ignored and features are limited */
William Lallemand8a734cb2022-05-04 16:10:47 +020053static int hard_error_resolvers = 0;
54static char *resolvers_id = NULL;
William Lallemand7c5a7ef2022-05-04 15:59:44 +020055static char *resolvers_prefer = NULL;
William Lallemande279f592023-05-11 21:08:38 +020056static int resolvers_disabled = 0;
William Lallemandeaa703e2022-04-22 17:52:33 +020057
William Lallemand03a4eb12021-08-18 16:46:21 +020058/* --- This part of the file implement an HTTP client over the CLI ---
59 * The functions will be starting by "hc_cli" for "httpclient cli"
60 */
61
Willy Tarreau89a7c412022-05-05 19:38:21 +020062/* the CLI context for the httpclient command */
63struct hcli_svc_ctx {
64 struct httpclient *hc; /* the httpclient instance */
65 uint flags; /* flags from HC_CLI_F_* above */
66};
William Lallemand03a4eb12021-08-18 16:46:21 +020067
68/* These are the callback used by the HTTP Client when it needs to notify new
Willy Tarreau89a7c412022-05-05 19:38:21 +020069 * data, we only sets a flag in the IO handler via the svcctx.
70 */
William Lallemand03a4eb12021-08-18 16:46:21 +020071void hc_cli_res_stline_cb(struct httpclient *hc)
72{
73 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +020074 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +020075
William Lallemanddfc3f892021-08-20 11:35:29 +020076 if (!appctx)
77 return;
78
Willy Tarreau89a7c412022-05-05 19:38:21 +020079 ctx = appctx->svcctx;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +010080 ctx->flags |= HC_F_RES_STLINE;
William Lallemanddfc3f892021-08-20 11:35:29 +020081 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020082}
83
84void hc_cli_res_headers_cb(struct httpclient *hc)
85{
86 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +020087 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +020088
William Lallemanddfc3f892021-08-20 11:35:29 +020089 if (!appctx)
90 return;
91
Willy Tarreau89a7c412022-05-05 19:38:21 +020092 ctx = appctx->svcctx;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +010093 ctx->flags |= HC_F_RES_HDR;
William Lallemanddfc3f892021-08-20 11:35:29 +020094 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020095}
96
97void hc_cli_res_body_cb(struct httpclient *hc)
98{
99 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +0200100 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +0200101
William Lallemanddfc3f892021-08-20 11:35:29 +0200102 if (!appctx)
103 return;
104
Willy Tarreau89a7c412022-05-05 19:38:21 +0200105 ctx = appctx->svcctx;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100106 ctx->flags |= HC_F_RES_BODY;
William Lallemanddfc3f892021-08-20 11:35:29 +0200107 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200108}
109
110void hc_cli_res_end_cb(struct httpclient *hc)
111{
112 struct appctx *appctx = hc->caller;
Willy Tarreau89a7c412022-05-05 19:38:21 +0200113 struct hcli_svc_ctx *ctx;
William Lallemand03a4eb12021-08-18 16:46:21 +0200114
William Lallemanddfc3f892021-08-20 11:35:29 +0200115 if (!appctx)
116 return;
117
Willy Tarreau89a7c412022-05-05 19:38:21 +0200118 ctx = appctx->svcctx;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100119 ctx->flags |= HC_F_RES_END;
William Lallemanddfc3f892021-08-20 11:35:29 +0200120 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200121}
122
123/*
124 * Parse an httpclient keyword on the cli:
125 * httpclient <ID> <method> <URI>
126 */
127static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private)
128{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200129 struct hcli_svc_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemand03a4eb12021-08-18 16:46:21 +0200130 struct httpclient *hc;
131 char *err = NULL;
132 enum http_meth_t meth;
133 char *meth_str;
134 struct ist uri;
William Lallemanddec25c32021-10-25 19:48:37 +0200135 struct ist body = IST_NULL;
William Lallemand03a4eb12021-08-18 16:46:21 +0200136
137 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
138 return 1;
139
140 if (!*args[1] || !*args[2]) {
141 memprintf(&err, ": not enough parameters");
142 goto err;
143 }
144
145 meth_str = args[1];
146 uri = ist(args[2]);
147
William Lallemanddec25c32021-10-25 19:48:37 +0200148 if (payload)
149 body = ist(payload);
150
William Lallemand03a4eb12021-08-18 16:46:21 +0200151 meth = find_http_meth(meth_str, strlen(meth_str));
152
153 hc = httpclient_new(appctx, meth, uri);
154 if (!hc) {
155 goto err;
156 }
157
158 /* update the httpclient callbacks */
159 hc->ops.res_stline = hc_cli_res_stline_cb;
160 hc->ops.res_headers = hc_cli_res_headers_cb;
161 hc->ops.res_payload = hc_cli_res_body_cb;
162 hc->ops.res_end = hc_cli_res_end_cb;
163
Willy Tarreau89a7c412022-05-05 19:38:21 +0200164 ctx->hc = hc; /* store the httpclient ptr in the applet */
165 ctx->flags = 0;
William Lallemand03a4eb12021-08-18 16:46:21 +0200166
William Lallemandbad9c8c2022-01-14 14:10:33 +0100167 if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE)
William Lallemand03a4eb12021-08-18 16:46:21 +0200168 goto err;
169
170
171 if (!httpclient_start(hc))
172 goto err;
173
174 return 0;
175
176err:
177 memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : "");
178 return cli_err(appctx, err);
179}
180
181/* This function dumps the content of the httpclient receive buffer
182 * on the CLI output
183 *
184 * Return 1 when the processing is finished
185 * return 0 if it needs to be called again
186 */
187static int hc_cli_io_handler(struct appctx *appctx)
188{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200189 struct hcli_svc_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200190 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau89a7c412022-05-05 19:38:21 +0200191 struct httpclient *hc = ctx->hc;
William Lallemand03a4eb12021-08-18 16:46:21 +0200192 struct http_hdr *hdrs, *hdr;
193
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100194 if (ctx->flags & HC_F_RES_STLINE) {
Christopher Faulet0158bb22022-06-01 17:08:19 +0200195 chunk_printf(&trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn),
196 hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason));
197 if (applet_putchk(appctx, &trash) == -1)
198 goto more;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100199 ctx->flags &= ~HC_F_RES_STLINE;
William Lallemand03a4eb12021-08-18 16:46:21 +0200200 }
201
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100202 if (ctx->flags & HC_F_RES_HDR) {
Christopher Faulet0158bb22022-06-01 17:08:19 +0200203 chunk_reset(&trash);
William Lallemand03a4eb12021-08-18 16:46:21 +0200204 hdrs = hc->res.hdrs;
205 for (hdr = hdrs; isttest(hdr->v); hdr++) {
Christopher Faulet0158bb22022-06-01 17:08:19 +0200206 if (!h1_format_htx_hdr(hdr->n, hdr->v, &trash))
207 goto too_many_hdrs;
William Lallemand03a4eb12021-08-18 16:46:21 +0200208 }
Christopher Faulet0158bb22022-06-01 17:08:19 +0200209 if (!chunk_memcat(&trash, "\r\n", 2))
210 goto too_many_hdrs;
211 if (applet_putchk(appctx, &trash) == -1)
212 goto more;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100213 ctx->flags &= ~HC_F_RES_HDR;
William Lallemand03a4eb12021-08-18 16:46:21 +0200214 }
215
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100216 if (ctx->flags & HC_F_RES_BODY) {
William Lallemand03a4eb12021-08-18 16:46:21 +0200217 int ret;
218
Willy Tarreau475e4632022-05-27 10:26:46 +0200219 ret = httpclient_res_xfer(hc, sc_ib(sc));
220 channel_add_input(sc_ic(sc), ret); /* forward what we put in the buffer channel */
William Lallemand03a4eb12021-08-18 16:46:21 +0200221
Christopher Faulet0158bb22022-06-01 17:08:19 +0200222 /* remove the flag if the buffer was emptied */
223 if (httpclient_data(hc))
224 goto more;
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100225 ctx->flags &= ~HC_F_RES_BODY;
William Lallemand03a4eb12021-08-18 16:46:21 +0200226 }
227
228 /* we must close only if F_END is the last flag */
Remi Tricot-Le Breton95e7cf12022-12-20 11:11:03 +0100229 if (ctx->flags == HC_F_RES_END) {
230 ctx->flags &= ~HC_F_RES_END;
Christopher Faulet89f26262022-06-01 17:17:24 +0200231 goto end;
William Lallemand03a4eb12021-08-18 16:46:21 +0200232 }
233
Christopher Faulet0158bb22022-06-01 17:08:19 +0200234more:
235 if (!ctx->flags)
236 applet_have_no_more_data(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200237 return 0;
Christopher Faulet89f26262022-06-01 17:17:24 +0200238end:
239 return 1;
Christopher Faulet0158bb22022-06-01 17:08:19 +0200240
241too_many_hdrs:
242 return cli_err(appctx, "Too many headers.\n");
William Lallemand03a4eb12021-08-18 16:46:21 +0200243}
244
245static void hc_cli_release(struct appctx *appctx)
246{
Willy Tarreau89a7c412022-05-05 19:38:21 +0200247 struct hcli_svc_ctx *ctx = appctx->svcctx;
248 struct httpclient *hc = ctx->hc;
William Lallemand03a4eb12021-08-18 16:46:21 +0200249
250 /* Everything possible was printed on the CLI, we can destroy the client */
William Lallemandecb83e12021-09-28 11:00:43 +0200251 httpclient_stop_and_destroy(hc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200252
253 return;
254}
255
256/* register cli keywords */
257static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2c8f9842022-02-18 16:26:36 +0100258 { { "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 +0200259 { { NULL }, NULL, NULL, NULL }
260}};
261
262INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
263
264
265/* --- This part of the file implements the actual HTTP client API --- */
266
William Lallemand33b0d092021-08-13 16:05:53 +0200267/*
268 * Generate a simple request and fill the httpclient request buffer with it.
269 * The request contains a request line generated from the absolute <url> and
270 * <meth> as well as list of headers <hdrs>.
271 *
272 * If the buffer was filled correctly the function returns 0, if not it returns
273 * an error_code but there is no guarantee that the buffer wasn't modified.
274 */
William Lallemanddec25c32021-10-25 19:48:37 +0200275int 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 +0200276{
277 struct htx_sl *sl;
278 struct htx *htx;
279 int err_code = 0;
280 struct ist meth_ist, vsn;
William Lallemanddec25c32021-10-25 19:48:37 +0200281 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 +0100282 int i;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100283 int foundhost = 0, foundaccept = 0, foundua = 0;
William Lallemand33b0d092021-08-13 16:05:53 +0200284
Christopher Faulet600985d2022-01-12 11:14:08 +0100285 if (!b_alloc(&hc->req.buf))
286 goto error;
287
William Lallemand33b0d092021-08-13 16:05:53 +0200288 if (meth >= HTTP_METH_OTHER)
289 goto error;
290
291 meth_ist = http_known_methods[meth];
292
293 vsn = ist("HTTP/1.1");
294
295 htx = htx_from_buf(&hc->req.buf);
296 if (!htx)
297 goto error;
William Lallemande1e045f2022-01-14 14:08:34 +0100298
299 if (!hc->ops.req_payload && !isttest(payload))
300 flags |= HTX_SL_F_BODYLESS;
301
William Lallemand33b0d092021-08-13 16:05:53 +0200302 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn);
303 if (!sl) {
304 goto error;
305 }
306 sl->info.req.meth = meth;
307
William Lallemandf03b53c2021-11-24 15:38:17 +0100308 for (i = 0; hdrs && hdrs[i].n.len; i++) {
309 /* Don't check the value length because a header value may be empty */
310 if (isttest(hdrs[i].v) == 0)
311 continue;
312
313 if (isteqi(hdrs[i].n, ist("host")))
314 foundhost = 1;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100315 else if (isteqi(hdrs[i].n, ist("accept")))
316 foundaccept = 1;
317 else if (isteqi(hdrs[i].n, ist("user-agent")))
318 foundua = 1;
William Lallemandf03b53c2021-11-24 15:38:17 +0100319
320 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
321 goto error;
322 }
William Lallemand33b0d092021-08-13 16:05:53 +0200323
William Lallemandf03b53c2021-11-24 15:38:17 +0100324 if (!foundhost) {
325 /* Add Host Header from URL */
326 if (!htx_add_header(htx, ist("Host"), ist("h")))
William Lallemand79a34782021-09-20 16:19:15 +0200327 goto error;
William Lallemandf03b53c2021-11-24 15:38:17 +0100328 if (!http_update_host(htx, sl, url))
William Lallemand79a34782021-09-20 16:19:15 +0200329 goto error;
330 }
William Lallemand33b0d092021-08-13 16:05:53 +0200331
William Lallemandbad9c8c2022-01-14 14:10:33 +0100332 if (!foundaccept) {
333 if (!htx_add_header(htx, ist("Accept"), ist("*/*")))
334 goto error;
335 }
336
337 if (!foundua) {
338 if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT)))
339 goto error;
340 }
341
342
William Lallemandf03b53c2021-11-24 15:38:17 +0100343 if (!htx_add_endof(htx, HTX_BLK_EOH))
344 goto error;
345
William Lallemanda80b22e2022-12-22 14:49:43 +0100346 if (isttest(payload) && istlen(payload)) {
William Lallemanddec25c32021-10-25 19:48:37 +0200347 /* add the payload if it can feat in the buffer, no need to set
348 * the Content-Length, the data will be sent chunked */
349 if (!htx_add_data_atonce(htx, payload))
350 goto error;
351 }
352
William Lallemand0da616e2021-10-28 15:34:26 +0200353 /* If req.payload was set, does not set the end of stream which *MUST*
354 * be set in the callback */
355 if (!hc->ops.req_payload)
356 htx->flags |= HTX_FL_EOM;
William Lallemand33b0d092021-08-13 16:05:53 +0200357
358 htx_to_buf(htx, &hc->req.buf);
359
360 return 0;
361error:
362 err_code |= ERR_ALERT | ERR_ABORT;
363 return err_code;
364}
365
366/*
367 * transfer the response to the destination buffer and wakeup the HTTP client
368 * applet so it could fill again its buffer.
369 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500370 * Return the number of bytes transferred.
William Lallemand33b0d092021-08-13 16:05:53 +0200371 */
372int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst)
373{
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100374 size_t room = b_room(dst);
William Lallemand33b0d092021-08-13 16:05:53 +0200375 int ret;
376
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100377 ret = b_force_xfer(dst, &hc->res.buf, MIN(room, b_data(&hc->res.buf)));
William Lallemand33b0d092021-08-13 16:05:53 +0200378 /* call the client once we consumed all data */
Christopher Faulet600985d2022-01-12 11:14:08 +0100379 if (!b_data(&hc->res.buf)) {
380 b_free(&hc->res.buf);
381 if (hc->appctx)
382 appctx_wakeup(hc->appctx);
383 }
William Lallemand33b0d092021-08-13 16:05:53 +0200384 return ret;
385}
386
387/*
William Lallemand0da616e2021-10-28 15:34:26 +0200388 * Transfer raw HTTP payload from src, and insert it into HTX format in the
389 * httpclient.
390 *
391 * Must be used to transfer the request body.
392 * Then wakeup the httpclient so it can transfer it.
393 *
394 * <end> tries to add the ending data flag if it succeed to copy all data.
395 *
396 * Return the number of bytes copied from src.
397 */
398int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end)
399{
400 int ret = 0;
401 struct htx *htx;
402
Christopher Faulet600985d2022-01-12 11:14:08 +0100403 if (!b_alloc(&hc->req.buf))
404 goto error;
405
William Lallemand0da616e2021-10-28 15:34:26 +0200406 htx = htx_from_buf(&hc->req.buf);
407 if (!htx)
408 goto error;
409
410 if (hc->appctx)
411 appctx_wakeup(hc->appctx);
412
413 ret += htx_add_data(htx, src);
414
415
416 /* if we copied all the data and the end flag is set */
417 if ((istlen(src) == ret) && end) {
Christopher Faulet48005de2022-10-14 15:10:24 +0200418 /* no more data are expected. If the HTX buffer is empty, be
419 * sure to add something (EOT block in this case) to have
420 * something to send. It is important to be sure the EOM flags
421 * will be handled by the endpoint. Because the message is
422 * empty, this should not fail. Otherwise it is an error
423 */
424 if (htx_is_empty(htx)) {
425 if (!htx_add_endof(htx, HTX_BLK_EOT))
426 goto error;
427 }
William Lallemand0da616e2021-10-28 15:34:26 +0200428 htx->flags |= HTX_FL_EOM;
429 }
430 htx_to_buf(htx, &hc->req.buf);
431
432error:
433
434 return ret;
435}
436
William Lallemandb4a4ef62022-02-23 14:18:16 +0100437/* Set the 'timeout server' in ms for the next httpclient request */
438void httpclient_set_timeout(struct httpclient *hc, int timeout)
439{
440 hc->timeout_server = timeout;
441}
442
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100443/*
444 * Sets a destination for the httpclient from an HAProxy addr format
445 * This will prevent to determine the destination from the URL
446 * Return 0 in case of success or -1 otherwise.
447 */
448int httpclient_set_dst(struct httpclient *hc, const char *dst)
449{
450 struct sockaddr_storage *sk;
451 char *errmsg = NULL;
452
453 sockaddr_free(&hc->dst);
454 /* 'sk' is statically allocated (no need to be freed). */
455 sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL,
456 &errmsg, NULL, NULL,
457 PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
458 if (!sk) {
459 ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg);
460 free(errmsg);
461 return -1;
462 }
463
464 if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) {
465 ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__);
466 return -1;
467 }
468
469 return 0;
470}
William Lallemand0da616e2021-10-28 15:34:26 +0200471
472/*
Thierry Fournier74a9eb52022-10-10 12:46:38 +0200473 * Split <url> in <scheme>, <host>, <port>
William Lallemand7f1df8f2022-04-14 17:50:20 +0200474 */
Thierry Fournier74a9eb52022-10-10 12:46:38 +0200475static int httpclient_spliturl(struct ist url, enum http_scheme *scheme,
476 struct ist *host, int *port)
William Lallemand7f1df8f2022-04-14 17:50:20 +0200477{
478 enum http_scheme scheme_tmp = SCH_HTTP;
479 int port_tmp = 0;
480 struct ist scheme_ist, authority_ist, host_ist, port_ist;
481 char *p, *end;
482 struct http_uri_parser parser;
483
484 parser = http_uri_parser_init(url);
485 scheme_ist = http_parse_scheme(&parser);
Thierry Fournier74a9eb52022-10-10 12:46:38 +0200486 if (!isttest(scheme_ist)) {
487 return 0;
488 }
William Lallemand7f1df8f2022-04-14 17:50:20 +0200489
490 if (isteqi(scheme_ist, ist("http://"))){
491 scheme_tmp = SCH_HTTP;
492 port_tmp = 80;
493 } else if (isteqi(scheme_ist, ist("https://"))) {
494 scheme_tmp = SCH_HTTPS;
495 port_tmp = 443;
496 }
497
498 authority_ist = http_parse_authority(&parser, 1);
Thierry Fournier74a9eb52022-10-10 12:46:38 +0200499 if (!isttest(authority_ist)) {
500 return 0;
501 }
William Lallemand7f1df8f2022-04-14 17:50:20 +0200502 p = end = istend(authority_ist);
503
504 /* look for a port at the end of the authority */
505 while (p > istptr(authority_ist) && isdigit((unsigned char)*--p))
506 ;
507
508 if (*p == ':') {
509 host_ist = ist2(istptr(authority_ist), p - istptr(authority_ist));
510 port_ist = istnext(ist2(p, end - p));
511 ist2str(trash.area, port_ist);
512 port_tmp = atoi(trash.area);
513 } else {
514 host_ist = authority_ist;
515 }
516
517 if (scheme)
518 *scheme = scheme_tmp;
519 if (host)
520 *host = host_ist;
521 if (port)
522 *port = port_tmp;
523
Thierry Fournier74a9eb52022-10-10 12:46:38 +0200524 return 1;
William Lallemand7f1df8f2022-04-14 17:50:20 +0200525}
526
527/*
William Lallemand33b0d092021-08-13 16:05:53 +0200528 * Start the HTTP client
529 * Create the appctx, session, stream and wakeup the applet
530 *
William Lallemand33b0d092021-08-13 16:05:53 +0200531 * Return the <appctx> or NULL if it failed
532 */
533struct appctx *httpclient_start(struct httpclient *hc)
534{
535 struct applet *applet = &httpclient_applet;
536 struct appctx *appctx;
William Lallemand33b0d092021-08-13 16:05:53 +0200537
William Lallemand5085bc32022-02-17 12:52:09 +0100538 /* if the client was started and not ended, an applet is already
539 * running, we shouldn't try anything */
540 if (httpclient_started(hc) && !httpclient_ended(hc))
541 return NULL;
542
William Lallemand33b0d092021-08-13 16:05:53 +0200543 /* The HTTP client will be created in the same thread as the caller,
544 * avoiding threading issues */
Christopher Faulet6095d572022-05-16 17:09:48 +0200545 appctx = appctx_new_here(applet, NULL);
William Lallemand33b0d092021-08-13 16:05:53 +0200546 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100547 goto out;
Christopher Fauletb1e08362022-05-12 15:33:14 +0200548 appctx->svcctx = hc;
549 hc->flags = 0;
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100550
Christopher Fauletb1e08362022-05-12 15:33:14 +0200551 if (appctx_init(appctx) == -1) {
552 ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__);
Christopher Faulet92202da2022-05-11 12:22:10 +0200553 goto out_free_appctx;
William Lallemand85332732022-05-04 10:59:51 +0200554 }
555
William Lallemand33b0d092021-08-13 16:05:53 +0200556 return appctx;
557
William Lallemand33b0d092021-08-13 16:05:53 +0200558out_free_appctx:
Christopher Fauletb1e08362022-05-12 15:33:14 +0200559 appctx_free_on_early_error(appctx);
William Lallemand33b0d092021-08-13 16:05:53 +0200560out:
561
562 return NULL;
563}
564
William Lallemandecb83e12021-09-28 11:00:43 +0200565/*
566 * This function tries to destroy the httpclient if it wasn't running.
567 * If it was running, stop the client and ask it to autodestroy itself.
568 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500569 * Once this function is used, all pointer sto the client must be removed
William Lallemandecb83e12021-09-28 11:00:43 +0200570 *
571 */
572void httpclient_stop_and_destroy(struct httpclient *hc)
573{
574
William Lallemandb8b13702021-09-28 12:15:37 +0200575 /* The httpclient was already stopped or never started, we can safely destroy it */
576 if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) {
William Lallemandecb83e12021-09-28 11:00:43 +0200577 httpclient_destroy(hc);
578 } else {
Willy Tarreaub4829202022-09-01 20:40:26 +0200579 /* if the client wasn't stopped, ask for a stop and destroy */
William Lallemandecb83e12021-09-28 11:00:43 +0200580 hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP);
Willy Tarreaub4829202022-09-01 20:40:26 +0200581 /* the calling applet doesn't exist anymore */
582 hc->caller = NULL;
William Lallemandecb83e12021-09-28 11:00:43 +0200583 if (hc->appctx)
584 appctx_wakeup(hc->appctx);
585 }
586}
587
William Lallemand33b0d092021-08-13 16:05:53 +0200588/* Free the httpclient */
589void httpclient_destroy(struct httpclient *hc)
590{
William Lallemand03f5a1c2021-09-27 15:17:47 +0200591 struct http_hdr *hdrs;
592
593
William Lallemand33b0d092021-08-13 16:05:53 +0200594 if (!hc)
595 return;
William Lallemandecb83e12021-09-28 11:00:43 +0200596
William Lallemand2a879002021-10-05 15:50:45 +0200597 /* we should never destroy a client which was started but not stopped */
598 BUG_ON(httpclient_started(hc) && !httpclient_ended(hc));
William Lallemandecb83e12021-09-28 11:00:43 +0200599
William Lallemand03f5a1c2021-09-27 15:17:47 +0200600 /* request */
601 istfree(&hc->req.url);
William Lallemand33b0d092021-08-13 16:05:53 +0200602 b_free(&hc->req.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200603 /* response */
604 istfree(&hc->res.vsn);
605 istfree(&hc->res.reason);
606 hdrs = hc->res.hdrs;
607 while (hdrs && isttest(hdrs->n)) {
608 istfree(&hdrs->n);
609 istfree(&hdrs->v);
610 hdrs++;
611 }
612 ha_free(&hc->res.hdrs);
William Lallemand33b0d092021-08-13 16:05:53 +0200613 b_free(&hc->res.buf);
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100614 sockaddr_free(&hc->dst);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200615
William Lallemand33b0d092021-08-13 16:05:53 +0200616 free(hc);
617
618 return;
619}
620
621/* Allocate an httpclient and its buffers
William Lallemand992ad622022-09-12 17:39:04 +0200622 * Use the default httpclient_proxy
623 *
William Lallemand33b0d092021-08-13 16:05:53 +0200624 * Return NULL on failure */
625struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url)
626{
627 struct httpclient *hc;
William Lallemand33b0d092021-08-13 16:05:53 +0200628
629 hc = calloc(1, sizeof(*hc));
630 if (!hc)
631 goto err;
632
Christopher Faulet600985d2022-01-12 11:14:08 +0100633 hc->req.buf = BUF_NULL;
634 hc->res.buf = BUF_NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200635 hc->caller = caller;
William Lallemand67b77842021-11-10 16:57:25 +0100636 hc->req.url = istdup(url);
William Lallemand33b0d092021-08-13 16:05:53 +0200637 hc->req.meth = meth;
William Lallemand992ad622022-09-12 17:39:04 +0200638 httpclient_set_proxy(hc, httpclient_proxy);
William Lallemand33b0d092021-08-13 16:05:53 +0200639
640 return hc;
641
642err:
643 httpclient_destroy(hc);
644 return NULL;
645}
646
William Lallemand992ad622022-09-12 17:39:04 +0200647/* Allocate an httpclient and its buffers,
648 * Use the proxy <px>
649 *
650 * Return and httpclient or NULL.
651 */
652struct httpclient *httpclient_new_from_proxy(struct proxy *px, void *caller, enum http_meth_t meth, struct ist url)
653{
654 struct httpclient *hc;
655
656 hc = httpclient_new(caller, meth, url);
657 if (!hc)
658 return NULL;
659
660 httpclient_set_proxy(hc, px);
661
662 return hc;
663}
664
665/*
666 * Configure an httpclient with a specific proxy <px>
667 *
668 * The proxy <px> must contains 2 srv, one configured for clear connections, the other for SSL.
669 *
670 */
671int httpclient_set_proxy(struct httpclient *hc, struct proxy *px)
672{
673 struct server *srv;
674
675 hc->px = px;
676
677 for (srv = px->srv; srv != NULL; srv = srv->next) {
678 if (srv->xprt == xprt_get(XPRT_RAW)) {
679 hc->srv_raw = srv;
680#ifdef USE_OPENSSL
681 } else if (srv->xprt == xprt_get(XPRT_SSL)) {
682 hc->srv_ssl = srv;
683#endif
684 }
685 }
686
687 return 0;
688}
689
William Lallemand33b0d092021-08-13 16:05:53 +0200690static void httpclient_applet_io_handler(struct appctx *appctx)
691{
Willy Tarreau1eea6652022-05-05 20:12:01 +0200692 struct httpclient *hc = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200693 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaub89f8722022-05-27 10:37:32 +0200694 struct stream *s = __sc_strm(sc);
William Lallemand33b0d092021-08-13 16:05:53 +0200695 struct channel *req = &s->req;
696 struct channel *res = &s->res;
697 struct htx_blk *blk = NULL;
698 struct htx *htx;
William Lallemandb7020302021-08-20 11:24:13 +0200699 struct htx_sl *sl = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200700 uint32_t hdr_num;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100701 uint32_t sz;
William Lallemand933fe392021-11-04 09:45:58 +0100702 int ret;
William Lallemand33b0d092021-08-13 16:05:53 +0200703
Christopher Faulet1901c1b2023-04-11 07:38:34 +0200704 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
705 if (co_data(res)) {
706 htx = htx_from_buf(&res->buf);
707 co_htx_skip(res, htx, co_data(res));
708 htx_to_buf(htx, &res->buf);
709 }
Christopher Fauletbe08df82023-03-31 11:30:32 +0200710 goto out;
Christopher Faulet1901c1b2023-04-11 07:38:34 +0200711 }
William Lallemanda93eac42022-10-20 18:36:03 +0200712 /* The IO handler could be called after the release, so we need to
713 * check if hc is still there to run the IO handler */
714 if (!hc)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200715 goto out;
William Lallemanda93eac42022-10-20 18:36:03 +0200716
William Lallemand33b0d092021-08-13 16:05:53 +0200717 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200718
719 /* required to stop */
720 if (hc->flags & HTTPCLIENT_FA_STOP)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200721 goto error;
William Lallemandecb83e12021-09-28 11:00:43 +0200722
William Lallemand33b0d092021-08-13 16:05:53 +0200723 switch(appctx->st0) {
724
725 case HTTPCLIENT_S_REQ:
William Lallemanddb8a1f32021-11-08 16:55:14 +0100726 /* we know that the buffer is empty here, since
727 * it's the first call, we can freely copy the
728 * request from the httpclient buffer */
William Lallemand933fe392021-11-04 09:45:58 +0100729 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
Christopher Faulet7b3d38a2023-05-05 11:28:45 +0200730 if (!ret) {
731 sc_need_room(sc, 0);
732 goto out;
733 }
William Lallemand933fe392021-11-04 09:45:58 +0100734
Christopher Faulet600985d2022-01-12 11:14:08 +0100735 if (!b_data(&hc->req.buf))
736 b_free(&hc->req.buf);
737
William Lallemanddb8a1f32021-11-08 16:55:14 +0100738 htx = htx_from_buf(&req->buf);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +0200739 if (!htx) {
740 sc_need_room(sc, 0);
741 goto out;
742 }
William Lallemand933fe392021-11-04 09:45:58 +0100743
William Lallemanddb8a1f32021-11-08 16:55:14 +0100744 channel_add_input(req, htx->data);
745
William Lallemand933fe392021-11-04 09:45:58 +0100746 if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */
747 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
748 else
749 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
750
Christopher Fauletbe08df82023-03-31 11:30:32 +0200751 goto out; /* we need to leave the IO handler once we wrote the request */
752 break;
753
William Lallemand0da616e2021-10-28 15:34:26 +0200754 case HTTPCLIENT_S_REQ_BODY:
755 /* call the payload callback */
756 {
757 if (hc->ops.req_payload) {
William Lallemandccc7ee42022-03-18 17:57:15 +0100758 struct htx *hc_htx;
William Lallemand0da616e2021-10-28 15:34:26 +0200759
William Lallemand0da616e2021-10-28 15:34:26 +0200760 /* call the request callback */
761 hc->ops.req_payload(hc);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100762
William Lallemandccc7ee42022-03-18 17:57:15 +0100763 hc_htx = htx_from_buf(&hc->req.buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100764 htx = htx_from_buf(&req->buf);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100765
William Lallemandccc7ee42022-03-18 17:57:15 +0100766 if (htx_is_empty(hc_htx))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200767 goto out;
Christopher Faulet600985d2022-01-12 11:14:08 +0100768
William Lallemandccc7ee42022-03-18 17:57:15 +0100769 if (htx_is_empty(htx)) {
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200770 size_t data = hc_htx->data;
771
William Lallemandccc7ee42022-03-18 17:57:15 +0100772 /* Here htx_to_buf() will set buffer data to 0 because
773 * the HTX is empty, and allow us to do an xfer.
774 */
775 htx_to_buf(hc_htx, &hc->req.buf);
776 htx_to_buf(htx, &req->buf);
William Lallemandccc7ee42022-03-18 17:57:15 +0100777 b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
Christopher Fauletdca3b5b2022-04-07 10:47:07 +0200778 channel_add_input(req, data);
William Lallemandccc7ee42022-03-18 17:57:15 +0100779 } else {
780 struct htx_ret ret;
Christopher Faulet600985d2022-01-12 11:14:08 +0100781
Christopher Faulet6b4f1f62022-04-29 13:56:12 +0200782 ret = htx_xfer_blks(htx, hc_htx, htx_used_space(hc_htx), HTX_BLK_UNUSED);
William Lallemandccc7ee42022-03-18 17:57:15 +0100783 channel_add_input(req, ret.ret);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100784
William Lallemandccc7ee42022-03-18 17:57:15 +0100785 /* we must copy the EOM if we empty the buffer */
786 if (htx_is_empty(hc_htx)) {
787 htx->flags |= (hc_htx->flags & HTX_FL_EOM);
788 }
789 htx_to_buf(htx, &req->buf);
790 htx_to_buf(hc_htx, &hc->req.buf);
791 }
792
793
794 if (!b_data(&hc->req.buf))
795 b_free(&hc->req.buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200796 }
797
William Lallemanddb8a1f32021-11-08 16:55:14 +0100798 htx = htx_from_buf(&req->buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200799 if (!htx)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200800 goto out;
William Lallemand0da616e2021-10-28 15:34:26 +0200801
802 /* if the request contains the HTX_FL_EOM, we finished the request part. */
Christopher Fauletbe08df82023-03-31 11:30:32 +0200803 if (htx->flags & HTX_FL_EOM)
William Lallemand0da616e2021-10-28 15:34:26 +0200804 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
805
William Lallemand1eca8942022-03-17 14:57:23 +0100806 goto process_data; /* we need to leave the IO handler once we wrote the request */
William Lallemand0da616e2021-10-28 15:34:26 +0200807 }
Christopher Fauletbe08df82023-03-31 11:30:32 +0200808 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200809
810 case HTTPCLIENT_S_RES_STLINE:
Christopher Fauletbe08df82023-03-31 11:30:32 +0200811 /* Request is finished, report EOI */
812 se_fl_set(appctx->sedesc, SE_FL_EOI);
813
William Lallemand33b0d092021-08-13 16:05:53 +0200814 /* copy the start line in the hc structure,then remove the htx block */
William Lallemanda625b032022-03-17 14:45:46 +0100815 if (!co_data(res))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200816 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200817 htx = htxbuf(&res->buf);
818 if (!htx)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200819 goto out;
William Lallemand97f69c62022-03-10 17:23:40 +0100820 blk = htx_get_head_blk(htx);
William Lallemand33b0d092021-08-13 16:05:53 +0200821 if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL))
822 sl = htx_get_blk_ptr(htx, blk);
823 if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP)))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200824 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200825
826 /* copy the status line in the httpclient */
827 hc->res.status = sl->info.res.status;
828 hc->res.vsn = istdup(htx_sl_res_vsn(sl));
829 hc->res.reason = istdup(htx_sl_res_reason(sl));
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100830 sz = htx_get_blksz(blk);
Christopher Faulet0055d562022-04-29 14:09:03 +0200831 c_rew(res, sz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100832 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200833 /* caller callback */
834 if (hc->ops.res_stline)
835 hc->ops.res_stline(hc);
836
837 /* if there is no HTX data anymore and the EOM flag is
838 * set, leave (no body) */
839 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM)
840 appctx->st0 = HTTPCLIENT_S_RES_END;
841 else
842 appctx->st0 = HTTPCLIENT_S_RES_HDR;
843 break;
844
845 case HTTPCLIENT_S_RES_HDR:
846 /* first copy the headers in a local hdrs
847 * structure, once we the total numbers of the
848 * header we allocate the right size and copy
849 * them. The htx block of the headers are
850 * removed each time one is read */
851 {
852 struct http_hdr hdrs[global.tune.max_http_hdr];
853
William Lallemanda625b032022-03-17 14:45:46 +0100854 if (!co_data(res))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200855 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200856 htx = htxbuf(&res->buf);
857 if (!htx)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200858 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200859
860 hdr_num = 0;
Christopher Faulet534645d2022-04-29 13:44:46 +0200861 blk = htx_get_head_blk(htx);
862 while (blk) {
William Lallemand33b0d092021-08-13 16:05:53 +0200863 enum htx_blk_type type = htx_get_blk_type(blk);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100864 uint32_t sz = htx_get_blksz(blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200865
Christopher Faulet534645d2022-04-29 13:44:46 +0200866 c_rew(res, sz);
William Lallemandc020b252022-03-09 18:56:02 +0100867
Christopher Faulet18de6f22022-06-01 16:37:49 +0200868 if (type == HTX_BLK_HDR) {
William Lallemandc020b252022-03-09 18:56:02 +0100869 hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk));
870 hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk));
William Lallemandc020b252022-03-09 18:56:02 +0100871 hdr_num++;
872 }
Christopher Faulet534645d2022-04-29 13:44:46 +0200873 else if (type == HTX_BLK_EOH) {
874 /* create a NULL end of array and leave the loop */
William Lallemand33b0d092021-08-13 16:05:53 +0200875 hdrs[hdr_num].n = IST_NULL;
876 hdrs[hdr_num].v = IST_NULL;
Christopher Faulet18de6f22022-06-01 16:37:49 +0200877 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200878 break;
879 }
Christopher Faulet18de6f22022-06-01 16:37:49 +0200880 blk = htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200881 }
882
William Lallemand0d6f7792021-08-20 11:59:49 +0200883 if (hdr_num) {
884 /* alloc and copy the headers in the httpclient struct */
885 hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs));
886 if (!hc->res.hdrs)
Christopher Fauletbe08df82023-03-31 11:30:32 +0200887 goto error;
William Lallemand0d6f7792021-08-20 11:59:49 +0200888 memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
William Lallemand33b0d092021-08-13 16:05:53 +0200889
William Lallemand0d6f7792021-08-20 11:59:49 +0200890 /* caller callback */
891 if (hc->ops.res_headers)
892 hc->ops.res_headers(hc);
893 }
William Lallemand33b0d092021-08-13 16:05:53 +0200894
895 /* if there is no HTX data anymore and the EOM flag is
896 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200897 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200898 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200899 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200900 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200901 }
William Lallemand33b0d092021-08-13 16:05:53 +0200902 }
Christopher Fauletbe08df82023-03-31 11:30:32 +0200903 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200904
905 case HTTPCLIENT_S_RES_BODY:
906 /*
907 * The IO handler removes the htx blocks in the response buffer and
908 * push them in the hc->res.buf buffer in a raw format.
909 */
William Lallemanda625b032022-03-17 14:45:46 +0100910 if (!co_data(res))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200911 goto out;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100912
William Lallemand33b0d092021-08-13 16:05:53 +0200913 htx = htxbuf(&res->buf);
914 if (!htx || htx_is_empty(htx))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200915 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200916
Christopher Faulet600985d2022-01-12 11:14:08 +0100917 if (!b_alloc(&hc->res.buf))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200918 goto out;
Christopher Faulet600985d2022-01-12 11:14:08 +0100919
William Lallemand33b0d092021-08-13 16:05:53 +0200920 if (b_full(&hc->res.buf))
Christopher Faulet600985d2022-01-12 11:14:08 +0100921 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200922
923 /* decapsule the htx data to raw data */
Christopher Faulet534645d2022-04-29 13:44:46 +0200924 blk = htx_get_head_blk(htx);
925 while (blk) {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100926 enum htx_blk_type type = htx_get_blk_type(blk);
927 size_t count = co_data(res);
928 uint32_t blksz = htx_get_blksz(blk);
929 uint32_t room = b_room(&hc->res.buf);
930 uint32_t vlen;
William Lallemand33b0d092021-08-13 16:05:53 +0200931
William Lallemandc8f1eb92022-03-09 11:58:51 +0100932 /* we should try to copy the maximum output data in a block, which fit
933 * the destination buffer */
934 vlen = MIN(count, blksz);
935 vlen = MIN(vlen, room);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100936
William Lallemandc8f1eb92022-03-09 11:58:51 +0100937 if (vlen == 0)
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100938 goto process_data;
939
William Lallemand33b0d092021-08-13 16:05:53 +0200940 if (type == HTX_BLK_DATA) {
941 struct ist v = htx_get_blk_value(htx, blk);
942
William Lallemandc8f1eb92022-03-09 11:58:51 +0100943 __b_putblk(&hc->res.buf, v.ptr, vlen);
944 c_rew(res, vlen);
William Lallemand33b0d092021-08-13 16:05:53 +0200945
William Lallemandc8f1eb92022-03-09 11:58:51 +0100946 if (vlen == blksz)
Christopher Faulet534645d2022-04-29 13:44:46 +0200947 blk = htx_remove_blk(htx, blk);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100948 else
949 htx_cut_data_blk(htx, blk, vlen);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100950
William Lallemand33b0d092021-08-13 16:05:53 +0200951 /* the data must be processed by the caller in the receive phase */
952 if (hc->ops.res_payload)
953 hc->ops.res_payload(hc);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100954
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500955 /* cannot copy everything, need to process */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100956 if (vlen != blksz)
957 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200958 } else {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100959 if (vlen != blksz)
960 goto process_data;
961
William Lallemand33b0d092021-08-13 16:05:53 +0200962 /* remove any block which is not a data block */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100963 c_rew(res, blksz);
Christopher Faulet534645d2022-04-29 13:44:46 +0200964 blk = htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200965 }
966 }
William Lallemandc8f1eb92022-03-09 11:58:51 +0100967
William Lallemand33b0d092021-08-13 16:05:53 +0200968 /* if not finished, should be called again */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100969 if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)))
Christopher Fauletbe08df82023-03-31 11:30:32 +0200970 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200971
William Lallemandc8f1eb92022-03-09 11:58:51 +0100972
William Lallemand33b0d092021-08-13 16:05:53 +0200973 /* end of message, we should quit */
974 appctx->st0 = HTTPCLIENT_S_RES_END;
Christopher Fauletbe08df82023-03-31 11:30:32 +0200975 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200976
977 case HTTPCLIENT_S_RES_END:
Christopher Fauletbe08df82023-03-31 11:30:32 +0200978 se_fl_set(appctx->sedesc, SE_FL_EOS);
979 goto out;
980 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200981 }
982 }
983
Christopher Fauletbe08df82023-03-31 11:30:32 +0200984out:
985 return;
William Lallemand33b0d092021-08-13 16:05:53 +0200986
Christopher Fauletbe08df82023-03-31 11:30:32 +0200987process_data:
Willy Tarreaub89f8722022-05-27 10:37:32 +0200988 sc_will_read(sc);
Christopher Fauletbe08df82023-03-31 11:30:32 +0200989 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200990
Christopher Fauletbe08df82023-03-31 11:30:32 +0200991error:
992 se_fl_set(appctx->sedesc, SE_FL_ERROR);
993 goto out;
William Lallemand33b0d092021-08-13 16:05:53 +0200994}
995
Christopher Fauletb1e08362022-05-12 15:33:14 +0200996static int httpclient_applet_init(struct appctx *appctx)
997{
998 struct httpclient *hc = appctx->svcctx;
999 struct stream *s;
1000 struct sockaddr_storage *addr = NULL;
1001 struct sockaddr_storage ss_url = {};
1002 struct sockaddr_storage *ss_dst;
1003 enum obj_type *target = NULL;
1004 struct ist host = IST_NULL;
1005 enum http_scheme scheme;
1006 int port;
1007 int doresolve = 0;
1008
1009
1010 /* parse the URL and */
Thierry Fournier74a9eb52022-10-10 12:46:38 +02001011 if (!httpclient_spliturl(hc->req.url, &scheme, &host, &port))
1012 goto out_error;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001013
1014 if (hc->dst) {
1015 /* if httpclient_set_dst() was used, sets the alternative address */
1016 ss_dst = hc->dst;
1017 } else {
1018 /* set the dst using the host, or 0.0.0.0 to resolve */
1019 ist2str(trash.area, host);
1020 ss_dst = str2ip2(trash.area, &ss_url, 0);
1021 if (!ss_dst) { /* couldn't get an IP from that, try to resolve */
1022 doresolve = 1;
1023 ss_dst = str2ip2("0.0.0.0", &ss_url, 0);
1024 }
1025 sock_inet_set_port(ss_dst, port);
1026 }
1027
Christopher Fauletb1e08362022-05-12 15:33:14 +02001028 if (!sockaddr_alloc(&addr, ss_dst, sizeof(*ss_dst)))
1029 goto out_error;
1030
1031 /* choose the SSL server or not */
1032 switch (scheme) {
1033 case SCH_HTTP:
William Lallemand992ad622022-09-12 17:39:04 +02001034 target = &hc->srv_raw->obj_type;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001035 break;
1036 case SCH_HTTPS:
1037#ifdef USE_OPENSSL
William Lallemand992ad622022-09-12 17:39:04 +02001038 if (hc->srv_ssl) {
1039 target = &hc->srv_ssl->obj_type;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001040 } else {
1041 ha_alert("httpclient: SSL was disabled (wrong verify/ca-file)!\n");
1042 goto out_free_addr;
1043 }
1044#else
1045 ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__);
1046 goto out_free_addr;
1047#endif
1048 break;
1049 }
1050
William Lallemand992ad622022-09-12 17:39:04 +02001051 if (appctx_finalize_startup(appctx, hc->px, &hc->req.buf) == -1) {
Christopher Fauletb1e08362022-05-12 15:33:14 +02001052 ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__);
1053 goto out_free_addr;
1054 }
1055
1056 s = appctx_strm(appctx);
1057 s->target = target;
1058 /* set the "timeout server" */
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001059 s->scb->ioto = hc->timeout_server;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001060
1061 if (doresolve) {
1062 /* in order to do the set-dst we need to put the address on the front */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001063 s->scf->dst = addr;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001064 } else {
1065 /* in cases we don't use the resolve we already have the address
1066 * and must put it on the backend side, some of the cases are
1067 * not meant to be used on the frontend (sockpair, unix socket etc.) */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001068 s->scb->dst = addr;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001069 }
1070
Christopher Faulet9a790f62023-03-16 14:40:03 +01001071 s->scb->flags |= (SC_FL_RCV_ONCE|SC_FL_NOLINGER);
Christopher Fauletb1e08362022-05-12 15:33:14 +02001072 s->flags |= SF_ASSIGNED;
Christopher Fauletb1e08362022-05-12 15:33:14 +02001073
1074 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001075 applet_need_more_data(appctx);
Christopher Fauletb1e08362022-05-12 15:33:14 +02001076 appctx_wakeup(appctx);
1077
1078 hc->appctx = appctx;
1079 hc->flags |= HTTPCLIENT_FS_STARTED;
1080
1081 /* The request was transferred when the stream was created. So switch
1082 * directly to REQ_BODY or RES_STLINE state
1083 */
1084 appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE);
1085 return 0;
1086
1087 out_free_addr:
1088 sockaddr_free(&addr);
1089 out_error:
1090 return -1;
1091}
1092
William Lallemand33b0d092021-08-13 16:05:53 +02001093static void httpclient_applet_release(struct appctx *appctx)
1094{
Willy Tarreau1eea6652022-05-05 20:12:01 +02001095 struct httpclient *hc = appctx->svcctx;
William Lallemand33b0d092021-08-13 16:05:53 +02001096
William Lallemand1123dde2021-09-21 10:58:10 +02001097 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +02001098 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +02001099 /* the applet is leaving, remove the ptr so we don't try to call it
1100 * again from the caller */
1101 hc->appctx = NULL;
1102
William Lallemandeb0d4c42022-04-06 14:12:37 +02001103 if (hc->ops.res_end)
1104 hc->ops.res_end(hc);
William Lallemandecb83e12021-09-28 11:00:43 +02001105
1106 /* destroy the httpclient when set to autotokill */
1107 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
1108 httpclient_destroy(hc);
1109 }
1110
William Lallemanda93eac42022-10-20 18:36:03 +02001111 /* be sure not to use this ptr anymore if the IO handler is called a
1112 * last time */
1113 appctx->svcctx = NULL;
1114
William Lallemand33b0d092021-08-13 16:05:53 +02001115 return;
1116}
1117
1118/* HTTP client applet */
1119static struct applet httpclient_applet = {
1120 .obj_type = OBJ_TYPE_APPLET,
1121 .name = "<HTTPCLIENT>",
1122 .fct = httpclient_applet_io_handler,
Christopher Fauletb1e08362022-05-12 15:33:14 +02001123 .init = httpclient_applet_init,
William Lallemand33b0d092021-08-13 16:05:53 +02001124 .release = httpclient_applet_release,
1125};
1126
William Lallemand5392ff62022-04-28 16:55:02 +02001127
William Lallemand54aec5f2022-09-12 16:46:35 +02001128static int httpclient_resolve_init(struct proxy *px)
William Lallemand5392ff62022-04-28 16:55:02 +02001129{
1130 struct act_rule *rule;
1131 int i;
William Lallemand8a734cb2022-05-04 16:10:47 +02001132 char *do_resolve = NULL;
1133 char *http_rules[][11] = {
William Lallemand5392ff62022-04-28 16:55:02 +02001134 { "set-var(txn.hc_ip)", "dst", "" },
William Lallemandd78dfe72022-08-26 16:45:13 +02001135 { do_resolve, "hdr(Host),host_only", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" },
William Lallemand5392ff62022-04-28 16:55:02 +02001136 { "return", "status", "503", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" },
1137 { "capture", "var(txn.hc_ip)", "len", "40", "" },
1138 { "set-dst", "var(txn.hc_ip)", "" },
1139 { "" }
1140 };
1141
William Lallemande279f592023-05-11 21:08:38 +02001142
1143 if (resolvers_disabled)
1144 return 0;
1145
William Lallemand8a734cb2022-05-04 16:10:47 +02001146 if (!resolvers_id)
1147 resolvers_id = strdup("default");
1148
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001149 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 +02001150 http_rules[1][0] = do_resolve;
1151
William Lallemand7867f632022-05-05 19:02:59 +02001152 /* Try to create the default resolvers section */
1153 resolvers_create_default();
1154
William Lallemand8a734cb2022-05-04 16:10:47 +02001155 /* if the resolver does not exist and no hard_error was set, simply ignore resolving */
1156 if (!find_resolvers_by_id(resolvers_id) && !hard_error_resolvers) {
1157 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001158 return 0;
William Lallemand8a734cb2022-05-04 16:10:47 +02001159 }
William Lallemand5392ff62022-04-28 16:55:02 +02001160
1161
1162 for (i = 0; *http_rules[i][0] != '\0'; i++) {
William Lallemand54aec5f2022-09-12 16:46:35 +02001163 rule = parse_http_req_cond((const char **)http_rules[i], "httpclient", 0, px);
William Lallemand5392ff62022-04-28 16:55:02 +02001164 if (!rule) {
William Lallemand8a734cb2022-05-04 16:10:47 +02001165 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001166 ha_alert("Couldn't setup the httpclient resolver.\n");
1167 return 1;
1168 }
William Lallemand54aec5f2022-09-12 16:46:35 +02001169 LIST_APPEND(&px->http_req_rules, &rule->list);
William Lallemand5392ff62022-04-28 16:55:02 +02001170 }
1171
William Lallemand8a734cb2022-05-04 16:10:47 +02001172 free(do_resolve);
William Lallemand5392ff62022-04-28 16:55:02 +02001173 return 0;
1174}
1175
William Lallemand83614a92021-08-13 14:47:57 +02001176/*
William Lallemand54aec5f2022-09-12 16:46:35 +02001177 * Creates an internal proxy which will be used for httpclient.
1178 * This will allocate 2 servers (raw and ssl) and 1 proxy.
1179 *
1180 * This function must be called from a precheck callback.
1181 *
1182 * Return a proxy or NULL.
William Lallemand83614a92021-08-13 14:47:57 +02001183 */
William Lallemand54aec5f2022-09-12 16:46:35 +02001184struct proxy *httpclient_create_proxy(const char *id)
William Lallemand83614a92021-08-13 14:47:57 +02001185{
William Lallemand85af49c2022-05-04 14:33:57 +02001186 int err_code = ERR_NONE;
William Lallemand83614a92021-08-13 14:47:57 +02001187 char *errmsg = NULL;
William Lallemand54aec5f2022-09-12 16:46:35 +02001188 struct proxy *px = NULL;
1189 struct server *srv_raw = NULL;
1190#ifdef USE_OPENSSL
1191 struct server *srv_ssl = NULL;
1192#endif
William Lallemand83614a92021-08-13 14:47:57 +02001193
William Lallemandc6ceba32022-04-22 16:49:53 +02001194 if (global.mode & MODE_MWORKER_WAIT)
William Lallemand85af49c2022-05-04 14:33:57 +02001195 return ERR_NONE;
William Lallemandc6ceba32022-04-22 16:49:53 +02001196
William Lallemand54aec5f2022-09-12 16:46:35 +02001197 px = alloc_new_proxy(id, PR_CAP_LISTEN|PR_CAP_INT|PR_CAP_HTTPCLIENT, &errmsg);
1198 if (!px) {
William Lallemand85af49c2022-05-04 14:33:57 +02001199 memprintf(&errmsg, "couldn't allocate proxy.");
William Lallemand83614a92021-08-13 14:47:57 +02001200 err_code |= ERR_ALERT | ERR_FATAL;
1201 goto err;
1202 }
1203
William Lallemand54aec5f2022-09-12 16:46:35 +02001204 proxy_preset_defaults(px);
Willy Tarreau0e72e402021-08-20 10:23:12 +02001205
William Lallemand54aec5f2022-09-12 16:46:35 +02001206 px->options |= PR_O_WREQ_BODY;
1207 px->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED | PR_RE_TIMEOUT;
1208 px->options2 |= PR_O2_INDEPSTR;
1209 px->mode = PR_MODE_HTTP;
1210 px->maxconn = 0;
1211 px->accept = NULL;
1212 px->conn_retries = CONN_RETRIES;
1213 px->timeout.client = TICK_ETERNITY;
William Lallemand83614a92021-08-13 14:47:57 +02001214 /* The HTTP Client use the "option httplog" with the global log server */
William Lallemandd793ca22022-12-22 15:13:59 +01001215 px->conf.logformat_string = httpclient_log_format;
William Lallemand54aec5f2022-09-12 16:46:35 +02001216 px->http_needed = 1;
William Lallemand83614a92021-08-13 14:47:57 +02001217
1218 /* clear HTTP server */
William Lallemand54aec5f2022-09-12 16:46:35 +02001219 srv_raw = new_server(px);
1220 if (!srv_raw) {
William Lallemand83614a92021-08-13 14:47:57 +02001221 memprintf(&errmsg, "out of memory.");
William Lallemand85af49c2022-05-04 14:33:57 +02001222 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001223 goto err;
1224 }
1225
William Lallemand54aec5f2022-09-12 16:46:35 +02001226 srv_settings_cpy(srv_raw, &px->defsrv, 0);
1227 srv_raw->iweight = 0;
1228 srv_raw->uweight = 0;
1229 srv_raw->xprt = xprt_get(XPRT_RAW);
1230 srv_raw->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */
1231 srv_raw->id = strdup("<HTTPCLIENT>");
1232 if (!srv_raw->id) {
William Lallemand85af49c2022-05-04 14:33:57 +02001233 memprintf(&errmsg, "out of memory.");
1234 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001235 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001236 }
William Lallemand83614a92021-08-13 14:47:57 +02001237
William Lallemand957ab132021-08-24 18:33:28 +02001238#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +02001239 /* SSL HTTP server */
William Lallemand54aec5f2022-09-12 16:46:35 +02001240 srv_ssl = new_server(px);
1241 if (!srv_ssl) {
William Lallemand83614a92021-08-13 14:47:57 +02001242 memprintf(&errmsg, "out of memory.");
1243 err_code |= ERR_ALERT | ERR_FATAL;
1244 goto err;
1245 }
William Lallemand54aec5f2022-09-12 16:46:35 +02001246 srv_settings_cpy(srv_ssl, &px->defsrv, 0);
1247 srv_ssl->iweight = 0;
1248 srv_ssl->uweight = 0;
1249 srv_ssl->xprt = xprt_get(XPRT_SSL);
1250 srv_ssl->use_ssl = 1;
1251 srv_ssl->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */
1252 srv_ssl->id = strdup("<HTTPSCLIENT>");
1253 if (!srv_ssl->id) {
William Lallemand85af49c2022-05-04 14:33:57 +02001254 memprintf(&errmsg, "out of memory.");
1255 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand83614a92021-08-13 14:47:57 +02001256 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001257 }
William Lallemand83614a92021-08-13 14:47:57 +02001258
Willy Tarreaudf3231c2022-09-02 09:02:21 +02001259#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
William Lallemand54aec5f2022-09-12 16:46:35 +02001260 if (ssl_sock_parse_alpn("h2,http/1.1", &srv_ssl->ssl_ctx.alpn_str, &srv_ssl->ssl_ctx.alpn_len, &errmsg) != 0) {
Willy Tarreaudf3231c2022-09-02 09:02:21 +02001261 err_code |= ERR_ALERT | ERR_FATAL;
1262 goto err;
1263 }
1264#endif
William Lallemand54aec5f2022-09-12 16:46:35 +02001265 srv_ssl->ssl_ctx.verify = httpclient_ssl_verify;
William Lallemand4006b0f2022-04-25 18:23:35 +02001266 /* if the verify is required, try to load the system CA */
William Lallemandeaa703e2022-04-22 17:52:33 +02001267 if (httpclient_ssl_verify == SSL_SOCK_VERIFY_REQUIRED) {
William Lallemand683fbb82022-05-04 15:43:01 +02001268
Miroslav Zagoraca2ec1922022-11-02 16:11:50 +01001269 srv_ssl->ssl_ctx.ca_file = strdup(httpclient_ssl_ca_file ? httpclient_ssl_ca_file : "@system-ca");
William Lallemand0a2d6322022-11-24 19:14:19 +01001270 if (!__ssl_store_load_locations_file(srv_ssl->ssl_ctx.ca_file, 1, CAFILE_CERT, !hard_error_ssl)) {
William Lallemand6fce46a2022-05-04 14:53:41 +02001271 /* if we failed to load the ca-file, only quits in
1272 * error with hard_error, otherwise just disable the
1273 * feature. */
1274 if (hard_error_ssl) {
William Lallemand54aec5f2022-09-12 16:46:35 +02001275 memprintf(&errmsg, "cannot initialize SSL verify with 'ca-file \"%s\"'.", srv_ssl->ssl_ctx.ca_file);
William Lallemand6fce46a2022-05-04 14:53:41 +02001276 err_code |= ERR_ALERT | ERR_FATAL;
1277 goto err;
1278 } else {
William Lallemand54aec5f2022-09-12 16:46:35 +02001279 ha_free(&srv_ssl->ssl_ctx.ca_file);
1280 srv_drop(srv_ssl);
1281 srv_ssl = NULL;
William Lallemand6fce46a2022-05-04 14:53:41 +02001282 }
William Lallemand4006b0f2022-04-25 18:23:35 +02001283 }
William Lallemandeaa703e2022-04-22 17:52:33 +02001284 }
William Lallemandcf5cb0b2022-04-22 14:48:45 +02001285
William Lallemand957ab132021-08-24 18:33:28 +02001286#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +02001287
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001288 /* add the proxy in the proxy list only if everything is successful */
William Lallemand54aec5f2022-09-12 16:46:35 +02001289 px->next = proxies_list;
1290 proxies_list = px;
William Lallemand83614a92021-08-13 14:47:57 +02001291
William Lallemand54aec5f2022-09-12 16:46:35 +02001292 if (httpclient_resolve_init(px) != 0) {
William Lallemand85af49c2022-05-04 14:33:57 +02001293 memprintf(&errmsg, "cannot initialize resolvers.");
1294 err_code |= ERR_ALERT | ERR_FATAL;
William Lallemand5392ff62022-04-28 16:55:02 +02001295 goto err;
William Lallemand85af49c2022-05-04 14:33:57 +02001296 }
William Lallemand5392ff62022-04-28 16:55:02 +02001297
William Lallemand211c9672021-08-24 17:18:13 +02001298 /* link the 2 servers in the proxy */
William Lallemand54aec5f2022-09-12 16:46:35 +02001299 srv_raw->next = px->srv;
1300 px->srv = srv_raw;
William Lallemand957ab132021-08-24 18:33:28 +02001301
1302#ifdef USE_OPENSSL
William Lallemand54aec5f2022-09-12 16:46:35 +02001303 if (srv_ssl) {
1304 srv_ssl->next = px->srv;
1305 px->srv = srv_ssl;
William Lallemand4006b0f2022-04-25 18:23:35 +02001306 }
William Lallemand957ab132021-08-24 18:33:28 +02001307#endif
1308
William Lallemand211c9672021-08-24 17:18:13 +02001309
William Lallemand83614a92021-08-13 14:47:57 +02001310err:
William Lallemand85af49c2022-05-04 14:33:57 +02001311 if (err_code & ERR_CODE) {
1312 ha_alert("httpclient: cannot initialize: %s\n", errmsg);
1313 free(errmsg);
William Lallemand54aec5f2022-09-12 16:46:35 +02001314 srv_drop(srv_raw);
1315#ifdef USE_OPENSSL
1316 srv_drop(srv_ssl);
1317#endif
1318 free_proxy(px);
1319
1320 return NULL;
1321 }
1322 return px;
1323}
1324
1325/*
1326 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
1327 * the other for HTTPS.
1328 */
1329static int httpclient_precheck()
1330{
William Lallemand54aec5f2022-09-12 16:46:35 +02001331 /* initialize the default httpclient_proxy which is used for the CLI and the lua */
1332
1333 httpclient_proxy = httpclient_create_proxy("<HTTPCLIENT>");
1334 if (!httpclient_proxy)
1335 return 1;
William Lallemand54aec5f2022-09-12 16:46:35 +02001336
1337 return 0;
William Lallemand83614a92021-08-13 14:47:57 +02001338}
1339
William Lallemand2c8b0842022-04-22 15:16:09 +02001340static int httpclient_postcheck()
William Lallemand83614a92021-08-13 14:47:57 +02001341{
William Lallemand85af49c2022-05-04 14:33:57 +02001342 int err_code = ERR_NONE;
William Lallemand83614a92021-08-13 14:47:57 +02001343 struct logsrv *logsrv;
William Lallemand992ad622022-09-12 17:39:04 +02001344 struct proxy *curproxy = NULL;
William Lallemand71e31582022-03-16 15:47:47 +01001345 char *errmsg = NULL;
William Lallemand992ad622022-09-12 17:39:04 +02001346#ifdef USE_OPENSSL
1347 struct server *srv = NULL;
1348 struct server *srv_ssl = NULL;
1349#endif
William Lallemand83614a92021-08-13 14:47:57 +02001350
William Lallemandc6ceba32022-04-22 16:49:53 +02001351 if (global.mode & MODE_MWORKER_WAIT)
William Lallemand85af49c2022-05-04 14:33:57 +02001352 return ERR_NONE;
William Lallemandc6ceba32022-04-22 16:49:53 +02001353
William Lallemand992ad622022-09-12 17:39:04 +02001354 /* Initialize the logs for every proxy dedicated to the httpclient */
1355 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand83614a92021-08-13 14:47:57 +02001356
William Lallemand992ad622022-09-12 17:39:04 +02001357 if (!(curproxy->cap & PR_CAP_HTTPCLIENT))
1358 continue;
William Lallemand83614a92021-08-13 14:47:57 +02001359
William Lallemand992ad622022-09-12 17:39:04 +02001360 /* copy logs from "global" log list */
1361 list_for_each_entry(logsrv, &global.logsrvs, list) {
1362 struct logsrv *node = malloc(sizeof(*node));
1363
1364 if (!node) {
1365 memprintf(&errmsg, "out of memory.");
1366 err_code |= ERR_ALERT | ERR_FATAL;
1367 goto err;
1368 }
1369
1370 memcpy(node, logsrv, sizeof(*node));
1371 LIST_INIT(&node->list);
1372 LIST_APPEND(&curproxy->logsrvs, &node->list);
1373 node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
1374 node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001375 }
William Lallemand992ad622022-09-12 17:39:04 +02001376 if (curproxy->conf.logformat_string) {
1377 curproxy->conf.args.ctx = ARGC_LOG;
1378 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
1379 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
1380 SMP_VAL_FE_LOG_END, &errmsg)) {
1381 memprintf(&errmsg, "failed to parse log-format : %s.", errmsg);
1382 err_code |= ERR_ALERT | ERR_FATAL;
1383 goto err;
1384 }
1385 curproxy->conf.args.file = NULL;
1386 curproxy->conf.args.line = 0;
1387 }
William Lallemand71e31582022-03-16 15:47:47 +01001388
1389#ifdef USE_OPENSSL
William Lallemand992ad622022-09-12 17:39:04 +02001390 /* initialize the SNI for the SSL servers */
1391
1392 for (srv = curproxy->srv; srv != NULL; srv = srv->next) {
1393 if (srv->xprt == xprt_get(XPRT_SSL)) {
1394 srv_ssl = srv;
1395 }
William Lallemand715c1012022-03-16 16:39:23 +01001396 }
Miroslav Zagoraccbfee3a2022-09-19 12:20:29 +02001397 if (srv_ssl && !srv_ssl->sni_expr) {
William Lallemand992ad622022-09-12 17:39:04 +02001398 /* init the SNI expression */
1399 /* always use the host header as SNI, without the port */
1400 srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)");
1401 err_code |= server_parse_sni_expr(srv_ssl, curproxy, &errmsg);
1402 if (err_code & ERR_CODE) {
1403 memprintf(&errmsg, "failed to configure sni: %s.", errmsg);
1404 goto err;
1405 }
1406 }
William Lallemand71e31582022-03-16 15:47:47 +01001407#endif
William Lallemand992ad622022-09-12 17:39:04 +02001408 }
William Lallemand71e31582022-03-16 15:47:47 +01001409
William Lallemand83614a92021-08-13 14:47:57 +02001410err:
William Lallemand85af49c2022-05-04 14:33:57 +02001411 if (err_code & ERR_CODE) {
1412 ha_alert("httpclient: failed to initialize: %s\n", errmsg);
1413 free(errmsg);
1414
1415 }
1416 return err_code;
William Lallemand83614a92021-08-13 14:47:57 +02001417}
1418
William Lallemand83614a92021-08-13 14:47:57 +02001419/* initialize the proxy and servers for the HTTP client */
1420
William Lallemand2c8b0842022-04-22 15:16:09 +02001421REGISTER_PRE_CHECK(httpclient_precheck);
1422REGISTER_POST_CHECK(httpclient_postcheck);
William Lallemandeaa703e2022-04-22 17:52:33 +02001423
William Lallemand8a734cb2022-05-04 16:10:47 +02001424static int httpclient_parse_global_resolvers(char **args, int section_type, struct proxy *curpx,
1425 const struct proxy *defpx, const char *file, int line,
1426 char **err)
1427{
1428 if (too_many_args(1, args, err, NULL))
1429 return -1;
1430
1431 /* any configuration should set the hard_error flag */
1432 hard_error_resolvers = 1;
1433
1434 free(resolvers_id);
1435 resolvers_id = strdup(args[1]);
1436
1437 return 0;
1438}
1439
William Lallemande279f592023-05-11 21:08:38 +02001440/* config parser for global "httpclient.resolvers.disabled", accepts "on" or "off" */
1441static int httpclient_parse_global_resolvers_disabled(char **args, int section_type, struct proxy *curpx,
1442 const struct proxy *defpx, const char *file, int line,
1443 char **err)
1444{
1445 if (too_many_args(1, args, err, NULL))
1446 return -1;
1447
1448 if (strcmp(args[1], "on") == 0)
1449 resolvers_disabled = 1;
1450 else if (strcmp(args[1], "off") == 0)
1451 resolvers_disabled = 0;
1452 else {
1453 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
1454 return -1;
1455 }
1456 return 0;
1457}
1458
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001459static int httpclient_parse_global_prefer(char **args, int section_type, struct proxy *curpx,
1460 const struct proxy *defpx, const char *file, int line,
1461 char **err)
1462{
1463 if (too_many_args(1, args, err, NULL))
1464 return -1;
1465
1466 /* any configuration should set the hard_error flag */
1467 hard_error_resolvers = 1;
1468
1469
1470 if (strcmp(args[1],"ipv4") == 0)
1471 resolvers_prefer = "ipv4";
1472 else if (strcmp(args[1],"ipv6") == 0)
1473 resolvers_prefer = "ipv6";
1474 else {
1475 ha_alert("parsing [%s:%d] : '%s' expects 'ipv4' or 'ipv6' as argument.\n", file, line, args[0]);
1476 return -1;
1477 }
1478
1479 return 0;
1480}
William Lallemand8a734cb2022-05-04 16:10:47 +02001481
1482
William Lallemandeaa703e2022-04-22 17:52:33 +02001483#ifdef USE_OPENSSL
William Lallemand683fbb82022-05-04 15:43:01 +02001484static int httpclient_parse_global_ca_file(char **args, int section_type, struct proxy *curpx,
1485 const struct proxy *defpx, const char *file, int line,
1486 char **err)
1487{
1488 if (too_many_args(1, args, err, NULL))
1489 return -1;
1490
1491 /* any configuration should set the hard_error flag */
1492 hard_error_ssl = 1;
1493
1494 free(httpclient_ssl_ca_file);
1495 httpclient_ssl_ca_file = strdup(args[1]);
1496
1497 return 0;
1498}
1499
William Lallemandeaa703e2022-04-22 17:52:33 +02001500static int httpclient_parse_global_verify(char **args, int section_type, struct proxy *curpx,
1501 const struct proxy *defpx, const char *file, int line,
1502 char **err)
1503{
1504 if (too_many_args(1, args, err, NULL))
1505 return -1;
1506
William Lallemand6fce46a2022-05-04 14:53:41 +02001507 /* any configuration should set the hard_error flag */
1508 hard_error_ssl = 1;
1509
William Lallemandeaa703e2022-04-22 17:52:33 +02001510 if (strcmp(args[1],"none") == 0)
William Lallemand04994de2022-04-28 19:35:21 +02001511 httpclient_ssl_verify = SSL_SOCK_VERIFY_NONE;
William Lallemandeaa703e2022-04-22 17:52:33 +02001512 else if (strcmp(args[1],"required") == 0)
William Lallemand04994de2022-04-28 19:35:21 +02001513 httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED;
William Lallemandeaa703e2022-04-22 17:52:33 +02001514 else {
1515 ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, line, args[0]);
1516 return -1;
1517 }
1518
1519 return 0;
1520}
William Lallemand8a734cb2022-05-04 16:10:47 +02001521#endif /* ! USE_OPENSSL */
William Lallemandeaa703e2022-04-22 17:52:33 +02001522
1523static struct cfg_kw_list cfg_kws = {ILH, {
William Lallemande279f592023-05-11 21:08:38 +02001524 { CFG_GLOBAL, "httpclient.resolvers.disabled", httpclient_parse_global_resolvers_disabled },
William Lallemand8a734cb2022-05-04 16:10:47 +02001525 { CFG_GLOBAL, "httpclient.resolvers.id", httpclient_parse_global_resolvers },
William Lallemand7c5a7ef2022-05-04 15:59:44 +02001526 { CFG_GLOBAL, "httpclient.resolvers.prefer", httpclient_parse_global_prefer },
William Lallemand8a734cb2022-05-04 16:10:47 +02001527#ifdef USE_OPENSSL
William Lallemand9ff95e22022-05-04 13:52:29 +02001528 { CFG_GLOBAL, "httpclient.ssl.verify", httpclient_parse_global_verify },
William Lallemand683fbb82022-05-04 15:43:01 +02001529 { CFG_GLOBAL, "httpclient.ssl.ca-file", httpclient_parse_global_ca_file },
William Lallemand8a734cb2022-05-04 16:10:47 +02001530#endif
William Lallemandeaa703e2022-04-22 17:52:33 +02001531 { 0, NULL, NULL },
1532}};
1533
1534INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);