MINOR: uri_normalizer: Add a `merge-slashes` normalizer to http-request normalize-uri
This normalizer merges adjacent slashes into a single slash, thus removing
empty path segments.
See GitHub Issue #714.
diff --git a/src/http_act.c b/src/http_act.c
index 134c903..2af4d47 100644
--- a/src/http_act.c
+++ b/src/http_act.c
@@ -215,8 +215,23 @@
goto fail_alloc;
switch ((enum act_normalize_uri) rule->action) {
- case ACT_NORMALIZE_URI_PLACEHOLDER:
- (void) uri;
+ case ACT_NORMALIZE_URI_MERGE_SLASHES: {
+ const struct ist path = http_get_path(uri);
+ struct ist newpath = ist2(replace->area, replace->size);
+
+ if (!isttest(path))
+ goto leave;
+
+ err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
+
+ if (err != URI_NORMALIZER_ERR_NONE)
+ break;
+
+ if (!http_replace_req_path(htx, newpath, 0))
+ goto fail_rewrite;
+
+ break;
+ }
}
switch (err) {
@@ -277,8 +292,10 @@
return ACT_RET_PRS_ERR;
}
- if (0) {
+ if (strcmp(args[cur_arg], "merge-slashes") == 0) {
+ cur_arg++;
+ rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES;
}
else {
memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
diff --git a/src/uri_normalizer.c b/src/uri_normalizer.c
index 7db47d1..abc029b 100644
--- a/src/uri_normalizer.c
+++ b/src/uri_normalizer.c
@@ -10,9 +10,48 @@
*
*/
+#include <import/ist.h>
+
#include <haproxy/api.h>
#include <haproxy/uri_normalizer.h>
+/* Merges adjacent slashes in the given path. */
+enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst)
+{
+ enum uri_normalizer_err err;
+
+ const size_t size = istclear(dst);
+ struct ist newpath = *dst;
+
+ struct ist scanner = path;
+
+ /* The path will either be shortened or have the same length. */
+ if (size < istlen(path)) {
+ err = URI_NORMALIZER_ERR_ALLOC;
+ goto fail;
+ }
+
+ while (istlen(scanner) > 0) {
+ const char current = istshift(&scanner);
+
+ if (current == '/') {
+ while (istlen(scanner) > 0 && *istptr(scanner) == '/')
+ scanner = istnext(scanner);
+ }
+
+ newpath = __istappend(newpath, current);
+ }
+
+ *dst = newpath;
+
+ return URI_NORMALIZER_ERR_NONE;
+
+ fail:
+
+ return err;
+}
+
+
/*
* Local variables:
* c-indent-level: 8