]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proto_htx: Add the internal function htx_del_hdr_value
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 24 Oct 2018 09:05:36 +0000 (11:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 18 Nov 2018 21:08:55 +0000 (22:08 +0100)
It is more or less the same than del_hdr_value but adapted to be called from HTX
analyzers. The main changes is that it takes pointers on the start and the end
of the header value.

src/proto_htx.c

index f229ab3b4d73875a7a5db0cc2b52393845e9b704..19ab0a3709b8d0054ce5975a8e4d23610eed6e35 100644 (file)
@@ -38,6 +38,7 @@ static void htx_end_request(struct stream *s);
 static void htx_end_response(struct stream *s);
 
 static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
+static int htx_del_hdr_value(char *start, char *end, char **from, char *next);
 static size_t htx_fmt_req_line(const union h1_sl sl, char *str, size_t len);
 static void htx_debug_stline(const char *dir, struct stream *s, const union h1_sl sl);
 static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
@@ -2879,6 +2880,57 @@ static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap
        }
 }
 
+/* Delete a value in a header between delimiters <from> and <next>. The header
+ * itself is delimited by <start> and <end> pointers. The number of characters
+ * displaced is returned, and the pointer to the first delimiter is updated if
+ * required. The function tries as much as possible to respect the following
+ * principles :
+ * - replace <from> delimiter by the <next> one unless <from> points to <start>,
+ *   in which case <next> is simply removed
+ * - set exactly one space character after the new first delimiter, unless there
+ *   are not enough characters in the block being moved to do so.
+ * - remove unneeded spaces before the previous delimiter and after the new
+ *   one.
+ *
+ * It is the caller's responsibility to ensure that :
+ *   - <from> points to a valid delimiter or <start> ;
+ *   - <next> points to a valid delimiter or <end> ;
+ *   - there are non-space chars before <from>.
+ */
+static int htx_del_hdr_value(char *start, char *end, char **from, char *next)
+{
+       char *prev = *from;
+
+       if (prev == start) {
+               /* We're removing the first value. eat the semicolon, if <next>
+                * is lower than <end> */
+               if (next < end)
+                       next++;
+
+               while (next < end && HTTP_IS_SPHT(*next))
+                       next++;
+       }
+       else {
+               /* Remove useless spaces before the old delimiter. */
+               while (HTTP_IS_SPHT(*(prev-1)))
+                       prev--;
+               *from = prev;
+
+               /* copy the delimiter and if possible a space if we're
+                * not at the end of the line.
+                */
+               if (next < end) {
+                       *prev++ = *next++;
+                       if (prev + 1 < next)
+                               *prev++ = ' ';
+                       while (next < end && HTTP_IS_SPHT(*next))
+                               next++;
+               }
+       }
+       memmove(prev, next, end - next);
+       return (prev - next);
+}
+
 
 /* Formats the start line of the request (without CRLF) and puts it in <str> and
  * return the written lenght. The line can be truncated if it exceeds <len>.