From: Christopher Faulet Date: Thu, 9 Apr 2020 12:48:48 +0000 (+0200) Subject: MINOR: proxy/checks: Move parsing of httpchk option in checks.c X-Git-Tag: v2.2-dev7~114 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6c2a743538ab92ae70e8fbd82203a5a486a0f598;p=thirdparty%2Fhaproxy.git MINOR: proxy/checks: Move parsing of httpchk option in checks.c Parsing of the proxy directive "option httpchk" have been moved in checks.c. --- diff --git a/include/common/defaults.h b/include/common/defaults.h index 0653a5310d..3350825185 100644 --- a/include/common/defaults.h +++ b/include/common/defaults.h @@ -175,7 +175,6 @@ #define DEF_RISETIME 2 #define DEF_AGENT_FALLTIME 1 #define DEF_AGENT_RISETIME 1 -#define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n" #define DEF_CHECK_PATH "" diff --git a/include/proto/checks.h b/include/proto/checks.h index 4dba34a675..cc0ab0b80f 100644 --- a/include/proto/checks.h +++ b/include/proto/checks.h @@ -83,6 +83,8 @@ int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, st const char *file, int line); int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, const char *file, int line); +int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, + const char *file, int line); int set_srv_agent_send(struct server *srv, const char *send); diff --git a/src/cfgparse-listen.c b/src/cfgparse-listen.c index 1111b743f4..4c8444c3b1 100644 --- a/src/cfgparse-listen.c +++ b/src/cfgparse-listen.c @@ -2330,70 +2330,8 @@ stats_error_parsing: curproxy->options |= PR_O_TCP_SRV_KA; } else if (!strcmp(args[1], "httpchk")) { - if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL)) - err_code |= ERR_WARN; - - /* use HTTP request to check servers' health */ - free(curproxy->check_req); - free(curproxy->check_hdrs); - free(curproxy->check_body); - curproxy->check_req = curproxy->check_hdrs = curproxy->check_body = NULL; - curproxy->check_len = curproxy->check_hdrs_len = curproxy->check_body_len = 0; - curproxy->options2 &= ~PR_O2_CHK_ANY; - curproxy->options2 |= PR_O2_HTTP_CHK; - if (!*args[2]) { /* no argument */ - curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ - curproxy->check_len = strlen(DEF_CHECK_REQ); - } else if (!*args[3]) { /* one argument : URI */ - int reqlen = strlen(args[2]) + strlen("OPTIONS HTTP/1.0\r\n") + 1; - curproxy->check_req = malloc(reqlen); - curproxy->check_len = snprintf(curproxy->check_req, reqlen, - "OPTIONS %s HTTP/1.0\r\n", args[2]); /* URI to use */ - } else if (!*args[4]) { /* two arguments : METHOD URI */ - int reqlen = strlen(args[2]) + strlen(args[3]) + strlen(" HTTP/1.0\r\n") + 1; - - curproxy->check_req = malloc(reqlen); - curproxy->check_len = snprintf(curproxy->check_req, reqlen, - "%s %s HTTP/1.0\r\n", args[2], args[3]); - } else { /* 3 arguments : METHOD URI HTTP_VER */ - char *vsn = args[4]; - char *hdrs = strstr(vsn, "\r\n"); - char *body = strstr(vsn, "\r\n\r\n"); - - if (hdrs || body) { - ha_warning("parsing [%s:%d]: '%s %s' : hiding headers or body at the end of the version string is deprecated." - " Please, consider to use 'http-check send' directive instead.\n", - file, linenum, args[0], args[1]); - err_code |= ERR_WARN; - } - - if (hdrs == body) - hdrs = NULL; - if (hdrs) { - *hdrs = '\0'; - hdrs += 2; - } - if (body) { - *body = '\0'; - body += 4; - } - - curproxy->check_len = strlen(args[2]) + strlen(args[3]) + strlen(vsn) + 4; - curproxy->check_req = malloc(curproxy->check_len+1); - snprintf(curproxy->check_req, curproxy->check_len+1, "%s %s %s\r\n", args[2], args[3], vsn); - - if (hdrs) { - curproxy->check_hdrs_len = strlen(hdrs) + 2; - curproxy->check_hdrs = malloc(curproxy->check_hdrs_len+1); - snprintf(curproxy->check_hdrs, curproxy->check_hdrs_len+1, "%s\r\n", hdrs); - } - - if (body) { - curproxy->check_body_len = strlen(body); - curproxy->check_body = strdup(body); - } - } - if (alertif_too_many_args_idx(3, 1, file, linenum, args, &err_code)) + err_code |= proxy_parse_httpchk_opt(args, 0, curproxy, &defproxy, file, linenum); + if (err_code & ERR_FATAL) goto out; } else if (!strcmp(args[1], "ssl-hello-chk")) { diff --git a/src/checks.c b/src/checks.c index a94a29cd24..ceb53ea585 100644 --- a/src/checks.c +++ b/src/checks.c @@ -6043,6 +6043,88 @@ int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, st goto out; } +int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, + const char *file, int line) +{ + static const char *http_req = "OPTIONS / HTTP/1.0\r\n"; + int err_code = 0; + + if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) + err_code |= ERR_WARN; + + if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) + goto out; + + /* use HTTP request to check servers' health */ + free(curpx->check_req); + free(curpx->check_hdrs); + free(curpx->check_body); + + curpx->check_req = curpx->check_hdrs = curpx->check_body = NULL; + curpx->check_len = curpx->check_hdrs_len = curpx->check_body_len = 0; + curpx->options2 &= ~PR_O2_CHK_ANY; + curpx->options2 |= PR_O2_HTTP_CHK; + + cur_arg += 2; + if (!*args[cur_arg]) { /* no argument */ + curpx->check_req = strdup(http_req); /* default request */ + curpx->check_len = strlen(http_req); + } + else if (!*args[cur_arg+1]) { /* one argument : URI */ + curpx->check_len = strlen(args[cur_arg]) + strlen("OPTIONS HTTP/1.0\r\n"); + curpx->check_req = malloc(curpx->check_len+1); + curpx->check_len = snprintf(curpx->check_req, curpx->check_len+1, + "OPTIONS %s HTTP/1.0\r\n", args[cur_arg]); + } + else if (!*args[cur_arg+2]) { /* two arguments : METHOD URI */ + curpx->check_len = strlen(args[cur_arg]) + strlen(args[cur_arg+1]) + strlen(" HTTP/1.0\r\n") + 1; + curpx->check_req = malloc(curpx->check_len+1); + curpx->check_len = snprintf(curpx->check_req, curpx->check_len+1, + "%s %s HTTP/1.0\r\n", args[cur_arg], args[cur_arg+1]); + } + else { /* 3 arguments : METHOD URI HTTP_VER */ + char *hdrs = strstr(args[cur_arg+2], "\r\n"); + char *body = strstr(args[cur_arg+2], "\r\n\r\n"); + + if (hdrs || body) { + ha_warning("parsing [%s:%d]: '%s %s' : hiding headers or body at the end of the version string is deprecated." + " Please, consider to use 'http-check send' directive instead.\n", + file, line, args[0], args[1]); + err_code |= ERR_WARN; + } + + if (hdrs == body) + hdrs = NULL; + if (hdrs) { + *hdrs = '\0'; + hdrs += 2; + } + if (body) { + *body = '\0'; + body += 4; + } + + curpx->check_len = strlen(args[cur_arg]) + strlen(args[cur_arg+1]) + strlen(args[cur_arg+2]) + 4; + curpx->check_req = malloc(curpx->check_len+1); + snprintf(curpx->check_req, curpx->check_len+1, "%s %s %s\r\n", + args[cur_arg], args[cur_arg+1], args[cur_arg+2]); + if (hdrs) { + curpx->check_hdrs_len = strlen(hdrs) + 2; + curpx->check_hdrs = malloc(curpx->check_hdrs_len+1); + snprintf(curpx->check_hdrs, curpx->check_hdrs_len+1, "%s\r\n", hdrs); + } + if (body) { + curpx->check_body_len = strlen(body); + curpx->check_body = strdup(body); + } + } + out: + return err_code; + + error: + err_code |= ERR_ALERT | ERR_FATAL; + goto out; +} /* Parse the "addr" server keyword */ static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,