BUG/MINOR: 51d: Fix bug when HTX is enabled

When HTX is enabled, the sample flags were set too early. When matching for
multiple HTTP headers, the sample is fetched more than once, meaning that the
flags would need to be set again. Instead, the flags are now set last (just
before the outermost function returns). This could be further improved by
passing around the message without calling prefetch again.

This patch must be backported as far as 1.9. it should fix bug #450.

(cherry picked from commit 6bf06727116eb48825cf4c4b65970b8305591925)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 84a6e9e474e3435849b4341a066079b7b93cd8e9)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/51d.c b/src/51d.c
index 344be41..4c16a79 100644
--- a/src/51d.c
+++ b/src/51d.c
@@ -461,6 +461,21 @@
 	smp->data.u.str.data = temp->data;
 }
 
+/* Sets the sample data as a constant string. This ensures that the
+ * string will be processed correctly.
+ */
+static void _51d_set_smp(struct sample *smp)
+{
+	/*
+	 * Data type has to be set to ensure the string output is processed
+	 * correctly.
+	 */
+	smp->data.type = SMP_T_STR;
+
+	/* Flags the sample to show it uses constant memory. */
+	smp->flags |= SMP_F_CONST;
+}
+
 static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
 {
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
@@ -484,14 +499,6 @@
 		/* Legacy version */
 		CHECK_HTTP_MESSAGE_FIRST(chn);
 	}
-	/*
-	 * Data type has to be reset to ensure the string output is processed
-	 * correctly.
-	 */
-	smp->data.type = SMP_T_STR;
-
-	/* Flags the sample to show it uses constant memory*/
-	smp->flags |= SMP_F_CONST;
 
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
 
@@ -519,6 +526,8 @@
 			fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
 			_51d_retrieve_cache_entry(smp, lru);
 			HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
+
+			_51d_set_smp(smp);
 			return 1;
 		}
 		HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
@@ -556,6 +565,7 @@
 		_51d_insert_cache_entry(smp, lru, (void*)args);
 #endif
 
+	_51d_set_smp(smp);
 	return 1;
 }
 
@@ -568,8 +578,6 @@
 #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
 	fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */
 #endif
-	/* Flags the sample to show it uses constant memory*/
-	smp->flags |= SMP_F_CONST;
 
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
 
@@ -631,6 +639,7 @@
 #endif
 #endif
 
+	_51d_set_smp(smp);
 	return 1;
 }
 
@@ -840,4 +849,4 @@
 #else
 	REGISTER_BUILD_OPTS("Built with 51Degrees Trie support (dummy library).");
 #endif
-#endif
\ No newline at end of file
+#endif