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.
diff --git a/include/haproxy/http_client-t.h b/include/haproxy/http_client-t.h
index 6e820c4..7112f88 100644
--- a/include/haproxy/http_client-t.h
+++ b/include/haproxy/http_client-t.h
@@ -51,5 +51,6 @@
HTTPCLIENT_S_RES_END,
};
+#define HTTPCLIENT_USERAGENT "HAProxy"
#endif /* ! _HAPROXY_HTTCLIENT__T_H */
diff --git a/src/http_client.c b/src/http_client.c
index 2fb1b48..9b46026 100644
--- a/src/http_client.c
+++ b/src/http_client.c
@@ -46,12 +46,6 @@
* 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 @@
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 @@
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 @@
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 @@
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;