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