cURL / Mailing Lists / curl-users / Single Mail

curl-users

Patches to make curl work as a netsaint plugin

From: Christoph Lameter <christoph_at_lameter.com>
Date: Thu, 18 Jan 2001 15:40:26 -0800 (PST)

The following patch adds netsant compatibility to curl and allows the use of
CURL directly as a netsaint plugin.

New options:

-g Netsaint URL check mode. Retrieves URL and returns
        one line of text describing the event and status 0=ok, 1=warning
        2-error

-G regexp Matches webpages against regexp. Returns error code 2
                for mismatches. Otherwise returns the same as -g

-j <http-code> Allows the treatment of an http error code as an OK
        condition. This is necessary when checking f.e. the presence of a http page
        that requries authentication (code 404). In that case 404 is evidence that
        the URL works as designed.

Enjoy

Christoph Lameter, January 18, 2001

The patch (and eventual updates to it) will be available from

ftp/http lameter.com/curl

Please CC me on all correspondence. I am not a member of this mailing list!

--- curl-ssl-7.5.1.orig/src/main.c
+++ curl-ssl-7.5.1/src/main.c
@@ -129,6 +129,7 @@
 #define CONF_HTTPPOST (1<<25) /* multipart/form-data HTTP POST */
 #define CONF_PUT (1<<27) /* PUT the input file */
 #define CONF_MUTE (1<<28) /* force NOPROGRESS */
+#define CONF_NETSAINT (1<<29) /* Netsaint plugin mode */
 
 #ifndef HAVE_STRDUP
 /* Ultrix doesn't have strdup(), so make a quick clone: */
@@ -259,6 +260,8 @@
        " --cacert <file> CA certifciate to verify peer against (HTTPS)\n"
        " -f/--fail Fail silently (no output at all) on errors (H)\n"
        " -F/--form <name=content> Specify HTTP POST data (H)\n"
+ " -g/--gnetsaint Netsaint plugin mode\n"
+ " -G/--gpattern Netsaint pattern match plugin mode\n"
 
        " -h/--help This help text\n"
        " -H/--header <line> Custom header to pass to server. (H)\n"
@@ -349,6 +352,8 @@
   bool nobuffer;
 
   char *writeout; /* %-styled format string to output */
+ char *pattern; /* Pattern to match */
+ long nuke_http_code; /* http error to ignore */
 
   FILE *errors; /* if stderr redirect is requested */
 
@@ -500,11 +505,14 @@
     {"Ea", "cacert", TRUE},
     {"f", "fail", FALSE},
     {"F", "form", TRUE},
+ {"g", "gnetsaint", FALSE},
+ {"G", "gpattern", TRUE},
 
     {"h", "help", FALSE},
     {"H", "header", TRUE},
     {"i", "include", FALSE},
     {"I", "head", FALSE},
+ {"j", "junk", TRUE},
     {"K", "config", TRUE},
     {"l", "list-only", FALSE},
     {"L", "location", FALSE},
@@ -765,7 +773,16 @@
       if(SetHTTPrequest(HTTPREQ_POST, &config->httpreq))
         return PARAM_BAD_USE;
       break;
-
+
+ case 'G' : /* Netsaint grep mode */
+ GetStr(&config->pattern, nextarg);
+ case 'g' : /* Netsaint mode */
+ config->conf |= (CONF_MUTE|CONF_NOPROGRESS|CONF_FAILONERROR|CONF_NETSAINT);
+ config->showerror = TRUE;
+ config->writeout = strdup("Ok. url=%{url_effective} http_code=%{http_code} |totaltime=%{time_total} lookuptime=%{time_namelookup} connecttime=%{time_connect} pretransfertime=%{time_pretransfer} speed=%{speed_download} size=%{size_download}\n");
+ config->outfile = strdup("/dev/null");
+ config->timeout=60;
+ break;
     case 'h': /* h for help */
       help();
       return PARAM_HELP_REQUESTED;
@@ -782,6 +799,10 @@
       if(SetHTTPrequest(HTTPREQ_HEAD, &config->httpreq))
         return PARAM_BAD_USE;
       break;
+ case 'j':
+ /* specify HTTP error to nuke */
+ config->nuke_http_code=atoi(nextarg);
+ break;
     case 'K':
       res = parseconfig(nextarg, config);
       config->configread = TRUE;
@@ -1718,10 +1739,12 @@
         curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, myprogress);
         curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &progressbar);
       }
-
+ curl_easy_setopt(curl, CURLOPT_NETSAINT, config->conf&CONF_NETSAINT);
+ if (config->pattern) curl_easy_setopt(curl, CURLOPT_PATTERN, config->pattern);
+ curl_easy_setopt(curl, CURLOPT_NUKE_HTTP_CODE,config->nuke_http_code);
       res = curl_easy_perform(curl);
 
- if(config->writeout) {
+ if(config->writeout && res==0) {
         ourWriteOut(curl, config->writeout);
       }
 
@@ -1729,7 +1752,10 @@
       curl_easy_cleanup(curl);
 
       if((res!=CURLE_OK) && config->showerror)
- fprintf(config->errors, "curl: (%d) %s\n", res, errorbuffer);
+ /* CL Netsaint adaption */
+ { fprintf(stdout, "%s\n", errorbuffer);
+ if (res<=CURLE_PATTERN_MISMATCH && res>=CURLE_UNSUPPORTED_PROTOCOL) res=2; else res=1;
+ }
     }
     else
       fprintf(config->errors, "curl: failed to init libcurl!\n");
--- curl-ssl-7.5.1.orig/lib/highlevel.c
+++ curl-ssl-7.5.1/lib/highlevel.c
@@ -142,6 +142,8 @@
   int offset = 0; /* possible resume offset read from the
                                    Content-Range: header */
   int code = 0; /* error code from the 'HTTP/1.? XXX' line */
+ char bmb[201]; /* Boundary match buffer for -G option CL */
+ int bmbb=0; /* Number of bytes in Boundary match buffer */
 
   /* for the low speed checks: */
   CURLcode urg;
@@ -359,11 +361,11 @@
                       ( ((data->bits.http_follow_location) && (code >= 400))
                         ||
                         (!data->bits.http_follow_location && (code >= 300)))
- && (data->bits.http_fail_on_error)) {
+ && (data->bits.http_fail_on_error && data->nuke_http_code !=code)) {
                     /* If we have been told to fail hard on HTTP-errors,
                        here is the check for that: */
                     /* serious error, go home! */
- failf (data, "The requested file was not found");
+ failf (data, "HTTP-Error http_code=%d",code);
                     return CURLE_HTTP_NOT_FOUND;
                   }
                   data->progress.httpcode = code;
@@ -503,6 +505,18 @@
 
             pgrsSetDownloadCounter(data, (double)bytecount);
             
+ if (data->pattern) {
+ if (bmbb) {
+ memcpy(bmb+bmbb,str,100);
+ bmb[bmbb+100]=0;
+ if (regexec(data->pattern,bmb,0,NULL,REG_NOTBOL|REG_NOTEOL)==0) data->matches++;
+ bmbb=0;
+ }
+ str[nread]=0;
+ if (regexec(data->pattern,str,0,NULL,REG_NOTBOL|REG_NOTEOL)==0) data->matches++;
+ bmbb=nread<100 ? nread : 100;
+ memcpy(bmb,str+nread-bmbb,bmbb);
+ } else
             urg = client_write(data, CLIENTWRITE_BODY, str, nread);
             if(urg)
               return urg;
@@ -746,6 +760,12 @@
 
   if(data->newurl)
     free(data->newurl);
+
+ if (res==CURLE_OK && data->pattern && data->matches ==0)
+ { /* Pattern Mismatch */
+ failf (data, "Pattern does NOT match!");
+ res=CURLE_PATTERN_MISMATCH;
+ }
 
   return res;
 }
--- curl-ssl-7.5.1.orig/lib/url.c
+++ curl-ssl-7.5.1/lib/url.c
@@ -543,6 +543,28 @@
     data->ssl.CAfile = va_arg(param, char *);
     data->ssl.CApath = NULL; /*This does not work on windows.*/
     break;
+ case CURLOPT_PATTERN:
+ data->pattern = malloc(sizeof(regex_t));
+ switch (regcomp(data->pattern,va_arg(param, char*),REG_NOSUB|REG_NEWLINE)) {
+ case REG_BADRPT:
+ case REG_BADBR:
+ case REG_EBRACE:
+ case REG_EBRACK:
+ case REG_ERANGE:
+ case REG_ECTYPE:
+ case REG_ECOLLATE:
+ case REG_EPAREN:
+ case REG_ESUBREG:
+ case REG_EEND:
+ case REG_EESCAPE:
+ case REG_BADPAT:
+ case REG_ESPACE: return CURLE_PATTERN_SYNTAX;
+ default: break;
+ };
+ break;
+ case CURLOPT_NUKE_HTTP_CODE:
+ data->nuke_http_code= va_arg(param, long);
+ break;
   default:
     /* unknown tag and its companion, just ignore: */
     return CURLE_READ_ERROR; /* correct this */
--- curl-ssl-7.5.1.orig/lib/urldata.h
+++ curl-ssl-7.5.1/lib/urldata.h
@@ -94,6 +94,8 @@
 
 #include "timeval.h"
 
+#include "regex.h"
+
 #include <curl/curl.h>
 
 /* Download buffer size, keep it fairly big for speed reasons */
@@ -529,6 +531,9 @@
 #endif
 
   struct timeval keeps_speed; /* this should be request-specific */
+ regex_t *pattern; /* If not NULL pattern to match */
+ long matches; /* Number of pattern matches */
+ int nuke_http_code; /* http code to nuke and treat as an ok status */
 };
 
 #define LIBCURL_NAME "libcurl"
--- curl-ssl-7.5.1.orig/include/curl/curl.h
+++ curl-ssl-7.5.1/include/curl/curl.h
@@ -168,12 +168,15 @@
   CURLE_ABORTED_BY_CALLBACK,
   CURLE_BAD_FUNCTION_ARGUMENT,
   CURLE_BAD_CALLING_ORDER,
-
+
   CURLE_HTTP_PORT_FAILED, /* HTTP Interface operation failed */
 
   CURLE_BAD_PASSWORD_ENTERED, /* when the my_getpass() returns fail */
   CURLE_TOO_MANY_REDIRECTS , /* catch endless re-direct loops */
 
+ CURLE_PATTERN_MISMATCH,
+ CURLE_PATTERN_SYNTAX,
+
   CURL_LAST
 } CURLcode;
 
@@ -426,6 +429,10 @@
      document! Pass a NULL to shut it off. */
   CINIT(FILETIME, OBJECTPOINT, 69),
 
+ CINIT(NETSAINT, LONG, 70),
+ CINIT(PATTERN, OBJECTPOINT, 71),
+ CINIT(NUKE_HTTP_CODE, LONG, 72),
+
   CURLOPT_LASTENTRY /* the last unusued */
 } CURLoption;
 
Received on 2001-01-19