]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: httpclient: set default Accept and User-Agent headers
authorWilliam Lallemand <wlallemand@haproxy.org>
Fri, 14 Jan 2022 13:10:33 +0000 (14:10 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 14 Jan 2022 19:46:21 +0000 (20:46 +0100)
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.

include/haproxy/http_client-t.h
src/http_client.c

index 6e820c42b1997c6cbe521f0e4cfc7bd615693fb4..7112f88e5811f81ab2bf652ab35add9514333acc 100644 (file)
@@ -51,5 +51,6 @@ enum {
        HTTPCLIENT_S_RES_END,
 };
 
+#define HTTPCLIENT_USERAGENT "HAProxy"
 
 #endif /* ! _HAPROXY_HTTCLIENT__T_H */
index 2fb1b486fe1dcebc59c5762ce6536c9c8cd5757c..9b460263db0cbbee67b51c6412c16a87a63ff70a 100644 (file)
@@ -46,12 +46,6 @@ static struct applet httpclient_applet;
  * 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 */
 #define HC_CLI_F_RES_STLINE     0x01
 #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.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;
 
 
@@ -266,7 +260,7 @@ int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_me
        struct ist meth_ist, vsn;
        unsigned int flags = HTX_SL_F_VER_11 | HTX_SL_F_NORMALIZED_URI | HTX_SL_F_HAS_SCHM;
        int i;
-       int foundhost = 0;
+       int foundhost = 0, foundaccept = 0, foundua = 0;
 
        if (meth >= HTTP_METH_OTHER)
                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")))
                        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))
                        goto error;
@@ -308,6 +306,17 @@ int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_me
                        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))
                goto error;