From 5297f5f8ba5497f3d539317c004184e990e481ee Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 21 Jan 2017 10:33:32 +0100 Subject: handling last HTTP header correctly in HTTP line parser (added a strrstr replacement) --- plugins/check_curl.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 45a52f0e..30c947fb 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -114,7 +114,7 @@ int curlhelp_initbuffer (curlhelp_curlbuf*); int curlhelp_buffer_callback (void*, size_t , size_t , void*); void curlhelp_freebuffer (curlhelp_curlbuf*); -int curlhelp_parse_statusline (char*, curlhelp_statusline *); +int curlhelp_parse_statusline (const char*, curlhelp_statusline *); void curlhelp_free_statusline (curlhelp_statusline *); void remove_newlines (char *); @@ -700,17 +700,58 @@ curlhelp_freebuffer (curlhelp_curlbuf *buf) buf->buf = NULL; } -/* TODO: when redirecting we get more than one HTTP header, make sure - * we parse the last one +/* TODO: should be moved into 'gl' and should be probed, glibc has + * a strrstr */ +const char* +strrstr2(const char *haystack, const char *needle) +{ + int counter; + size_t len; + const char *prev_pos; + const char *pos; + + if (haystack == NULL || needle == NULL) + return NULL; + + if (haystack[0] == '\0' || needle[0] == '\0') + return NULL; + + counter = 0; + prev_pos = NULL; + pos = haystack; + len = strlen (needle); + for (;;) { + pos = strstr (pos, needle); + if (pos == NULL) { + if (counter == 0) + return NULL; + else + return prev_pos; + } + counter++; + prev_pos = pos; + pos += len; + if (*pos == '\0') return prev_pos; + } +} + int -curlhelp_parse_statusline (char *buf, curlhelp_statusline *status_line) +curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line) { char *first_line_end; char *p; size_t first_line_len; char *pp; + const char *start; + /* find last start of a new header */ + start = strrstr2 (buf, "\r\nHTTP"); + if (start != NULL) { + start += 2; + buf = start; + } + first_line_end = strstr(buf, "\r\n"); if (first_line_end == NULL) return -1; -- cgit v1.2.3-74-g34f1