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> |
| 18 | #include <haproxy/dynbuf.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
| 20 | #include <haproxy/connection.h> |
| 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> |
| 25 | #include <haproxy/http_client.h> |
| 26 | #include <haproxy/http_htx.h> |
| 27 | #include <haproxy/htx.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 28 | #include <haproxy/log.h> |
| 29 | #include <haproxy/proxy.h> |
William Lallemand | 2a8fe8b | 2021-08-20 14:25:15 +0200 | [diff] [blame] | 30 | #include <haproxy/server.h> |
Willy Tarreau | 1df2042 | 2021-10-06 11:28:24 +0200 | [diff] [blame] | 31 | #include <haproxy/ssl_sock-t.h> |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 32 | #include <haproxy/stream_interface.h> |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 33 | #include <haproxy/tools.h> |
| 34 | |
| 35 | #include <string.h> |
| 36 | |
| 37 | |
| 38 | static struct proxy *httpclient_proxy; |
| 39 | static struct server *httpclient_srv_raw; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 40 | #ifdef USE_OPENSSL |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 41 | static struct server *httpclient_srv_ssl; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 42 | #endif |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 43 | static struct applet httpclient_applet; |
| 44 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 45 | /* --- This part of the file implement an HTTP client over the CLI --- |
| 46 | * The functions will be starting by "hc_cli" for "httpclient cli" |
| 47 | */ |
| 48 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 49 | /* What kind of data we need to read */ |
| 50 | #define HC_CLI_F_RES_STLINE 0x01 |
| 51 | #define HC_CLI_F_RES_HDR 0x02 |
| 52 | #define HC_CLI_F_RES_BODY 0x04 |
| 53 | #define HC_CLI_F_RES_END 0x08 |
| 54 | |
| 55 | |
| 56 | /* These are the callback used by the HTTP Client when it needs to notify new |
| 57 | * data, we only sets a flag in the IO handler */ |
| 58 | |
| 59 | void hc_cli_res_stline_cb(struct httpclient *hc) |
| 60 | { |
| 61 | struct appctx *appctx = hc->caller; |
| 62 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 63 | if (!appctx) |
| 64 | return; |
| 65 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 66 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_STLINE; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 67 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void hc_cli_res_headers_cb(struct httpclient *hc) |
| 71 | { |
| 72 | struct appctx *appctx = hc->caller; |
| 73 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 74 | if (!appctx) |
| 75 | return; |
| 76 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 77 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_HDR; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 78 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void hc_cli_res_body_cb(struct httpclient *hc) |
| 82 | { |
| 83 | struct appctx *appctx = hc->caller; |
| 84 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 85 | if (!appctx) |
| 86 | return; |
| 87 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 88 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_BODY; |
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_end_cb(struct httpclient *hc) |
| 93 | { |
| 94 | struct appctx *appctx = hc->caller; |
| 95 | |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 96 | if (!appctx) |
| 97 | return; |
| 98 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 99 | appctx->ctx.cli.i0 |= HC_CLI_F_RES_END; |
William Lallemand | dfc3f89 | 2021-08-20 11:35:29 +0200 | [diff] [blame] | 100 | appctx_wakeup(appctx); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Parse an httpclient keyword on the cli: |
| 105 | * httpclient <ID> <method> <URI> |
| 106 | */ |
| 107 | static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void *private) |
| 108 | { |
| 109 | struct httpclient *hc; |
| 110 | char *err = NULL; |
| 111 | enum http_meth_t meth; |
| 112 | char *meth_str; |
| 113 | struct ist uri; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 114 | struct ist body = IST_NULL; |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 115 | |
| 116 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 117 | return 1; |
| 118 | |
| 119 | if (!*args[1] || !*args[2]) { |
| 120 | memprintf(&err, ": not enough parameters"); |
| 121 | goto err; |
| 122 | } |
| 123 | |
| 124 | meth_str = args[1]; |
| 125 | uri = ist(args[2]); |
| 126 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 127 | if (payload) |
| 128 | body = ist(payload); |
| 129 | |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 130 | meth = find_http_meth(meth_str, strlen(meth_str)); |
| 131 | |
| 132 | hc = httpclient_new(appctx, meth, uri); |
| 133 | if (!hc) { |
| 134 | goto err; |
| 135 | } |
| 136 | |
| 137 | /* update the httpclient callbacks */ |
| 138 | hc->ops.res_stline = hc_cli_res_stline_cb; |
| 139 | hc->ops.res_headers = hc_cli_res_headers_cb; |
| 140 | hc->ops.res_payload = hc_cli_res_body_cb; |
| 141 | hc->ops.res_end = hc_cli_res_end_cb; |
| 142 | |
| 143 | appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */ |
| 144 | appctx->ctx.cli.i0 = 0; |
| 145 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 146 | 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] | 147 | goto err; |
| 148 | |
| 149 | |
| 150 | if (!httpclient_start(hc)) |
| 151 | goto err; |
| 152 | |
| 153 | return 0; |
| 154 | |
| 155 | err: |
| 156 | memprintf(&err, "Can't start the HTTP client%s.\n", err ? err : ""); |
| 157 | return cli_err(appctx, err); |
| 158 | } |
| 159 | |
| 160 | /* This function dumps the content of the httpclient receive buffer |
| 161 | * on the CLI output |
| 162 | * |
| 163 | * Return 1 when the processing is finished |
| 164 | * return 0 if it needs to be called again |
| 165 | */ |
| 166 | static int hc_cli_io_handler(struct appctx *appctx) |
| 167 | { |
| 168 | struct stream_interface *si = appctx->owner; |
| 169 | struct buffer *trash = alloc_trash_chunk(); |
| 170 | struct httpclient *hc = appctx->ctx.cli.p0; |
| 171 | struct http_hdr *hdrs, *hdr; |
| 172 | |
| 173 | if (!trash) |
| 174 | goto out; |
| 175 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_STLINE) { |
William Lallemand | 614e683 | 2021-09-26 18:12:43 +0200 | [diff] [blame] | 176 | chunk_appendf(trash, "%s %d %s\n",istptr(hc->res.vsn), hc->res.status, istptr(hc->res.reason)); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 177 | if (ci_putchk(si_ic(si), trash) == -1) |
| 178 | si_rx_room_blk(si); |
| 179 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_STLINE; |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_HDR) { |
| 184 | hdrs = hc->res.hdrs; |
| 185 | for (hdr = hdrs; isttest(hdr->v); hdr++) { |
| 186 | if (!h1_format_htx_hdr(hdr->n, hdr->v, trash)) |
| 187 | goto out; |
| 188 | } |
| 189 | if (!chunk_memcat(trash, "\r\n", 2)) |
| 190 | goto out; |
| 191 | if (ci_putchk(si_ic(si), trash) == -1) |
| 192 | si_rx_room_blk(si); |
| 193 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_HDR; |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | if (appctx->ctx.cli.i0 & HC_CLI_F_RES_BODY) { |
| 198 | int ret; |
| 199 | |
| 200 | ret = httpclient_res_xfer(hc, &si_ic(si)->buf); |
| 201 | channel_add_input(si_ic(si), ret); /* forward what we put in the buffer channel */ |
| 202 | |
William Lallemand | 518878e | 2021-09-21 10:45:34 +0200 | [diff] [blame] | 203 | if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */ |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 204 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_BODY; |
| 205 | } |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | /* we must close only if F_END is the last flag */ |
| 210 | if (appctx->ctx.cli.i0 == HC_CLI_F_RES_END) { |
| 211 | si_shutw(si); |
| 212 | si_shutr(si); |
| 213 | appctx->ctx.cli.i0 &= ~HC_CLI_F_RES_END; |
| 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | out: |
| 218 | /* we didn't clear every flags, we should come back to finish things */ |
| 219 | if (appctx->ctx.cli.i0) |
| 220 | si_rx_room_blk(si); |
| 221 | |
| 222 | free_trash_chunk(trash); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static void hc_cli_release(struct appctx *appctx) |
| 227 | { |
| 228 | struct httpclient *hc = appctx->ctx.cli.p0; |
| 229 | |
| 230 | /* Everything possible was printed on the CLI, we can destroy the client */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 231 | httpclient_stop_and_destroy(hc); |
William Lallemand | 03a4eb1 | 2021-08-18 16:46:21 +0200 | [diff] [blame] | 232 | |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* register cli keywords */ |
| 237 | static struct cli_kw_list cli_kws = {{ },{ |
William Lallemand | 34b3a93 | 2021-10-19 10:58:30 +0200 | [diff] [blame] | 238 | { { "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] | 239 | { { NULL }, NULL, NULL, NULL } |
| 240 | }}; |
| 241 | |
| 242 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 243 | |
| 244 | |
| 245 | /* --- This part of the file implements the actual HTTP client API --- */ |
| 246 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Generate a simple request and fill the httpclient request buffer with it. |
| 249 | * The request contains a request line generated from the absolute <url> and |
| 250 | * <meth> as well as list of headers <hdrs>. |
| 251 | * |
| 252 | * If the buffer was filled correctly the function returns 0, if not it returns |
| 253 | * an error_code but there is no guarantee that the buffer wasn't modified. |
| 254 | */ |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 255 | 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] | 256 | { |
| 257 | struct htx_sl *sl; |
| 258 | struct htx *htx; |
| 259 | int err_code = 0; |
| 260 | struct ist meth_ist, vsn; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 261 | 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] | 262 | int i; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 263 | int foundhost = 0, foundaccept = 0, foundua = 0; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 264 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 265 | if (!b_alloc(&hc->req.buf)) |
| 266 | goto error; |
| 267 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 268 | if (meth >= HTTP_METH_OTHER) |
| 269 | goto error; |
| 270 | |
| 271 | meth_ist = http_known_methods[meth]; |
| 272 | |
| 273 | vsn = ist("HTTP/1.1"); |
| 274 | |
| 275 | htx = htx_from_buf(&hc->req.buf); |
| 276 | if (!htx) |
| 277 | goto error; |
William Lallemand | e1e045f | 2022-01-14 14:08:34 +0100 | [diff] [blame] | 278 | |
| 279 | if (!hc->ops.req_payload && !isttest(payload)) |
| 280 | flags |= HTX_SL_F_BODYLESS; |
| 281 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 282 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_ist, url, vsn); |
| 283 | if (!sl) { |
| 284 | goto error; |
| 285 | } |
| 286 | sl->info.req.meth = meth; |
| 287 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 288 | for (i = 0; hdrs && hdrs[i].n.len; i++) { |
| 289 | /* Don't check the value length because a header value may be empty */ |
| 290 | if (isttest(hdrs[i].v) == 0) |
| 291 | continue; |
| 292 | |
| 293 | if (isteqi(hdrs[i].n, ist("host"))) |
| 294 | foundhost = 1; |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 295 | else if (isteqi(hdrs[i].n, ist("accept"))) |
| 296 | foundaccept = 1; |
| 297 | else if (isteqi(hdrs[i].n, ist("user-agent"))) |
| 298 | foundua = 1; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 299 | |
| 300 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 301 | goto error; |
| 302 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 303 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 304 | if (!foundhost) { |
| 305 | /* Add Host Header from URL */ |
| 306 | if (!htx_add_header(htx, ist("Host"), ist("h"))) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 307 | goto error; |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 308 | if (!http_update_host(htx, sl, url)) |
William Lallemand | 79a3478 | 2021-09-20 16:19:15 +0200 | [diff] [blame] | 309 | goto error; |
| 310 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 311 | |
William Lallemand | bad9c8c | 2022-01-14 14:10:33 +0100 | [diff] [blame] | 312 | if (!foundaccept) { |
| 313 | if (!htx_add_header(htx, ist("Accept"), ist("*/*"))) |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | if (!foundua) { |
| 318 | if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT))) |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | |
William Lallemand | f03b53c | 2021-11-24 15:38:17 +0100 | [diff] [blame] | 323 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 324 | goto error; |
| 325 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 326 | if (isttest(payload)) { |
| 327 | /* add the payload if it can feat in the buffer, no need to set |
| 328 | * the Content-Length, the data will be sent chunked */ |
| 329 | if (!htx_add_data_atonce(htx, payload)) |
| 330 | goto error; |
| 331 | } |
| 332 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 333 | /* If req.payload was set, does not set the end of stream which *MUST* |
| 334 | * be set in the callback */ |
| 335 | if (!hc->ops.req_payload) |
| 336 | htx->flags |= HTX_FL_EOM; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 337 | |
| 338 | htx_to_buf(htx, &hc->req.buf); |
| 339 | |
| 340 | return 0; |
| 341 | error: |
| 342 | err_code |= ERR_ALERT | ERR_ABORT; |
| 343 | return err_code; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * transfer the response to the destination buffer and wakeup the HTTP client |
| 348 | * applet so it could fill again its buffer. |
| 349 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 350 | * Return the number of bytes transferred. |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 351 | */ |
| 352 | int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst) |
| 353 | { |
| 354 | int ret; |
| 355 | |
William Lallemand | 4d60184 | 2021-09-30 10:07:57 +0200 | [diff] [blame] | 356 | ret = b_force_xfer(dst, &hc->res.buf, MIN(1024, b_data(&hc->res.buf))); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 357 | /* call the client once we consumed all data */ |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 358 | if (!b_data(&hc->res.buf)) { |
| 359 | b_free(&hc->res.buf); |
| 360 | if (hc->appctx) |
| 361 | appctx_wakeup(hc->appctx); |
| 362 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /* |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 367 | * Transfer raw HTTP payload from src, and insert it into HTX format in the |
| 368 | * httpclient. |
| 369 | * |
| 370 | * Must be used to transfer the request body. |
| 371 | * Then wakeup the httpclient so it can transfer it. |
| 372 | * |
| 373 | * <end> tries to add the ending data flag if it succeed to copy all data. |
| 374 | * |
| 375 | * Return the number of bytes copied from src. |
| 376 | */ |
| 377 | int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end) |
| 378 | { |
| 379 | int ret = 0; |
| 380 | struct htx *htx; |
| 381 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 382 | if (!b_alloc(&hc->req.buf)) |
| 383 | goto error; |
| 384 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 385 | htx = htx_from_buf(&hc->req.buf); |
| 386 | if (!htx) |
| 387 | goto error; |
| 388 | |
| 389 | if (hc->appctx) |
| 390 | appctx_wakeup(hc->appctx); |
| 391 | |
| 392 | ret += htx_add_data(htx, src); |
| 393 | |
| 394 | |
| 395 | /* if we copied all the data and the end flag is set */ |
| 396 | if ((istlen(src) == ret) && end) { |
| 397 | htx->flags |= HTX_FL_EOM; |
| 398 | } |
| 399 | htx_to_buf(htx, &hc->req.buf); |
| 400 | |
| 401 | error: |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /* |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 408 | * Start the HTTP client |
| 409 | * Create the appctx, session, stream and wakeup the applet |
| 410 | * |
| 411 | * FIXME: It also fill the sockaddr with the IP address, but currently only IP |
| 412 | * in the URL are supported, it lacks a resolver. |
| 413 | * |
| 414 | * Return the <appctx> or NULL if it failed |
| 415 | */ |
| 416 | struct appctx *httpclient_start(struct httpclient *hc) |
| 417 | { |
| 418 | struct applet *applet = &httpclient_applet; |
| 419 | struct appctx *appctx; |
| 420 | struct session *sess; |
| 421 | struct stream *s; |
| 422 | int len; |
| 423 | struct split_url out; |
| 424 | |
| 425 | /* parse URI and fill sockaddr_storage */ |
| 426 | /* FIXME: use a resolver */ |
William Lallemand | 614e683 | 2021-09-26 18:12:43 +0200 | [diff] [blame] | 427 | len = url2sa(istptr(hc->req.url), istlen(hc->req.url), &hc->dst, &out); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 428 | if (len == -1) { |
William Lallemand | 614e683 | 2021-09-26 18:12:43 +0200 | [diff] [blame] | 429 | ha_alert("httpclient: cannot parse uri '%s'.\n", istptr(hc->req.url)); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 430 | goto out; |
| 431 | } |
| 432 | |
| 433 | /* The HTTP client will be created in the same thread as the caller, |
| 434 | * avoiding threading issues */ |
Willy Tarreau | e612446 | 2021-09-13 10:07:38 +0200 | [diff] [blame] | 435 | appctx = appctx_new(applet); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 436 | if (!appctx) |
| 437 | goto out; |
| 438 | |
| 439 | sess = session_new(httpclient_proxy, NULL, &appctx->obj_type); |
| 440 | if (!sess) { |
| 441 | ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__); |
| 442 | goto out_free_appctx; |
| 443 | } |
Christopher Faulet | 6ced61d | 2022-01-12 15:27:41 +0100 | [diff] [blame^] | 444 | if ((s = stream_new(sess, &appctx->obj_type, &hc->req.buf)) == NULL) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 445 | ha_alert("httpclient: Failed to initialize stream %s:%d.\n", __FUNCTION__, __LINE__); |
| 446 | goto out_free_appctx; |
| 447 | } |
| 448 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 449 | if (!sockaddr_alloc(&s->si[1].dst, &hc->dst, sizeof(hc->dst))) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 450 | ha_alert("httpclient: Failed to initialize stream in %s:%d.\n", __FUNCTION__, __LINE__); |
| 451 | goto out_free_stream; |
| 452 | } |
| 453 | |
| 454 | /* choose the SSL server or not */ |
| 455 | switch (out.scheme) { |
| 456 | case SCH_HTTP: |
| 457 | s->target = &httpclient_srv_raw->obj_type; |
| 458 | break; |
| 459 | case SCH_HTTPS: |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 460 | #ifdef USE_OPENSSL |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 461 | s->target = &httpclient_srv_ssl->obj_type; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 462 | #else |
| 463 | ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__); |
| 464 | goto out_free_stream; |
| 465 | #endif |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | |
| 469 | s->flags |= SF_ASSIGNED|SF_ADDR_SET; |
| 470 | s->si[1].flags |= SI_FL_NOLINGER; |
| 471 | s->res.flags |= CF_READ_DONTWAIT; |
| 472 | |
| 473 | /* applet is waiting for data */ |
| 474 | si_cant_get(&s->si[0]); |
| 475 | appctx_wakeup(appctx); |
| 476 | |
| 477 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 478 | hc->appctx = appctx; |
William Lallemand | b8b1370 | 2021-09-28 12:15:37 +0200 | [diff] [blame] | 479 | hc->flags |= HTTPCLIENT_FS_STARTED; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 480 | appctx->ctx.httpclient.ptr = hc; |
Christopher Faulet | 6ced61d | 2022-01-12 15:27:41 +0100 | [diff] [blame^] | 481 | |
| 482 | /* The request was transferred when the stream was created. So switch |
| 483 | * directly to REQ_BODY or RES_STLINE state |
| 484 | */ |
| 485 | 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] | 486 | |
| 487 | return appctx; |
| 488 | |
| 489 | out_free_stream: |
| 490 | LIST_DELETE(&s->list); |
| 491 | pool_free(pool_head_stream, s); |
| 492 | out_free_sess: |
| 493 | session_free(sess); |
| 494 | out_free_appctx: |
| 495 | appctx_free(appctx); |
| 496 | out: |
| 497 | |
| 498 | return NULL; |
| 499 | } |
| 500 | |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 501 | /* |
| 502 | * This function tries to destroy the httpclient if it wasn't running. |
| 503 | * If it was running, stop the client and ask it to autodestroy itself. |
| 504 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 505 | * 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] | 506 | * |
| 507 | */ |
| 508 | void httpclient_stop_and_destroy(struct httpclient *hc) |
| 509 | { |
| 510 | |
William Lallemand | b8b1370 | 2021-09-28 12:15:37 +0200 | [diff] [blame] | 511 | /* The httpclient was already stopped or never started, we can safely destroy it */ |
| 512 | if (hc->flags & HTTPCLIENT_FS_ENDED || !(hc->flags & HTTPCLIENT_FS_STARTED)) { |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 513 | httpclient_destroy(hc); |
| 514 | } else { |
| 515 | /* if the client wasn't stopped, ask for a stop and destroy */ |
| 516 | hc->flags |= (HTTPCLIENT_FA_AUTOKILL | HTTPCLIENT_FA_STOP); |
| 517 | if (hc->appctx) |
| 518 | appctx_wakeup(hc->appctx); |
| 519 | } |
| 520 | } |
| 521 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 522 | /* Free the httpclient */ |
| 523 | void httpclient_destroy(struct httpclient *hc) |
| 524 | { |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 525 | struct http_hdr *hdrs; |
| 526 | |
| 527 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 528 | if (!hc) |
| 529 | return; |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 530 | |
William Lallemand | 2a87900 | 2021-10-05 15:50:45 +0200 | [diff] [blame] | 531 | /* we should never destroy a client which was started but not stopped */ |
| 532 | BUG_ON(httpclient_started(hc) && !httpclient_ended(hc)); |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 533 | |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 534 | /* request */ |
| 535 | istfree(&hc->req.url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 536 | b_free(&hc->req.buf); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 537 | /* response */ |
| 538 | istfree(&hc->res.vsn); |
| 539 | istfree(&hc->res.reason); |
| 540 | hdrs = hc->res.hdrs; |
| 541 | while (hdrs && isttest(hdrs->n)) { |
| 542 | istfree(&hdrs->n); |
| 543 | istfree(&hdrs->v); |
| 544 | hdrs++; |
| 545 | } |
| 546 | ha_free(&hc->res.hdrs); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 547 | b_free(&hc->res.buf); |
William Lallemand | 03f5a1c | 2021-09-27 15:17:47 +0200 | [diff] [blame] | 548 | |
| 549 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 550 | free(hc); |
| 551 | |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | /* Allocate an httpclient and its buffers |
| 556 | * Return NULL on failure */ |
| 557 | struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct ist url) |
| 558 | { |
| 559 | struct httpclient *hc; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 560 | |
| 561 | hc = calloc(1, sizeof(*hc)); |
| 562 | if (!hc) |
| 563 | goto err; |
| 564 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 565 | hc->req.buf = BUF_NULL; |
| 566 | hc->res.buf = BUF_NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 567 | hc->caller = caller; |
William Lallemand | 67b7784 | 2021-11-10 16:57:25 +0100 | [diff] [blame] | 568 | hc->req.url = istdup(url); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 569 | hc->req.meth = meth; |
| 570 | |
| 571 | return hc; |
| 572 | |
| 573 | err: |
| 574 | httpclient_destroy(hc); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | static void httpclient_applet_io_handler(struct appctx *appctx) |
| 579 | { |
| 580 | struct httpclient *hc = appctx->ctx.httpclient.ptr; |
| 581 | struct stream_interface *si = appctx->owner; |
| 582 | struct stream *s = si_strm(si); |
| 583 | struct channel *req = &s->req; |
| 584 | struct channel *res = &s->res; |
| 585 | struct htx_blk *blk = NULL; |
| 586 | struct htx *htx; |
William Lallemand | b702030 | 2021-08-20 11:24:13 +0200 | [diff] [blame] | 587 | struct htx_sl *sl = NULL; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 588 | int32_t pos; |
| 589 | uint32_t hdr_num; |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 590 | int ret; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 591 | |
| 592 | |
| 593 | while (1) { |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 594 | |
| 595 | /* required to stop */ |
| 596 | if (hc->flags & HTTPCLIENT_FA_STOP) |
| 597 | goto end; |
| 598 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 599 | switch(appctx->st0) { |
| 600 | |
| 601 | case HTTPCLIENT_S_REQ: |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 602 | /* we know that the buffer is empty here, since |
| 603 | * it's the first call, we can freely copy the |
| 604 | * request from the httpclient buffer */ |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 605 | 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] | 606 | if (!ret) |
| 607 | goto more; |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 608 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 609 | if (!b_data(&hc->req.buf)) |
| 610 | b_free(&hc->req.buf); |
| 611 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 612 | htx = htx_from_buf(&req->buf); |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 613 | if (!htx) |
| 614 | goto more; |
| 615 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 616 | channel_add_input(req, htx->data); |
| 617 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 618 | if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */ |
| 619 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
| 620 | else |
| 621 | appctx->st0 = HTTPCLIENT_S_REQ_BODY; |
| 622 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 623 | goto more; /* we need to leave the IO handler once we wrote the request */ |
| 624 | break; |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 625 | case HTTPCLIENT_S_REQ_BODY: |
| 626 | /* call the payload callback */ |
| 627 | { |
| 628 | if (hc->ops.req_payload) { |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 629 | |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 630 | /* call the request callback */ |
| 631 | hc->ops.req_payload(hc); |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 632 | /* check if the request buffer is empty */ |
| 633 | |
| 634 | htx = htx_from_buf(&req->buf); |
| 635 | if (!htx_is_empty(htx)) |
| 636 | goto more; |
| 637 | /* Here htx_to_buf() will set buffer data to 0 because |
| 638 | * the HTX is empty, and allow us to do an xfer. |
| 639 | */ |
| 640 | htx_to_buf(htx, &req->buf); |
| 641 | |
| 642 | ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf)); |
| 643 | if (!ret) |
| 644 | goto more; |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 645 | |
| 646 | if (!b_data(&hc->req.buf)) |
| 647 | b_free(&hc->req.buf); |
| 648 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 649 | htx = htx_from_buf(&req->buf); |
| 650 | if (!htx) |
| 651 | goto more; |
| 652 | |
| 653 | channel_add_input(req, htx->data); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 654 | } |
| 655 | |
William Lallemand | db8a1f3 | 2021-11-08 16:55:14 +0100 | [diff] [blame] | 656 | htx = htx_from_buf(&req->buf); |
William Lallemand | 0da616e | 2021-10-28 15:34:26 +0200 | [diff] [blame] | 657 | if (!htx) |
| 658 | goto more; |
| 659 | |
| 660 | /* if the request contains the HTX_FL_EOM, we finished the request part. */ |
| 661 | if (htx->flags & HTX_FL_EOM) |
| 662 | appctx->st0 = HTTPCLIENT_S_RES_STLINE; |
| 663 | |
| 664 | goto more; /* we need to leave the IO handler once we wrote the request */ |
| 665 | } |
| 666 | break; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 667 | |
| 668 | case HTTPCLIENT_S_RES_STLINE: |
| 669 | /* copy the start line in the hc structure,then remove the htx block */ |
| 670 | if (!b_data(&res->buf)) |
| 671 | goto more; |
| 672 | htx = htxbuf(&res->buf); |
| 673 | if (!htx) |
| 674 | goto more; |
| 675 | blk = htx_get_first_blk(htx); |
| 676 | if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL)) |
| 677 | sl = htx_get_blk_ptr(htx, blk); |
| 678 | if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP))) |
| 679 | goto more; |
| 680 | |
| 681 | /* copy the status line in the httpclient */ |
| 682 | hc->res.status = sl->info.res.status; |
| 683 | hc->res.vsn = istdup(htx_sl_res_vsn(sl)); |
| 684 | hc->res.reason = istdup(htx_sl_res_reason(sl)); |
| 685 | co_htx_remove_blk(res, htx, blk); |
| 686 | /* caller callback */ |
| 687 | if (hc->ops.res_stline) |
| 688 | hc->ops.res_stline(hc); |
| 689 | |
| 690 | /* if there is no HTX data anymore and the EOM flag is |
| 691 | * set, leave (no body) */ |
| 692 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) |
| 693 | appctx->st0 = HTTPCLIENT_S_RES_END; |
| 694 | else |
| 695 | appctx->st0 = HTTPCLIENT_S_RES_HDR; |
| 696 | break; |
| 697 | |
| 698 | case HTTPCLIENT_S_RES_HDR: |
| 699 | /* first copy the headers in a local hdrs |
| 700 | * structure, once we the total numbers of the |
| 701 | * header we allocate the right size and copy |
| 702 | * them. The htx block of the headers are |
| 703 | * removed each time one is read */ |
| 704 | { |
| 705 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 706 | |
| 707 | if (!b_data(&res->buf)) |
| 708 | goto more; |
| 709 | htx = htxbuf(&res->buf); |
| 710 | if (!htx) |
| 711 | goto more; |
| 712 | |
| 713 | hdr_num = 0; |
| 714 | |
| 715 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 716 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 717 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 718 | |
| 719 | if (type == HTX_BLK_EOH) { |
| 720 | hdrs[hdr_num].n = IST_NULL; |
| 721 | hdrs[hdr_num].v = IST_NULL; |
| 722 | co_htx_remove_blk(res, htx, blk); |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | if (type != HTX_BLK_HDR) |
| 727 | continue; |
| 728 | |
| 729 | hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk)); |
| 730 | hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk)); |
| 731 | if (!isttest(hdrs[hdr_num].v) || !isttest(hdrs[hdr_num].n)) |
| 732 | goto end; |
| 733 | co_htx_remove_blk(res, htx, blk); |
| 734 | hdr_num++; |
| 735 | } |
| 736 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 737 | if (hdr_num) { |
| 738 | /* alloc and copy the headers in the httpclient struct */ |
| 739 | hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs)); |
| 740 | if (!hc->res.hdrs) |
| 741 | goto end; |
| 742 | memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 743 | |
William Lallemand | 0d6f779 | 2021-08-20 11:59:49 +0200 | [diff] [blame] | 744 | /* caller callback */ |
| 745 | if (hc->ops.res_headers) |
| 746 | hc->ops.res_headers(hc); |
| 747 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 748 | |
| 749 | /* if there is no HTX data anymore and the EOM flag is |
| 750 | * set, leave (no body) */ |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 751 | if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 752 | appctx->st0 = HTTPCLIENT_S_RES_END; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 753 | } else { |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 754 | appctx->st0 = HTTPCLIENT_S_RES_BODY; |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 755 | } |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 756 | } |
| 757 | break; |
| 758 | |
| 759 | case HTTPCLIENT_S_RES_BODY: |
| 760 | /* |
| 761 | * The IO handler removes the htx blocks in the response buffer and |
| 762 | * push them in the hc->res.buf buffer in a raw format. |
| 763 | */ |
| 764 | htx = htxbuf(&res->buf); |
| 765 | if (!htx || htx_is_empty(htx)) |
| 766 | goto more; |
| 767 | |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 768 | if (!b_alloc(&hc->res.buf)) |
| 769 | goto more; |
| 770 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 771 | if (b_full(&hc->res.buf)) |
Christopher Faulet | 600985d | 2022-01-12 11:14:08 +0100 | [diff] [blame] | 772 | goto process_data; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 773 | |
| 774 | /* decapsule the htx data to raw data */ |
| 775 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 776 | enum htx_blk_type type; |
| 777 | |
| 778 | blk = htx_get_blk(htx, pos); |
| 779 | type = htx_get_blk_type(blk); |
| 780 | if (type == HTX_BLK_DATA) { |
| 781 | struct ist v = htx_get_blk_value(htx, blk); |
| 782 | |
| 783 | if ((b_room(&hc->res.buf) < v.len) ) |
| 784 | goto process_data; |
| 785 | |
| 786 | __b_putblk(&hc->res.buf, v.ptr, v.len); |
| 787 | co_htx_remove_blk(res, htx, blk); |
| 788 | /* the data must be processed by the caller in the receive phase */ |
| 789 | if (hc->ops.res_payload) |
| 790 | hc->ops.res_payload(hc); |
| 791 | } else { |
| 792 | /* remove any block which is not a data block */ |
| 793 | co_htx_remove_blk(res, htx, blk); |
| 794 | } |
| 795 | } |
| 796 | /* if not finished, should be called again */ |
| 797 | if (!(htx->flags & HTX_FL_EOM)) |
| 798 | goto more; |
| 799 | |
| 800 | /* end of message, we should quit */ |
| 801 | appctx->st0 = HTTPCLIENT_S_RES_END; |
| 802 | break; |
| 803 | |
| 804 | case HTTPCLIENT_S_RES_END: |
| 805 | goto end; |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | process_data: |
| 811 | |
| 812 | si_rx_chan_rdy(si); |
| 813 | |
| 814 | return; |
| 815 | more: |
| 816 | /* There was not enough data in the response channel */ |
| 817 | |
| 818 | si_rx_room_blk(si); |
| 819 | |
| 820 | if (appctx->st0 == HTTPCLIENT_S_RES_END) |
| 821 | goto end; |
| 822 | |
| 823 | /* The state machine tries to handle as much data as possible, if there |
| 824 | * isn't any data to handle and a shutdown is detected, let's stop |
| 825 | * everything */ |
| 826 | if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) || |
| 827 | (res->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 828 | goto end; |
| 829 | } |
| 830 | return; |
| 831 | |
| 832 | end: |
| 833 | if (hc->ops.res_end) |
| 834 | hc->ops.res_end(hc); |
| 835 | si_shutw(si); |
| 836 | si_shutr(si); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | static void httpclient_applet_release(struct appctx *appctx) |
| 841 | { |
| 842 | struct httpclient *hc = appctx->ctx.httpclient.ptr; |
| 843 | |
William Lallemand | 1123dde | 2021-09-21 10:58:10 +0200 | [diff] [blame] | 844 | /* mark the httpclient as ended */ |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 845 | hc->flags |= HTTPCLIENT_FS_ENDED; |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 846 | /* the applet is leaving, remove the ptr so we don't try to call it |
| 847 | * again from the caller */ |
| 848 | hc->appctx = NULL; |
| 849 | |
William Lallemand | ecb83e1 | 2021-09-28 11:00:43 +0200 | [diff] [blame] | 850 | |
| 851 | /* destroy the httpclient when set to autotokill */ |
| 852 | if (hc->flags & HTTPCLIENT_FA_AUTOKILL) { |
| 853 | httpclient_destroy(hc); |
| 854 | } |
| 855 | |
William Lallemand | 33b0d09 | 2021-08-13 16:05:53 +0200 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
| 859 | /* HTTP client applet */ |
| 860 | static struct applet httpclient_applet = { |
| 861 | .obj_type = OBJ_TYPE_APPLET, |
| 862 | .name = "<HTTPCLIENT>", |
| 863 | .fct = httpclient_applet_io_handler, |
| 864 | .release = httpclient_applet_release, |
| 865 | }; |
| 866 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 867 | /* |
| 868 | * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP, |
| 869 | * the other for HTTPS. |
| 870 | */ |
| 871 | |
| 872 | static int httpclient_init() |
| 873 | { |
| 874 | int err_code = 0; |
| 875 | char *errmsg = NULL; |
| 876 | |
| 877 | httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg); |
| 878 | if (!httpclient_proxy) { |
| 879 | err_code |= ERR_ALERT | ERR_FATAL; |
| 880 | goto err; |
| 881 | } |
| 882 | |
Willy Tarreau | 0e72e40 | 2021-08-20 10:23:12 +0200 | [diff] [blame] | 883 | proxy_preset_defaults(httpclient_proxy); |
| 884 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 885 | httpclient_proxy->options2 |= PR_O2_INDEPSTR; |
| 886 | httpclient_proxy->mode = PR_MODE_HTTP; |
| 887 | httpclient_proxy->maxconn = 0; |
| 888 | httpclient_proxy->accept = NULL; |
| 889 | httpclient_proxy->timeout.client = TICK_ETERNITY; |
| 890 | /* The HTTP Client use the "option httplog" with the global log server */ |
| 891 | httpclient_proxy->conf.logformat_string = default_http_log_format; |
| 892 | httpclient_proxy->http_needed = 1; |
| 893 | |
| 894 | /* clear HTTP server */ |
| 895 | httpclient_srv_raw = new_server(httpclient_proxy); |
| 896 | if (!httpclient_srv_raw) { |
| 897 | err_code |= ERR_ALERT | ERR_FATAL; |
| 898 | memprintf(&errmsg, "out of memory."); |
| 899 | goto err; |
| 900 | } |
| 901 | |
| 902 | httpclient_srv_raw->iweight = 0; |
| 903 | httpclient_srv_raw->uweight = 0; |
| 904 | httpclient_srv_raw->xprt = xprt_get(XPRT_RAW); |
| 905 | httpclient_srv_raw->id = strdup("<HTTPCLIENT>"); |
| 906 | if (!httpclient_srv_raw->id) |
| 907 | goto err; |
| 908 | |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 909 | #ifdef USE_OPENSSL |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 910 | /* SSL HTTP server */ |
| 911 | httpclient_srv_ssl = new_server(httpclient_proxy); |
| 912 | if (!httpclient_srv_ssl) { |
| 913 | memprintf(&errmsg, "out of memory."); |
| 914 | err_code |= ERR_ALERT | ERR_FATAL; |
| 915 | goto err; |
| 916 | } |
| 917 | httpclient_srv_ssl->iweight = 0; |
| 918 | httpclient_srv_ssl->uweight = 0; |
| 919 | httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL); |
| 920 | httpclient_srv_ssl->use_ssl = 1; |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 921 | httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>"); |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 922 | if (!httpclient_srv_ssl->id) |
| 923 | goto err; |
| 924 | |
William Lallemand | cfcbe9e | 2021-08-24 17:15:58 +0200 | [diff] [blame] | 925 | httpclient_srv_ssl->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 926 | #endif |
William Lallemand | cfcbe9e | 2021-08-24 17:15:58 +0200 | [diff] [blame] | 927 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 928 | /* add the proxy in the proxy list only if everything is successful */ |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 929 | httpclient_proxy->next = proxies_list; |
| 930 | proxies_list = httpclient_proxy; |
| 931 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 932 | /* link the 2 servers in the proxy */ |
| 933 | httpclient_srv_raw->next = httpclient_proxy->srv; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 934 | httpclient_proxy->srv = httpclient_srv_raw; |
| 935 | |
| 936 | #ifdef USE_OPENSSL |
| 937 | httpclient_srv_ssl->next = httpclient_proxy->srv; |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 938 | httpclient_proxy->srv = httpclient_srv_ssl; |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 939 | #endif |
| 940 | |
William Lallemand | 211c967 | 2021-08-24 17:18:13 +0200 | [diff] [blame] | 941 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 942 | return 0; |
| 943 | |
| 944 | err: |
| 945 | ha_alert("httpclient: cannot initialize.\n"); |
| 946 | free(errmsg); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 947 | srv_drop(httpclient_srv_raw); |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 948 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 949 | srv_drop(httpclient_srv_ssl); |
William Lallemand | 957ab13 | 2021-08-24 18:33:28 +0200 | [diff] [blame] | 950 | #endif |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 951 | free_proxy(httpclient_proxy); |
| 952 | return err_code; |
| 953 | } |
| 954 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 955 | static int httpclient_cfg_postparser() |
| 956 | { |
| 957 | struct logsrv *logsrv; |
| 958 | struct proxy *curproxy = httpclient_proxy; |
| 959 | |
| 960 | /* copy logs from "global" log list */ |
| 961 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
| 962 | struct logsrv *node = malloc(sizeof(*node)); |
| 963 | |
| 964 | if (!node) { |
| 965 | ha_alert("httpclient: cannot allocate memory.\n"); |
| 966 | goto err; |
| 967 | } |
| 968 | |
| 969 | memcpy(node, logsrv, sizeof(*node)); |
| 970 | LIST_INIT(&node->list); |
| 971 | LIST_APPEND(&curproxy->logsrvs, &node->list); |
| 972 | } |
| 973 | if (curproxy->conf.logformat_string) { |
| 974 | char *err = NULL; |
| 975 | |
| 976 | curproxy->conf.args.ctx = ARGC_LOG; |
| 977 | if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat, |
| 978 | LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES, |
| 979 | SMP_VAL_FE_LOG_END, &err)) { |
| 980 | ha_alert("httpclient: failed to parse log-format : %s.\n", err); |
| 981 | free(err); |
| 982 | goto err; |
| 983 | } |
| 984 | curproxy->conf.args.file = NULL; |
| 985 | curproxy->conf.args.line = 0; |
| 986 | } |
| 987 | return 0; |
| 988 | err: |
| 989 | return 1; |
| 990 | } |
| 991 | |
William Lallemand | 83614a9 | 2021-08-13 14:47:57 +0200 | [diff] [blame] | 992 | /* initialize the proxy and servers for the HTTP client */ |
| 993 | |
| 994 | INITCALL0(STG_REGISTER, httpclient_init); |
| 995 | REGISTER_CONFIG_POSTPARSER("httpclient", httpclient_cfg_postparser); |