MINOR: uri_normalizer: Add `fragment-encode` normalizer
This normalizer encodes '#' as '%23'.
See GitHub Issue #714.
diff --git a/src/http_act.c b/src/http_act.c
index f30694e..f613624 100644
--- a/src/http_act.c
+++ b/src/http_act.c
@@ -337,6 +337,23 @@
break;
}
+ case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
+ const struct ist path = http_get_path(uri);
+ struct ist newpath = ist2(replace->area, replace->size);
+
+ if (!isttest(path))
+ goto leave;
+
+ err = uri_normalizer_fragment_encode(path, &newpath);
+
+ if (err != URI_NORMALIZER_ERR_NONE)
+ break;
+
+ if (!http_replace_req_path(htx, newpath, 1))
+ goto fail_rewrite;
+
+ break;
+ }
}
switch (err) {
@@ -462,6 +479,11 @@
rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
}
+ else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
+ cur_arg++;
+
+ rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
+ }
else {
memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
return ACT_RET_PRS_ERR;
diff --git a/src/uri_normalizer.c b/src/uri_normalizer.c
index 4fd783d..bc793f2 100644
--- a/src/uri_normalizer.c
+++ b/src/uri_normalizer.c
@@ -18,6 +18,41 @@
#include <haproxy/tools.h>
#include <haproxy/uri_normalizer.h>
+/* Encodes '#' as '%23'. */
+enum uri_normalizer_err uri_normalizer_fragment_encode(const struct ist input, struct ist *dst)
+{
+ enum uri_normalizer_err err;
+
+ const size_t size = istclear(dst);
+ struct ist output = *dst;
+
+ struct ist scanner = input;
+
+ while (istlen(scanner)) {
+ const struct ist before_hash = istsplit(&scanner, '#');
+
+ if (istcat(&output, before_hash, size) < 0) {
+ err = URI_NORMALIZER_ERR_ALLOC;
+ goto fail;
+ }
+
+ if (istend(before_hash) != istend(scanner)) {
+ if (istcat(&output, ist("%23"), size) < 0) {
+ err = URI_NORMALIZER_ERR_ALLOC;
+ goto fail;
+ }
+ }
+ }
+
+ *dst = output;
+
+ return URI_NORMALIZER_ERR_NONE;
+
+ fail:
+
+ return err;
+}
+
/* Returns 1 if the given character is part of the 'unreserved' set in the
* RFC 3986 ABNF.
* Returns 0 if not.