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> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 21 | #include <haproxy/global.h> |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 22 | #include <haproxy/istbuf.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 23 | #include <haproxy/h1_htx.h> |
| 24 | #include <haproxy/http.h> |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 25 | #include <haproxy/http_ana-t.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 26 | #include <haproxy/http_client.h> |
| 27 | #include <haproxy/http_htx.h> |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 28 | #include <haproxy/http_rules.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 29 | #include <haproxy/htx.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
| 31 | #include <haproxy/proxy.h> |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 32 | #include <haproxy/resolvers.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 33 | #include <haproxy/sc_strm.h> |
William Lallemand | 2a8fe8b | 2021-08-20 14:25:15 +0200 | [diff] [blame] | 34 | #include <haproxy/server.h> |
Willy Tarreau | df3231c | 2022-09-02 09:02:21 +0200 | [diff] [blame] | 35 | #include <haproxy/ssl_sock.h> |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 36 | #include <haproxy/sock_inet.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 37 | #include <haproxy/stconn.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 38 | #include <haproxy/tools.h> |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 42 | static struct proxy *httpclient_proxy; |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 43 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 44 | #ifdef USE_OPENSSL |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 45 | /* if the httpclient is not configured, error are ignored and features are limited */ |
| 46 | static int hard_error_ssl = 0; |
William Lallemand | f1344b3 | 2022-04-26 12:00:06 +0200 | [diff] [blame] | 47 | static int httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED; |
William Lallemand | 683fbb8 | 2022-05-04 15:43:01 +0200 | [diff] [blame] | 48 | static char *httpclient_ssl_ca_file = NULL; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 49 | #endif |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 50 | static struct applet httpclient_applet; |
| 51 | |
William Lallemand | 7c5a7ef | 2022-05-04 15:59:44 +0200 | [diff] [blame] | 52 | /* if the httpclient is not configured, error are ignored and features are limited */ |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 53 | static int hard_error_resolvers = 0; |
| 54 | static char *resolvers_id = NULL; |
William Lallemand | 7c5a7ef | 2022-05-04 15:59:44 +0200 | [diff] [blame] | 55 | static char *resolvers_prefer = NULL; |
William Lallemand | e279f59 | 2023-05-11 21:08:38 +0200 | [diff] [blame] | 56 | static int resolvers_disabled = 0; |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 57 | |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 58 | static int httpclient_retries = CONN_RETRIES; |
William Lallemand | b9ed157 | 2023-09-05 16:42:27 +0200 | [diff] [blame] | 59 | static int httpclient_timeout_connect = MS_TO_TICKS(5000); |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 60 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 61 | /* --- This part of the file implement an HTTP client over the CLI --- |
| 62 | * The functions will be starting by "hc_cli" for "httpclient cli" |
| 63 | */ |
| 64 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 65 | /* the CLI context for the httpclient command */ |
| 66 | struct hcli_svc_ctx { |
| 67 | struct httpclient *hc; /* the httpclient instance */ |
| 68 | uint flags; /* flags from HC_CLI_F_* above */ |
| 69 | }; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 70 | |
| 71 | /* These are the callback used by the HTTP Client when it needs to notify new |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 72 | * data, we only sets a flag in the IO handler via the svcctx. |
| 73 | */ |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 74 | void hc_cli_res_stline_cb(struct httpclient *hc) |
| 75 | { |
| 76 | struct appctx *appctx = hc->caller; |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 77 | struct hcli_svc_ctx *ctx; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 78 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 79 | if (!appctx) |
| 80 | return; |
| 81 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 82 | ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 83 | ctx->flags |= HC_F_RES_STLINE; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 84 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void hc_cli_res_headers_cb(struct httpclient *hc) |
| 88 | { |
| 89 | struct appctx *appctx = hc->caller; |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 90 | struct hcli_svc_ctx *ctx; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 91 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 92 | if (!appctx) |
| 93 | return; |
| 94 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 95 | ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 96 | ctx->flags |= HC_F_RES_HDR; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 97 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void hc_cli_res_body_cb(struct httpclient *hc) |
| 101 | { |
| 102 | struct appctx *appctx = hc->caller; |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 103 | struct hcli_svc_ctx *ctx; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 104 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 105 | if (!appctx) |
| 106 | return; |
| 107 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 108 | ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 109 | ctx->flags |= HC_F_RES_BODY; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 110 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void hc_cli_res_end_cb(struct httpclient *hc) |
| 114 | { |
| 115 | struct appctx *appctx = hc->caller; |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 116 | struct hcli_svc_ctx *ctx; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 117 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 118 | if (!appctx) |
| 119 | return; |
| 120 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 121 | ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 122 | ctx->flags |= HC_F_RES_END; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 123 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Parse an httpclient keyword on the cli: |
| 128 | * httpclient <ID> <method> <URI> |
| 129 | */ |
| 130 | static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private) |
| 131 | { |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 132 | struct hcli_svc_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 133 | struct httpclient *hc; |
| 134 | char *err = NULL; |
| 135 | enum http_meth_t meth; |
| 136 | char *meth_str; |
| 137 | struct ist uri; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 138 | struct ist body = IST_NULL; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 139 | |
| 140 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 141 | return 1; |
| 142 | |
| 143 | if (!*args[1] || !*args[2]) { |
| 144 | memprintf(&err, ": not enough parameters"); |
| 145 | goto err; |
| 146 | } |
| 147 | |
| 148 | meth_str = args[1]; |
| 149 | uri = ist(args[2]); |
| 150 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 151 | if (payload) |
| 152 | body = ist(payload); |
| 153 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 154 | meth = find_http_meth(meth_str, strlen(meth_str)); |
| 155 | |
| 156 | hc = httpclient_new(appctx, meth, uri); |
| 157 | if (!hc) { |
| 158 | goto err; |
| 159 | } |
| 160 | |
| 161 | /* update the httpclient callbacks */ |
| 162 | hc->ops.res_stline = hc_cli_res_stline_cb; |
| 163 | hc->ops.res_headers = hc_cli_res_headers_cb; |
| 164 | hc->ops.res_payload = hc_cli_res_body_cb; |
| 165 | hc->ops.res_end = hc_cli_res_end_cb; |
| 166 | |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 167 | ctx->hc = hc; /* store the httpclient ptr in the applet */ |
| 168 | ctx->flags = 0; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 169 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 170 | 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] | 171 | goto err; |
| 172 | |
| 173 | |
| 174 | if (!httpclient_start(hc)) |
| 175 | goto err; |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | err: |
| 180 | memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : ""); |
| 181 | return cli_err(appctx, err); |
| 182 | } |
| 183 | |
| 184 | /* This function dumps the content of the httpclient receive buffer |
| 185 | * on the CLI output |
| 186 | * |
| 187 | * Return 1 when the processing is finished |
| 188 | * return 0 if it needs to be called again |
| 189 | */ |
| 190 | static int hc_cli_io_handler(struct appctx *appctx) |
| 191 | { |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 192 | struct hcli_svc_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 193 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 194 | struct httpclient *hc = ctx->hc; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 195 | struct http_hdr *hdrs, *hdr; |
| 196 | |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 197 | if (ctx->flags & HC_F_RES_STLINE) { |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 198 | chunk_printf(&trash, "%.*s %d %.*s\n", (unsigned int)istlen(hc->res.vsn), istptr(hc->res.vsn), |
| 199 | hc->res.status, (unsigned int)istlen(hc->res.reason), istptr(hc->res.reason)); |
| 200 | if (applet_putchk(appctx, &trash) == -1) |
| 201 | goto more; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 202 | ctx->flags &= ~HC_F_RES_STLINE; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 205 | if (ctx->flags & HC_F_RES_HDR) { |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 206 | chunk_reset(&trash); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 207 | hdrs = hc->res.hdrs; |
| 208 | for (hdr = hdrs; isttest(hdr->v); hdr++) { |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 209 | if (!h1_format_htx_hdr(hdr->n, hdr->v, &trash)) |
| 210 | goto too_many_hdrs; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 211 | } |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 212 | if (!chunk_memcat(&trash, "\r\n", 2)) |
| 213 | goto too_many_hdrs; |
| 214 | if (applet_putchk(appctx, &trash) == -1) |
| 215 | goto more; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 216 | ctx->flags &= ~HC_F_RES_HDR; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 219 | if (ctx->flags & HC_F_RES_BODY) { |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 220 | int ret; |
| 221 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 222 | ret = httpclient_res_xfer(hc, sc_ib(sc)); |
| 223 | channel_add_input(sc_ic(sc), ret); /* forward what we put in the buffer channel */ |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 224 | |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 225 | /* remove the flag if the buffer was emptied */ |
| 226 | if (httpclient_data(hc)) |
| 227 | goto more; |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 228 | ctx->flags &= ~HC_F_RES_BODY; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* we must close only if F_END is the last flag */ |
Remi Tricot-Le Breton | 95e7cf1 | 2022-12-20 11:11:03 +0100 | [diff] [blame] | 232 | if (ctx->flags == HC_F_RES_END) { |
| 233 | ctx->flags &= ~HC_F_RES_END; |
Christopher Faulet | 89f2626 | 2022-06-01 17:17:24 +0200 | [diff] [blame] | 234 | goto end; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 237 | more: |
| 238 | if (!ctx->flags) |
| 239 | applet_have_no_more_data(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 240 | return 0; |
Christopher Faulet | 89f2626 | 2022-06-01 17:17:24 +0200 | [diff] [blame] | 241 | end: |
| 242 | return 1; |
Christopher Faulet | 0158bb2 | 2022-06-01 17:08:19 +0200 | [diff] [blame] | 243 | |
| 244 | too_many_hdrs: |
| 245 | return cli_err(appctx, "Too many headers.\n"); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void hc_cli_release(struct appctx *appctx) |
| 249 | { |
Willy Tarreau | 89a7c41 | 2022-05-05 19:38:21 +0200 | [diff] [blame] | 250 | struct hcli_svc_ctx *ctx = appctx->svcctx; |
| 251 | struct httpclient *hc = ctx->hc; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 252 | |
| 253 | /* Everything possible was printed on the CLI, we can destroy the client */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 254 | httpclient_stop_and_destroy(hc); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 255 | |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | /* register cli keywords */ |
| 260 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 2c8f984 | 2022-02-18 16:26:36 +0100 | [diff] [blame] | 261 | { { "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] | 262 | { { NULL }, NULL, NULL, NULL } |
| 263 | }}; |
| 264 | |
| 265 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 266 | |
| 267 | |
| 268 | /* --- This part of the file implements the actual HTTP client API --- */ |
| 269 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 270 | /* |
| 271 | * Generate a simple request and fill the httpclient request buffer with it. |
| 272 | * The request contains a request line generated from the absolute <url> and |
| 273 | * <meth> as well as list of headers <hdrs>. |
| 274 | * |
| 275 | * If the buffer was filled correctly the function returns 0, if not it returns |
| 276 | * an error_code but there is no guarantee that the buffer wasn't modified. |
| 277 | */ |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 278 | 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] | 279 | { |
| 280 | struct htx_sl *sl; |
| 281 | struct htx *htx; |
| 282 | int err_code = 0; |
| 283 | struct ist meth_ist, vsn; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 284 | 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] | 285 | int i; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 286 | int foundhost = 0, foundaccept = 0, foundua = 0; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 287 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 288 | if (!b_alloc(&hc->req.buf)) |
| 289 | goto error; |
| 290 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 291 | if (meth >= HTTP_METH_OTHER) |
| 292 | goto error; |
| 293 | |
| 294 | meth_ist = http_known_methods[meth]; |
| 295 | |
| 296 | vsn = ist("HTTP/1.1"); |
| 297 | |
| 298 | htx = htx_from_buf(&hc->req.buf); |
| 299 | if (!htx) |
| 300 | goto error; |
William Lallemand | e1e045f | 2022-01-14 14:08:34 +0100 | [diff] [blame] | 301 | |
| 302 | if (!hc->ops.req_payload && !isttest(payload)) |
| 303 | flags |= HTX_SL_F_BODYLESS; |
| 304 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 305 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn); |
| 306 | if (!sl) { |
| 307 | goto error; |
| 308 | } |
| 309 | sl->info.req.meth = meth; |
| 310 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 311 | for (i = 0; hdrs && hdrs[i].n.len; i++) { |
| 312 | /* Don't check the value length because a header value may be empty */ |
| 313 | if (isttest(hdrs[i].v) == 0) |
| 314 | continue; |
| 315 | |
| 316 | if (isteqi(hdrs[i].n, ist("host"))) |
| 317 | foundhost = 1; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 318 | else if (isteqi(hdrs[i].n, ist("accept"))) |
| 319 | foundaccept = 1; |
| 320 | else if (isteqi(hdrs[i].n, ist("user-agent"))) |
| 321 | foundua = 1; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 322 | |
| 323 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 324 | goto error; |
| 325 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 326 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 327 | if (!foundhost) { |
| 328 | /* Add Host Header from URL */ |
| 329 | if (!htx_add_header(htx, ist("Host"), ist("h"))) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 330 | goto error; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 331 | if (!http_update_host(htx, sl, url)) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 332 | goto error; |
| 333 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 334 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 335 | if (!foundaccept) { |
| 336 | if (!htx_add_header(htx, ist("Accept"), ist("*/*"))) |
| 337 | goto error; |
| 338 | } |
| 339 | |
| 340 | if (!foundua) { |
| 341 | if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT))) |
| 342 | goto error; |
| 343 | } |
| 344 | |
| 345 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 346 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 347 | goto error; |
| 348 | |
William Lallemand | a80b22e | 2022-12-22 14:49:43 +0100 | [diff] [blame] | 349 | if (isttest(payload) && istlen(payload)) { |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 350 | /* add the payload if it can feat in the buffer, no need to set |
| 351 | * the Content-Length, the data will be sent chunked */ |
| 352 | if (!htx_add_data_atonce(htx, payload)) |
| 353 | goto error; |
| 354 | } |
| 355 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 356 | /* If req.payload was set, does not set the end of stream which *MUST* |
| 357 | * be set in the callback */ |
| 358 | if (!hc->ops.req_payload) |
| 359 | htx->flags |= HTX_FL_EOM; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 360 | |
| 361 | htx_to_buf(htx, &hc->req.buf); |
| 362 | |
| 363 | return 0; |
| 364 | error: |
| 365 | err_code |= ERR_ALERT | ERR_ABORT; |
| 366 | return err_code; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * transfer the response to the destination buffer and wakeup the HTTP client |
| 371 | * applet so it could fill again its buffer. |
| 372 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 373 | * Return the number of bytes transferred. |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 374 | */ |
| 375 | int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst) |
| 376 | { |
Willy Tarreau | 11adb1d | 2022-02-18 17:28:25 +0100 | [diff] [blame] | 377 | size_t room = b_room(dst); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 378 | int ret; |
| 379 | |
Willy Tarreau | 11adb1d | 2022-02-18 17:28:25 +0100 | [diff] [blame] | 380 | 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] | 381 | /* call the client once we consumed all data */ |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 382 | if (!b_data(&hc->res.buf)) { |
| 383 | b_free(&hc->res.buf); |
| 384 | if (hc->appctx) |
| 385 | appctx_wakeup(hc->appctx); |
| 386 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 391 | * Transfer raw HTTP payload from src, and insert it into HTX format in the |
| 392 | * httpclient. |
| 393 | * |
| 394 | * Must be used to transfer the request body. |
| 395 | * Then wakeup the httpclient so it can transfer it. |
| 396 | * |
| 397 | * <end> tries to add the ending data flag if it succeed to copy all data. |
| 398 | * |
| 399 | * Return the number of bytes copied from src. |
| 400 | */ |
| 401 | int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end) |
| 402 | { |
| 403 | int ret = 0; |
| 404 | struct htx *htx; |
| 405 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 406 | if (!b_alloc(&hc->req.buf)) |
| 407 | goto error; |
| 408 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 409 | htx = htx_from_buf(&hc->req.buf); |
| 410 | if (!htx) |
| 411 | goto error; |
| 412 | |
| 413 | if (hc->appctx) |
| 414 | appctx_wakeup(hc->appctx); |
| 415 | |
| 416 | ret += htx_add_data(htx, src); |
| 417 | |
| 418 | |
| 419 | /* if we copied all the data and the end flag is set */ |
| 420 | if ((istlen(src) == ret) && end) { |
Christopher Faulet | 48005de | 2022-10-14 15:10:24 +0200 | [diff] [blame] | 421 | /* no more data are expected. If the HTX buffer is empty, be |
| 422 | * sure to add something (EOT block in this case) to have |
| 423 | * something to send. It is important to be sure the EOM flags |
| 424 | * will be handled by the endpoint. Because the message is |
| 425 | * empty, this should not fail. Otherwise it is an error |
| 426 | */ |
| 427 | if (htx_is_empty(htx)) { |
| 428 | if (!htx_add_endof(htx, HTX_BLK_EOT)) |
| 429 | goto error; |
| 430 | } |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 431 | htx->flags |= HTX_FL_EOM; |
| 432 | } |
| 433 | htx_to_buf(htx, &hc->req.buf); |
| 434 | |
| 435 | error: |
| 436 | |
| 437 | return ret; |
| 438 | } |
| 439 | |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 440 | /* Set the 'timeout server' in ms for the next httpclient request */ |
| 441 | void httpclient_set_timeout(struct httpclient *hc, int timeout) |
| 442 | { |
| 443 | hc->timeout_server = timeout; |
| 444 | } |
| 445 | |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 446 | /* |
| 447 | * Sets a destination for the httpclient from an HAProxy addr format |
| 448 | * This will prevent to determine the destination from the URL |
| 449 | * Return 0 in case of success or -1 otherwise. |
| 450 | */ |
| 451 | int httpclient_set_dst(struct httpclient *hc, const char *dst) |
| 452 | { |
| 453 | struct sockaddr_storage *sk; |
| 454 | char *errmsg = NULL; |
| 455 | |
| 456 | sockaddr_free(&hc->dst); |
| 457 | /* 'sk' is statically allocated (no need to be freed). */ |
| 458 | sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL, |
| 459 | &errmsg, NULL, NULL, |
| 460 | PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT); |
| 461 | if (!sk) { |
| 462 | ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg); |
| 463 | free(errmsg); |
| 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | if (!sockaddr_alloc(&hc->dst, sk, sizeof(*sk))) { |
| 468 | ha_alert("httpclient: Failed to allocate sockaddr in %s:%d.\n", __FUNCTION__, __LINE__); |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 474 | |
| 475 | /* |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 476 | * Split <url> in <scheme>, <host>, <port> |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 477 | */ |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 478 | static int httpclient_spliturl(struct ist url, enum http_scheme *scheme, |
| 479 | struct ist *host, int *port) |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 480 | { |
| 481 | enum http_scheme scheme_tmp = SCH_HTTP; |
| 482 | int port_tmp = 0; |
| 483 | struct ist scheme_ist, authority_ist, host_ist, port_ist; |
| 484 | char *p, *end; |
| 485 | struct http_uri_parser parser; |
| 486 | |
| 487 | parser = http_uri_parser_init(url); |
| 488 | scheme_ist = http_parse_scheme(&parser); |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 489 | if (!isttest(scheme_ist)) { |
| 490 | return 0; |
| 491 | } |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 492 | |
| 493 | if (isteqi(scheme_ist, ist("http://"))){ |
| 494 | scheme_tmp = SCH_HTTP; |
| 495 | port_tmp = 80; |
| 496 | } else if (isteqi(scheme_ist, ist("https://"))) { |
| 497 | scheme_tmp = SCH_HTTPS; |
| 498 | port_tmp = 443; |
| 499 | } |
| 500 | |
| 501 | authority_ist = http_parse_authority(&parser, 1); |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 502 | if (!isttest(authority_ist)) { |
| 503 | return 0; |
| 504 | } |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 505 | p = end = istend(authority_ist); |
| 506 | |
| 507 | /* look for a port at the end of the authority */ |
| 508 | while (p > istptr(authority_ist) && isdigit((unsigned char)*--p)) |
| 509 | ; |
| 510 | |
| 511 | if (*p == ':') { |
| 512 | host_ist = ist2(istptr(authority_ist), p - istptr(authority_ist)); |
| 513 | port_ist = istnext(ist2(p, end - p)); |
| 514 | ist2str(trash.area, port_ist); |
| 515 | port_tmp = atoi(trash.area); |
| 516 | } else { |
| 517 | host_ist = authority_ist; |
| 518 | } |
| 519 | |
| 520 | if (scheme) |
| 521 | *scheme = scheme_tmp; |
| 522 | if (host) |
| 523 | *host = host_ist; |
| 524 | if (port) |
| 525 | *port = port_tmp; |
| 526 | |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 527 | return 1; |
William Lallemand | 7f1df8f | 2022-04-14 17:50:20 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 531 | * Start the HTTP client |
| 532 | * Create the appctx, session, stream and wakeup the applet |
| 533 | * |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 534 | * Return the <appctx> or NULL if it failed |
| 535 | */ |
| 536 | struct appctx *httpclient_start(struct httpclient *hc) |
| 537 | { |
| 538 | struct applet *applet = &httpclient_applet; |
| 539 | struct appctx *appctx; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 540 | |
William Lallemand | 5085bc3 | 2022-02-17 12:52:09 +0100 | [diff] [blame] | 541 | /* if the client was started and not ended, an applet is already |
| 542 | * running, we shouldn't try anything */ |
| 543 | if (httpclient_started(hc) && !httpclient_ended(hc)) |
| 544 | return NULL; |
| 545 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 546 | /* The HTTP client will be created in the same thread as the caller, |
| 547 | * avoiding threading issues */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 548 | appctx = appctx_new_here(applet, NULL); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 549 | if (!appctx) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 550 | goto out; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 551 | appctx->svcctx = hc; |
| 552 | hc->flags = 0; |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 553 | |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 554 | if (appctx_init(appctx) == -1) { |
| 555 | ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__); |
Christopher Faulet | 92202da | 2022-05-11 12:22:10 +0200 | [diff] [blame] | 556 | goto out_free_appctx; |
William Lallemand | 8533273 | 2022-05-04 10:59:51 +0200 | [diff] [blame] | 557 | } |
| 558 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 559 | return appctx; |
| 560 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 561 | out_free_appctx: |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 562 | appctx_free_on_early_error(appctx); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 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 { |
Willy Tarreau | b482920 | 2022-09-01 20:40:26 +0200 | [diff] [blame] | 582 | /* if the client wasn't stopped, ask for a stop and destroy */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 583 | hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP); |
Willy Tarreau | b482920 | 2022-09-01 20:40:26 +0200 | [diff] [blame] | 584 | /* the calling applet doesn't exist anymore */ |
| 585 | hc->caller = NULL; |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 586 | if (hc->appctx) |
| 587 | appctx_wakeup(hc->appctx); |
| 588 | } |
| 589 | } |
| 590 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 591 | /* Free the httpclient */ |
| 592 | void httpclient_destroy(struct httpclient *hc) |
| 593 | { |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 594 | struct http_hdr *hdrs; |
| 595 | |
| 596 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 597 | if (!hc) |
| 598 | return; |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 599 | |
William Lallemand | 2a87900 | 2021-10-05 15:50:45 +0200 | [diff] [blame] | 600 | /* we should never destroy a client which was started but not stopped */ |
| 601 | BUG_ON(httpclient_started(hc) && !httpclient_ended(hc)); |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 602 | |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 603 | /* request */ |
| 604 | istfree(&hc->req.url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 605 | b_free(&hc->req.buf); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 606 | /* response */ |
| 607 | istfree(&hc->res.vsn); |
| 608 | istfree(&hc->res.reason); |
| 609 | hdrs = hc->res.hdrs; |
| 610 | while (hdrs && isttest(hdrs->n)) { |
| 611 | istfree(&hdrs->n); |
| 612 | istfree(&hdrs->v); |
| 613 | hdrs++; |
| 614 | } |
| 615 | ha_free(&hc->res.hdrs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 616 | b_free(&hc->res.buf); |
William Lallemand | 7b2e0ee | 2022-02-17 19:10:55 +0100 | [diff] [blame] | 617 | sockaddr_free(&hc->dst); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 618 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 619 | free(hc); |
| 620 | |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | /* Allocate an httpclient and its buffers |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 625 | * Use the default httpclient_proxy |
| 626 | * |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 627 | * Return NULL on failure */ |
| 628 | struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url) |
| 629 | { |
| 630 | struct httpclient *hc; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 631 | |
| 632 | hc = calloc(1, sizeof(*hc)); |
| 633 | if (!hc) |
| 634 | goto err; |
| 635 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 636 | hc->req.buf = BUF_NULL; |
| 637 | hc->res.buf = BUF_NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 638 | hc->caller = caller; |
William Lallemand | 67b7784 | 2021-11-10 16:57:25 +0100 | [diff] [blame] | 639 | hc->req.url = istdup(url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 640 | hc->req.meth = meth; |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 641 | httpclient_set_proxy(hc, httpclient_proxy); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 642 | |
| 643 | return hc; |
| 644 | |
| 645 | err: |
| 646 | httpclient_destroy(hc); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 650 | /* Allocate an httpclient and its buffers, |
| 651 | * Use the proxy <px> |
| 652 | * |
| 653 | * Return and httpclient or NULL. |
| 654 | */ |
| 655 | struct httpclient *httpclient_new_from_proxy(struct proxy *px, void *caller, enum http_meth_t meth, struct ist url) |
| 656 | { |
| 657 | struct httpclient *hc; |
| 658 | |
| 659 | hc = httpclient_new(caller, meth, url); |
| 660 | if (!hc) |
| 661 | return NULL; |
| 662 | |
| 663 | httpclient_set_proxy(hc, px); |
| 664 | |
| 665 | return hc; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Configure an httpclient with a specific proxy <px> |
| 670 | * |
| 671 | * The proxy <px> must contains 2 srv, one configured for clear connections, the other for SSL. |
| 672 | * |
| 673 | */ |
| 674 | int httpclient_set_proxy(struct httpclient *hc, struct proxy *px) |
| 675 | { |
| 676 | struct server *srv; |
| 677 | |
| 678 | hc->px = px; |
| 679 | |
| 680 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
| 681 | if (srv->xprt == xprt_get(XPRT_RAW)) { |
| 682 | hc->srv_raw = srv; |
| 683 | #ifdef USE_OPENSSL |
| 684 | } else if (srv->xprt == xprt_get(XPRT_SSL)) { |
| 685 | hc->srv_ssl = srv; |
| 686 | #endif |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 693 | static void httpclient_applet_io_handler(struct appctx *appctx) |
| 694 | { |
Willy Tarreau | 1eea665 | 2022-05-05 20:12:01 +0200 | [diff] [blame] | 695 | struct httpclient *hc = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 696 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | b89f872 | 2022-05-27 10:37:32 +0200 | [diff] [blame] | 697 | struct stream *s = __sc_strm(sc); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 698 | struct channel *req = &s->req; |
| 699 | struct channel *res = &s->res; |
| 700 | struct htx_blk *blk = NULL; |
| 701 | struct htx *htx; |
William Lallemand | b702030 | 2021-08-20 11:24:13 +0200 | [diff] [blame] | 702 | struct htx_sl *sl = NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 703 | uint32_t hdr_num; |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 704 | uint32_t sz; |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 705 | int ret; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 706 | |
Christopher Faulet | 1901c1b | 2023-04-11 07:38:34 +0200 | [diff] [blame] | 707 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 708 | if (co_data(res)) { |
| 709 | htx = htx_from_buf(&res->buf); |
| 710 | co_htx_skip(res, htx, co_data(res)); |
| 711 | htx_to_buf(htx, &res->buf); |
| 712 | } |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 713 | goto out; |
Christopher Faulet | 1901c1b | 2023-04-11 07:38:34 +0200 | [diff] [blame] | 714 | } |
William Lallemand | a93eac4 | 2022-10-20 18:36:03 +0200 | [diff] [blame] | 715 | /* The IO handler could be called after the release, so we need to |
| 716 | * check if hc is still there to run the IO handler */ |
| 717 | if (!hc) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 718 | goto out; |
William Lallemand | a93eac4 | 2022-10-20 18:36:03 +0200 | [diff] [blame] | 719 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 720 | while (1) { |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 721 | |
| 722 | /* required to stop */ |
| 723 | if (hc->flags & HTTPCLIENT_FA_STOP) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 724 | goto error; |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 725 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 726 | switch(appctx->st0) { |
| 727 | |
| 728 | case HTTPCLIENT_S_REQ: |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 729 | /* we know that the buffer is empty here, since |
| 730 | * it's the first call, we can freely copy the |
| 731 | * request from the httpclient buffer */ |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 732 | ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf)); |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 733 | if (!ret) { |
| 734 | sc_need_room(sc, 0); |
| 735 | goto out; |
| 736 | } |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 737 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 738 | if (!b_data(&hc->req.buf)) |
| 739 | b_free(&hc->req.buf); |
| 740 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 741 | htx = htx_from_buf(&req->buf); |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 742 | if (!htx) { |
| 743 | sc_need_room(sc, 0); |
| 744 | goto out; |
| 745 | } |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 746 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 747 | channel_add_input(req, htx->data); |
| 748 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 749 | if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */ |
| 750 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
| 751 | else |
| 752 | appctx->st0 = HTTPCLIENT_S_REQ_BODY; |
| 753 | |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 754 | goto out; /* we need to leave the IO handler once we wrote the request */ |
| 755 | break; |
| 756 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 757 | case HTTPCLIENT_S_REQ_BODY: |
| 758 | /* call the payload callback */ |
| 759 | { |
| 760 | if (hc->ops.req_payload) { |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 761 | struct htx *hc_htx; |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 762 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 763 | /* call the request callback */ |
| 764 | hc->ops.req_payload(hc); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 765 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 766 | hc_htx = htxbuf(&hc->req.buf); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 767 | if (htx_is_empty(hc_htx)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 768 | goto out; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 769 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 770 | htx = htx_from_buf(&req->buf); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 771 | if (htx_is_empty(htx)) { |
Christopher Faulet | dca3b5b | 2022-04-07 10:47:07 +0200 | [diff] [blame] | 772 | size_t data = hc_htx->data; |
| 773 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 774 | /* Here htx_to_buf() will set buffer data to 0 because |
| 775 | * the HTX is empty, and allow us to do an xfer. |
| 776 | */ |
| 777 | htx_to_buf(hc_htx, &hc->req.buf); |
| 778 | htx_to_buf(htx, &req->buf); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 779 | b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf)); |
Christopher Faulet | dca3b5b | 2022-04-07 10:47:07 +0200 | [diff] [blame] | 780 | channel_add_input(req, data); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 781 | } else { |
| 782 | struct htx_ret ret; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 783 | |
Christopher Faulet | 6b4f1f6 | 2022-04-29 13:56:12 +0200 | [diff] [blame] | 784 | ret = htx_xfer_blks(htx, hc_htx, htx_used_space(hc_htx), HTX_BLK_UNUSED); |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 785 | channel_add_input(req, ret.ret); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 786 | |
William Lallemand | ccc7ee4 | 2022-03-18 17:57:15 +0100 | [diff] [blame] | 787 | /* we must copy the EOM if we empty the buffer */ |
| 788 | if (htx_is_empty(hc_htx)) { |
| 789 | htx->flags |= (hc_htx->flags & HTX_FL_EOM); |
| 790 | } |
| 791 | htx_to_buf(htx, &req->buf); |
| 792 | htx_to_buf(hc_htx, &hc->req.buf); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | if (!b_data(&hc->req.buf)) |
| 797 | b_free(&hc->req.buf); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 800 | htx = htxbuf(&req->buf); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 801 | |
| 802 | /* if the request contains the HTX_FL_EOM, we finished the request part. */ |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 803 | if (htx->flags & HTX_FL_EOM) |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 804 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
| 805 | |
William Lallemand | 1eca894 | 2022-03-17 14:57:23 +0100 | [diff] [blame] | 806 | 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] | 807 | } |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 808 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 809 | |
| 810 | case HTTPCLIENT_S_RES_STLINE: |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 811 | /* Request is finished, report EOI */ |
| 812 | se_fl_set(appctx->sedesc, SE_FL_EOI); |
| 813 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 814 | /* 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] | 815 | if (!co_data(res)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 816 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 817 | htx = htxbuf(&res->buf); |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 818 | if (htx_is_empty(htx)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 819 | goto out; |
William Lallemand | 97f69c6 | 2022-03-10 17:23:40 +0100 | [diff] [blame] | 820 | blk = htx_get_head_blk(htx); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 821 | if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL)) |
| 822 | sl = htx_get_blk_ptr(htx, blk); |
| 823 | if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP))) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 824 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 825 | |
| 826 | /* copy the status line in the httpclient */ |
| 827 | hc->res.status = sl->info.res.status; |
| 828 | hc->res.vsn = istdup(htx_sl_res_vsn(sl)); |
| 829 | hc->res.reason = istdup(htx_sl_res_reason(sl)); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 830 | sz = htx_get_blksz(blk); |
Christopher Faulet | 0055d56 | 2022-04-29 14:09:03 +0200 | [diff] [blame] | 831 | c_rew(res, sz); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 832 | htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 833 | /* caller callback */ |
| 834 | if (hc->ops.res_stline) |
| 835 | hc->ops.res_stline(hc); |
| 836 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 837 | htx_to_buf(htx, &res->buf); |
| 838 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 839 | /* if there is no HTX data anymore and the EOM flag is |
| 840 | * set, leave (no body) */ |
| 841 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) |
| 842 | appctx->st0 = HTTPCLIENT_S_RES_END; |
| 843 | else |
| 844 | appctx->st0 = HTTPCLIENT_S_RES_HDR; |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 845 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 846 | break; |
| 847 | |
| 848 | case HTTPCLIENT_S_RES_HDR: |
| 849 | /* first copy the headers in a local hdrs |
| 850 | * structure, once we the total numbers of the |
| 851 | * header we allocate the right size and copy |
| 852 | * them. The htx block of the headers are |
| 853 | * removed each time one is read */ |
| 854 | { |
| 855 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 856 | |
William Lallemand | a625b03 | 2022-03-17 14:45:46 +0100 | [diff] [blame] | 857 | if (!co_data(res)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 858 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 859 | htx = htxbuf(&res->buf); |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 860 | if (htx_is_empty(htx)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 861 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 862 | |
| 863 | hdr_num = 0; |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 864 | blk = htx_get_head_blk(htx); |
| 865 | while (blk) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 866 | enum htx_blk_type type = htx_get_blk_type(blk); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 867 | uint32_t sz = htx_get_blksz(blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 868 | |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 869 | c_rew(res, sz); |
William Lallemand | c020b25 | 2022-03-09 18:56:02 +0100 | [diff] [blame] | 870 | |
Christopher Faulet | 18de6f2 | 2022-06-01 16:37:49 +0200 | [diff] [blame] | 871 | if (type == HTX_BLK_HDR) { |
William Lallemand | c020b25 | 2022-03-09 18:56:02 +0100 | [diff] [blame] | 872 | hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk)); |
| 873 | hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk)); |
William Lallemand | c020b25 | 2022-03-09 18:56:02 +0100 | [diff] [blame] | 874 | hdr_num++; |
| 875 | } |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 876 | else if (type == HTX_BLK_EOH) { |
| 877 | /* create a NULL end of array and leave the loop */ |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 878 | hdrs[hdr_num].n = IST_NULL; |
| 879 | hdrs[hdr_num].v = IST_NULL; |
Christopher Faulet | 18de6f2 | 2022-06-01 16:37:49 +0200 | [diff] [blame] | 880 | htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 881 | break; |
| 882 | } |
Christopher Faulet | 18de6f2 | 2022-06-01 16:37:49 +0200 | [diff] [blame] | 883 | blk = htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 884 | } |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 885 | htx_to_buf(htx, &res->buf); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 886 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 887 | if (hdr_num) { |
| 888 | /* alloc and copy the headers in the httpclient struct */ |
| 889 | hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs)); |
| 890 | if (!hc->res.hdrs) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 891 | goto error; |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 892 | memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 893 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 894 | /* caller callback */ |
| 895 | if (hc->ops.res_headers) |
| 896 | hc->ops.res_headers(hc); |
| 897 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 898 | |
| 899 | /* if there is no HTX data anymore and the EOM flag is |
| 900 | * set, leave (no body) */ |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 901 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 902 | appctx->st0 = HTTPCLIENT_S_RES_END; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 903 | } else { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 904 | appctx->st0 = HTTPCLIENT_S_RES_BODY; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 905 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 906 | } |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 907 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 908 | |
| 909 | case HTTPCLIENT_S_RES_BODY: |
| 910 | /* |
| 911 | * The IO handler removes the htx blocks in the response buffer and |
| 912 | * push them in the hc->res.buf buffer in a raw format. |
| 913 | */ |
William Lallemand | a625b03 | 2022-03-17 14:45:46 +0100 | [diff] [blame] | 914 | if (!co_data(res)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 915 | goto out; |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 916 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 917 | htx = htxbuf(&res->buf); |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 918 | if (htx_is_empty(htx)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 919 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 920 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 921 | if (!b_alloc(&hc->res.buf)) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 922 | goto out; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 923 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 924 | if (b_full(&hc->res.buf)) |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 925 | goto process_data; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 926 | |
| 927 | /* decapsule the htx data to raw data */ |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 928 | blk = htx_get_head_blk(htx); |
| 929 | while (blk) { |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 930 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 931 | size_t count = co_data(res); |
| 932 | uint32_t blksz = htx_get_blksz(blk); |
| 933 | uint32_t room = b_room(&hc->res.buf); |
| 934 | uint32_t vlen; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 935 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 936 | /* we should try to copy the maximum output data in a block, which fit |
| 937 | * the destination buffer */ |
| 938 | vlen = MIN(count, blksz); |
| 939 | vlen = MIN(vlen, room); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 940 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 941 | if (vlen == 0) { |
| 942 | htx_to_buf(htx, &res->buf); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 943 | goto process_data; |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 944 | } |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 945 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 946 | if (type == HTX_BLK_DATA) { |
| 947 | struct ist v = htx_get_blk_value(htx, blk); |
| 948 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 949 | __b_putblk(&hc->res.buf, v.ptr, vlen); |
| 950 | c_rew(res, vlen); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 951 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 952 | if (vlen == blksz) |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 953 | blk = htx_remove_blk(htx, blk); |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 954 | else |
| 955 | htx_cut_data_blk(htx, blk, vlen); |
William Lallemand | 2b7dc4e | 2022-02-24 16:55:41 +0100 | [diff] [blame] | 956 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 957 | /* the data must be processed by the caller in the receive phase */ |
| 958 | if (hc->ops.res_payload) |
| 959 | hc->ops.res_payload(hc); |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 960 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 961 | /* cannot copy everything, need to process */ |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 962 | if (vlen != blksz) { |
| 963 | htx_to_buf(htx, &res->buf); |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 964 | goto process_data; |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 965 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 966 | } else { |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 967 | if (vlen != blksz) { |
| 968 | htx_to_buf(htx, &res->buf); |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 969 | goto process_data; |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 970 | } |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 971 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 972 | /* remove any block which is not a data block */ |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 973 | c_rew(res, blksz); |
Christopher Faulet | 534645d | 2022-04-29 13:44:46 +0200 | [diff] [blame] | 974 | blk = htx_remove_blk(htx, blk); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 975 | } |
| 976 | } |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 977 | |
Christopher Faulet | ca90de2 | 2023-08-04 14:28:23 +0200 | [diff] [blame] | 978 | htx_to_buf(htx, &res->buf); |
| 979 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 980 | /* if not finished, should be called again */ |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 981 | if (!(htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))) |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 982 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 983 | |
William Lallemand | c8f1eb9 | 2022-03-09 11:58:51 +0100 | [diff] [blame] | 984 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 985 | /* end of message, we should quit */ |
| 986 | appctx->st0 = HTTPCLIENT_S_RES_END; |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 987 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 988 | |
| 989 | case HTTPCLIENT_S_RES_END: |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 990 | se_fl_set(appctx->sedesc, SE_FL_EOS); |
| 991 | goto out; |
| 992 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 996 | out: |
| 997 | return; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 998 | |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 999 | process_data: |
Willy Tarreau | b89f872 | 2022-05-27 10:37:32 +0200 | [diff] [blame] | 1000 | sc_will_read(sc); |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 1001 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1002 | |
Christopher Faulet | be08df8 | 2023-03-31 11:30:32 +0200 | [diff] [blame] | 1003 | error: |
| 1004 | se_fl_set(appctx->sedesc, SE_FL_ERROR); |
| 1005 | goto out; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1008 | static int httpclient_applet_init(struct appctx *appctx) |
| 1009 | { |
| 1010 | struct httpclient *hc = appctx->svcctx; |
| 1011 | struct stream *s; |
| 1012 | struct sockaddr_storage *addr = NULL; |
| 1013 | struct sockaddr_storage ss_url = {}; |
| 1014 | struct sockaddr_storage *ss_dst; |
| 1015 | enum obj_type *target = NULL; |
| 1016 | struct ist host = IST_NULL; |
| 1017 | enum http_scheme scheme; |
| 1018 | int port; |
| 1019 | int doresolve = 0; |
| 1020 | |
| 1021 | |
| 1022 | /* parse the URL and */ |
Thierry Fournier | 74a9eb5 | 2022-10-10 12:46:38 +0200 | [diff] [blame] | 1023 | if (!httpclient_spliturl(hc->req.url, &scheme, &host, &port)) |
| 1024 | goto out_error; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1025 | |
| 1026 | if (hc->dst) { |
| 1027 | /* if httpclient_set_dst() was used, sets the alternative address */ |
| 1028 | ss_dst = hc->dst; |
| 1029 | } else { |
| 1030 | /* set the dst using the host, or 0.0.0.0 to resolve */ |
| 1031 | ist2str(trash.area, host); |
| 1032 | ss_dst = str2ip2(trash.area, &ss_url, 0); |
| 1033 | if (!ss_dst) { /* couldn't get an IP from that, try to resolve */ |
| 1034 | doresolve = 1; |
| 1035 | ss_dst = str2ip2("0.0.0.0", &ss_url, 0); |
| 1036 | } |
| 1037 | sock_inet_set_port(ss_dst, port); |
| 1038 | } |
| 1039 | |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1040 | if (!sockaddr_alloc(&addr, ss_dst, sizeof(*ss_dst))) |
| 1041 | goto out_error; |
| 1042 | |
| 1043 | /* choose the SSL server or not */ |
| 1044 | switch (scheme) { |
| 1045 | case SCH_HTTP: |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1046 | target = &hc->srv_raw->obj_type; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1047 | break; |
| 1048 | case SCH_HTTPS: |
| 1049 | #ifdef USE_OPENSSL |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1050 | if (hc->srv_ssl) { |
| 1051 | target = &hc->srv_ssl->obj_type; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1052 | } else { |
| 1053 | ha_alert("httpclient: SSL was disabled (wrong verify/ca-file)!\n"); |
| 1054 | goto out_free_addr; |
| 1055 | } |
| 1056 | #else |
| 1057 | ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__); |
| 1058 | goto out_free_addr; |
| 1059 | #endif |
| 1060 | break; |
| 1061 | } |
| 1062 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1063 | if (appctx_finalize_startup(appctx, hc->px, &hc->req.buf) == -1) { |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1064 | ha_alert("httpclient: Failed to initialize appctx %s:%d.\n", __FUNCTION__, __LINE__); |
| 1065 | goto out_free_addr; |
| 1066 | } |
| 1067 | |
| 1068 | s = appctx_strm(appctx); |
| 1069 | s->target = target; |
| 1070 | /* set the "timeout server" */ |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 1071 | s->scb->ioto = hc->timeout_server; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1072 | |
| 1073 | if (doresolve) { |
| 1074 | /* in order to do the set-dst we need to put the address on the front */ |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1075 | s->scf->dst = addr; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1076 | } else { |
| 1077 | /* in cases we don't use the resolve we already have the address |
| 1078 | * and must put it on the backend side, some of the cases are |
| 1079 | * not meant to be used on the frontend (sockpair, unix socket etc.) */ |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1080 | s->scb->dst = addr; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Christopher Faulet | 9a790f6 | 2023-03-16 14:40:03 +0100 | [diff] [blame] | 1083 | s->scb->flags |= (SC_FL_RCV_ONCE|SC_FL_NOLINGER); |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1084 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1085 | |
| 1086 | /* applet is waiting for data */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 1087 | applet_need_more_data(appctx); |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1088 | appctx_wakeup(appctx); |
| 1089 | |
| 1090 | hc->appctx = appctx; |
| 1091 | hc->flags |= HTTPCLIENT_FS_STARTED; |
| 1092 | |
| 1093 | /* The request was transferred when the stream was created. So switch |
| 1094 | * directly to REQ_BODY or RES_STLINE state |
| 1095 | */ |
| 1096 | appctx->st0 = (hc->ops.req_payload ? HTTPCLIENT_S_REQ_BODY : HTTPCLIENT_S_RES_STLINE); |
| 1097 | return 0; |
| 1098 | |
| 1099 | out_free_addr: |
| 1100 | sockaddr_free(&addr); |
| 1101 | out_error: |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1105 | static void httpclient_applet_release(struct appctx *appctx) |
| 1106 | { |
Willy Tarreau | 1eea665 | 2022-05-05 20:12:01 +0200 | [diff] [blame] | 1107 | struct httpclient *hc = appctx->svcctx; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1108 | |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 1109 | /* mark the httpclient as ended */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 1110 | hc->flags |= HTTPCLIENT_FS_ENDED; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1111 | /* the applet is leaving, remove the ptr so we don't try to call it |
| 1112 | * again from the caller */ |
| 1113 | hc->appctx = NULL; |
| 1114 | |
William Lallemand | eb0d4c4 | 2022-04-06 14:12:37 +0200 | [diff] [blame] | 1115 | if (hc->ops.res_end) |
| 1116 | hc->ops.res_end(hc); |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 1117 | |
| 1118 | /* destroy the httpclient when set to autotokill */ |
| 1119 | if (hc->flags & HTTPCLIENT_FA_AUTOKILL) { |
| 1120 | httpclient_destroy(hc); |
| 1121 | } |
| 1122 | |
William Lallemand | a93eac4 | 2022-10-20 18:36:03 +0200 | [diff] [blame] | 1123 | /* be sure not to use this ptr anymore if the IO handler is called a |
| 1124 | * last time */ |
| 1125 | appctx->svcctx = NULL; |
| 1126 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | /* HTTP client applet */ |
| 1131 | static struct applet httpclient_applet = { |
| 1132 | .obj_type = OBJ_TYPE_APPLET, |
| 1133 | .name = "<HTTPCLIENT>", |
| 1134 | .fct = httpclient_applet_io_handler, |
Christopher Faulet | b1e0836 | 2022-05-12 15:33:14 +0200 | [diff] [blame] | 1135 | .init = httpclient_applet_init, |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 1136 | .release = httpclient_applet_release, |
| 1137 | }; |
| 1138 | |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1139 | |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1140 | static int httpclient_resolve_init(struct proxy *px) |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1141 | { |
| 1142 | struct act_rule *rule; |
| 1143 | int i; |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1144 | char *do_resolve = NULL; |
| 1145 | char *http_rules[][11] = { |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1146 | { "set-var(txn.hc_ip)", "dst", "" }, |
William Lallemand | d78dfe7 | 2022-08-26 16:45:13 +0200 | [diff] [blame] | 1147 | { do_resolve, "hdr(Host),host_only", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" }, |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1148 | { "return", "status", "503", "if", "{", "var(txn.hc_ip)", "-m", "ip", "0.0.0.0", "}", "" }, |
| 1149 | { "capture", "var(txn.hc_ip)", "len", "40", "" }, |
| 1150 | { "set-dst", "var(txn.hc_ip)", "" }, |
| 1151 | { "" } |
| 1152 | }; |
| 1153 | |
William Lallemand | e279f59 | 2023-05-11 21:08:38 +0200 | [diff] [blame] | 1154 | |
| 1155 | if (resolvers_disabled) |
| 1156 | return 0; |
| 1157 | |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1158 | if (!resolvers_id) |
| 1159 | resolvers_id = strdup("default"); |
| 1160 | |
William Lallemand | 7c5a7ef | 2022-05-04 15:59:44 +0200 | [diff] [blame] | 1161 | memprintf(&do_resolve, "do-resolve(txn.hc_ip,%s%s%s)", resolvers_id, resolvers_prefer ? "," : "", resolvers_prefer ? resolvers_prefer : ""); |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1162 | http_rules[1][0] = do_resolve; |
| 1163 | |
William Lallemand | 7867f63 | 2022-05-05 19:02:59 +0200 | [diff] [blame] | 1164 | /* Try to create the default resolvers section */ |
| 1165 | resolvers_create_default(); |
| 1166 | |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1167 | /* if the resolver does not exist and no hard_error was set, simply ignore resolving */ |
| 1168 | if (!find_resolvers_by_id(resolvers_id) && !hard_error_resolvers) { |
| 1169 | free(do_resolve); |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1170 | return 0; |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1171 | } |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1172 | |
| 1173 | |
| 1174 | for (i = 0; *http_rules[i][0] != '\0'; i++) { |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1175 | rule = parse_http_req_cond((const char **)http_rules[i], "httpclient", 0, px); |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1176 | if (!rule) { |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1177 | free(do_resolve); |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1178 | ha_alert("Couldn't setup the httpclient resolver.\n"); |
| 1179 | return 1; |
| 1180 | } |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1181 | LIST_APPEND(&px->http_req_rules, &rule->list); |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1184 | free(do_resolve); |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1188 | /* |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1189 | * Creates an internal proxy which will be used for httpclient. |
| 1190 | * This will allocate 2 servers (raw and ssl) and 1 proxy. |
| 1191 | * |
| 1192 | * This function must be called from a precheck callback. |
| 1193 | * |
| 1194 | * Return a proxy or NULL. |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1195 | */ |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1196 | struct proxy *httpclient_create_proxy(const char *id) |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1197 | { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1198 | int err_code = ERR_NONE; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1199 | char *errmsg = NULL; |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1200 | struct proxy *px = NULL; |
| 1201 | struct server *srv_raw = NULL; |
| 1202 | #ifdef USE_OPENSSL |
| 1203 | struct server *srv_ssl = NULL; |
| 1204 | #endif |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1205 | |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 1206 | if (global.mode & MODE_MWORKER_WAIT) |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1207 | return ERR_NONE; |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 1208 | |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1209 | px = alloc_new_proxy(id, PR_CAP_LISTEN|PR_CAP_INT|PR_CAP_HTTPCLIENT, &errmsg); |
| 1210 | if (!px) { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1211 | memprintf(&errmsg, "couldn't allocate proxy."); |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1212 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1213 | goto err; |
| 1214 | } |
| 1215 | |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1216 | px->options |= PR_O_WREQ_BODY; |
| 1217 | px->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED | PR_RE_TIMEOUT; |
| 1218 | px->options2 |= PR_O2_INDEPSTR; |
| 1219 | px->mode = PR_MODE_HTTP; |
| 1220 | px->maxconn = 0; |
| 1221 | px->accept = NULL; |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 1222 | px->conn_retries = httpclient_retries; |
William Lallemand | b9ed157 | 2023-09-05 16:42:27 +0200 | [diff] [blame] | 1223 | px->timeout.connect = httpclient_timeout_connect; |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1224 | px->timeout.client = TICK_ETERNITY; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1225 | /* The HTTP Client use the "option httplog" with the global log server */ |
William Lallemand | d793ca2 | 2022-12-22 15:13:59 +0100 | [diff] [blame] | 1226 | px->conf.logformat_string = httpclient_log_format; |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1227 | px->http_needed = 1; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1228 | |
| 1229 | /* clear HTTP server */ |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1230 | srv_raw = new_server(px); |
| 1231 | if (!srv_raw) { |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1232 | memprintf(&errmsg, "out of memory."); |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1233 | err_code |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1234 | goto err; |
| 1235 | } |
| 1236 | |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1237 | srv_settings_cpy(srv_raw, &px->defsrv, 0); |
| 1238 | srv_raw->iweight = 0; |
| 1239 | srv_raw->uweight = 0; |
| 1240 | srv_raw->xprt = xprt_get(XPRT_RAW); |
| 1241 | srv_raw->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */ |
| 1242 | srv_raw->id = strdup("<HTTPCLIENT>"); |
| 1243 | if (!srv_raw->id) { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1244 | memprintf(&errmsg, "out of memory."); |
| 1245 | err_code |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1246 | goto err; |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1247 | } |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1248 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1249 | #ifdef USE_OPENSSL |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1250 | /* SSL HTTP server */ |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1251 | srv_ssl = new_server(px); |
| 1252 | if (!srv_ssl) { |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1253 | memprintf(&errmsg, "out of memory."); |
| 1254 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1255 | goto err; |
| 1256 | } |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1257 | srv_settings_cpy(srv_ssl, &px->defsrv, 0); |
| 1258 | srv_ssl->iweight = 0; |
| 1259 | srv_ssl->uweight = 0; |
| 1260 | srv_ssl->xprt = xprt_get(XPRT_SSL); |
| 1261 | srv_ssl->use_ssl = 1; |
| 1262 | srv_ssl->flags |= SRV_F_MAPPORTS; /* needed to apply the port change with resolving */ |
| 1263 | srv_ssl->id = strdup("<HTTPSCLIENT>"); |
| 1264 | if (!srv_ssl->id) { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1265 | memprintf(&errmsg, "out of memory."); |
| 1266 | err_code |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1267 | goto err; |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1268 | } |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1269 | |
Willy Tarreau | df3231c | 2022-09-02 09:02:21 +0200 | [diff] [blame] | 1270 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1271 | if (ssl_sock_parse_alpn("h2,http/1.1", &srv_ssl->ssl_ctx.alpn_str, &srv_ssl->ssl_ctx.alpn_len, &errmsg) != 0) { |
Willy Tarreau | df3231c | 2022-09-02 09:02:21 +0200 | [diff] [blame] | 1272 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1273 | goto err; |
| 1274 | } |
| 1275 | #endif |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1276 | srv_ssl->ssl_ctx.verify = httpclient_ssl_verify; |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1277 | /* if the verify is required, try to load the system CA */ |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1278 | if (httpclient_ssl_verify == SSL_SOCK_VERIFY_REQUIRED) { |
William Lallemand | 683fbb8 | 2022-05-04 15:43:01 +0200 | [diff] [blame] | 1279 | |
Miroslav Zagorac | a2ec192 | 2022-11-02 16:11:50 +0100 | [diff] [blame] | 1280 | srv_ssl->ssl_ctx.ca_file = strdup(httpclient_ssl_ca_file ? httpclient_ssl_ca_file : "@system-ca"); |
William Lallemand | 0a2d632 | 2022-11-24 19:14:19 +0100 | [diff] [blame] | 1281 | if (!__ssl_store_load_locations_file(srv_ssl->ssl_ctx.ca_file, 1, CAFILE_CERT, !hard_error_ssl)) { |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 1282 | /* if we failed to load the ca-file, only quits in |
| 1283 | * error with hard_error, otherwise just disable the |
| 1284 | * feature. */ |
| 1285 | if (hard_error_ssl) { |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1286 | memprintf(&errmsg, "cannot initialize SSL verify with 'ca-file \"%s\"'.", srv_ssl->ssl_ctx.ca_file); |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 1287 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1288 | goto err; |
| 1289 | } else { |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1290 | ha_free(&srv_ssl->ssl_ctx.ca_file); |
| 1291 | srv_drop(srv_ssl); |
| 1292 | srv_ssl = NULL; |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 1293 | } |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1294 | } |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1295 | } |
William Lallemand | cf5cb0b | 2022-04-22 14:48:45 +0200 | [diff] [blame] | 1296 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1297 | #endif |
William Lallemand | cfcbe9e | 2021-08-24 17:15:58 +0200 | [diff] [blame] | 1298 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 1299 | /* add the proxy in the proxy list only if everything is successful */ |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1300 | px->next = proxies_list; |
| 1301 | proxies_list = px; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1302 | |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1303 | if (httpclient_resolve_init(px) != 0) { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1304 | memprintf(&errmsg, "cannot initialize resolvers."); |
| 1305 | err_code |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1306 | goto err; |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1307 | } |
William Lallemand | 5392ff6 | 2022-04-28 16:55:02 +0200 | [diff] [blame] | 1308 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 1309 | /* link the 2 servers in the proxy */ |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1310 | srv_raw->next = px->srv; |
| 1311 | px->srv = srv_raw; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1312 | |
| 1313 | #ifdef USE_OPENSSL |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1314 | if (srv_ssl) { |
| 1315 | srv_ssl->next = px->srv; |
| 1316 | px->srv = srv_ssl; |
William Lallemand | 4006b0f | 2022-04-25 18:23:35 +0200 | [diff] [blame] | 1317 | } |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 1318 | #endif |
| 1319 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 1320 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1321 | err: |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1322 | if (err_code & ERR_CODE) { |
| 1323 | ha_alert("httpclient: cannot initialize: %s\n", errmsg); |
| 1324 | free(errmsg); |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1325 | srv_drop(srv_raw); |
| 1326 | #ifdef USE_OPENSSL |
| 1327 | srv_drop(srv_ssl); |
| 1328 | #endif |
| 1329 | free_proxy(px); |
| 1330 | |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | return px; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP, |
| 1338 | * the other for HTTPS. |
| 1339 | */ |
| 1340 | static int httpclient_precheck() |
| 1341 | { |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1342 | /* initialize the default httpclient_proxy which is used for the CLI and the lua */ |
| 1343 | |
| 1344 | httpclient_proxy = httpclient_create_proxy("<HTTPCLIENT>"); |
| 1345 | if (!httpclient_proxy) |
| 1346 | return 1; |
William Lallemand | 54aec5f | 2022-09-12 16:46:35 +0200 | [diff] [blame] | 1347 | |
| 1348 | return 0; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
William Lallemand | 2c8b084 | 2022-04-22 15:16:09 +0200 | [diff] [blame] | 1351 | static int httpclient_postcheck() |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1352 | { |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1353 | int err_code = ERR_NONE; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1354 | struct logsrv *logsrv; |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1355 | struct proxy *curproxy = NULL; |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1356 | char *errmsg = NULL; |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1357 | #ifdef USE_OPENSSL |
| 1358 | struct server *srv = NULL; |
| 1359 | struct server *srv_ssl = NULL; |
| 1360 | #endif |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1361 | |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 1362 | if (global.mode & MODE_MWORKER_WAIT) |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1363 | return ERR_NONE; |
William Lallemand | c6ceba3 | 2022-04-22 16:49:53 +0200 | [diff] [blame] | 1364 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1365 | /* Initialize the logs for every proxy dedicated to the httpclient */ |
| 1366 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1367 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1368 | if (!(curproxy->cap & PR_CAP_HTTPCLIENT)) |
| 1369 | continue; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1370 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1371 | /* copy logs from "global" log list */ |
| 1372 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
Aurelien DARRAGON | 7767931 | 2023-07-05 15:52:19 +0200 | [diff] [blame] | 1373 | struct logsrv *node = dup_logsrv(logsrv); |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1374 | |
| 1375 | if (!node) { |
| 1376 | memprintf(&errmsg, "out of memory."); |
| 1377 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1378 | goto err; |
| 1379 | } |
| 1380 | |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1381 | LIST_APPEND(&curproxy->logsrvs, &node->list); |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1382 | } |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1383 | if (curproxy->conf.logformat_string) { |
| 1384 | curproxy->conf.args.ctx = ARGC_LOG; |
| 1385 | if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat, |
| 1386 | LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES, |
| 1387 | SMP_VAL_FE_LOG_END, &errmsg)) { |
| 1388 | memprintf(&errmsg, "failed to parse log-format : %s.", errmsg); |
| 1389 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1390 | goto err; |
| 1391 | } |
| 1392 | curproxy->conf.args.file = NULL; |
| 1393 | curproxy->conf.args.line = 0; |
| 1394 | } |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1395 | |
| 1396 | #ifdef USE_OPENSSL |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1397 | /* initialize the SNI for the SSL servers */ |
| 1398 | |
| 1399 | for (srv = curproxy->srv; srv != NULL; srv = srv->next) { |
| 1400 | if (srv->xprt == xprt_get(XPRT_SSL)) { |
| 1401 | srv_ssl = srv; |
| 1402 | } |
William Lallemand | 715c101 | 2022-03-16 16:39:23 +0100 | [diff] [blame] | 1403 | } |
Miroslav Zagorac | cbfee3a | 2022-09-19 12:20:29 +0200 | [diff] [blame] | 1404 | if (srv_ssl && !srv_ssl->sni_expr) { |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1405 | /* init the SNI expression */ |
| 1406 | /* always use the host header as SNI, without the port */ |
| 1407 | srv_ssl->sni_expr = strdup("req.hdr(host),field(1,:)"); |
| 1408 | err_code |= server_parse_sni_expr(srv_ssl, curproxy, &errmsg); |
| 1409 | if (err_code & ERR_CODE) { |
| 1410 | memprintf(&errmsg, "failed to configure sni: %s.", errmsg); |
| 1411 | goto err; |
| 1412 | } |
| 1413 | } |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1414 | #endif |
William Lallemand | 992ad62 | 2022-09-12 17:39:04 +0200 | [diff] [blame] | 1415 | } |
William Lallemand | 71e3158 | 2022-03-16 15:47:47 +0100 | [diff] [blame] | 1416 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1417 | err: |
William Lallemand | 85af49c | 2022-05-04 14:33:57 +0200 | [diff] [blame] | 1418 | if (err_code & ERR_CODE) { |
| 1419 | ha_alert("httpclient: failed to initialize: %s\n", errmsg); |
| 1420 | free(errmsg); |
| 1421 | |
| 1422 | } |
| 1423 | return err_code; |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 1426 | /* initialize the proxy and servers for the HTTP client */ |
| 1427 | |
William Lallemand | 2c8b084 | 2022-04-22 15:16:09 +0200 | [diff] [blame] | 1428 | REGISTER_PRE_CHECK(httpclient_precheck); |
| 1429 | REGISTER_POST_CHECK(httpclient_postcheck); |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1430 | |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1431 | static int httpclient_parse_global_resolvers(char **args, int section_type, struct proxy *curpx, |
| 1432 | const struct proxy *defpx, const char *file, int line, |
| 1433 | char **err) |
| 1434 | { |
| 1435 | if (too_many_args(1, args, err, NULL)) |
| 1436 | return -1; |
| 1437 | |
| 1438 | /* any configuration should set the hard_error flag */ |
| 1439 | hard_error_resolvers = 1; |
| 1440 | |
| 1441 | free(resolvers_id); |
| 1442 | resolvers_id = strdup(args[1]); |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
William Lallemand | e279f59 | 2023-05-11 21:08:38 +0200 | [diff] [blame] | 1447 | /* config parser for global "httpclient.resolvers.disabled", accepts "on" or "off" */ |
| 1448 | static int httpclient_parse_global_resolvers_disabled(char **args, int section_type, struct proxy *curpx, |
| 1449 | const struct proxy *defpx, const char *file, int line, |
| 1450 | char **err) |
| 1451 | { |
| 1452 | if (too_many_args(1, args, err, NULL)) |
| 1453 | return -1; |
| 1454 | |
| 1455 | if (strcmp(args[1], "on") == 0) |
| 1456 | resolvers_disabled = 1; |
| 1457 | else if (strcmp(args[1], "off") == 0) |
| 1458 | resolvers_disabled = 0; |
| 1459 | else { |
| 1460 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
William Lallemand | 7c5a7ef | 2022-05-04 15:59:44 +0200 | [diff] [blame] | 1466 | static int httpclient_parse_global_prefer(char **args, int section_type, struct proxy *curpx, |
| 1467 | const struct proxy *defpx, const char *file, int line, |
| 1468 | char **err) |
| 1469 | { |
| 1470 | if (too_many_args(1, args, err, NULL)) |
| 1471 | return -1; |
| 1472 | |
| 1473 | /* any configuration should set the hard_error flag */ |
| 1474 | hard_error_resolvers = 1; |
| 1475 | |
| 1476 | |
| 1477 | if (strcmp(args[1],"ipv4") == 0) |
| 1478 | resolvers_prefer = "ipv4"; |
| 1479 | else if (strcmp(args[1],"ipv6") == 0) |
| 1480 | resolvers_prefer = "ipv6"; |
| 1481 | else { |
| 1482 | ha_alert("parsing [%s:%d] : '%s' expects 'ipv4' or 'ipv6' as argument.\n", file, line, args[0]); |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
| 1486 | return 0; |
| 1487 | } |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1488 | |
| 1489 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1490 | #ifdef USE_OPENSSL |
William Lallemand | 683fbb8 | 2022-05-04 15:43:01 +0200 | [diff] [blame] | 1491 | static int httpclient_parse_global_ca_file(char **args, int section_type, struct proxy *curpx, |
| 1492 | const struct proxy *defpx, const char *file, int line, |
| 1493 | char **err) |
| 1494 | { |
| 1495 | if (too_many_args(1, args, err, NULL)) |
| 1496 | return -1; |
| 1497 | |
| 1498 | /* any configuration should set the hard_error flag */ |
| 1499 | hard_error_ssl = 1; |
| 1500 | |
| 1501 | free(httpclient_ssl_ca_file); |
| 1502 | httpclient_ssl_ca_file = strdup(args[1]); |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1507 | static int httpclient_parse_global_verify(char **args, int section_type, struct proxy *curpx, |
| 1508 | const struct proxy *defpx, const char *file, int line, |
| 1509 | char **err) |
| 1510 | { |
| 1511 | if (too_many_args(1, args, err, NULL)) |
| 1512 | return -1; |
| 1513 | |
William Lallemand | 6fce46a | 2022-05-04 14:53:41 +0200 | [diff] [blame] | 1514 | /* any configuration should set the hard_error flag */ |
| 1515 | hard_error_ssl = 1; |
| 1516 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1517 | if (strcmp(args[1],"none") == 0) |
William Lallemand | 04994de | 2022-04-28 19:35:21 +0200 | [diff] [blame] | 1518 | httpclient_ssl_verify = SSL_SOCK_VERIFY_NONE; |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1519 | else if (strcmp(args[1],"required") == 0) |
William Lallemand | 04994de | 2022-04-28 19:35:21 +0200 | [diff] [blame] | 1520 | httpclient_ssl_verify = SSL_SOCK_VERIFY_REQUIRED; |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1521 | else { |
| 1522 | ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, line, args[0]); |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1528 | #endif /* ! USE_OPENSSL */ |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1529 | |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 1530 | static int httpclient_parse_global_retries(char **args, int section_type, struct proxy *curpx, |
| 1531 | const struct proxy *defpx, const char *file, int line, |
| 1532 | char **err) |
| 1533 | { |
| 1534 | if (too_many_args(1, args, err, NULL)) |
| 1535 | return -1; |
| 1536 | |
| 1537 | if (*(args[1]) == 0) { |
| 1538 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 1539 | file, line, args[0]); |
| 1540 | return -1; |
| 1541 | } |
| 1542 | httpclient_retries = atol(args[1]); |
| 1543 | |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
William Lallemand | b9ed157 | 2023-09-05 16:42:27 +0200 | [diff] [blame] | 1547 | static int httpclient_parse_global_timeout_connect(char **args, int section_type, struct proxy *curpx, |
| 1548 | const struct proxy *defpx, const char *file, int line, |
| 1549 | char **err) |
| 1550 | { |
| 1551 | const char *res; |
| 1552 | unsigned timeout; |
| 1553 | |
| 1554 | if (too_many_args(1, args, err, NULL)) |
| 1555 | return -1; |
| 1556 | |
| 1557 | if (*(args[1]) == 0) { |
| 1558 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 1559 | file, line, args[0]); |
| 1560 | return -1; |
| 1561 | } |
| 1562 | |
| 1563 | res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 1564 | if (res == PARSE_TIME_OVER) { |
| 1565 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 1566 | args[1], args[0]); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | else if (res == PARSE_TIME_UNDER) { |
| 1570 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 1571 | args[1], args[0]); |
| 1572 | return -1; |
| 1573 | } |
| 1574 | else if (res) { |
| 1575 | memprintf(err, "unexpected character '%c' in '%s'", *res, args[0]); |
| 1576 | return -1; |
| 1577 | } |
| 1578 | |
| 1579 | if (*args[2] != 0) { |
| 1580 | memprintf(err, "'%s' : unexpected extra argument '%s' after value '%s'.", args[0], args[2], args[1]); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | httpclient_timeout_connect = MS_TO_TICKS(timeout); |
| 1585 | |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 1589 | |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1590 | static struct cfg_kw_list cfg_kws = {ILH, { |
William Lallemand | e279f59 | 2023-05-11 21:08:38 +0200 | [diff] [blame] | 1591 | { CFG_GLOBAL, "httpclient.resolvers.disabled", httpclient_parse_global_resolvers_disabled }, |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1592 | { CFG_GLOBAL, "httpclient.resolvers.id", httpclient_parse_global_resolvers }, |
William Lallemand | 7c5a7ef | 2022-05-04 15:59:44 +0200 | [diff] [blame] | 1593 | { CFG_GLOBAL, "httpclient.resolvers.prefer", httpclient_parse_global_prefer }, |
William Lallemand | 4ad693e | 2023-09-05 15:55:04 +0200 | [diff] [blame] | 1594 | { CFG_GLOBAL, "httpclient.retries", httpclient_parse_global_retries }, |
William Lallemand | b9ed157 | 2023-09-05 16:42:27 +0200 | [diff] [blame] | 1595 | { CFG_GLOBAL, "httpclient.timeout.connect", httpclient_parse_global_timeout_connect }, |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1596 | #ifdef USE_OPENSSL |
William Lallemand | 9ff95e2 | 2022-05-04 13:52:29 +0200 | [diff] [blame] | 1597 | { CFG_GLOBAL, "httpclient.ssl.verify", httpclient_parse_global_verify }, |
William Lallemand | 683fbb8 | 2022-05-04 15:43:01 +0200 | [diff] [blame] | 1598 | { CFG_GLOBAL, "httpclient.ssl.ca-file", httpclient_parse_global_ca_file }, |
William Lallemand | 8a734cb | 2022-05-04 16:10:47 +0200 | [diff] [blame] | 1599 | #endif |
William Lallemand | eaa703e | 2022-04-22 17:52:33 +0200 | [diff] [blame] | 1600 | { 0, NULL, NULL }, |
| 1601 | }}; |
| 1602 | |
| 1603 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |