]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http-htx: Add functions to create HTX redirect message
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 16 Jan 2020 14:51:59 +0000 (15:51 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 20 Jan 2020 14:18:45 +0000 (15:18 +0100)
http_parse_errorloc() may now be used to create an HTTP 302 or 303 redirect
message with a specific url passed as parameter. A parameter is used to known if
it is a 302 or a 303 redirect. A status code is passed as parameter. It must be
one of the supported HTTP error codes to be valid. Otherwise an error is
returned. It aims to be used to parse "errorloc" directives. It relies on
http_load_errormsg() to do most of the job, ie converting it in HTX.

include/proto/http_htx.h
src/http_htx.c

index 8bf1b99db4cd9c8f030990fe700faf3e7bd1d276..0cec9db5875748748af541d001dc7bd46b258f52 100644 (file)
@@ -50,6 +50,8 @@ unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
 int http_str_to_htx(struct buffer *buf, struct ist raw);
 
 int http_load_errorfile(const char *file, struct buffer *buf, char **errmsg);
+int http_load_errormsg(const struct ist msg, struct buffer *buf, char **errmsg);
 int http_parse_errorfile(int status, const char *file, struct buffer *buf, char **errmsg);
+int http_parse_errorloc(int errloc, int status, const char *url, struct buffer *buf, char **errmsg);
 
 #endif /* _PROTO_HTTP_HTX_H */
index fbe25022768f25476f747b0a28a381e1da2a9944..2b368a4ec8bf561e3049858983ef64eb6b492cbf 100644 (file)
@@ -890,6 +890,26 @@ int http_load_errorfile(const char *file, struct buffer *buf, char **errmsg)
        return ret;
 }
 
+/* Convert the raw http message <msg> into an HTX message. On success, the
+ * result is stored in <buf> and 1 is returned. On error, 0 is returned and an
+ * error message is written into the <errmsg> buffer. It is this function
+ * responsibility to allocate <buf> and to release it if an error occurred.
+ */
+int http_load_errormsg(const struct ist msg, struct buffer *buf, char **errmsg)
+{
+       int ret = 0;
+
+       /* Convert the error file into an HTX message */
+       if (!http_str_to_htx(buf, msg)) {
+               memprintf(errmsg, "unable to convert message in HTX.");
+               goto out;
+       }
+       ret = 1;
+
+  out:
+       return ret;
+}
+
 
 /* This function parses the raw HTTP error file <file> for the status code
  * <status>. On success, it returns the HTTP_ERR_* value corresponding to the
@@ -913,6 +933,46 @@ int http_parse_errorfile(int status, const char *file, struct buffer *buf, char
        return ret;
 }
 
+/* This function creates HTX error message corresponding to a redirect message
+ * for the status code <status>. <url> is used as location url for the
+ * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. On
+ * success, it returns the HTTP_ERR_* value corresponding to the specified
+ * status code and it allocated and fills the buffer <buf> with the HTX
+ * message. On error, it returns -1 and nothing is allocated.
+ */
+int http_parse_errorloc(int errloc, int status, const char *url, struct buffer *buf, char **errmsg)
+{
+       const char *msg;
+       char *err = NULL;
+       int rc, errlen;
+       int ret = -1;
+
+       for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
+               if (http_err_codes[rc] == status) {
+                       /* Create the error message */
+                       msg = (errloc == 302 ? HTTP_302 : HTTP_303);
+                       errlen = strlen(msg) + strlen(url) + 5;
+                       err = malloc(errlen);
+                       if (!err) {
+                               memprintf(errmsg, "out of memory.");
+                               goto out;
+                       }
+                       errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
+
+                       /* Load it */
+                       if (http_load_errormsg(ist2(err, errlen), buf, errmsg))
+                               ret = rc;
+                       break;
+               }
+       }
+
+       if (rc >= HTTP_ERR_SIZE)
+               memprintf(errmsg, "status code '%d' not handled.", status);
+out:
+       free(err);
+       return ret;
+}
+
 /************************************************************************/
 /*                             HTX sample fetches                       */
 /************************************************************************/