blob: fe85f050b80489f2e75cb0d9609955090d427f22 [file] [log] [blame]
William Lallemand83614a92021-08-13 14:47:57 +02001/*
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 Lallemand83614a92021-08-13 14:47:57 +020014
William Lallemand2a8fe8b2021-08-20 14:25:15 +020015#include <haproxy/api.h>
William Lallemand33b0d092021-08-13 16:05:53 +020016#include <haproxy/applet.h>
17#include <haproxy/cli.h>
18#include <haproxy/dynbuf.h>
William Lallemand83614a92021-08-13 14:47:57 +020019#include <haproxy/cfgparse.h>
20#include <haproxy/connection.h>
21#include <haproxy/global.h>
William Lallemand0da616e2021-10-28 15:34:26 +020022#include <haproxy/istbuf.h>
William Lallemand33b0d092021-08-13 16:05:53 +020023#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 Lallemand83614a92021-08-13 14:47:57 +020028#include <haproxy/log.h>
29#include <haproxy/proxy.h>
William Lallemand2a8fe8b2021-08-20 14:25:15 +020030#include <haproxy/server.h>
Willy Tarreau1df20422021-10-06 11:28:24 +020031#include <haproxy/ssl_sock-t.h>
William Lallemand33b0d092021-08-13 16:05:53 +020032#include <haproxy/stream_interface.h>
William Lallemand83614a92021-08-13 14:47:57 +020033#include <haproxy/tools.h>
34
35#include <string.h>
36
37
38static struct proxy *httpclient_proxy;
39static struct server *httpclient_srv_raw;
William Lallemand957ab132021-08-24 18:33:28 +020040#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +020041static struct server *httpclient_srv_ssl;
William Lallemand957ab132021-08-24 18:33:28 +020042#endif
William Lallemand33b0d092021-08-13 16:05:53 +020043static struct applet httpclient_applet;
44
William Lallemand03a4eb12021-08-18 16:46:21 +020045/* --- 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
49static struct http_hdr default_httpclient_hdrs[2] = {
William Lallemand2484da52021-08-19 15:55:19 +020050 { .n = IST("User-Agent"), .v = IST("HAProxy") },
William Lallemand03a4eb12021-08-18 16:46:21 +020051 { .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
65void hc_cli_res_stline_cb(struct httpclient *hc)
66{
67 struct appctx *appctx = hc->caller;
68
William Lallemanddfc3f892021-08-20 11:35:29 +020069 if (!appctx)
70 return;
71
William Lallemand03a4eb12021-08-18 16:46:21 +020072 appctx->ctx.cli.i0 |= HC_CLI_F_RES_STLINE;
William Lallemanddfc3f892021-08-20 11:35:29 +020073 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020074}
75
76void hc_cli_res_headers_cb(struct httpclient *hc)
77{
78 struct appctx *appctx = hc->caller;
79
William Lallemanddfc3f892021-08-20 11:35:29 +020080 if (!appctx)
81 return;
82
William Lallemand03a4eb12021-08-18 16:46:21 +020083 appctx->ctx.cli.i0 |= HC_CLI_F_RES_HDR;
William Lallemanddfc3f892021-08-20 11:35:29 +020084 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020085}
86
87void hc_cli_res_body_cb(struct httpclient *hc)
88{
89 struct appctx *appctx = hc->caller;
90
William Lallemanddfc3f892021-08-20 11:35:29 +020091 if (!appctx)
92 return;
93
William Lallemand03a4eb12021-08-18 16:46:21 +020094 appctx->ctx.cli.i0 |= HC_CLI_F_RES_BODY;
William Lallemanddfc3f892021-08-20 11:35:29 +020095 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +020096}
97
98void hc_cli_res_end_cb(struct httpclient *hc)
99{
100 struct appctx *appctx = hc->caller;
101
William Lallemanddfc3f892021-08-20 11:35:29 +0200102 if (!appctx)
103 return;
104
William Lallemand03a4eb12021-08-18 16:46:21 +0200105 appctx->ctx.cli.i0 |= HC_CLI_F_RES_END;
William Lallemanddfc3f892021-08-20 11:35:29 +0200106 appctx_wakeup(appctx);
William Lallemand03a4eb12021-08-18 16:46:21 +0200107}
108
109/*
110 * Parse an httpclient keyword on the cli:
111 * httpclient <ID> <method> <URI>
112 */
113static 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 Lallemanddec25c32021-10-25 19:48:37 +0200120 struct ist body = IST_NULL;
William Lallemand03a4eb12021-08-18 16:46:21 +0200121
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 Lallemanddec25c32021-10-25 19:48:37 +0200133 if (payload)
134 body = ist(payload);
135
William Lallemand03a4eb12021-08-18 16:46:21 +0200136 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 Lallemanddec25c32021-10-25 19:48:37 +0200152 if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, default_httpclient_hdrs, body) != ERR_NONE)
William Lallemand03a4eb12021-08-18 16:46:21 +0200153 goto err;
154
155
156 if (!httpclient_start(hc))
157 goto err;
158
159 return 0;
160
161err:
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 */
172static 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 Lallemand614e6832021-09-26 18:12:43 +0200182 chunk_appendf(trash, "%s %d %s\n",istptr(hc->res.vsn), hc->res.status, istptr(hc->res.reason));
William Lallemand03a4eb12021-08-18 16:46:21 +0200183 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 Lallemand518878e2021-09-21 10:45:34 +0200209 if (!httpclient_data(hc)) {/* remove the flag if the buffer was emptied */
William Lallemand03a4eb12021-08-18 16:46:21 +0200210 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
223out:
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
232static 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 Lallemandecb83e12021-09-28 11:00:43 +0200237 httpclient_stop_and_destroy(hc);
William Lallemand03a4eb12021-08-18 16:46:21 +0200238
239 return;
240}
241
242/* register cli keywords */
243static struct cli_kw_list cli_kws = {{ },{
William Lallemand34b3a932021-10-19 10:58:30 +0200244 { { "httpclient", NULL }, "httpclient <method> <URI> : launch an HTTP request", hc_cli_parse, hc_cli_io_handler, hc_cli_release, NULL, ACCESS_EXPERT},
William Lallemand03a4eb12021-08-18 16:46:21 +0200245 { { NULL }, NULL, NULL, NULL }
246}};
247
248INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
249
250
251/* --- This part of the file implements the actual HTTP client API --- */
252
William Lallemand33b0d092021-08-13 16:05:53 +0200253/*
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 Lallemanddec25c32021-10-25 19:48:37 +0200261int 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 Lallemand33b0d092021-08-13 16:05:53 +0200262{
263 struct htx_sl *sl;
264 struct htx *htx;
265 int err_code = 0;
266 struct ist meth_ist, vsn;
William Lallemanddec25c32021-10-25 19:48:37 +0200267 unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM;
William Lallemand33b0d092021-08-13 16:05:53 +0200268
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 Lallemand0f41c382021-11-02 15:22:10 +0100286 if (!htx_add_header(htx, ist("Host"), ist("h")))
William Lallemand4463b172021-08-24 17:53:03 +0200287 goto error;
William Lallemand33b0d092021-08-13 16:05:53 +0200288 if (!http_update_host(htx, sl, url))
289 goto error;
290
291 /* add the headers and EOH */
William Lallemand79a34782021-09-20 16:19:15 +0200292 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 Lallemand33b0d092021-08-13 16:05:53 +0200299
William Lallemanddec25c32021-10-25 19:48:37 +0200300 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 Lallemand0da616e2021-10-28 15:34:26 +0200307 /* 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 Lallemand33b0d092021-08-13 16:05:53 +0200311
312 htx_to_buf(htx, &hc->req.buf);
313
314 return 0;
315error:
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 Shipitsinbd6b4be2021-10-15 16:18:21 +0500324 * Return the number of bytes transferred.
William Lallemand33b0d092021-08-13 16:05:53 +0200325 */
326int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst)
327{
328 int ret;
329
William Lallemand4d601842021-09-30 10:07:57 +0200330 ret = b_force_xfer(dst, &hc->res.buf, MIN(1024, b_data(&hc->res.buf)));
William Lallemand33b0d092021-08-13 16:05:53 +0200331 /* 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 Lallemand0da616e2021-10-28 15:34:26 +0200338 * 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 */
348int 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
369error:
370
371 return ret;
372}
373
374
375/*
William Lallemand33b0d092021-08-13 16:05:53 +0200376 * 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 */
384struct 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 Lallemand614e6832021-09-26 18:12:43 +0200395 len = url2sa(istptr(hc->req.url), istlen(hc->req.url), &hc->dst, &out);
William Lallemand33b0d092021-08-13 16:05:53 +0200396 if (len == -1) {
William Lallemand614e6832021-09-26 18:12:43 +0200397 ha_alert("httpclient: cannot parse uri '%s'.\n", istptr(hc->req.url));
William Lallemand33b0d092021-08-13 16:05:53 +0200398 goto out;
399 }
400
401 /* The HTTP client will be created in the same thread as the caller,
402 * avoiding threading issues */
Willy Tarreaue6124462021-09-13 10:07:38 +0200403 appctx = appctx_new(applet);
William Lallemand33b0d092021-08-13 16:05:53 +0200404 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 Faulet16f16af2021-10-27 09:34:56 +0200417 if (!sockaddr_alloc(&s->si[1].dst, &hc->dst, sizeof(hc->dst))) {
William Lallemand33b0d092021-08-13 16:05:53 +0200418 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 Lallemand957ab132021-08-24 18:33:28 +0200428#ifdef USE_OPENSSL
William Lallemand33b0d092021-08-13 16:05:53 +0200429 s->target = &httpclient_srv_ssl->obj_type;
William Lallemand957ab132021-08-24 18:33:28 +0200430#else
431 ha_alert("httpclient: OpenSSL is not available %s:%d.\n", __FUNCTION__, __LINE__);
432 goto out_free_stream;
433#endif
William Lallemand33b0d092021-08-13 16:05:53 +0200434 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 Lallemandb8b13702021-09-28 12:15:37 +0200447 hc->flags |= HTTPCLIENT_FS_STARTED;
William Lallemand33b0d092021-08-13 16:05:53 +0200448 appctx->ctx.httpclient.ptr = hc;
449 appctx->st0 = HTTPCLIENT_S_REQ;
450
451 return appctx;
452
453out_free_stream:
454 LIST_DELETE(&s->list);
455 pool_free(pool_head_stream, s);
456out_free_sess:
457 session_free(sess);
458out_free_appctx:
459 appctx_free(appctx);
460out:
461
462 return NULL;
463}
464
William Lallemandecb83e12021-09-28 11:00:43 +0200465/*
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 Shipitsinbd6b4be2021-10-15 16:18:21 +0500469 * Once this function is used, all pointer sto the client must be removed
William Lallemandecb83e12021-09-28 11:00:43 +0200470 *
471 */
472void httpclient_stop_and_destroy(struct httpclient *hc)
473{
474
William Lallemandb8b13702021-09-28 12:15:37 +0200475 /* 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 Lallemandecb83e12021-09-28 11:00:43 +0200477 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 Lallemand33b0d092021-08-13 16:05:53 +0200486/* Free the httpclient */
487void httpclient_destroy(struct httpclient *hc)
488{
William Lallemand03f5a1c2021-09-27 15:17:47 +0200489 struct http_hdr *hdrs;
490
491
William Lallemand33b0d092021-08-13 16:05:53 +0200492 if (!hc)
493 return;
William Lallemandecb83e12021-09-28 11:00:43 +0200494
William Lallemand2a879002021-10-05 15:50:45 +0200495 /* we should never destroy a client which was started but not stopped */
496 BUG_ON(httpclient_started(hc) && !httpclient_ended(hc));
William Lallemandecb83e12021-09-28 11:00:43 +0200497
William Lallemand03f5a1c2021-09-27 15:17:47 +0200498 /* request */
499 istfree(&hc->req.url);
William Lallemand33b0d092021-08-13 16:05:53 +0200500 b_free(&hc->req.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200501 /* 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 Lallemand33b0d092021-08-13 16:05:53 +0200511 b_free(&hc->res.buf);
William Lallemand03f5a1c2021-09-27 15:17:47 +0200512
513
William Lallemand33b0d092021-08-13 16:05:53 +0200514 free(hc);
515
516 return;
517}
518
519/* Allocate an httpclient and its buffers
520 * Return NULL on failure */
521struct 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
543err:
544 httpclient_destroy(hc);
545 return NULL;
546}
547
548static 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 Lallemandb7020302021-08-20 11:24:13 +0200557 struct htx_sl *sl = NULL;
William Lallemand33b0d092021-08-13 16:05:53 +0200558 int32_t pos;
559 uint32_t hdr_num;
William Lallemand933fe392021-11-04 09:45:58 +0100560 int ret;
William Lallemand33b0d092021-08-13 16:05:53 +0200561
562
563 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200564
565 /* required to stop */
566 if (hc->flags & HTTPCLIENT_FA_STOP)
567 goto end;
568
William Lallemand33b0d092021-08-13 16:05:53 +0200569 switch(appctx->st0) {
570
571 case HTTPCLIENT_S_REQ:
William Lallemanddb8a1f32021-11-08 16:55:14 +0100572 /* we know that the buffer is empty here, since
573 * it's the first call, we can freely copy the
574 * request from the httpclient buffer */
William Lallemand933fe392021-11-04 09:45:58 +0100575 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
William Lallemanddb8a1f32021-11-08 16:55:14 +0100576 if (!ret)
577 goto more;
William Lallemand933fe392021-11-04 09:45:58 +0100578
William Lallemanddb8a1f32021-11-08 16:55:14 +0100579 htx = htx_from_buf(&req->buf);
William Lallemand933fe392021-11-04 09:45:58 +0100580 if (!htx)
581 goto more;
582
William Lallemanddb8a1f32021-11-08 16:55:14 +0100583 channel_add_input(req, htx->data);
584
William Lallemand933fe392021-11-04 09:45:58 +0100585 if (htx->flags & HTX_FL_EOM) /* check if a body need to be added */
586 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
587 else
588 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
589
William Lallemand33b0d092021-08-13 16:05:53 +0200590 goto more; /* we need to leave the IO handler once we wrote the request */
591 break;
William Lallemand0da616e2021-10-28 15:34:26 +0200592 case HTTPCLIENT_S_REQ_BODY:
593 /* call the payload callback */
594 {
595 if (hc->ops.req_payload) {
William Lallemand0da616e2021-10-28 15:34:26 +0200596
William Lallemand0da616e2021-10-28 15:34:26 +0200597 /* call the request callback */
598 hc->ops.req_payload(hc);
William Lallemanddb8a1f32021-11-08 16:55:14 +0100599 /* check if the request buffer is empty */
600
601 htx = htx_from_buf(&req->buf);
602 if (!htx_is_empty(htx))
603 goto more;
604 /* Here htx_to_buf() will set buffer data to 0 because
605 * the HTX is empty, and allow us to do an xfer.
606 */
607 htx_to_buf(htx, &req->buf);
608
609 ret = b_xfer(&req->buf, &hc->req.buf, b_data(&hc->req.buf));
610 if (!ret)
611 goto more;
612 htx = htx_from_buf(&req->buf);
613 if (!htx)
614 goto more;
615
616 channel_add_input(req, htx->data);
William Lallemand0da616e2021-10-28 15:34:26 +0200617 }
618
William Lallemanddb8a1f32021-11-08 16:55:14 +0100619 htx = htx_from_buf(&req->buf);
William Lallemand0da616e2021-10-28 15:34:26 +0200620 if (!htx)
621 goto more;
622
623 /* if the request contains the HTX_FL_EOM, we finished the request part. */
624 if (htx->flags & HTX_FL_EOM)
625 appctx->st0 = HTTPCLIENT_S_RES_STLINE;
626
627 goto more; /* we need to leave the IO handler once we wrote the request */
628 }
629 break;
William Lallemand33b0d092021-08-13 16:05:53 +0200630
631 case HTTPCLIENT_S_RES_STLINE:
632 /* copy the start line in the hc structure,then remove the htx block */
633 if (!b_data(&res->buf))
634 goto more;
635 htx = htxbuf(&res->buf);
636 if (!htx)
637 goto more;
638 blk = htx_get_first_blk(htx);
639 if (blk && (htx_get_blk_type(blk) == HTX_BLK_RES_SL))
640 sl = htx_get_blk_ptr(htx, blk);
641 if (!sl || (!(sl->flags & HTX_SL_F_IS_RESP)))
642 goto more;
643
644 /* copy the status line in the httpclient */
645 hc->res.status = sl->info.res.status;
646 hc->res.vsn = istdup(htx_sl_res_vsn(sl));
647 hc->res.reason = istdup(htx_sl_res_reason(sl));
648 co_htx_remove_blk(res, htx, blk);
649 /* caller callback */
650 if (hc->ops.res_stline)
651 hc->ops.res_stline(hc);
652
653 /* if there is no HTX data anymore and the EOM flag is
654 * set, leave (no body) */
655 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM)
656 appctx->st0 = HTTPCLIENT_S_RES_END;
657 else
658 appctx->st0 = HTTPCLIENT_S_RES_HDR;
659 break;
660
661 case HTTPCLIENT_S_RES_HDR:
662 /* first copy the headers in a local hdrs
663 * structure, once we the total numbers of the
664 * header we allocate the right size and copy
665 * them. The htx block of the headers are
666 * removed each time one is read */
667 {
668 struct http_hdr hdrs[global.tune.max_http_hdr];
669
670 if (!b_data(&res->buf))
671 goto more;
672 htx = htxbuf(&res->buf);
673 if (!htx)
674 goto more;
675
676 hdr_num = 0;
677
678 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
679 struct htx_blk *blk = htx_get_blk(htx, pos);
680 enum htx_blk_type type = htx_get_blk_type(blk);
681
682 if (type == HTX_BLK_EOH) {
683 hdrs[hdr_num].n = IST_NULL;
684 hdrs[hdr_num].v = IST_NULL;
685 co_htx_remove_blk(res, htx, blk);
686 break;
687 }
688
689 if (type != HTX_BLK_HDR)
690 continue;
691
692 hdrs[hdr_num].n = istdup(htx_get_blk_name(htx, blk));
693 hdrs[hdr_num].v = istdup(htx_get_blk_value(htx, blk));
694 if (!isttest(hdrs[hdr_num].v) || !isttest(hdrs[hdr_num].n))
695 goto end;
696 co_htx_remove_blk(res, htx, blk);
697 hdr_num++;
698 }
699
William Lallemand0d6f7792021-08-20 11:59:49 +0200700 if (hdr_num) {
701 /* alloc and copy the headers in the httpclient struct */
702 hc->res.hdrs = calloc((hdr_num + 1), sizeof(*hc->res.hdrs));
703 if (!hc->res.hdrs)
704 goto end;
705 memcpy(hc->res.hdrs, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
William Lallemand33b0d092021-08-13 16:05:53 +0200706
William Lallemand0d6f7792021-08-20 11:59:49 +0200707 /* caller callback */
708 if (hc->ops.res_headers)
709 hc->ops.res_headers(hc);
710 }
William Lallemand33b0d092021-08-13 16:05:53 +0200711
712 /* if there is no HTX data anymore and the EOM flag is
713 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200714 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200715 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200716 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200717 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200718 }
William Lallemand33b0d092021-08-13 16:05:53 +0200719 }
720 break;
721
722 case HTTPCLIENT_S_RES_BODY:
723 /*
724 * The IO handler removes the htx blocks in the response buffer and
725 * push them in the hc->res.buf buffer in a raw format.
726 */
727 htx = htxbuf(&res->buf);
728 if (!htx || htx_is_empty(htx))
729 goto more;
730
731 if (b_full(&hc->res.buf))
732 goto process_data;
733
734 /* decapsule the htx data to raw data */
735 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
736 enum htx_blk_type type;
737
738 blk = htx_get_blk(htx, pos);
739 type = htx_get_blk_type(blk);
740 if (type == HTX_BLK_DATA) {
741 struct ist v = htx_get_blk_value(htx, blk);
742
743 if ((b_room(&hc->res.buf) < v.len) )
744 goto process_data;
745
746 __b_putblk(&hc->res.buf, v.ptr, v.len);
747 co_htx_remove_blk(res, htx, blk);
748 /* the data must be processed by the caller in the receive phase */
749 if (hc->ops.res_payload)
750 hc->ops.res_payload(hc);
751 } else {
752 /* remove any block which is not a data block */
753 co_htx_remove_blk(res, htx, blk);
754 }
755 }
756 /* if not finished, should be called again */
757 if (!(htx->flags & HTX_FL_EOM))
758 goto more;
759
760 /* end of message, we should quit */
761 appctx->st0 = HTTPCLIENT_S_RES_END;
762 break;
763
764 case HTTPCLIENT_S_RES_END:
765 goto end;
766 break;
767 }
768 }
769
770process_data:
771
772 si_rx_chan_rdy(si);
773
774 return;
775more:
776 /* There was not enough data in the response channel */
777
778 si_rx_room_blk(si);
779
780 if (appctx->st0 == HTTPCLIENT_S_RES_END)
781 goto end;
782
783 /* The state machine tries to handle as much data as possible, if there
784 * isn't any data to handle and a shutdown is detected, let's stop
785 * everything */
786 if ((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) ||
787 (res->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
788 goto end;
789 }
790 return;
791
792end:
793 if (hc->ops.res_end)
794 hc->ops.res_end(hc);
795 si_shutw(si);
796 si_shutr(si);
797 return;
798}
799
800static void httpclient_applet_release(struct appctx *appctx)
801{
802 struct httpclient *hc = appctx->ctx.httpclient.ptr;
803
William Lallemand1123dde2021-09-21 10:58:10 +0200804 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +0200805 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +0200806 /* the applet is leaving, remove the ptr so we don't try to call it
807 * again from the caller */
808 hc->appctx = NULL;
809
William Lallemandecb83e12021-09-28 11:00:43 +0200810
811 /* destroy the httpclient when set to autotokill */
812 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
813 httpclient_destroy(hc);
814 }
815
William Lallemand33b0d092021-08-13 16:05:53 +0200816 return;
817}
818
819/* HTTP client applet */
820static struct applet httpclient_applet = {
821 .obj_type = OBJ_TYPE_APPLET,
822 .name = "<HTTPCLIENT>",
823 .fct = httpclient_applet_io_handler,
824 .release = httpclient_applet_release,
825};
826
William Lallemand83614a92021-08-13 14:47:57 +0200827/*
828 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
829 * the other for HTTPS.
830 */
831
832static int httpclient_init()
833{
834 int err_code = 0;
835 char *errmsg = NULL;
836
837 httpclient_proxy = alloc_new_proxy("<HTTPCLIENT>", PR_CAP_LISTEN|PR_CAP_INT, &errmsg);
838 if (!httpclient_proxy) {
839 err_code |= ERR_ALERT | ERR_FATAL;
840 goto err;
841 }
842
Willy Tarreau0e72e402021-08-20 10:23:12 +0200843 proxy_preset_defaults(httpclient_proxy);
844
William Lallemand83614a92021-08-13 14:47:57 +0200845 httpclient_proxy->options2 |= PR_O2_INDEPSTR;
846 httpclient_proxy->mode = PR_MODE_HTTP;
847 httpclient_proxy->maxconn = 0;
848 httpclient_proxy->accept = NULL;
849 httpclient_proxy->timeout.client = TICK_ETERNITY;
850 /* The HTTP Client use the "option httplog" with the global log server */
851 httpclient_proxy->conf.logformat_string = default_http_log_format;
852 httpclient_proxy->http_needed = 1;
853
854 /* clear HTTP server */
855 httpclient_srv_raw = new_server(httpclient_proxy);
856 if (!httpclient_srv_raw) {
857 err_code |= ERR_ALERT | ERR_FATAL;
858 memprintf(&errmsg, "out of memory.");
859 goto err;
860 }
861
862 httpclient_srv_raw->iweight = 0;
863 httpclient_srv_raw->uweight = 0;
864 httpclient_srv_raw->xprt = xprt_get(XPRT_RAW);
865 httpclient_srv_raw->id = strdup("<HTTPCLIENT>");
866 if (!httpclient_srv_raw->id)
867 goto err;
868
William Lallemand957ab132021-08-24 18:33:28 +0200869#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +0200870 /* SSL HTTP server */
871 httpclient_srv_ssl = new_server(httpclient_proxy);
872 if (!httpclient_srv_ssl) {
873 memprintf(&errmsg, "out of memory.");
874 err_code |= ERR_ALERT | ERR_FATAL;
875 goto err;
876 }
877 httpclient_srv_ssl->iweight = 0;
878 httpclient_srv_ssl->uweight = 0;
879 httpclient_srv_ssl->xprt = xprt_get(XPRT_SSL);
880 httpclient_srv_ssl->use_ssl = 1;
William Lallemand211c9672021-08-24 17:18:13 +0200881 httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>");
William Lallemand83614a92021-08-13 14:47:57 +0200882 if (!httpclient_srv_ssl->id)
883 goto err;
884
William Lallemandcfcbe9e2021-08-24 17:15:58 +0200885 httpclient_srv_ssl->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
William Lallemand957ab132021-08-24 18:33:28 +0200886#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +0200887
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500888 /* add the proxy in the proxy list only if everything is successful */
William Lallemand83614a92021-08-13 14:47:57 +0200889 httpclient_proxy->next = proxies_list;
890 proxies_list = httpclient_proxy;
891
William Lallemand211c9672021-08-24 17:18:13 +0200892 /* link the 2 servers in the proxy */
893 httpclient_srv_raw->next = httpclient_proxy->srv;
William Lallemand957ab132021-08-24 18:33:28 +0200894 httpclient_proxy->srv = httpclient_srv_raw;
895
896#ifdef USE_OPENSSL
897 httpclient_srv_ssl->next = httpclient_proxy->srv;
William Lallemand211c9672021-08-24 17:18:13 +0200898 httpclient_proxy->srv = httpclient_srv_ssl;
William Lallemand957ab132021-08-24 18:33:28 +0200899#endif
900
William Lallemand211c9672021-08-24 17:18:13 +0200901
William Lallemand83614a92021-08-13 14:47:57 +0200902 return 0;
903
904err:
905 ha_alert("httpclient: cannot initialize.\n");
906 free(errmsg);
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +0200907 srv_drop(httpclient_srv_raw);
William Lallemand957ab132021-08-24 18:33:28 +0200908#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +0200909 srv_drop(httpclient_srv_ssl);
William Lallemand957ab132021-08-24 18:33:28 +0200910#endif
William Lallemand83614a92021-08-13 14:47:57 +0200911 free_proxy(httpclient_proxy);
912 return err_code;
913}
914
William Lallemand83614a92021-08-13 14:47:57 +0200915static int httpclient_cfg_postparser()
916{
917 struct logsrv *logsrv;
918 struct proxy *curproxy = httpclient_proxy;
919
920 /* copy logs from "global" log list */
921 list_for_each_entry(logsrv, &global.logsrvs, list) {
922 struct logsrv *node = malloc(sizeof(*node));
923
924 if (!node) {
925 ha_alert("httpclient: cannot allocate memory.\n");
926 goto err;
927 }
928
929 memcpy(node, logsrv, sizeof(*node));
930 LIST_INIT(&node->list);
931 LIST_APPEND(&curproxy->logsrvs, &node->list);
932 }
933 if (curproxy->conf.logformat_string) {
934 char *err = NULL;
935
936 curproxy->conf.args.ctx = ARGC_LOG;
937 if (!parse_logformat_string(curproxy->conf.logformat_string, curproxy, &curproxy->logformat,
938 LOG_OPT_MANDATORY|LOG_OPT_MERGE_SPACES,
939 SMP_VAL_FE_LOG_END, &err)) {
940 ha_alert("httpclient: failed to parse log-format : %s.\n", err);
941 free(err);
942 goto err;
943 }
944 curproxy->conf.args.file = NULL;
945 curproxy->conf.args.line = 0;
946 }
947 return 0;
948err:
949 return 1;
950}
951
William Lallemand83614a92021-08-13 14:47:57 +0200952/* initialize the proxy and servers for the HTTP client */
953
954INITCALL0(STG_REGISTER, httpclient_init);
955REGISTER_CONFIG_POSTPARSER("httpclient", httpclient_cfg_postparser);