[nanny] Regular expression in expected string
Sébastien Bonnet <[email protected]>
| Newsgroups | gmane.linux.redhat.piranha |
|---|---|
| Message-ID | <[email protected]> |
Hi, I've been surprised yesterday by nanny's behaviour in regard to the expected string. The man page doesn't state that expected_string is compared to the first strlen(received_string) characters of received_string. This makes impossible to expect something not being the beginning of the received string. It's quite limitating :( I've thus enhanced nanny to accept a regular expression as the expected string. This way, you can look for whatever you want within the first 255 characters of your server's answer. Quite handy when it comes to check both apache and a module it's running. Example: I've a PHP page, made of static HTML code and also dynamic content (say it prints 'MY APP IF FINE'). The expected_string, which formerly could (at most) only be something like "HTTP/1.1 200 OK" now is "HTTP/1.1 200 OK.*MY APP IF FINE". I'm thus checking that Apache if OK (HTTP 200) and that my app is working just fine (replace just print something by a full status check). For those interested by the enhancement, I've attached the patch against piranha-0.7.0. Just add -DUSE_REGEX in the CFLAGS (in Makefile), make nanny and copy it to the right location and you're done. FYI, if the provided expected string is not a correct regex, no real server will ever appear. If this is the case, just have a look at your favorite log files ... Enjoy :) -- Sébastien Bonnet Centre de contacts - Experian France
nanny-regex.diff
(text/plain, 1.6 KB)
--- piranha.1/nanny.c Thu Dec 13 21:30:26 2001
+++ piranha.2/nanny.c Thu Oct 10 15:53:00 2002
@@ -139,6 +139,11 @@
#include <math.h> /* for pow() */
#include <time.h>
+#if defined(USE_REGEX)
+ #include <sys/types.h>
+ #include <regex.h>
+#endif
+
#include "globals.h"
#include "util.h"
#include "nanny.h"
@@ -269,6 +274,25 @@
struct iphdr *ip;
#endif
+#if defined(USE_REGEX)
+ static regex_t regExpr;
+ static char regCompiled = 0;
+ char regError[255];
+#endif
+
+#if defined(USE_REGEX)
+ if (!regCompiled) {
+ if ((i = regcomp (®Expr, expect_str, REG_EXTENDED)) == 0) {
+ regCompiled = 1;
+ } else {
+ regerror (i, ®Expr, regError, sizeof (regError));
+ piranha_log (flags, (char *)
+ "Error '%s' processing expected regular expression",
+ regError);
+ }
+ }
+#endif
+
if (pingTimeout < 500)
pingTimeout = 500;
@@ -502,7 +526,11 @@
*/
if (expect_str)
+#if defined(USE_REGEX)
+ read_len = sizeof (read_buf) - 1;
+#else
read_len = strlen (expect_str);
+#endif
else
read_len = 1; /* for UDP */
@@ -555,11 +583,15 @@
}
if ((read_len == 1) && (*expect_str == '*')) {
+ /* Wildcard */
read_buf[0] = '*';
read_buf[1] = 0;
i = 1;
}
- /* Wildcard */
+#if defined(USE_REGEX)
+ if (!regCompiled || regexec (®Expr, read_buf, 0, NULL, 0))
+ return_status = 1;
+#else
if (i >= read_len) {
if (strncmp ((const char *) expect_str,
(const char *) read_buf,
@@ -572,6 +604,7 @@
port);
return_status = 1;
}
+#endif
}
}
}