blob: 5a3d8a4ee2c57a5d0a6429c8083a1f8e558539e0 [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>
18#include <haproxy/dynbuf.h>
William Lallemand83614a92021-08-13 14:47:57 +020019#include <haproxy/cfgparse.h>
20#include <haproxy/connection.h>
21#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>
28#include <haproxy/htx.h>
William Lallemand83614a92021-08-13 14:47:57 +020029#include <haproxy/log.h>
30#include <haproxy/proxy.h>
William Lallemand2a8fe8b2021-08-20 14:25:15 +020031#include <haproxy/server.h>
Willy Tarreau1df20422021-10-06 11:28:24 +020032#include <haproxy/ssl_sock-t.h>
William Lallemand33b0d092021-08-13 16:05:53 +020033#include <haproxy/stream_interface.h>
William Lallemand83614a92021-08-13 14:47:57 +020034#include <haproxy/tools.h>
35
36#include <string.h>
37
38
39static struct proxy *httpclient_proxy;
40static struct server *httpclient_srv_raw;
William Lallemand957ab132021-08-24 18:33:28 +020041#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +020042static struct server *httpclient_srv_ssl;
William Lallemand957ab132021-08-24 18:33:28 +020043#endif
William Lallemand33b0d092021-08-13 16:05:53 +020044static struct applet httpclient_applet;
45
William Lallemand03a4eb12021-08-18 16:46:21 +020046/* --- This part of the file implement an HTTP client over the CLI ---
47 * The functions will be starting by "hc_cli" for "httpclient cli"
48 */
49
William Lallemand03a4eb12021-08-18 16:46:21 +020050/* What kind of data we need to read */
51#define HC_CLI_F_RES_STLINE 0x01
52#define HC_CLI_F_RES_HDR 0x02
53#define HC_CLI_F_RES_BODY 0x04
54#define HC_CLI_F_RES_END 0x08
55
56
57/* These are the callback used by the HTTP Client when it needs to notify new
58 * data, we only sets a flag in the IO handler */
59
60void hc_cli_res_stline_cb(struct httpclient *hc)
61{
62 struct appctx *appctx = hc->caller;
63
William Lallemanddfc3f892021-08-20 11:35:29 +020064 if (!appctx)
65 return;
66
William Lallemand03a4eb12021-08-18 16:46:21 +020067 appctx->ctx.cli.i0 |= HC_CLI_F_RES_STLINE;
William Lallemanddfc3f892021-08-20 11:35:29 +020068 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020069}
70
71void hc_cli_res_headers_cb(struct httpclient *hc)
72{
73 struct appctx *appctx = hc->caller;
74
William Lallemanddfc3f892021-08-20 11:35:29 +020075 if (!appctx)
76 return;
77
William Lallemand03a4eb12021-08-18 16:46:21 +020078 appctx->ctx.cli.i0 |= HC_CLI_F_RES_HDR;
William Lallemanddfc3f892021-08-20 11:35:29 +020079 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020080}
81
82void hc_cli_res_body_cb(struct httpclient *hc)
83{
84 struct appctx *appctx = hc->caller;
85
William Lallemanddfc3f892021-08-20 11:35:29 +020086 if (!appctx)
87 return;
88
William Lallemand03a4eb12021-08-18 16:46:21 +020089 appctx->ctx.cli.i0 |= HC_CLI_F_RES_BODY;
William Lallemanddfc3f892021-08-20 11:35:29 +020090 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020091}
92
93void hc_cli_res_end_cb(struct httpclient *hc)
94{
95 struct appctx *appctx = hc->caller;
96
William Lallemanddfc3f892021-08-20 11:35:29 +020097 if (!appctx)
98 return;
99
William Lallemand03a4eb12021-08-18 16:46:21 +0200100 appctx->ctx.cli.i0 |= HC_CLI_F_RES_END;
William Lallemanddfc3f892021-08-20 11:35:29 +0200101 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200102}
103
104/*
105 * Parse an httpclient keyword on the cli:
106 * httpclient <ID> <method> <URI>
107 */
108static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private)
109{
110 struct httpclient *hc;
111 char *err = NULL;
112 enum http_meth_t meth;
113 char *meth_str;
114 struct ist uri;
William Lallemanddec25c32021-10-25 19:48:37 +0200115 struct ist body = IST_NULL;
William Lallemand03a4eb12021-08-18 16:46:21 +0200116
117 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
118 return 1;
119
120 if (!*args[1] || !*args[2]) {
121 memprintf(&err, ": not enough parameters");
122 goto err;
123 }
124
125 meth_str = args[1];
126 uri = ist(args[2]);
127
William Lallemanddec25c32021-10-25 19:48:37 +0200128 if (payload)
129 body = ist(payload);
130
William Lallemand03a4eb12021-08-18 16:46:21 +0200131 meth = find_http_meth(meth_str, strlen(meth_str));
132
133 hc = httpclient_new(appctx, meth, uri);
134 if (!hc) {
135 goto err;
136 }
137
138 /* update the httpclient callbacks */
139 hc->ops.res_stline = hc_cli_res_stline_cb;
140 hc->ops.res_headers = hc_cli_res_headers_cb;
141 hc->ops.res_payload = hc_cli_res_body_cb;
142 hc->ops.res_end = hc_cli_res_end_cb;
143
144 appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */
145 appctx->ctx.cli.i0 = 0;
146
William Lallemandbad9c8c2022-01-14 14:10:33 +0100147 if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE)
William Lallemand03a4eb12021-08-18 16:46:21 +0200148 goto err;
149
150
151 if (!httpclient_start(hc))
152 goto err;
153
154 return 0;
155
156err:
157 memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : "");
158 return cli_err(appctx, err);
159}
160
161/* This function dumps the content of the httpclient receive buffer
162 * on the CLI output
163 *
164 * Return 1 when the processing is finished
165 * return 0 if it needs to be called again
166 */
167static int hc_cli_io_handler(struct appctx *appctx)
168{
Christopher Faulet86e1c332021-12-20 17:09:39 +0100169 struct stream_interface *si = cs_si(appctx->owner);
William Lallemand03a4eb12021-08-18 16:46:21 +0200170 struct buffer *trash = alloc_trash_chunk();
171 struct httpclient *hc = appctx->ctx.cli.p0;
172 struct http_hdr *hdrs, *hdr;
173
174 if (!trash)
175 goto out;
176 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_STLINE) {
William Lallemandde6ecc32022-02-16 11:37:02 +0100177 chunk_appendf(trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn),
178 hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason));
William Lallemand03a4eb12021-08-18 16:46:21 +0200179 if (ci_putchk(si_ic(si), trash) == -1)
180 si_rx_room_blk(si);
181 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_STLINE;
182 goto out;
183 }
184
185 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_HDR) {
186 hdrs = hc->res.hdrs;
187 for (hdr = hdrs; isttest(hdr->v); hdr++) {
188 if (!h1_format_htx_hdr(hdr->n, hdr->v, trash))
189 goto out;
190 }
191 if (!chunk_memcat(trash, "\r\n", 2))
192 goto out;
193 if (ci_putchk(si_ic(si), trash) == -1)
194 si_rx_room_blk(si);
195 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_HDR;
196 goto out;
197 }
198
199 if (appctx->ctx.cli.i0 & HC_CLI_F_RES_BODY) {
200 int ret;
201
202 ret = httpclient_res_xfer(hc, &si_ic(si)->buf);
203 channel_add_input(si_ic(si), ret); /* forward what we put in the buffer channel */
204
William Lallemand518878e2021-09-21 10:45:34 +0200205 if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */
William Lallemand03a4eb12021-08-18 16:46:21 +0200206 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_BODY;
207 }
208 goto out;
209 }
210
211 /* we must close only if F_END is the last flag */
212 if (appctx->ctx.cli.i0 == HC_CLI_F_RES_END) {
213 si_shutw(si);
214 si_shutr(si);
215 appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_END;
216 goto out;
217 }
218
219out:
220 /* we didn't clear every flags, we should come back to finish things */
221 if (appctx->ctx.cli.i0)
222 si_rx_room_blk(si);
223
224 free_trash_chunk(trash);
225 return 0;
226}
227
228static void hc_cli_release(struct appctx *appctx)
229{
230 struct httpclient *hc = appctx->ctx.cli.p0;
231
232 /* Everything possible was printed on the CLI, we can destroy the client */
William Lallemandecb83e12021-09-28 11:00:43 +0200233 httpclient_stop_and_destroy(hc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200234
235 return;
236}
237
238/* register cli keywords */
239static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2c8f9842022-02-18 16:26:36 +0100240 { { "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 +0200241 { { NULL }, NULL, NULL, NULL }
242}};
243
244INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
245
246
247/* --- This part of the file implements the actual HTTP client API --- */
248
William Lallemand33b0d092021-08-13 16:05:53 +0200249/*
250 * Generate a simple request and fill the httpclient request buffer with it.
251 * The request contains a request line generated from the absolute <url> and
252 * <meth> as well as list of headers <hdrs>.
253 *
254 * If the buffer was filled correctly the function returns 0, if not it returns
255 * an error_code but there is no guarantee that the buffer wasn't modified.
256 */
William Lallemanddec25c32021-10-25 19:48:37 +0200257int 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 +0200258{
259 struct htx_sl *sl;
260 struct htx *htx;
261 int err_code = 0;
262 struct ist meth_ist, vsn;
William Lallemanddec25c32021-10-25 19:48:37 +0200263 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 +0100264 int i;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100265 int foundhost = 0, foundaccept = 0, foundua = 0;
William Lallemand33b0d092021-08-13 16:05:53 +0200266
Christopher Faulet600985d2022-01-12 11:14:08 +0100267 if (!b_alloc(&hc->req.buf))
268 goto error;
269
William Lallemand33b0d092021-08-13 16:05:53 +0200270 if (meth >= HTTP_METH_OTHER)
271 goto error;
272
273 meth_ist = http_known_methods[meth];
274
275 vsn = ist("HTTP/1.1");
276
277 htx = htx_from_buf(&hc->req.buf);
278 if (!htx)
279 goto error;
William Lallemande1e045f2022-01-14 14:08:34 +0100280
281 if (!hc->ops.req_payload && !isttest(payload))
282 flags |= HTX_SL_F_BODYLESS;
283
William Lallemand33b0d092021-08-13 16:05:53 +0200284 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn);
285 if (!sl) {
286 goto error;
287 }
288 sl->info.req.meth = meth;
289
William Lallemandf03b53c2021-11-24 15:38:17 +0100290 for (i = 0; hdrs && hdrs[i].n.len; i++) {
291 /* Don't check the value length because a header value may be empty */
292 if (isttest(hdrs[i].v) == 0)
293 continue;
294
295 if (isteqi(hdrs[i].n, ist("host")))
296 foundhost = 1;
William Lallemandbad9c8c2022-01-14 14:10:33 +0100297 else if (isteqi(hdrs[i].n, ist("accept")))
298 foundaccept = 1;
299 else if (isteqi(hdrs[i].n, ist("user-agent")))
300 foundua = 1;
William Lallemandf03b53c2021-11-24 15:38:17 +0100301
302 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
303 goto error;
304 }
William Lallemand33b0d092021-08-13 16:05:53 +0200305
William Lallemandf03b53c2021-11-24 15:38:17 +0100306 if (!foundhost) {
307 /* Add Host Header from URL */
308 if (!htx_add_header(htx, ist("Host"), ist("h")))
William Lallemand79a34782021-09-20 16:19:15 +0200309 goto error;
William Lallemandf03b53c2021-11-24 15:38:17 +0100310 if (!http_update_host(htx, sl, url))
William Lallemand79a34782021-09-20 16:19:15 +0200311 goto error;
312 }
William Lallemand33b0d092021-08-13 16:05:53 +0200313
William Lallemandbad9c8c2022-01-14 14:10:33 +0100314 if (!foundaccept) {
315 if (!htx_add_header(htx, ist("Accept"), ist("*/*")))
316 goto error;
317 }
318
319 if (!foundua) {
320 if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT)))
321 goto error;
322 }
323
324
William Lallemandf03b53c2021-11-24 15:38:17 +0100325 if (!htx_add_endof(htx, HTX_BLK_EOH))
326 goto error;
327
William Lallemanddec25c32021-10-25 19:48:37 +0200328 if (isttest(payload)) {
329 /* add the payload if it can feat in the buffer, no need to set
330 * the Content-Length, the data will be sent chunked */
331 if (!htx_add_data_atonce(htx, payload))
332 goto error;
333 }
334
William Lallemand0da616e2021-10-28 15:34:26 +0200335 /* If req.payload was set, does not set the end of stream which *MUST*
336 * be set in the callback */
337 if (!hc->ops.req_payload)
338 htx->flags |= HTX_FL_EOM;
William Lallemand33b0d092021-08-13 16:05:53 +0200339
340 htx_to_buf(htx, &hc->req.buf);
341
342 return 0;
343error:
344 err_code |= ERR_ALERT | ERR_ABORT;
345 return err_code;
346}
347
348/*
349 * transfer the response to the destination buffer and wakeup the HTTP client
350 * applet so it could fill again its buffer.
351 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500352 * Return the number of bytes transferred.
William Lallemand33b0d092021-08-13 16:05:53 +0200353 */
354int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst)
355{
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100356 size_t room = b_room(dst);
William Lallemand33b0d092021-08-13 16:05:53 +0200357 int ret;
358
Willy Tarreau11adb1d2022-02-18 17:28:25 +0100359 ret = b_force_xfer(dst, &hc->res.buf, MIN(room, b_data(&hc->res.buf)));
William Lallemand33b0d092021-08-13 16:05:53 +0200360 /* call the client once we consumed all data */
Christopher Faulet600985d2022-01-12 11:14:08 +0100361 if (!b_data(&hc->res.buf)) {
362 b_free(&hc->res.buf);
363 if (hc->appctx)
364 appctx_wakeup(hc->appctx);
365 }
William Lallemand33b0d092021-08-13 16:05:53 +0200366 return ret;
367}
368
369/*
William Lallemand0da616e2021-10-28 15:34:26 +0200370 * Transfer raw HTTP payload from src, and insert it into HTX format in the
371 * httpclient.
372 *
373 * Must be used to transfer the request body.
374 * Then wakeup the httpclient so it can transfer it.
375 *
376 * <end> tries to add the ending data flag if it succeed to copy all data.
377 *
378 * Return the number of bytes copied from src.
379 */
380int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end)
381{
382 int ret = 0;
383 struct htx *htx;
384
Christopher Faulet600985d2022-01-12 11:14:08 +0100385 if (!b_alloc(&hc->req.buf))
386 goto error;
387
William Lallemand0da616e2021-10-28 15:34:26 +0200388 htx = htx_from_buf(&hc->req.buf);
389 if (!htx)
390 goto error;
391
392 if (hc->appctx)
393 appctx_wakeup(hc->appctx);
394
395 ret += htx_add_data(htx, src);
396
397
398 /* if we copied all the data and the end flag is set */
399 if ((istlen(src) == ret) && end) {
400 htx->flags |= HTX_FL_EOM;
401 }
402 htx_to_buf(htx, &hc->req.buf);
403
404error:
405
406 return ret;
407}
408
William Lallemandb4a4ef62022-02-23 14:18:16 +0100409/* Set the 'timeout server' in ms for the next httpclient request */
410void httpclient_set_timeout(struct httpclient *hc, int timeout)
411{
412 hc->timeout_server = timeout;
413}
414
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100415/*
416 * Sets a destination for the httpclient from an HAProxy addr format
417 * This will prevent to determine the destination from the URL
418 * Return 0 in case of success or -1 otherwise.
419 */
420int httpclient_set_dst(struct httpclient *hc, const char *dst)
421{
422 struct sockaddr_storage *sk;
423 char *errmsg = NULL;
424
425 sockaddr_free(&hc->dst);
426 /* 'sk' is statically allocated (no need to be freed). */
427 sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL,
428 &errmsg, NULL, NULL,
429 PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
430 if (!sk) {
431 ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg);
432 free(errmsg);
433 return -1;
434 }
435
436 if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) {
437 ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__);
438 return -1;
439 }
440
441 return 0;
442}
William Lallemand0da616e2021-10-28 15:34:26 +0200443
444/*
William Lallemand33b0d092021-08-13 16:05:53 +0200445 * Start the HTTP client
446 * Create the appctx, session, stream and wakeup the applet
447 *
448 * FIXME: It also fill the sockaddr with the IP address, but currently only IP
449 * in the URL are supported, it lacks a resolver.
450 *
451 * Return the <appctx> or NULL if it failed
452 */
453struct appctx *httpclient_start(struct httpclient *hc)
454{
455 struct applet *applet = &httpclient_applet;
456 struct appctx *appctx;
457 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100458 struct conn_stream *cs;
William Lallemand33b0d092021-08-13 16:05:53 +0200459 struct stream *s;
460 int len;
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100461 struct sockaddr_storage ss_url;
462 struct sockaddr_storage* ss_dst;
William Lallemand33b0d092021-08-13 16:05:53 +0200463 struct split_url out;
464
William Lallemand5085bc32022-02-17 12:52:09 +0100465 /* if the client was started and not ended, an applet is already
466 * running, we shouldn't try anything */
467 if (httpclient_started(hc) && !httpclient_ended(hc))
468 return NULL;
469
470 hc->flags = 0;
471
William Lallemand33b0d092021-08-13 16:05:53 +0200472 /* parse URI and fill sockaddr_storage */
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100473 len = url2sa(istptr(hc->req.url), istlen(hc->req.url), &ss_url, &out);
William Lallemand33b0d092021-08-13 16:05:53 +0200474 if (len == -1) {
William Lallemand614e6832021-09-26 18:12:43 +0200475 ha_alert("httpclient: cannot parse uri '%s'.\n", istptr(hc->req.url));
William Lallemand33b0d092021-08-13 16:05:53 +0200476 goto out;
477 }
478
479 /* The HTTP client will be created in the same thread as the caller,
480 * avoiding threading issues */
Willy Tarreaue6124462021-09-13 10:07:38 +0200481 appctx = appctx_new(applet);
William Lallemand33b0d092021-08-13 16:05:53 +0200482 if (!appctx)
483 goto out;
484
485 sess = session_new(httpclient_proxy, NULL, &appctx->obj_type);
486 if (!sess) {
487 ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__);
488 goto out_free_appctx;
489 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100490 cs = cs_new();
Christopher Faulet13a35e52021-12-20 15:34:16 +0100491 if (!cs) {
492 ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__);
493 goto out_free_sess;
494 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100495 cs_attach_endp(cs, &appctx->obj_type, appctx);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100496 if ((s = stream_new(sess, cs, &hc->req.buf)) == NULL) {
William Lallemand33b0d092021-08-13 16:05:53 +0200497 ha_alert("httpclient: Failed to initialize stream %s:%d.\n", __FUNCTION__, __LINE__);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100498 goto out_free_cs;
William Lallemand33b0d092021-08-13 16:05:53 +0200499 }
500
William Lallemandb4a4ef62022-02-23 14:18:16 +0100501 /* set the "timeout server" */
502 s->req.wto = hc->timeout_server;
503 s->res.rto = hc->timeout_server;
504
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100505 /* if httpclient_set_dst() was used, sets the alternative address */
506 if (hc->dst)
507 ss_dst = hc->dst;
508 else
509 ss_dst = &ss_url;
510
Christopher Fauletb91afea2021-12-23 13:58:12 +0100511 if (!sockaddr_alloc(&cs_si(s->csb)->dst, ss_dst, sizeof(*hc->dst))) {
William Lallemand33b0d092021-08-13 16:05:53 +0200512 ha_alert("httpclient: Failed to initialize stream in %s:%d.\n", __FUNCTION__, __LINE__);
513 goto out_free_stream;
514 }
515
516 /* choose the SSL server or not */
517 switch (out.scheme) {
518 case SCH_HTTP:
519 s->target = &httpclient_srv_raw->obj_type;
520 break;
521 case SCH_HTTPS:
William Lallemand957ab132021-08-24 18:33:28 +0200522#ifdef USE_OPENSSL
William Lallemand33b0d092021-08-13 16:05:53 +0200523 s->target = &httpclient_srv_ssl->obj_type;
William Lallemand957ab132021-08-24 18:33:28 +0200524#else
525 ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__);
526 goto out_free_stream;
527#endif
William Lallemand33b0d092021-08-13 16:05:53 +0200528 break;
529 }
530
531 s->flags |= SF_ASSIGNED|SF_ADDR_SET;
Christopher Fauletb91afea2021-12-23 13:58:12 +0100532 cs_si(s->csb)->flags |= SI_FL_NOLINGER;
William Lallemand33b0d092021-08-13 16:05:53 +0200533 s->res.flags |= CF_READ_DONTWAIT;
534
535 /* applet is waiting for data */
Christopher Fauletb91afea2021-12-23 13:58:12 +0100536 si_cant_get(cs_si(s->csf));
William Lallemand33b0d092021-08-13 16:05:53 +0200537 appctx_wakeup(appctx);
538
William Lallemand33b0d092021-08-13 16:05:53 +0200539 hc->appctx = appctx;
William Lallemandb8b13702021-09-28 12:15:37 +0200540 hc->flags |= HTTPCLIENT_FS_STARTED;
William Lallemand33b0d092021-08-13 16:05:53 +0200541 appctx->ctx.httpclient.ptr = hc;
Christopher Faulet6ced61d2022-01-12 15:27:41 +0100542
543 /* The request was transferred when the stream was created. So switch
544 * directly to REQ_BODY or RES_STLINE state
545 */
546 appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE);
William Lallemand33b0d092021-08-13 16:05:53 +0200547
548 return appctx;
549
550out_free_stream:
551 LIST_DELETE(&s->list);
552 pool_free(pool_head_stream, s);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100553out_free_cs:
554 cs_free(cs);
William Lallemand33b0d092021-08-13 16:05:53 +0200555out_free_sess:
556 session_free(sess);
557out_free_appctx:
558 appctx_free(appctx);
559out:
560
561 return NULL;
562}
563
William Lallemandecb83e12021-09-28 11:00:43 +0200564/*
565 * This function tries to destroy the httpclient if it wasn't running.
566 * If it was running, stop the client and ask it to autodestroy itself.
567 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500568 * Once this function is used, all pointer sto the client must be removed
William Lallemandecb83e12021-09-28 11:00:43 +0200569 *
570 */
571void httpclient_stop_and_destroy(struct httpclient *hc)
572{
573
William Lallemandb8b13702021-09-28 12:15:37 +0200574 /* The httpclient was already stopped or never started, we can safely destroy it */
575 if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) {
William Lallemandecb83e12021-09-28 11:00:43 +0200576 httpclient_destroy(hc);
577 } else {
578 /* if the client wasn't stopped, ask for a stop and destroy */
579 hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP);
580 if (hc->appctx)
581 appctx_wakeup(hc->appctx);
582 }
583}
584
William Lallemand33b0d092021-08-13 16:05:53 +0200585/* Free the httpclient */
586void httpclient_destroy(struct httpclient *hc)
587{
William Lallemand03f5a1c2021-09-27 15:17:47 +0200588 struct http_hdr *hdrs;
589
590
William Lallemand33b0d092021-08-13 16:05:53 +0200591 if (!hc)
592 return;
William Lallemandecb83e12021-09-28 11:00:43 +0200593
William Lallemand2a879002021-10-05 15:50:45 +0200594 /* we should never destroy a client which was started but not stopped */
595 BUG_ON(httpclient_started(hc) && !httpclient_ended(hc));
William Lallemandecb83e12021-09-28 11:00:43 +0200596
William Lallemand03f5a1c2021-09-27 15:17:47 +0200597 /* request */
598 istfree(&hc->req.url);
William Lallemand33b0d092021-08-13 16:05:53 +0200599 b_free(&hc->req.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200600 /* response */
601 istfree(&hc->res.vsn);
602 istfree(&hc->res.reason);
603 hdrs = hc->res.hdrs;
604 while (hdrs && isttest(hdrs->n)) {
605 istfree(&hdrs->n);
606 istfree(&hdrs->v);
607 hdrs++;
608 }
609 ha_free(&hc->res.hdrs);
William Lallemand33b0d092021-08-13 16:05:53 +0200610 b_free(&hc->res.buf);
William Lallemand7b2e0ee2022-02-17 19:10:55 +0100611 sockaddr_free(&hc->dst);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200612
William Lallemand33b0d092021-08-13 16:05:53 +0200613 free(hc);
614
615 return;
616}
617
618/* Allocate an httpclient and its buffers
619 * Return NULL on failure */
620struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url)
621{
622 struct httpclient *hc;
William Lallemand33b0d092021-08-13 16:05:53 +0200623
624 hc = calloc(1, sizeof(*hc));
625 if (!hc)
626 goto err;
627
Christopher Faulet600985d2022-01-12 11:14:08 +0100628 hc->req.buf = BUF_NULL;
629 hc->res.buf = BUF_NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200630 hc->caller = caller;
William Lallemand67b77842021-11-10 16:57:25 +0100631 hc->req.url = istdup(url);
William Lallemand33b0d092021-08-13 16:05:53 +0200632 hc->req.meth = meth;
633
634 return hc;
635
636err:
637 httpclient_destroy(hc);
638 return NULL;
639}
640
641static void httpclient_applet_io_handler(struct appctx *appctx)
642{
643 struct httpclient *hc = appctx->ctx.httpclient.ptr;
Christopher Faulet86e1c332021-12-20 17:09:39 +0100644 struct stream_interface *si = cs_si(appctx->owner);
William Lallemand33b0d092021-08-13 16:05:53 +0200645 struct stream *s = si_strm(si);
646 struct channel *req = &s->req;
647 struct channel *res = &s->res;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100648 struct http_msg *msg = &s->txn->rsp;
William Lallemand33b0d092021-08-13 16:05:53 +0200649 struct htx_blk *blk = NULL;
650 struct htx *htx;
William Lallemandb7020302021-08-20 11:24:13 +0200651 struct htx_sl *sl = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200652 int32_t pos;
653 uint32_t hdr_num;
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100654 uint32_t sz;
William Lallemand933fe392021-11-04 09:45:58 +0100655 int ret;
William Lallemand33b0d092021-08-13 16:05:53 +0200656
657
658 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200659
660 /* required to stop */
661 if (hc->flags & HTTPCLIENT_FA_STOP)
662 goto end;
663
William Lallemand33b0d092021-08-13 16:05:53 +0200664 switch(appctx->st0) {
665
666 case HTTPCLIENT_S_REQ:
William Lallemanddb8a1f32021-11-08 16:55:14 +0100667 /* we know that the buffer is empty here, since
668 * it's the first call, we can freely copy the
669 * request from the httpclient buffer */
William Lallemand933fe392021-11-04 09:45:58 +0100670 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
William Lallemanddb8a1f32021-11-08 16:55:14 +0100671 if (!ret)
672 goto more;
William Lallemand933fe392021-11-04 09:45:58 +0100673
Christopher Faulet600985d2022-01-12 11:14:08 +0100674 if (!b_data(&hc->req.buf))
675 b_free(&hc->req.buf);
676
William Lallemanddb8a1f32021-11-08 16:55:14 +0100677 htx = htx_from_buf(&req->buf);
William Lallemand933fe392021-11-04 09:45:58 +0100678 if (!htx)
679 goto more;
680
William Lallemanddb8a1f32021-11-08 16:55:14 +0100681 channel_add_input(req, htx->data);
682
William Lallemand933fe392021-11-04 09:45:58 +0100683 if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */
684 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
685 else
686 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
687
William Lallemand33b0d092021-08-13 16:05:53 +0200688 goto more; /* we need to leave the IO handler once we wrote the request */
689 break;
William Lallemand0da616e2021-10-28 15:34:26 +0200690 case HTTPCLIENT_S_REQ_BODY:
691 /* call the payload callback */
692 {
693 if (hc->ops.req_payload) {
William Lallemand0da616e2021-10-28 15:34:26 +0200694
William Lallemand0da616e2021-10-28 15:34:26 +0200695 /* call the request callback */
696 hc->ops.req_payload(hc);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100697 /* check if the request buffer is empty */
698
699 htx = htx_from_buf(&req->buf);
700 if (!htx_is_empty(htx))
701 goto more;
702 /* Here htx_to_buf() will set buffer data to 0 because
703 * the HTX is empty, and allow us to do an xfer.
704 */
705 htx_to_buf(htx, &req->buf);
706
707 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
708 if (!ret)
709 goto more;
Christopher Faulet600985d2022-01-12 11:14:08 +0100710
711 if (!b_data(&hc->req.buf))
712 b_free(&hc->req.buf);
713
William Lallemanddb8a1f32021-11-08 16:55:14 +0100714 htx = htx_from_buf(&req->buf);
715 if (!htx)
716 goto more;
717
718 channel_add_input(req, htx->data);
William Lallemand0da616e2021-10-28 15:34:26 +0200719 }
720
William Lallemanddb8a1f32021-11-08 16:55:14 +0100721 htx = htx_from_buf(&req->buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200722 if (!htx)
723 goto more;
724
725 /* if the request contains the HTX_FL_EOM, we finished the request part. */
Christopher Faulet3d433242022-03-03 15:38:39 +0100726 if (htx->flags & HTX_FL_EOM) {
727 si->cs->flags |= CS_FL_EOI;
728 req->flags |= CF_EOI;
William Lallemand0da616e2021-10-28 15:34:26 +0200729 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
Christopher Faulet3d433242022-03-03 15:38:39 +0100730 }
William Lallemand0da616e2021-10-28 15:34:26 +0200731
732 goto more; /* we need to leave the IO handler once we wrote the request */
733 }
734 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200735
736 case HTTPCLIENT_S_RES_STLINE:
737 /* copy the start line in the hc structure,then remove the htx block */
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100738 if (!co_data(res) || (msg->msg_state < HTTP_MSG_DATA))
William Lallemand33b0d092021-08-13 16:05:53 +0200739 goto more;
740 htx = htxbuf(&res->buf);
741 if (!htx)
742 goto more;
William Lallemand97f69c62022-03-10 17:23:40 +0100743 blk = htx_get_head_blk(htx);
William Lallemand33b0d092021-08-13 16:05:53 +0200744 if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL))
745 sl = htx_get_blk_ptr(htx, blk);
746 if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP)))
747 goto more;
748
749 /* copy the status line in the httpclient */
750 hc->res.status = sl->info.res.status;
751 hc->res.vsn = istdup(htx_sl_res_vsn(sl));
752 hc->res.reason = istdup(htx_sl_res_reason(sl));
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100753 sz = htx_get_blksz(blk);
754 co_set_data(res, co_data(res) - sz);
755 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200756 /* caller callback */
757 if (hc->ops.res_stline)
758 hc->ops.res_stline(hc);
759
760 /* if there is no HTX data anymore and the EOM flag is
761 * set, leave (no body) */
762 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM)
763 appctx->st0 = HTTPCLIENT_S_RES_END;
764 else
765 appctx->st0 = HTTPCLIENT_S_RES_HDR;
766 break;
767
768 case HTTPCLIENT_S_RES_HDR:
769 /* first copy the headers in a local hdrs
770 * structure, once we the total numbers of the
771 * header we allocate the right size and copy
772 * them. The htx block of the headers are
773 * removed each time one is read */
774 {
775 struct http_hdr hdrs[global.tune.max_http_hdr];
776
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100777 if (!co_data(res) || (msg->msg_state < HTTP_MSG_DATA))
William Lallemand33b0d092021-08-13 16:05:53 +0200778 goto more;
779 htx = htxbuf(&res->buf);
780 if (!htx)
781 goto more;
782
783 hdr_num = 0;
784
William Lallemand97f69c62022-03-10 17:23:40 +0100785 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
William Lallemand33b0d092021-08-13 16:05:53 +0200786 struct htx_blk *blk = htx_get_blk(htx, pos);
787 enum htx_blk_type type = htx_get_blk_type(blk);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100788 uint32_t sz = htx_get_blksz(blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200789
William Lallemandc020b252022-03-09 18:56:02 +0100790 if (type == HTX_BLK_UNUSED) {
791 c_rew(res, sz);
792 htx_remove_blk(htx, blk);
793 }
794
795 if (type == HTX_BLK_HDR) {
796 hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk));
797 hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk));
798 if (!isttest(hdrs[hdr_num].v) || !isttest(hdrs[hdr_num].n))
799 goto end;
800 c_rew(res, sz);
801 htx_remove_blk(htx, blk);
802 hdr_num++;
803 }
804
805 /* create a NULL end of array and leave the loop */
William Lallemand33b0d092021-08-13 16:05:53 +0200806 if (type == HTX_BLK_EOH) {
807 hdrs[hdr_num].n = IST_NULL;
808 hdrs[hdr_num].v = IST_NULL;
William Lallemandc020b252022-03-09 18:56:02 +0100809 c_rew(res, sz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100810 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200811 break;
812 }
William Lallemand33b0d092021-08-13 16:05:53 +0200813 }
814
William Lallemand0d6f7792021-08-20 11:59:49 +0200815 if (hdr_num) {
816 /* alloc and copy the headers in the httpclient struct */
817 hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs));
818 if (!hc->res.hdrs)
819 goto end;
820 memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
William Lallemand33b0d092021-08-13 16:05:53 +0200821
William Lallemand0d6f7792021-08-20 11:59:49 +0200822 /* caller callback */
823 if (hc->ops.res_headers)
824 hc->ops.res_headers(hc);
825 }
William Lallemand33b0d092021-08-13 16:05:53 +0200826
827 /* if there is no HTX data anymore and the EOM flag is
828 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200829 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200830 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200831 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200832 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200833 }
William Lallemand33b0d092021-08-13 16:05:53 +0200834 }
835 break;
836
837 case HTTPCLIENT_S_RES_BODY:
838 /*
839 * The IO handler removes the htx blocks in the response buffer and
840 * push them in the hc->res.buf buffer in a raw format.
841 */
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100842 if (!co_data(res) || (msg->msg_state < HTTP_MSG_DATA))
843 goto more;
844
William Lallemand33b0d092021-08-13 16:05:53 +0200845 htx = htxbuf(&res->buf);
846 if (!htx || htx_is_empty(htx))
847 goto more;
848
Christopher Faulet600985d2022-01-12 11:14:08 +0100849 if (!b_alloc(&hc->res.buf))
850 goto more;
851
William Lallemand33b0d092021-08-13 16:05:53 +0200852 if (b_full(&hc->res.buf))
Christopher Faulet600985d2022-01-12 11:14:08 +0100853 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200854
855 /* decapsule the htx data to raw data */
William Lallemand97f69c62022-03-10 17:23:40 +0100856 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100857 struct htx_blk *blk = htx_get_blk(htx, pos);
858 enum htx_blk_type type = htx_get_blk_type(blk);
859 size_t count = co_data(res);
860 uint32_t blksz = htx_get_blksz(blk);
861 uint32_t room = b_room(&hc->res.buf);
862 uint32_t vlen;
William Lallemand33b0d092021-08-13 16:05:53 +0200863
William Lallemandc8f1eb92022-03-09 11:58:51 +0100864 /* we should try to copy the maximum output data in a block, which fit
865 * the destination buffer */
866 vlen = MIN(count, blksz);
867 vlen = MIN(vlen, room);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100868
William Lallemandc8f1eb92022-03-09 11:58:51 +0100869 if (vlen == 0)
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100870 goto process_data;
871
William Lallemand33b0d092021-08-13 16:05:53 +0200872 if (type == HTX_BLK_DATA) {
873 struct ist v = htx_get_blk_value(htx, blk);
874
William Lallemandc8f1eb92022-03-09 11:58:51 +0100875 __b_putblk(&hc->res.buf, v.ptr, vlen);
876 c_rew(res, vlen);
William Lallemand33b0d092021-08-13 16:05:53 +0200877
William Lallemandc8f1eb92022-03-09 11:58:51 +0100878 if (vlen == blksz)
879 htx_remove_blk(htx, blk);
880 else
881 htx_cut_data_blk(htx, blk, vlen);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100882
William Lallemand33b0d092021-08-13 16:05:53 +0200883 /* the data must be processed by the caller in the receive phase */
884 if (hc->ops.res_payload)
885 hc->ops.res_payload(hc);
William Lallemandc8f1eb92022-03-09 11:58:51 +0100886
887 /* cannot copy everything, need to processs */
888 if (vlen != blksz)
889 goto process_data;
William Lallemand33b0d092021-08-13 16:05:53 +0200890 } else {
William Lallemandc8f1eb92022-03-09 11:58:51 +0100891 if (vlen != blksz)
892 goto process_data;
893
William Lallemand33b0d092021-08-13 16:05:53 +0200894 /* remove any block which is not a data block */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100895 c_rew(res, blksz);
William Lallemand2b7dc4e2022-02-24 16:55:41 +0100896 htx_remove_blk(htx, blk);
William Lallemand33b0d092021-08-13 16:05:53 +0200897 }
898 }
William Lallemandc8f1eb92022-03-09 11:58:51 +0100899
William Lallemand33b0d092021-08-13 16:05:53 +0200900 /* if not finished, should be called again */
William Lallemandc8f1eb92022-03-09 11:58:51 +0100901 if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)))
William Lallemand33b0d092021-08-13 16:05:53 +0200902 goto more;
903
William Lallemandc8f1eb92022-03-09 11:58:51 +0100904
William Lallemand33b0d092021-08-13 16:05:53 +0200905 /* end of message, we should quit */
906 appctx->st0 = HTTPCLIENT_S_RES_END;
907 break;
908
909 case HTTPCLIENT_S_RES_END:
910 goto end;
911 break;
912 }
913 }
914
915process_data:
916
917 si_rx_chan_rdy(si);
918
919 return;
920more:
921 /* There was not enough data in the response channel */
922
923 si_rx_room_blk(si);
924
925 if (appctx->st0 == HTTPCLIENT_S_RES_END)
926 goto end;
927
928 /* The state machine tries to handle as much data as possible, if there
929 * isn't any data to handle and a shutdown is detected, let's stop
930 * everything */
931 if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) ||
932 (res->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
933 goto end;
934 }
935 return;
936
937end:
938 if (hc->ops.res_end)
939 hc->ops.res_end(hc);
940 si_shutw(si);
941 si_shutr(si);
942 return;
943}
944
945static void httpclient_applet_release(struct appctx *appctx)
946{
947 struct httpclient *hc = appctx->ctx.httpclient.ptr;
948
William Lallemand1123dde2021-09-21 10:58:10 +0200949 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +0200950 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +0200951 /* the applet is leaving, remove the ptr so we don't try to call it
952 * again from the caller */
953 hc->appctx = NULL;
954
William Lallemandecb83e12021-09-28 11:00:43 +0200955
956 /* destroy the httpclient when set to autotokill */
957 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
958 httpclient_destroy(hc);
959 }
960
William Lallemand33b0d092021-08-13 16:05:53 +0200961 return;
962}
963
964/* HTTP client applet */
965static struct applet httpclient_applet = {
966 .obj_type = OBJ_TYPE_APPLET,
967 .name = "<HTTPCLIENT>",
968 .fct = httpclient_applet_io_handler,
969 .release = httpclient_applet_release,
970};
971
William Lallemand83614a92021-08-13 14:47:57 +0200972/*
973 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
974 * the other for HTTPS.
975 */
976
977static int httpclient_init()
978{
979 int err_code = 0;
980 char *errmsg = NULL;
981
982 httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg);
983 if (!httpclient_proxy) {
984 err_code |= ERR_ALERT | ERR_FATAL;
985 goto err;
986 }
987
Willy Tarreau0e72e402021-08-20 10:23:12 +0200988 proxy_preset_defaults(httpclient_proxy);
989
William Lallemand83614a92021-08-13 14:47:57 +0200990 httpclient_proxy->options2 |= PR_O2_INDEPSTR;
991 httpclient_proxy->mode = PR_MODE_HTTP;
992 httpclient_proxy->maxconn = 0;
993 httpclient_proxy->accept = NULL;
994 httpclient_proxy->timeout.client = TICK_ETERNITY;
995 /* The HTTP Client use the "option httplog" with the global log server */
996 httpclient_proxy->conf.logformat_string = default_http_log_format;
997 httpclient_proxy->http_needed = 1;
998
999 /* clear HTTP server */
1000 httpclient_srv_raw = new_server(httpclient_proxy);
1001 if (!httpclient_srv_raw) {
1002 err_code |= ERR_ALERT | ERR_FATAL;
1003 memprintf(&errmsg, "out of memory.");
1004 goto err;
1005 }
1006
1007 httpclient_srv_raw->iweight = 0;
1008 httpclient_srv_raw->uweight = 0;
1009 httpclient_srv_raw->xprt = xprt_get(XPRT_RAW);
1010 httpclient_srv_raw->id = strdup("<HTTPCLIENT>");
1011 if (!httpclient_srv_raw->id)
1012 goto err;
1013
William Lallemand957ab132021-08-24 18:33:28 +02001014#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +02001015 /* SSL HTTP server */
1016 httpclient_srv_ssl = new_server(httpclient_proxy);
1017 if (!httpclient_srv_ssl) {
1018 memprintf(&errmsg, "out of memory.");
1019 err_code |= ERR_ALERT | ERR_FATAL;
1020 goto err;
1021 }
1022 httpclient_srv_ssl->iweight = 0;
1023 httpclient_srv_ssl->uweight = 0;
1024 httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL);
1025 httpclient_srv_ssl->use_ssl = 1;
William Lallemand211c9672021-08-24 17:18:13 +02001026 httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>");
William Lallemand83614a92021-08-13 14:47:57 +02001027 if (!httpclient_srv_ssl->id)
1028 goto err;
1029
William Lallemandcfcbe9e2021-08-24 17:15:58 +02001030 httpclient_srv_ssl->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
William Lallemand957ab132021-08-24 18:33:28 +02001031#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +02001032
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001033 /* add the proxy in the proxy list only if everything is successful */
William Lallemand83614a92021-08-13 14:47:57 +02001034 httpclient_proxy->next = proxies_list;
1035 proxies_list = httpclient_proxy;
1036
William Lallemand211c9672021-08-24 17:18:13 +02001037 /* link the 2 servers in the proxy */
1038 httpclient_srv_raw->next = httpclient_proxy->srv;
William Lallemand957ab132021-08-24 18:33:28 +02001039 httpclient_proxy->srv = httpclient_srv_raw;
1040
1041#ifdef USE_OPENSSL
1042 httpclient_srv_ssl->next = httpclient_proxy->srv;
William Lallemand211c9672021-08-24 17:18:13 +02001043 httpclient_proxy->srv = httpclient_srv_ssl;
William Lallemand957ab132021-08-24 18:33:28 +02001044#endif
1045
William Lallemand211c9672021-08-24 17:18:13 +02001046
William Lallemand83614a92021-08-13 14:47:57 +02001047 return 0;
1048
1049err:
1050 ha_alert("httpclient: cannot initialize.\n");
1051 free(errmsg);
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001052 srv_drop(httpclient_srv_raw);
William Lallemand957ab132021-08-24 18:33:28 +02001053#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001054 srv_drop(httpclient_srv_ssl);
William Lallemand957ab132021-08-24 18:33:28 +02001055#endif
William Lallemand83614a92021-08-13 14:47:57 +02001056 free_proxy(httpclient_proxy);
1057 return err_code;
1058}
1059
William Lallemand83614a92021-08-13 14:47:57 +02001060static int httpclient_cfg_postparser()
1061{
1062 struct logsrv *logsrv;
1063 struct proxy *curproxy = httpclient_proxy;
William Lallemand71e31582022-03-16 15:47:47 +01001064 char *errmsg = NULL;
William Lallemand83614a92021-08-13 14:47:57 +02001065
1066 /* copy logs from "global" log list */
1067 list_for_each_entry(logsrv, &global.logsrvs, list) {
1068 struct logsrv *node = malloc(sizeof(*node));
1069
1070 if (!node) {
1071 ha_alert("httpclient: cannot allocate memory.\n");
1072 goto err;
1073 }
1074
1075 memcpy(node, logsrv, sizeof(*node));
1076 LIST_INIT(&node->list);
1077 LIST_APPEND(&curproxy->logsrvs, &node->list);
1078 }
1079 if (curproxy->conf.logformat_string) {
William Lallemand83614a92021-08-13 14:47:57 +02001080 curproxy->conf.args.ctx = ARGC_LOG;
1081 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
1082 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
William Lallemand715c1012022-03-16 16:39:23 +01001083 SMP_VAL_FE_LOG_END, &errmsg)) {
1084 ha_alert("httpclient: failed to parse log-format : %s.\n", errmsg);
1085 free(errmsg);
William Lallemand83614a92021-08-13 14:47:57 +02001086 goto err;
1087 }
1088 curproxy->conf.args.file = NULL;
1089 curproxy->conf.args.line = 0;
1090 }
William Lallemand71e31582022-03-16 15:47:47 +01001091
1092#ifdef USE_OPENSSL
William Lallemand715c1012022-03-16 16:39:23 +01001093 {
1094 int err_code = 0;
1095
1096 /* init the SNI expression */
1097 /* always use the host header as SNI, without the port */
1098 httpclient_srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)");
1099 err_code |= server_parse_sni_expr(httpclient_srv_ssl, httpclient_proxy, &errmsg);
1100 if (err_code & ERR_CODE) {
1101 ha_alert("httpclient: failed to configure sni: %s.\n", errmsg);
1102 free(errmsg);
1103 goto err;
1104 }
William Lallemand71e31582022-03-16 15:47:47 +01001105 }
1106#endif
1107
William Lallemand83614a92021-08-13 14:47:57 +02001108 return 0;
1109err:
1110 return 1;
1111}
1112
William Lallemand83614a92021-08-13 14:47:57 +02001113/* initialize the proxy and servers for the HTTP client */
1114
Willy Tarreau5b4b6ca2022-02-18 16:23:14 +01001115INITCALL0(STG_INIT, httpclient_init);
William Lallemand83614a92021-08-13 14:47:57 +02001116REGISTER_CONFIG_POSTPARSER("httpclient", httpclient_cfg_postparser);