blob: 35de71af767c656cd3aa1b2ff78151ada9711fa0 [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;
560
561
562 while (1) {
William Lallemandecb83e12021-09-28 11:00:43 +0200563
564 /* required to stop */
565 if (hc->flags & HTTPCLIENT_FA_STOP)
566 goto end;
567
William Lallemand33b0d092021-08-13 16:05:53 +0200568 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 Lallemand0da616e2021-10-28 15:34:26 +0200577 appctx->st0 = HTTPCLIENT_S_REQ_BODY;
William Lallemand33b0d092021-08-13 16:05:53 +0200578 goto more; /* we need to leave the IO handler once we wrote the request */
579 break;
William Lallemand0da616e2021-10-28 15:34:26 +0200580 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 Lallemand33b0d092021-08-13 16:05:53 +0200605
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 Lallemand0d6f7792021-08-20 11:59:49 +0200675 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 Lallemand33b0d092021-08-13 16:05:53 +0200681
William Lallemand0d6f7792021-08-20 11:59:49 +0200682 /* caller callback */
683 if (hc->ops.res_headers)
684 hc->ops.res_headers(hc);
685 }
William Lallemand33b0d092021-08-13 16:05:53 +0200686
687 /* if there is no HTX data anymore and the EOM flag is
688 * set, leave (no body) */
William Lallemand1123dde2021-09-21 10:58:10 +0200689 if (htx_is_empty(htx) && htx->flags & HTX_FL_EOM) {
William Lallemand33b0d092021-08-13 16:05:53 +0200690 appctx->st0 = HTTPCLIENT_S_RES_END;
William Lallemand1123dde2021-09-21 10:58:10 +0200691 } else {
William Lallemand33b0d092021-08-13 16:05:53 +0200692 appctx->st0 = HTTPCLIENT_S_RES_BODY;
William Lallemand1123dde2021-09-21 10:58:10 +0200693 }
William Lallemand33b0d092021-08-13 16:05:53 +0200694 }
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
745process_data:
746
747 si_rx_chan_rdy(si);
748
749 return;
750more:
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
767end:
768 if (hc->ops.res_end)
769 hc->ops.res_end(hc);
770 si_shutw(si);
771 si_shutr(si);
772 return;
773}
774
775static void httpclient_applet_release(struct appctx *appctx)
776{
777 struct httpclient *hc = appctx->ctx.httpclient.ptr;
778
William Lallemand1123dde2021-09-21 10:58:10 +0200779 /* mark the httpclient as ended */
William Lallemandecb83e12021-09-28 11:00:43 +0200780 hc->flags |= HTTPCLIENT_FS_ENDED;
William Lallemand33b0d092021-08-13 16:05:53 +0200781 /* 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 Lallemandecb83e12021-09-28 11:00:43 +0200785
786 /* destroy the httpclient when set to autotokill */
787 if (hc->flags & HTTPCLIENT_FA_AUTOKILL) {
788 httpclient_destroy(hc);
789 }
790
William Lallemand33b0d092021-08-13 16:05:53 +0200791 return;
792}
793
794/* HTTP client applet */
795static 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 Lallemand83614a92021-08-13 14:47:57 +0200802/*
803 * Initialize the proxy for the HTTP client with 2 servers, one for raw HTTP,
804 * the other for HTTPS.
805 */
806
807static 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 Tarreau0e72e402021-08-20 10:23:12 +0200818 proxy_preset_defaults(httpclient_proxy);
819
William Lallemand83614a92021-08-13 14:47:57 +0200820 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 Lallemand957ab132021-08-24 18:33:28 +0200844#ifdef USE_OPENSSL
William Lallemand83614a92021-08-13 14:47:57 +0200845 /* 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 Lallemand211c9672021-08-24 17:18:13 +0200856 httpclient_srv_ssl->id = strdup("<HTTPSCLIENT>");
William Lallemand83614a92021-08-13 14:47:57 +0200857 if (!httpclient_srv_ssl->id)
858 goto err;
859
William Lallemandcfcbe9e2021-08-24 17:15:58 +0200860 httpclient_srv_ssl->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
William Lallemand957ab132021-08-24 18:33:28 +0200861#endif
William Lallemandcfcbe9e2021-08-24 17:15:58 +0200862
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +0500863 /* add the proxy in the proxy list only if everything is successful */
William Lallemand83614a92021-08-13 14:47:57 +0200864 httpclient_proxy->next = proxies_list;
865 proxies_list = httpclient_proxy;
866
William Lallemand211c9672021-08-24 17:18:13 +0200867 /* link the 2 servers in the proxy */
868 httpclient_srv_raw->next = httpclient_proxy->srv;
William Lallemand957ab132021-08-24 18:33:28 +0200869 httpclient_proxy->srv = httpclient_srv_raw;
870
871#ifdef USE_OPENSSL
872 httpclient_srv_ssl->next = httpclient_proxy->srv;
William Lallemand211c9672021-08-24 17:18:13 +0200873 httpclient_proxy->srv = httpclient_srv_ssl;
William Lallemand957ab132021-08-24 18:33:28 +0200874#endif
875
William Lallemand211c9672021-08-24 17:18:13 +0200876
William Lallemand83614a92021-08-13 14:47:57 +0200877 return 0;
878
879err:
880 ha_alert("httpclient: cannot initialize.\n");
881 free(errmsg);
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +0200882 srv_drop(httpclient_srv_raw);
William Lallemand957ab132021-08-24 18:33:28 +0200883#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +0200884 srv_drop(httpclient_srv_ssl);
William Lallemand957ab132021-08-24 18:33:28 +0200885#endif
William Lallemand83614a92021-08-13 14:47:57 +0200886 free_proxy(httpclient_proxy);
887 return err_code;
888}
889
William Lallemand83614a92021-08-13 14:47:57 +0200890static 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;
923err:
924 return 1;
925}
926
William Lallemand83614a92021-08-13 14:47:57 +0200927/* initialize the proxy and servers for the HTTP client */
928
929INITCALL0(STG_REGISTER, httpclient_init);
930REGISTER_CONFIG_POSTPARSER("httpclient", httpclient_cfg_postparser);