summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-01-21 09:33:32 (GMT)
committerSven Nierlein <sven@nierlein.de>2018-10-22 14:28:51 (GMT)
commit035fe1eb79787e01f4f393dc5713fbbe56ce7cee (patch)
tree90fcf2a097ab595aaf54e9142446a362ac4e2eab
parentcefe325e947ec9023bdf6235e3f8568ed69d7c2d (diff)
downloadmonitoring-plugins-035fe1eb79787e01f4f393dc5713fbbe56ce7cee.tar.gz
handling last HTTP header correctly in HTTP line parser (added a strrstr replacement)
-rw-r--r--plugins/check_curl.c49
1 files changed, 45 insertions, 4 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c
index 45a52f0..30c947f 100644
--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -114,7 +114,7 @@ int curlhelp_initbuffer (curlhelp_curlbuf*);
114int curlhelp_buffer_callback (void*, size_t , size_t , void*); 114int curlhelp_buffer_callback (void*, size_t , size_t , void*);
115void curlhelp_freebuffer (curlhelp_curlbuf*); 115void curlhelp_freebuffer (curlhelp_curlbuf*);
116 116
117int curlhelp_parse_statusline (char*, curlhelp_statusline *); 117int curlhelp_parse_statusline (const char*, curlhelp_statusline *);
118void curlhelp_free_statusline (curlhelp_statusline *); 118void curlhelp_free_statusline (curlhelp_statusline *);
119 119
120void remove_newlines (char *); 120void remove_newlines (char *);
@@ -700,17 +700,58 @@ curlhelp_freebuffer (curlhelp_curlbuf *buf)
700 buf->buf = NULL; 700 buf->buf = NULL;
701} 701}
702 702
703/* TODO: when redirecting we get more than one HTTP header, make sure 703/* TODO: should be moved into 'gl' and should be probed, glibc has
704 * we parse the last one 704 * a strrstr
705 */ 705 */
706const char*
707strrstr2(const char *haystack, const char *needle)
708{
709 int counter;
710 size_t len;
711 const char *prev_pos;
712 const char *pos;
713
714 if (haystack == NULL || needle == NULL)
715 return NULL;
716
717 if (haystack[0] == '\0' || needle[0] == '\0')
718 return NULL;
719
720 counter = 0;
721 prev_pos = NULL;
722 pos = haystack;
723 len = strlen (needle);
724 for (;;) {
725 pos = strstr (pos, needle);
726 if (pos == NULL) {
727 if (counter == 0)
728 return NULL;
729 else
730 return prev_pos;
731 }
732 counter++;
733 prev_pos = pos;
734 pos += len;
735 if (*pos == '\0') return prev_pos;
736 }
737}
738
706int 739int
707curlhelp_parse_statusline (char *buf, curlhelp_statusline *status_line) 740curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line)
708{ 741{
709 char *first_line_end; 742 char *first_line_end;
710 char *p; 743 char *p;
711 size_t first_line_len; 744 size_t first_line_len;
712 char *pp; 745 char *pp;
746 const char *start;
713 747
748 /* find last start of a new header */
749 start = strrstr2 (buf, "\r\nHTTP");
750 if (start != NULL) {
751 start += 2;
752 buf = start;
753 }
754
714 first_line_end = strstr(buf, "\r\n"); 755 first_line_end = strstr(buf, "\r\n");
715 if (first_line_end == NULL) return -1; 756 if (first_line_end == NULL) return -1;
716 757