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