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