William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1 | /* |
| 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 Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 14 | |
William Lallemand | 2a8fe8b | 2021-08-20 14:25:15 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 16 | #include <haproxy/applet.h> |
| 17 | #include <haproxy/cli.h> |
William Lallemand | cf5cb0b | 2022-04-22 14:48:45 +0200 | [diff] [blame] | 18 | #include <haproxy/ssl_ckch.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 19 | #include <haproxy/dynbuf.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 21 | #include <haproxy/conn_stream.h> |
| 22 | #include <haproxy/cs_utils.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 23 | #include <haproxy/global.h> |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 24 | #include <haproxy/istbuf.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 25 | #include <haproxy/h1_htx.h> |
| 26 | #include <haproxy/http.h> |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 27 | #include <haproxy/http_ana-t.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 28 | #include <haproxy/http_client.h> |
| 29 | #include <haproxy/http_htx.h> |
| 30 | #include <haproxy/htx.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
| 32 | #include <haproxy/proxy.h> |
William Lallemand | 2a8fe8b | 2021-08-20 14:25:15 +0200 | [diff] [blame] | 33 | #include <haproxy/server.h> |
Willy Tarreau | 1df2042 | 2021-10-06 11:28:24 +0200 | [diff] [blame] | 34 | #include <haproxy/ssl_sock-t.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | |
| 40 | static struct proxy *httpclient_proxy; |
| 41 | static struct server *httpclient_srv_raw; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 42 | #ifdef USE_OPENSSL |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 43 | static struct server *httpclient_srv_ssl; |
William Lallemand | f1344b3 | 2022-04-26 12:00:06 +0200 | [diff] [blame^] | 44 | static int httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 45 | #endif |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 46 | static struct applet httpclient_applet; |
| 47 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 48 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 49 | /* --- This part of the file implement an HTTP client over the CLI --- |
| 50 | * The functions will be starting by "hc_cli" for "httpclient cli" |
| 51 | */ |
| 52 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 53 | /* What kind of data we need to read */ |
| 54 | #define HC_CLI_F_RES_STLINE 0x01 |
| 55 | #define HC_CLI_F_RES_HDR 0x02 |
| 56 | #define HC_CLI_F_RES_BODY 0x04 |
| 57 | #define HC_CLI_F_RES_END 0x08 |
| 58 | |
| 59 | |
| 60 | /* These are the callback used by the HTTP Client when it needs to notify new |
| 61 | * data, we only sets a flag in the IO handler */ |
| 62 | |
| 63 | void hc_cli_res_stline_cb(struct httpclient *hc) |
| 64 | { |
| 65 | struct appctx *appctx = hc->caller; |
| 66 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 67 | if (!appctx) |
| 68 | return; |
| 69 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 70 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_STLINE; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 71 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void hc_cli_res_headers_cb(struct httpclient *hc) |
| 75 | { |
| 76 | struct appctx *appctx = hc->caller; |
| 77 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 78 | if (!appctx) |
| 79 | return; |
| 80 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 81 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_HDR; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 82 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void hc_cli_res_body_cb(struct httpclient *hc) |
| 86 | { |
| 87 | struct appctx *appctx = hc->caller; |
| 88 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 89 | if (!appctx) |
| 90 | return; |
| 91 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 92 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_BODY; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 93 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void hc_cli_res_end_cb(struct httpclient *hc) |
| 97 | { |
| 98 | struct appctx *appctx = hc->caller; |
| 99 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 100 | if (!appctx) |
| 101 | return; |
| 102 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 103 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_END; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 104 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Parse an httpclient keyword on the cli: |
| 109 | * httpclient <ID> <method> <URI> |
| 110 | */ |
| 111 | static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private) |
| 112 | { |
| 113 | struct httpclient *hc; |
| 114 | char *err = NULL; |
| 115 | enum http_meth_t meth; |
| 116 | char *meth_str; |
| 117 | struct ist uri; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 118 | struct ist body = IST_NULL; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 119 | |
| 120 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 121 | return 1; |
| 122 | |
| 123 | if (!*args[1] || !*args[2]) { |
| 124 | memprintf(&err, ": not enough parameters"); |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | meth_str = args[1]; |
| 129 | uri = ist(args[2]); |
| 130 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 131 | if (payload) |
| 132 | body = ist(payload); |
| 133 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 134 | meth = find_http_meth(meth_str, strlen(meth_str)); |
| 135 | |
| 136 | hc = httpclient_new(appctx, meth, uri); |
| 137 | if (!hc) { |
| 138 | goto err; |
| 139 | } |
| 140 | |
| 141 | /* update the httpclient callbacks */ |
| 142 | hc->ops.res_stline = hc_cli_res_stline_cb; |
| 143 | hc->ops.res_headers = hc_cli_res_headers_cb; |
| 144 | hc->ops.res_payload = hc_cli_res_body_cb; |
| 145 | hc->ops.res_end = hc_cli_res_end_cb; |
| 146 | |
| 147 | appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */ |
| 148 | appctx->ctx.cli.i0 = 0; |
| 149 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 150 | if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE) |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 151 | goto err; |
| 152 | |
| 153 | |
| 154 | if (!httpclient_start(hc)) |
| 155 | goto err; |
| 156 | |
| 157 | return 0; |
| 158 | |
| 159 | err: |
| 160 | memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : ""); |
| 161 | return cli_err(appctx, err); |
| 162 | } |
| 163 | |
| 164 | /* This function dumps the content of the httpclient receive buffer |
| 165 | * on the CLI output |
| 166 | * |
| 167 | * Return 1 when the processing is finished |
| 168 | * return 0 if it needs to be called again |
| 169 | */ |
| 170 | static int hc_cli_io_handler(struct appctx *appctx) |
| 171 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 172 | struct conn_stream *cs = appctx->owner; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 173 | struct buffer *trash = alloc_trash_chunk(); |
| 174 | struct httpclient *hc = appctx->ctx.cli.p0; |
| 175 | struct http_hdr *hdrs, *hdr; |
| 176 | |
| 177 | if (!trash) |
| 178 | goto out; |
| 179 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_STLINE) { |
William Lallemand | de6ecc3 | 2022-02-16 11:37:02 +0100 | [diff] [blame] | 180 | chunk_appendf(trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn), |
| 181 | hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 182 | if (ci_putchk(cs_ic(cs), trash) == -1) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 183 | cs_rx_room_blk(cs); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 184 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_STLINE; |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_HDR) { |
| 189 | hdrs = hc->res.hdrs; |
| 190 | for (hdr = hdrs; isttest(hdr->v); hdr++) { |
| 191 | if (!h1_format_htx_hdr(hdr->n, hdr->v, trash)) |
| 192 | goto out; |
| 193 | } |
| 194 | if (!chunk_memcat(trash, "\r\n", 2)) |
| 195 | goto out; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 196 | if (ci_putchk(cs_ic(cs), trash) == -1) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 197 | cs_rx_room_blk(cs); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 198 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_HDR; |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_BODY) { |
| 203 | int ret; |
| 204 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 205 | ret = httpclient_res_xfer(hc, cs_ib(cs)); |
| 206 | channel_add_input(cs_ic(cs), ret); /* forward what we put in the buffer channel */ |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 207 | |
William Lallemand | 518878e | 2021-09-21 10:45:34 +0200 | [diff] [blame] | 208 | if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */ |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 209 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_BODY; |
| 210 | } |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | /* we must close only if F_END is the last flag */ |
| 215 | if (appctx->ctx.cli.i0 == HC_CLI_F_RES_END) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 216 | cs_shutw(cs); |
| 217 | cs_shutr(cs); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 218 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_END; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | out: |
| 223 | /* we didn't clear every flags, we should come back to finish things */ |
| 224 | if (appctx->ctx.cli.i0) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 225 | cs_rx_room_blk(cs); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 226 | |
| 227 | free_trash_chunk(trash); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static void hc_cli_release(struct appctx *appctx) |
| 232 | { |
| 233 | struct httpclient *hc = appctx->ctx.cli.p0; |
| 234 | |
| 235 | /* Everything possible was printed on the CLI, we can destroy the client */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 236 | httpclient_stop_and_destroy(hc); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 237 | |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | /* register cli keywords */ |
| 242 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 2c8f984 | 2022-02-18 16:26:36 +0100 | [diff] [blame] | 243 | { { "httpclient", NULL }, "httpclient <method> <URI> : launch an HTTP request", hc_cli_parse, hc_cli_io_handler, hc_cli_release, NULL, ACCESS_EXPERT}, |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 244 | { { NULL }, NULL, NULL, NULL } |
| 245 | }}; |
| 246 | |
| 247 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 248 | |
| 249 | |
| 250 | /* --- This part of the file implements the actual HTTP client API --- */ |
| 251 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 252 | /* |
| 253 | * Generate a simple request and fill the httpclient request buffer with it. |
| 254 | * The request contains a request line generated from the absolute <url> and |
| 255 | * <meth> as well as list of headers <hdrs>. |
| 256 | * |
| 257 | * If the buffer was filled correctly the function returns 0, if not it returns |
| 258 | * an error_code but there is no guarantee that the buffer wasn't modified. |
| 259 | */ |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 260 | int 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 Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 261 | { |
| 262 | struct htx_sl *sl; |
| 263 | struct htx *htx; |
| 264 | int err_code = 0; |
| 265 | struct ist meth_ist, vsn; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 266 | unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 267 | int i; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 268 | int foundhost = 0, foundaccept = 0, foundua = 0; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 269 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 270 | if (!b_alloc(&hc->req.buf)) |
| 271 | goto error; |
| 272 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 273 | if (meth >= HTTP_METH_OTHER) |
| 274 | goto error; |
| 275 | |
| 276 | meth_ist = http_known_methods[meth]; |
| 277 | |
| 278 | vsn = ist("HTTP/1.1"); |
| 279 | |
| 280 | htx = htx_from_buf(&hc->req.buf); |
| 281 | if (!htx) |
| 282 | goto error; |
William Lallemand | e1e045f | 2022-01-14 14:08:34 +0100 | [diff] [blame] | 283 | |
| 284 | if (!hc->ops.req_payload && !isttest(payload)) |
| 285 | flags |= HTX_SL_F_BODYLESS; |
| 286 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 287 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn); |
| 288 | if (!sl) { |
| 289 | goto error; |
| 290 | } |
| 291 | sl->info.req.meth = meth; |
| 292 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 293 | for (i = 0; hdrs && hdrs[i].n.len; i++) { |
| 294 | /* Don't check the value length because a header value may be empty */ |
| 295 | if (isttest(hdrs[i].v) == 0) |
| 296 | continue; |
| 297 | |
| 298 | if (isteqi(hdrs[i].n, ist("host"))) |
| 299 | foundhost = 1; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 300 | else if (isteqi(hdrs[i].n, ist("accept"))) |
| 301 | foundaccept = 1; |
| 302 | else if (isteqi(hdrs[i].n, ist("user-agent"))) |
| 303 | foundua = 1; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 304 | |
| 305 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 306 | goto error; |
| 307 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 308 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 309 | if (!foundhost) { |
| 310 | /* Add Host Header from URL */ |
| 311 | if (!htx_add_header(htx, ist("Host"), ist("h"))) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 312 | goto error; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 313 | if (!http_update_host(htx, sl, url)) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 314 | goto error; |
| 315 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 316 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 317 | if (!foundaccept) { |
| 318 | if (!htx_add_header(htx, ist("Accept"), ist("*/*"))) |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | if (!foundua) { |
| 323 | if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT))) |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 328 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 329 | goto error; |
| 330 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 331 | if (isttest(payload)) { |
| 332 | /* add the payload if it can feat in the buffer, no need to set |
| 333 | * the Content-Length, the data will be sent chunked */ |
| 334 | if (!htx_add_data_atonce(htx, payload)) |
| 335 | goto error; |
| 336 | } |
| 337 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 338 | /* If req.payload was set, does not set the end of stream which *MUST* |
| 339 | * be set in the callback */ |
| 340 | if (!hc->ops.req_payload) |
| 341 | htx->flags |= HTX_FL_EOM; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 342 | |
| 343 | htx_to_buf(htx, &hc->req.buf); |
| 344 | |
| 345 | return 0; |
| 346 | error: |
| 347 | err_code |= ERR_ALERT | ERR_ABORT; |
| 348 | return err_code; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * transfer the response to the destination buffer and wakeup the HTTP client |
| 353 | * applet so it could fill again its buffer. |
| 354 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 355 | * Return the number of bytes transferred. |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 356 | */ |
| 357 | int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst) |
| 358 | { |
Willy Tarreau | 11adb1d | 2022-02-18 17:28:25 +0100 | [diff] [blame] | 359 | size_t room = b_room(dst); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 360 | int ret; |
| 361 | |
Willy Tarreau | 11adb1d | 2022-02-18 17:28:25 +0100 | [diff] [blame] | 362 | ret = b_force_xfer(dst, &hc->res.buf, MIN(room, b_data(&hc->res.buf))); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 363 | /* call the client once we consumed all data */ |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 364 | if (!b_data(&hc->res.buf)) { |
| 365 | b_free(&hc->res.buf); |
| 366 | if (hc->appctx) |
| 367 | appctx_wakeup(hc->appctx); |
| 368 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /* |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 373 | * Transfer raw HTTP payload from src, and insert it into HTX format in the |
| 374 | * httpclient. |
| 375 | * |
| 376 | * Must be used to transfer the request body. |
| 377 | * Then wakeup the httpclient so it can transfer it. |
| 378 | * |
| 379 | * <end> tries to add the ending data flag if it succeed to copy all data. |
| 380 | * |
| 381 | * Return the number of bytes copied from src. |
| 382 | */ |
| 383 | int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end) |
| 384 | { |
| 385 | int ret = 0; |
| 386 | struct htx *htx; |
| 387 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 388 | if (!b_alloc(&hc->req.buf)) |
| 389 | goto error; |
| 390 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 391 | htx = htx_from_buf(&hc->req.buf); |
| 392 | if (!htx) |
| 393 | goto error; |
| 394 | |
| 395 | if (hc->appctx) |
| 396 | appctx_wakeup(hc->appctx); |
| 397 | |
| 398 | ret += htx_add_data(htx, src); |
| 399 | |
| 400 | |
| 401 | /* if we copied all the data and the end flag is set */ |
| 402 | if ((istlen(src) == ret) && end) { |
| 403 | htx->flags |= HTX_FL_EOM; |
| 404 | } |
| 405 | htx_to_buf(htx, &hc->req.buf); |
| 406 | |
| 407 | error: |
| 408 | |
| 409 | return ret; |
| 410 | } |
| 411 | |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 412 | /* Set the 'timeout server' in ms for the next httpclient request */ |
| 413 | void httpclient_set_timeout(struct httpclient *hc, int timeout) |
| 414 | { |
| 415 | hc->timeout_server = timeout; |
| 416 | } |
| 417 | |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 418 | /* |
| 419 | * Sets a destination for the httpclient from an HAProxy addr format |
| 420 | * This will prevent to determine the destination from the URL |
| 421 | * Return 0 in case of success or -1 otherwise. |
| 422 | */ |
| 423 | int httpclient_set_dst(struct httpclient *hc, const char *dst) |
| 424 | { |
| 425 | struct sockaddr_storage *sk; |
| 426 | char *errmsg = NULL; |
| 427 | |
| 428 | sockaddr_free(&hc->dst); |
| 429 | /* 'sk' is statically allocated (no need to be freed). */ |
| 430 | sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL, |
| 431 | &errmsg, NULL, NULL, |
| 432 | PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT); |
| 433 | if (!sk) { |
| 434 | ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg); |
| 435 | free(errmsg); |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) { |
| 440 | ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 446 | |
| 447 | /* |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 448 | * Start the HTTP client |
| 449 | * Create the appctx, session, stream and wakeup the applet |
| 450 | * |
| 451 | * FIXME: It also fill the sockaddr with the IP address, but currently only IP |
| 452 | * in the URL are supported, it lacks a resolver. |
| 453 | * |
| 454 | * Return the <appctx> or NULL if it failed |
| 455 | */ |
| 456 | struct appctx *httpclient_start(struct httpclient *hc) |
| 457 | { |
| 458 | struct applet *applet = &httpclient_applet; |
| 459 | struct appctx *appctx; |
| 460 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 461 | struct conn_stream *cs; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 462 | struct stream *s; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 463 | struct sockaddr_storage *addr = NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 464 | int len; |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 465 | struct sockaddr_storage ss_url; |
| 466 | struct sockaddr_storage* ss_dst; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 467 | struct split_url out; |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 468 | enum obj_type *target = NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 469 | |
William Lallemand | 5085bc3 | 2022-02-17 12:52:09 +0100 | [diff] [blame] | 470 | /* if the client was started and not ended, an applet is already |
| 471 | * running, we shouldn't try anything */ |
| 472 | if (httpclient_started(hc) && !httpclient_ended(hc)) |
| 473 | return NULL; |
| 474 | |
| 475 | hc->flags = 0; |
| 476 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 477 | /* parse URI and fill sockaddr_storage */ |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 478 | len = url2sa(istptr(hc->req.url), istlen(hc->req.url), &ss_url, &out); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 479 | if (len == -1) { |
William Lallemand | 614e683 | 2021-09-26 18:12:43 +0200 | [diff] [blame] | 480 | ha_alert("httpclient: cannot parse uri '%s'.\n", istptr(hc->req.url)); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | /* The HTTP client will be created in the same thread as the caller, |
| 485 | * avoiding threading issues */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 486 | appctx = appctx_new(applet, NULL); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 487 | if (!appctx) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 488 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 489 | |
| 490 | sess = session_new(httpclient_proxy, NULL, &appctx->obj_type); |
| 491 | if (!sess) { |
| 492 | ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__); |
| 493 | goto out_free_appctx; |
| 494 | } |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 495 | |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 496 | /* choose the SSL server or not */ |
| 497 | switch (out.scheme) { |
| 498 | case SCH_HTTP: |
| 499 | target = &httpclient_srv_raw->obj_type; |
| 500 | break; |
| 501 | case SCH_HTTPS: |
| 502 | #ifdef USE_OPENSSL |
| 503 | if (httpclient_srv_ssl) { |
| 504 | target = &httpclient_srv_ssl->obj_type; |
| 505 | } else { |
| 506 | ha_alert("httpclient: SSL was disabled (wrong verify/ca-file)!\n"); |
| 507 | goto out_free_sess; |
| 508 | } |
| 509 | #else |
| 510 | ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__); |
| 511 | goto out_free_sess; |
| 512 | #endif |
| 513 | break; |
| 514 | } |
| 515 | |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 516 | /* if httpclient_set_dst() was used, sets the alternative address */ |
| 517 | if (hc->dst) |
| 518 | ss_dst = hc->dst; |
| 519 | else |
| 520 | ss_dst = &ss_url; |
| 521 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 522 | if (!sockaddr_alloc(&addr, ss_dst, sizeof(*hc->dst))) |
| 523 | goto out_free_sess; |
| 524 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 525 | cs = cs_new_from_applet(appctx->endp, sess, &hc->req.buf); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 526 | if (!cs) { |
| 527 | ha_alert("httpclient: Failed to initialize stream %s:%d.\n", __FUNCTION__, __LINE__); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 528 | goto out_free_addr; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 529 | } |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 530 | s = DISGUISE(cs_strm(cs)); |
| 531 | |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 532 | s->target = target; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 533 | /* set the "timeout server" */ |
| 534 | s->req.wto = hc->timeout_server; |
| 535 | s->res.rto = hc->timeout_server; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 536 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 537 | s->csb->dst = addr; |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 538 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 539 | s->flags |= SF_ASSIGNED|SF_ADDR_SET; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 540 | s->res.flags |= CF_READ_DONTWAIT; |
| 541 | |
| 542 | /* applet is waiting for data */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 543 | cs_cant_get(s->csf); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 544 | appctx_wakeup(appctx); |
| 545 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 546 | hc->appctx = appctx; |
William Lallemand | b8b1370 | 2021-09-28 12:15:37 +0200 | [diff] [blame] | 547 | hc->flags |= HTTPCLIENT_FS_STARTED; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 548 | appctx->ctx.httpclient.ptr = hc; |
Christopher Faulet | 6ced61d | 2022-01-12 15:27:41 +0100 | [diff] [blame] | 549 | |
| 550 | /* The request was transferred when the stream was created. So switch |
| 551 | * directly to REQ_BODY or RES_STLINE state |
| 552 | */ |
| 553 | appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 554 | |
| 555 | return appctx; |
| 556 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 557 | out_free_addr: |
| 558 | sockaddr_free(&addr); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 559 | out_free_sess: |
| 560 | session_free(sess); |
| 561 | out_free_appctx: |
| 562 | appctx_free(appctx); |
| 563 | out: |
| 564 | |
| 565 | return NULL; |
| 566 | } |
| 567 | |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 568 | /* |
| 569 | * This function tries to destroy the httpclient if it wasn't running. |
| 570 | * If it was running, stop the client and ask it to autodestroy itself. |
| 571 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 572 | * Once this function is used, all pointer sto the client must be removed |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 573 | * |
| 574 | */ |
| 575 | void httpclient_stop_and_destroy(struct httpclient *hc) |
| 576 | { |
| 577 | |
William Lallemand | b8b1370 | 2021-09-28 12:15:37 +0200 | [diff] [blame] | 578 | /* The httpclient was already stopped or never started, we can safely destroy it */ |
| 579 | if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) { |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 580 | httpclient_destroy(hc); |
| 581 | } else { |
| 582 | /* if the client wasn't stopped, ask for a stop and destroy */ |
| 583 | hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP); |
| 584 | if (hc->appctx) |
| 585 | appctx_wakeup(hc->appctx); |
| 586 | } |
| 587 | } |
| 588 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 589 | /* Free the httpclient */ |
| 590 | void httpclient_destroy(struct httpclient *hc) |
| 591 | { |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 592 | struct http_hdr *hdrs; |
| 593 | |
| 594 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 595 | if (!hc) |
| 596 | return; |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 597 | |
William Lallemand | 2a87900 | 2021-10-05 15:50:45 +0200 | [diff] [blame] | 598 | /* we should never destroy a client which was started but not stopped */ |
| 599 | BUG_ON(httpclient_started(hc) && !httpclient_ended(hc)); |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 600 | |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 601 | /* request */ |
| 602 | istfree(&hc->req.url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 603 | b_free(&hc->req.buf); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 604 | /* response */ |
| 605 | istfree(&hc->res.vsn); |
| 606 | istfree(&hc->res.reason); |
| 607 | hdrs = hc->res.hdrs; |
| 608 | while (hdrs && isttest(hdrs->n)) { |
| 609 | istfree(&hdrs->n); |
| 610 | istfree(&hdrs->v); |
| 611 | hdrs++; |
| 612 | } |
| 613 | ha_free(&hc->res.hdrs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 614 | b_free(&hc->res.buf); |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 615 | sockaddr_free(&hc->dst); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 616 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 617 | free(hc); |
| 618 | |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | /* Allocate an httpclient and its buffers |
| 623 | * Return NULL on failure */ |
| 624 | struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url) |
| 625 | { |
| 626 | struct httpclient *hc; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 627 | |
| 628 | hc = calloc(1, sizeof(*hc)); |
| 629 | if (!hc) |
| 630 | goto err; |
| 631 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 632 | hc->req.buf = BUF_NULL; |
| 633 | hc->res.buf = BUF_NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 634 | hc->caller = caller; |
William Lallemand | 67b7784 | 2021-11-10 16:57:25 +0100 | [diff] [blame] | 635 | hc->req.url = istdup(url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 636 | hc->req.meth = meth; |
| 637 | |
| 638 | return hc; |
| 639 | |
| 640 | err: |
| 641 | httpclient_destroy(hc); |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | static void httpclient_applet_io_handler(struct appctx *appctx) |
| 646 | { |
| 647 | struct httpclient *hc = appctx->ctx.httpclient.ptr; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 648 | struct conn_stream *cs = appctx->owner; |
| 649 | struct stream *s = __cs_strm(cs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 650 | struct channel *req = &s->req; |
| 651 | struct channel *res = &s->res; |
| 652 | struct htx_blk *blk = NULL; |
| 653 | struct htx *htx; |
William Lallemand | b702030 | 2021-08-20 11:24:13 +0200 | [diff] [blame] | 654 | struct htx_sl *sl = NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 655 | int32_t pos; |
| 656 | uint32_t hdr_num; |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 657 | uint32_t sz; |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 658 | int ret; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 659 | |
| 660 | |
| 661 | while (1) { |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 662 | |
| 663 | /* required to stop */ |
| 664 | if (hc->flags & HTTPCLIENT_FA_STOP) |
| 665 | goto end; |
| 666 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 667 | switch(appctx->st0) { |
| 668 | |
| 669 | case HTTPCLIENT_S_REQ: |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 670 | /* we know that the buffer is empty here, since |
| 671 | * it's the first call, we can freely copy the |
| 672 | * request from the httpclient buffer */ |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 673 | ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf)); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 674 | if (!ret) |
| 675 | goto more; |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 676 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 677 | if (!b_data(&hc->req.buf)) |
| 678 | b_free(&hc->req.buf); |
| 679 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 680 | htx = htx_from_buf(&req->buf); |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 681 | if (!htx) |
| 682 | goto more; |
| 683 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 684 | channel_add_input(req, htx->data); |
| 685 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 686 | if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */ |
| 687 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
| 688 | else |
| 689 | appctx->st0 = HTTPCLIENT_S_REQ_BODY; |
| 690 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 691 | goto more; /* we need to leave the IO handler once we wrote the request */ |
| 692 | break; |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 693 | case HTTPCLIENT_S_REQ_BODY: |
| 694 | /* call the payload callback */ |
| 695 | { |
| 696 | if (hc->ops.req_payload) { |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 697 | struct htx *hc_htx; |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 698 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 699 | /* call the request callback */ |
| 700 | hc->ops.req_payload(hc); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 701 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 702 | hc_htx = htx_from_buf(&hc->req.buf); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 703 | htx = htx_from_buf(&req->buf); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 704 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 705 | if (htx_is_empty(hc_htx)) |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 706 | goto more; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 707 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 708 | if (htx_is_empty(htx)) { |
Christopher Faulet | dca3b5b | 2022-04-07 10:47:07 +0200 | [diff] [blame] | 709 | size_t data = hc_htx->data; |
| 710 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 711 | /* Here htx_to_buf() will set buffer data to 0 because |
| 712 | * the HTX is empty, and allow us to do an xfer. |
| 713 | */ |
| 714 | htx_to_buf(hc_htx, &hc->req.buf); |
| 715 | htx_to_buf(htx, &req->buf); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 716 | b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf)); |
Christopher Faulet | dca3b5b | 2022-04-07 10:47:07 +0200 | [diff] [blame] | 717 | channel_add_input(req, data); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 718 | } else { |
| 719 | struct htx_ret ret; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 720 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 721 | ret = htx_xfer_blks(htx, hc_htx, hc_htx->data, HTX_BLK_UNUSED); |
| 722 | channel_add_input(req, ret.ret); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 723 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 724 | /* we must copy the EOM if we empty the buffer */ |
| 725 | if (htx_is_empty(hc_htx)) { |
| 726 | htx->flags |= (hc_htx->flags & HTX_FL_EOM); |
| 727 | } |
| 728 | htx_to_buf(htx, &req->buf); |
| 729 | htx_to_buf(hc_htx, &hc->req.buf); |
| 730 | } |
| 731 | |
| 732 | |
| 733 | if (!b_data(&hc->req.buf)) |
| 734 | b_free(&hc->req.buf); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 735 | } |
| 736 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 737 | htx = htx_from_buf(&req->buf); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 738 | if (!htx) |
| 739 | goto more; |
| 740 | |
| 741 | /* if the request contains the HTX_FL_EOM, we finished the request part. */ |
Christopher Faulet | 3d43324 | 2022-03-03 15:38:39 +0100 | [diff] [blame] | 742 | if (htx->flags & HTX_FL_EOM) { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 743 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | 3d43324 | 2022-03-03 15:38:39 +0100 | [diff] [blame] | 744 | req->flags |= CF_EOI; |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 745 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
Christopher Faulet | 3d43324 | 2022-03-03 15:38:39 +0100 | [diff] [blame] | 746 | } |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 747 | |
William Lallemand | 1eca894 | 2022-03-17 14:57:23 +0100 | [diff] [blame] | 748 | goto process_data; /* we need to leave the IO handler once we wrote the request */ |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 749 | } |
| 750 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 751 | |
| 752 | case HTTPCLIENT_S_RES_STLINE: |
| 753 | /* copy the start line in the hc structure,then remove the htx block */ |
William Lallemand | a625b03 | 2022-03-17 14:45:46 +0100 | [diff] [blame] | 754 | if (!co_data(res)) |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 755 | goto more; |
| 756 | htx = htxbuf(&res->buf); |
| 757 | if (!htx) |
| 758 | goto more; |
William Lallemand | 97f69c6 | 2022-03-10 17:23:40 +0100 | [diff] [blame] | 759 | blk = htx_get_head_blk(htx); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 760 | if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL)) |
| 761 | sl = htx_get_blk_ptr(htx, blk); |
| 762 | if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP))) |
| 763 | goto more; |
| 764 | |
| 765 | /* copy the status line in the httpclient */ |
| 766 | hc->res.status = sl->info.res.status; |
| 767 | hc->res.vsn = istdup(htx_sl_res_vsn(sl)); |
| 768 | hc->res.reason = istdup(htx_sl_res_reason(sl)); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 769 | sz = htx_get_blksz(blk); |
| 770 | co_set_data(res, co_data(res) - sz); |
| 771 | htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 772 | /* caller callback */ |
| 773 | if (hc->ops.res_stline) |
| 774 | hc->ops.res_stline(hc); |
| 775 | |
| 776 | /* if there is no HTX data anymore and the EOM flag is |
| 777 | * set, leave (no body) */ |
| 778 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) |
| 779 | appctx->st0 = HTTPCLIENT_S_RES_END; |
| 780 | else |
| 781 | appctx->st0 = HTTPCLIENT_S_RES_HDR; |
| 782 | break; |
| 783 | |
| 784 | case HTTPCLIENT_S_RES_HDR: |
| 785 | /* first copy the headers in a local hdrs |
| 786 | * structure, once we the total numbers of the |
| 787 | * header we allocate the right size and copy |
| 788 | * them. The htx block of the headers are |
| 789 | * removed each time one is read */ |
| 790 | { |
| 791 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 792 | |
William Lallemand | a625b03 | 2022-03-17 14:45:46 +0100 | [diff] [blame] | 793 | if (!co_data(res)) |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 794 | goto more; |
| 795 | htx = htxbuf(&res->buf); |
| 796 | if (!htx) |
| 797 | goto more; |
| 798 | |
| 799 | hdr_num = 0; |
| 800 | |
William Lallemand | 97f69c6 | 2022-03-10 17:23:40 +0100 | [diff] [blame] | 801 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 802 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 803 | enum htx_blk_type type = htx_get_blk_type(blk); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 804 | uint32_t sz = htx_get_blksz(blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 805 | |
William Lallemand | c020b25 | 2022-03-09 18:56:02 +0100 | [diff] [blame] | 806 | if (type == HTX_BLK_UNUSED) { |
| 807 | c_rew(res, sz); |
| 808 | htx_remove_blk(htx, blk); |
| 809 | } |
| 810 | |
| 811 | if (type == HTX_BLK_HDR) { |
| 812 | hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk)); |
| 813 | hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk)); |
| 814 | if (!isttest(hdrs[hdr_num].v) || !isttest(hdrs[hdr_num].n)) |
| 815 | goto end; |
| 816 | c_rew(res, sz); |
| 817 | htx_remove_blk(htx, blk); |
| 818 | hdr_num++; |
| 819 | } |
| 820 | |
| 821 | /* create a NULL end of array and leave the loop */ |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 822 | if (type == HTX_BLK_EOH) { |
| 823 | hdrs[hdr_num].n = IST_NULL; |
| 824 | hdrs[hdr_num].v = IST_NULL; |
William Lallemand | c020b25 | 2022-03-09 18:56:02 +0100 | [diff] [blame] | 825 | c_rew(res, sz); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 826 | htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 827 | break; |
| 828 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 829 | } |
| 830 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 831 | if (hdr_num) { |
| 832 | /* alloc and copy the headers in the httpclient struct */ |
| 833 | hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs)); |
| 834 | if (!hc->res.hdrs) |
| 835 | goto end; |
| 836 | memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 837 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 838 | /* caller callback */ |
| 839 | if (hc->ops.res_headers) |
| 840 | hc->ops.res_headers(hc); |
| 841 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 842 | |
| 843 | /* if there is no HTX data anymore and the EOM flag is |
| 844 | * set, leave (no body) */ |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 845 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 846 | appctx->st0 = HTTPCLIENT_S_RES_END; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 847 | } else { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 848 | appctx->st0 = HTTPCLIENT_S_RES_BODY; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 849 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 850 | } |
| 851 | break; |
| 852 | |
| 853 | case HTTPCLIENT_S_RES_BODY: |
| 854 | /* |
| 855 | * The IO handler removes the htx blocks in the response buffer and |
| 856 | * push them in the hc->res.buf buffer in a raw format. |
| 857 | */ |
William Lallemand | a625b03 | 2022-03-17 14:45:46 +0100 | [diff] [blame] | 858 | if (!co_data(res)) |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 859 | goto more; |
| 860 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 861 | htx = htxbuf(&res->buf); |
| 862 | if (!htx || htx_is_empty(htx)) |
| 863 | goto more; |
| 864 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 865 | if (!b_alloc(&hc->res.buf)) |
| 866 | goto more; |
| 867 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 868 | if (b_full(&hc->res.buf)) |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 869 | goto process_data; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 870 | |
| 871 | /* decapsule the htx data to raw data */ |
William Lallemand | 97f69c6 | 2022-03-10 17:23:40 +0100 | [diff] [blame] | 872 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 873 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 874 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 875 | size_t count = co_data(res); |
| 876 | uint32_t blksz = htx_get_blksz(blk); |
| 877 | uint32_t room = b_room(&hc->res.buf); |
| 878 | uint32_t vlen; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 879 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 880 | /* we should try to copy the maximum output data in a block, which fit |
| 881 | * the destination buffer */ |
| 882 | vlen = MIN(count, blksz); |
| 883 | vlen = MIN(vlen, room); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 884 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 885 | if (vlen == 0) |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 886 | goto process_data; |
| 887 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 888 | if (type == HTX_BLK_DATA) { |
| 889 | struct ist v = htx_get_blk_value(htx, blk); |
| 890 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 891 | __b_putblk(&hc->res.buf, v.ptr, vlen); |
| 892 | c_rew(res, vlen); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 893 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 894 | if (vlen == blksz) |
| 895 | htx_remove_blk(htx, blk); |
| 896 | else |
| 897 | htx_cut_data_blk(htx, blk, vlen); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 898 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 899 | /* the data must be processed by the caller in the receive phase */ |
| 900 | if (hc->ops.res_payload) |
| 901 | hc->ops.res_payload(hc); |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 902 | |
| 903 | /* cannot copy everything, need to processs */ |
| 904 | if (vlen != blksz) |
| 905 | goto process_data; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 906 | } else { |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 907 | if (vlen != blksz) |
| 908 | goto process_data; |
| 909 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 910 | /* remove any block which is not a data block */ |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 911 | c_rew(res, blksz); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 912 | htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 913 | } |
| 914 | } |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 915 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 916 | /* if not finished, should be called again */ |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 917 | if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))) |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 918 | goto more; |
| 919 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 920 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 921 | /* end of message, we should quit */ |
| 922 | appctx->st0 = HTTPCLIENT_S_RES_END; |
| 923 | break; |
| 924 | |
| 925 | case HTTPCLIENT_S_RES_END: |
| 926 | goto end; |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | process_data: |
| 932 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 933 | cs_rx_chan_rdy(cs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 934 | |
| 935 | return; |
| 936 | more: |
| 937 | /* There was not enough data in the response channel */ |
| 938 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 939 | cs_rx_room_blk(cs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 940 | |
| 941 | if (appctx->st0 == HTTPCLIENT_S_RES_END) |
| 942 | goto end; |
| 943 | |
| 944 | /* The state machine tries to handle as much data as possible, if there |
| 945 | * isn't any data to handle and a shutdown is detected, let's stop |
| 946 | * everything */ |
| 947 | if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) || |
William Lallemand | 58a81ae | 2022-03-17 15:14:15 +0100 | [diff] [blame] | 948 | (res->flags & CF_SHUTW) || |
| 949 | ((res->flags & CF_SHUTW_NOW) && channel_is_empty(res))) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 950 | goto end; |
| 951 | } |
| 952 | return; |
| 953 | |
| 954 | end: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 955 | cs_shutw(cs); |
| 956 | cs_shutr(cs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 957 | return; |
| 958 | } |
| 959 | |
| 960 | static void httpclient_applet_release(struct appctx *appctx) |
| 961 | { |
| 962 | struct httpclient *hc = appctx->ctx.httpclient.ptr; |
| 963 | |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 964 | /* mark the httpclient as ended */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 965 | hc->flags |= HTTPCLIENT_FS_ENDED; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 966 | /* the applet is leaving, remove the ptr so we don't try to call it |
| 967 | * again from the caller */ |
| 968 | hc->appctx = NULL; |
| 969 | |
William Lallemand | eb0d4c4 | 2022-04-06 14:12:37 +0200 | [diff] [blame] | 970 | if (hc->ops.res_end) |
| 971 | hc->ops.res_end(hc); |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 972 | |
| 973 | /* destroy the httpclient when set to autotokill */ |
| 974 | if (hc->flags & HTTPCLIENT_FA_AUTOKILL) { |
| 975 | httpclient_destroy(hc); |
| 976 | } |
| 977 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 978 | return; |
| 979 | } |
| 980 | |
| 981 | /* HTTP client applet */ |
| 982 | static struct applet httpclient_applet = { |
| 983 | .obj_type = OBJ_TYPE_APPLET, |
| 984 | .name = "<HTTPCLIENT>", |
| 985 | .fct = httpclient_applet_io_handler, |
| 986 | .release = httpclient_applet_release, |
| 987 | }; |
| 988 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 989 | /* |
| 990 | * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP, |
| 991 | * the other for HTTPS. |
| 992 | */ |
William Lallemand | 2c8b084 | 2022-04-22 15:16:09 +0200 | [diff] [blame] | 993 | static int httpclient_precheck() |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 994 | { |
| 995 | int err_code = 0; |
| 996 | char *errmsg = NULL; |
| 997 | |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 998 | if (global.mode & MODE_MWORKER_WAIT) |
| 999 | return 0; |
| 1000 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1001 | httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg); |
| 1002 | if (!httpclient_proxy) { |
| 1003 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1004 | goto err; |
| 1005 | } |
| 1006 | |
Willy Tarreau | 0e72e40 | 2021-08-20 10:23:12 +0200 | [diff] [blame] | 1007 | proxy_preset_defaults(httpclient_proxy); |
| 1008 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 1009 | httpclient_proxy->options |= PR_O_WREQ_BODY; |
William Lallemand | 71abad0 | 2022-03-17 15:24:28 +0100 | [diff] [blame] | 1010 | httpclient_proxy->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED | PR_RE_TIMEOUT; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1011 | httpclient_proxy->options2 |= PR_O2_INDEPSTR; |
| 1012 | httpclient_proxy->mode = PR_MODE_HTTP; |
| 1013 | httpclient_proxy->maxconn = 0; |
| 1014 | httpclient_proxy->accept = NULL; |
William Lallemand | 71abad0 | 2022-03-17 15:24:28 +0100 | [diff] [blame] | 1015 | httpclient_proxy->conn_retries = CONN_RETRIES; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1016 | httpclient_proxy->timeout.client = TICK_ETERNITY; |
| 1017 | /* The HTTP Client use the "option httplog" with the global log server */ |
| 1018 | httpclient_proxy->conf.logformat_string = default_http_log_format; |
| 1019 | httpclient_proxy->http_needed = 1; |
| 1020 | |
| 1021 | /* clear HTTP server */ |
| 1022 | httpclient_srv_raw = new_server(httpclient_proxy); |
| 1023 | if (!httpclient_srv_raw) { |
| 1024 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1025 | memprintf(&errmsg, "out of memory."); |
| 1026 | goto err; |
| 1027 | } |
| 1028 | |
| 1029 | httpclient_srv_raw->iweight = 0; |
| 1030 | httpclient_srv_raw->uweight = 0; |
| 1031 | httpclient_srv_raw->xprt = xprt_get(XPRT_RAW); |
| 1032 | httpclient_srv_raw->id = strdup("<HTTPCLIENT>"); |
| 1033 | if (!httpclient_srv_raw->id) |
| 1034 | goto err; |
| 1035 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1036 | #ifdef USE_OPENSSL |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1037 | /* SSL HTTP server */ |
| 1038 | httpclient_srv_ssl = new_server(httpclient_proxy); |
| 1039 | if (!httpclient_srv_ssl) { |
| 1040 | memprintf(&errmsg, "out of memory."); |
| 1041 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1042 | goto err; |
| 1043 | } |
| 1044 | httpclient_srv_ssl->iweight = 0; |
| 1045 | httpclient_srv_ssl->uweight = 0; |
| 1046 | httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL); |
| 1047 | httpclient_srv_ssl->use_ssl = 1; |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 1048 | httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>"); |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1049 | if (!httpclient_srv_ssl->id) |
| 1050 | goto err; |
| 1051 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1052 | httpclient_srv_ssl->ssl_ctx.verify = httpclient_ssl_verify; |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1053 | /* if the verify is required, try to load the system CA */ |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1054 | if (httpclient_ssl_verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 1055 | httpclient_srv_ssl->ssl_ctx.ca_file = strdup("@system-ca"); |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1056 | if (!ssl_store_load_locations_file(httpclient_srv_ssl->ssl_ctx.ca_file, 1, CAFILE_CERT)) { |
| 1057 | ha_warning("httpclient: cannot initialize SSL verify with 'ca-file \"%s\"'. Disabling SSL.\n", httpclient_srv_ssl->ssl_ctx.ca_file); |
| 1058 | ha_free(&httpclient_srv_ssl->ssl_ctx.ca_file); |
| 1059 | srv_drop(httpclient_srv_ssl); |
| 1060 | httpclient_srv_ssl = NULL; |
| 1061 | } |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1062 | } |
William Lallemand | cf5cb0b | 2022-04-22 14:48:45 +0200 | [diff] [blame] | 1063 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1064 | #endif |
William Lallemand | cfcbe9e | 2021-08-24 17:15:58 +0200 | [diff] [blame] | 1065 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 1066 | /* add the proxy in the proxy list only if everything is successful */ |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1067 | httpclient_proxy->next = proxies_list; |
| 1068 | proxies_list = httpclient_proxy; |
| 1069 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 1070 | /* link the 2 servers in the proxy */ |
| 1071 | httpclient_srv_raw->next = httpclient_proxy->srv; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1072 | httpclient_proxy->srv = httpclient_srv_raw; |
| 1073 | |
| 1074 | #ifdef USE_OPENSSL |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1075 | if (httpclient_srv_ssl) { |
| 1076 | httpclient_srv_ssl->next = httpclient_proxy->srv; |
| 1077 | httpclient_proxy->srv = httpclient_srv_ssl; |
| 1078 | } |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1079 | #endif |
| 1080 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 1081 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1082 | return 0; |
| 1083 | |
| 1084 | err: |
| 1085 | ha_alert("httpclient: cannot initialize.\n"); |
| 1086 | free(errmsg); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1087 | srv_drop(httpclient_srv_raw); |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1088 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1089 | srv_drop(httpclient_srv_ssl); |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1090 | #endif |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1091 | free_proxy(httpclient_proxy); |
| 1092 | return err_code; |
| 1093 | } |
| 1094 | |
William Lallemand | 2c8b084 | 2022-04-22 15:16:09 +0200 | [diff] [blame] | 1095 | static int httpclient_postcheck() |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1096 | { |
| 1097 | struct logsrv *logsrv; |
| 1098 | struct proxy *curproxy = httpclient_proxy; |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1099 | char *errmsg = NULL; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1100 | |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 1101 | if (global.mode & MODE_MWORKER_WAIT) |
| 1102 | return 0; |
| 1103 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1104 | /* copy logs from "global" log list */ |
| 1105 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
| 1106 | struct logsrv *node = malloc(sizeof(*node)); |
| 1107 | |
| 1108 | if (!node) { |
| 1109 | ha_alert("httpclient: cannot allocate memory.\n"); |
| 1110 | goto err; |
| 1111 | } |
| 1112 | |
| 1113 | memcpy(node, logsrv, sizeof(*node)); |
| 1114 | LIST_INIT(&node->list); |
| 1115 | LIST_APPEND(&curproxy->logsrvs, &node->list); |
Willy Tarreau | e1e9f6b | 2022-04-21 14:14:28 +0200 | [diff] [blame] | 1116 | node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL; |
| 1117 | node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1118 | } |
| 1119 | if (curproxy->conf.logformat_string) { |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1120 | curproxy->conf.args.ctx = ARGC_LOG; |
| 1121 | if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat, |
| 1122 | LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES, |
William Lallemand | 715c101 | 2022-03-16 16:39:23 +0100 | [diff] [blame] | 1123 | SMP_VAL_FE_LOG_END, &errmsg)) { |
| 1124 | ha_alert("httpclient: failed to parse log-format : %s.\n", errmsg); |
| 1125 | free(errmsg); |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1126 | goto err; |
| 1127 | } |
| 1128 | curproxy->conf.args.file = NULL; |
| 1129 | curproxy->conf.args.line = 0; |
| 1130 | } |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1131 | |
| 1132 | #ifdef USE_OPENSSL |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1133 | if (httpclient_srv_ssl) { |
William Lallemand | 715c101 | 2022-03-16 16:39:23 +0100 | [diff] [blame] | 1134 | int err_code = 0; |
| 1135 | |
| 1136 | /* init the SNI expression */ |
| 1137 | /* always use the host header as SNI, without the port */ |
| 1138 | httpclient_srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)"); |
| 1139 | err_code |= server_parse_sni_expr(httpclient_srv_ssl, httpclient_proxy, &errmsg); |
| 1140 | if (err_code & ERR_CODE) { |
| 1141 | ha_alert("httpclient: failed to configure sni: %s.\n", errmsg); |
| 1142 | free(errmsg); |
| 1143 | goto err; |
| 1144 | } |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1145 | } |
| 1146 | #endif |
| 1147 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1148 | return 0; |
| 1149 | err: |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1153 | /* initialize the proxy and servers for the HTTP client */ |
| 1154 | |
William Lallemand | 2c8b084 | 2022-04-22 15:16:09 +0200 | [diff] [blame] | 1155 | REGISTER_PRE_CHECK(httpclient_precheck); |
| 1156 | REGISTER_POST_CHECK(httpclient_postcheck); |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1157 | |
| 1158 | #ifdef USE_OPENSSL |
| 1159 | static int httpclient_parse_global_verify(char **args, int section_type, struct proxy *curpx, |
| 1160 | const struct proxy *defpx, const char *file, int line, |
| 1161 | char **err) |
| 1162 | { |
| 1163 | if (too_many_args(1, args, err, NULL)) |
| 1164 | return -1; |
| 1165 | |
| 1166 | if (strcmp(args[1],"none") == 0) |
| 1167 | httpclient_ssl_verify = SSL_SERVER_VERIFY_NONE; |
| 1168 | else if (strcmp(args[1],"required") == 0) |
| 1169 | httpclient_ssl_verify = SSL_SERVER_VERIFY_REQUIRED; |
| 1170 | else { |
| 1171 | ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, line, args[0]); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1179 | { CFG_GLOBAL, "httpclient-ssl-verify", httpclient_parse_global_verify }, |
| 1180 | { 0, NULL, NULL }, |
| 1181 | }}; |
| 1182 | |
| 1183 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1184 | #endif |