[monitoring-plugins] improved curlhelp_parse_statusline to handle both ...

Andreas Baumann git at monitoring-plugins.org
Sat Sep 7 15:40:11 CEST 2019


 Module: monitoring-plugins
 Branch: feature_check_curl
 Commit: 95ee6ace094109d156286ab61d3c76709e43d642
 Author: Andreas Baumann <mail at andreasbaumann.cc>
   Date: Sat Sep  7 15:31:15 2019 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=95ee6ac

improved curlhelp_parse_statusline to handle both HTTP/1.x and HTTP/2

---

 plugins/check_curl.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/plugins/check_curl.c b/plugins/check_curl.c
index 8d8cb9f..3a6f2af 100644
--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -59,7 +59,7 @@ const char *email = "devel at monitoring-plugins.org";
 
 #define DEFAULT_BUFFER_SIZE 2048
 #define DEFAULT_SERVER_URL "/"
-#define HTTP_EXPECT "HTTP/1."
+#define HTTP_EXPECT "HTTP/"
 #define DEFAULT_MAX_REDIRS 15
 #define INET_ADDR_MAX_SIZE INET6_ADDRSTRLEN
 enum {
@@ -1915,44 +1915,55 @@ curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line)
   status_line->first_line[first_line_len] = '\0';
   first_line_buf = strdup( status_line->first_line );
 
-  /* protocol and version: "HTTP/x.x" SP */
+  /* protocol and version: "HTTP/x.x" SP or "HTTP/2" SP */
 
   p = strtok(first_line_buf, "/");
   if( p == NULL ) { free( first_line_buf ); return -1; }
   if( strcmp( p, "HTTP" ) != 0 ) { free( first_line_buf ); return -1; }
 
-  p = strtok( NULL, "." );
-  if( p == NULL ) { free( first_line_buf ); return -1; }
-  status_line->http_major = (int)strtol( p, &pp, 10 );
-  if( *pp != '\0' ) { free( first_line_buf ); return -1; }
-
   p = strtok( NULL, " " );
   if( p == NULL ) { free( first_line_buf ); return -1; }
-  status_line->http_minor = (int)strtol( p, &pp, 10 );
-  if( *pp != '\0' ) { free( first_line_buf ); return -1; }
+  if( strchr( p, '.' ) != NULL ) {
+    
+    /* HTTP 1.x case */
+    char *ppp;
+    ppp = strtok( p, "." );
+    status_line->http_major = (int)strtol( p, &pp, 10 );
+    if( *pp != '\0' ) { free( first_line_buf ); return -1; }
+    ppp = strtok( NULL, " " );
+    status_line->http_minor = (int)strtol( p, &pp, 10 );
+    if( *pp != '\0' ) { free( first_line_buf ); return -1; }
+    p += 4; /* 1.x SP */
+  } else {
+    /* HTTP 2 case */
+    status_line->http_major = (int)strtol( p, &pp, 10 );
+    status_line->http_minor = 0;
+    p += 2; /* 2 SP */
+  }
 
   /* status code: "404" or "404.1", then SP */
 
-  p = strtok( NULL, " ." );
+  p = strtok( p, " " );
   if( p == NULL ) { free( first_line_buf ); return -1; }
   if( strchr( p, '.' ) != NULL ) {
     char *ppp;
     ppp = strtok( p, "." );
     status_line->http_code = (int)strtol( ppp, &pp, 10 );
     if( *pp != '\0' ) { free( first_line_buf ); return -1; }
-
     ppp = strtok( NULL, "" );
     status_line->http_subcode = (int)strtol( ppp, &pp, 10 );
     if( *pp != '\0' ) { free( first_line_buf ); return -1; }
+    p += 6; /* 400.1 SP */
   } else {
     status_line->http_code = (int)strtol( p, &pp, 10 );
     status_line->http_subcode = -1;
     if( *pp != '\0' ) { free( first_line_buf ); return -1; }
+    p += 4; /* 400 SP */
   }
 
   /* Human readable message: "Not Found" CRLF */
 
-  p = strtok( NULL, "" );
+  p = strtok( p, "" );
   if( p == NULL ) { status_line->msg = ""; return 0; }
   status_line->msg = status_line->first_line + ( p - first_line_buf );
   free( first_line_buf );



More information about the Commits mailing list