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