BUG/MINOR: httpclient: set default Accept and User-Agent headers

Some servers require at least an Accept and a User-Agent header in the
request. This patch sets some default value.

Must be backported in 2.5.
This commit is contained in:
William Lallemand 2022-01-14 14:10:33 +01:00
parent e1e045f4d7
commit bad9c8cac4
2 changed files with 18 additions and 8 deletions

View File

@ -51,5 +51,6 @@ enum {
HTTPCLIENT_S_RES_END, HTTPCLIENT_S_RES_END,
}; };
#define HTTPCLIENT_USERAGENT "HAProxy"
#endif /* ! _HAPROXY_HTTCLIENT__T_H */ #endif /* ! _HAPROXY_HTTCLIENT__T_H */

View File

@ -46,12 +46,6 @@ static struct applet httpclient_applet;
* The functions will be starting by "hc_cli" for "httpclient cli" * The functions will be starting by "hc_cli" for "httpclient cli"
*/ */
static struct http_hdr default_httpclient_hdrs[2] = {
{ .n = IST("User-Agent"), .v = IST("HAProxy") },
{ .n = IST_NULL, .v = IST_NULL },
};
/* What kind of data we need to read */ /* What kind of data we need to read */
#define HC_CLI_F_RES_STLINE 0x01 #define HC_CLI_F_RES_STLINE 0x01
#define HC_CLI_F_RES_HDR 0x02 #define HC_CLI_F_RES_HDR 0x02
@ -149,7 +143,7 @@ static int hc_cli_parse(char **args, char *payload, struct appctx *appctx, void
appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */ appctx->ctx.cli.p0 = hc; /* store the httpclient ptr in the applet */
appctx->ctx.cli.i0 = 0; appctx->ctx.cli.i0 = 0;
if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, default_httpclient_hdrs, body) != ERR_NONE) if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, NULL, body) != ERR_NONE)
goto err; goto err;
@ -266,7 +260,7 @@ int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_me
struct ist meth_ist, vsn; struct ist meth_ist, vsn;
unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM; unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM;
int i; int i;
int foundhost = 0; int foundhost = 0, foundaccept = 0, foundua = 0;
if (meth >= HTTP_METH_OTHER) if (meth >= HTTP_METH_OTHER)
goto error; goto error;
@ -295,6 +289,10 @@ int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_me
if (isteqi(hdrs[i].n, ist("host"))) if (isteqi(hdrs[i].n, ist("host")))
foundhost = 1; foundhost = 1;
else if (isteqi(hdrs[i].n, ist("accept")))
foundaccept = 1;
else if (isteqi(hdrs[i].n, ist("user-agent")))
foundua = 1;
if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
goto error; goto error;
@ -308,6 +306,17 @@ int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_me
goto error; goto error;
} }
if (!foundaccept) {
if (!htx_add_header(htx, ist("Accept"), ist("*/*")))
goto error;
}
if (!foundua) {
if (!htx_add_header(htx, ist("User-Agent"), ist(HTTPCLIENT_USERAGENT)))
goto error;
}
if (!htx_add_endof(htx, HTX_BLK_EOH)) if (!htx_add_endof(htx, HTX_BLK_EOH))
goto error; goto error;