1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
check_curl - Nagios Curl-based check plugin
Copyright (C) 2008 Andreas Baumann
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h> /* for memcpy */
#include <stdlib.h> /* for malloc, free */
#include "cmdline.h"
#include "curlhelper.h"
int curlhelp_initbuffer( curlhelp_curlbuf *buf ) {
buf->bufsize = 2048;
buf->buflen = 0;
buf->buf = (char *)malloc( (size_t)2048 );
if( buf->buf == NULL ) return -1;
return 0;
}
int curlhelp_buffer_callback( void *buffer, size_t size, size_t nmemb, void *stream ) {
curlhelp_curlbuf *buf = (curlhelp_curlbuf *)stream;
while( buf->bufsize < buf->buflen + size * nmemb + 1 ) {
buf->bufsize *= buf->bufsize * 2;
buf->buf = (char *)realloc( buf->buf, buf->bufsize );
if( buf->buf == NULL ) {
return -1;
}
}
memcpy( buf->buf + buf->buflen, buffer, size * nmemb );
buf->buflen += size * nmemb;
buf->buf[buf->buflen] = '\0';
return (int)( size * nmemb );
}
void curlhelp_freebuffer( curlhelp_curlbuf *buf ) {
free( buf->buf );
buf->buf = NULL;
}
int curlhelp_parse_statusline( char *buf, curlhelp_statusline *status_line ) {
char *first_line_end;
char *p;
size_t first_line_len;
char *pp;
first_line_end = strstr( buf, "\r\n" );
if( first_line_end == NULL ) return -1;
first_line_len = (size_t)( first_line_end - buf );
status_line->first_line = (char *)malloc( first_line_len + 1 );
if( status_line->first_line == NULL ) return -1;
memcpy( status_line->first_line, buf, first_line_len );
status_line->first_line[first_line_len] = '\0';
/* protocol and version: "HTTP/x.x" SP */
p = strtok( status_line->first_line, "/" );
if( p == NULL ) { free( status_line->first_line ); return -1; }
if( strcmp( p, "HTTP" ) != 0 ) { free( status_line->first_line ); return -1; }
p = strtok( NULL, "." );
if( p == NULL ) { free( status_line->first_line ); return -1; }
status_line->http_major = (int)strtol( p, &pp, 10 );
if( *pp != '\0' ) { free( status_line->first_line ); return -1; }
p = strtok( NULL, " " );
if( p == NULL ) { free( status_line->first_line ); return -1; }
status_line->http_minor = (int)strtol( p, &pp, 10 );
if( *pp != '\0' ) { free( status_line->first_line ); return -1; }
/* status code: "404" or "404.1", then SP */
p = strtok( NULL, " ." );
if( p == NULL ) { free( status_line->first_line ); return -1; }
if( strchr( p, '.' ) != NULL ) {
char *ppp;
ppp = strtok( p, "." );
status_line->http_code = (int)strtol( ppp, &pp, 10 );
if( *pp != '\0' ) { free( status_line->first_line ); return -1; }
ppp = strtok( NULL, "" );
status_line->http_subcode = (int)strtol( ppp, &pp, 10 );
if( *pp != '\0' ) { free( status_line->first_line ); return -1; }
} else {
status_line->http_code = (int)strtol( p, &pp, 10 );
status_line->http_subcode = -1;
if( *pp != '\0' ) { free( status_line->first_line ); return -1; }
}
/* Human readable message: "Not Found" CRLF */
p = strtok( NULL, "" );
if( p == NULL ) { free( status_line->first_line ); return -1; }
status_line->msg = p;
return 0;
}
void curlhelp_free_statusline( curlhelp_statusline *status_line ) {
free( status_line->first_line );
}
|