]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy/checks: Move parsing of httpchk option in checks.c
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 9 Apr 2020 12:48:48 +0000 (14:48 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 27 Apr 2020 07:39:38 +0000 (09:39 +0200)
Parsing of the proxy directive "option httpchk" have been moved in checks.c.

include/common/defaults.h
include/proto/checks.h
src/cfgparse-listen.c
src/checks.c

index 0653a5310d191bebba291102a63ccac2437dada4..335082518559e49e53309e985db95636f90a10f3 100644 (file)
 #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  ""
 
 
index 4dba34a6755f27ed48a88f49887209758c973871..cc0ab0b80f1247ee32f4f6fce923fb9d5c7c9314 100644 (file)
@@ -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);
 
index 1111b743f46fb5c479393babc382d953619bf9dc..4c8444c3b1c6e1baacfb11ad97d33bc9c165c264 100644 (file)
@@ -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")) {
index a94a29cd2444e38fe1688269875c57e2c3a7b51a..ceb53ea58529c19fca50e0087c3169bc9d93399f 100644 (file)
@@ -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,