4 * Copyright (C) 2006 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: load_http.c,v 1.1.2.2 2009/07/23 20:01:12 darrenr Exp
14 * Because the URL can be included twice into the buffer, once as the
15 * full path for the "GET" and once as the "Host:", the buffer it is
16 * put in needs to be larger than 512*2 to make room for the supporting
17 * text. Why not just use snprintf and truncate? The warning about the
18 * URL being too long tells you something is wrong and does not fetch
19 * any data - just truncating the URL (with snprintf, etc) and sending
20 * that to the server is allowing an unknown and unintentioned action
23 #define MAX_URL_LEN 512
24 #define LOAD_BUFSIZE (MAX_URL_LEN * 2 + 128)
27 * Format expected is one addres per line, at the start of each line.
32 char *s
, *t
, *u
, buffer
[LOAD_BUFSIZE
], *myurl
;
33 int fd
, len
, left
, port
, endhdr
, removed
;
34 alist_t
*a
, *rtop
, *rbot
;
35 struct sockaddr_in sin
;
40 * More than this would just be absurd.
42 if (strlen(url
) > MAX_URL_LEN
) {
43 fprintf(stderr
, "load_http has a URL > %d bytes?!\n",
57 left
= snprintf(buffer
, rem
, "GET %s HTTP/1.0\r\n", url
);
58 if (left
< 0 || left
> rem
)
62 s
= myurl
+ 7; /* http:// */
65 fprintf(stderr
, "load_http has a malformed URL '%s'\n", url
);
71 * 10 is the length of 'Host: \r\n\r\n' below.
73 if (strlen(s
) + strlen(buffer
) + 10 > sizeof(buffer
)) {
74 fprintf(stderr
, "load_http has a malformed URL '%s'\n", url
);
83 left
= snprintf(buffer
+ left
, rem
, "Host: %s\r\n\r\n", s
);
84 if (left
< 0 || left
> rem
)
92 if (port
< 0 || port
> 65535)
98 memset(&sin
, 0, sizeof(sin
));
99 sin
.sin_family
= AF_INET
;
100 sin
.sin_port
= htons(port
);
102 if (isdigit((unsigned char)*s
)) {
103 if (inet_aton(s
, &sin
.sin_addr
) == -1) {
107 host
= gethostbyname(s
);
110 memcpy(&sin
.sin_addr
, host
->h_addr_list
[0],
111 sizeof(sin
.sin_addr
));
114 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
118 if (connect(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1)
121 len
= strlen(buffer
);
122 if (write(fd
, buffer
, len
) != len
)
127 left
= sizeof(buffer
) - 1;
129 while ((len
= read(fd
, s
, left
)) > 0) {
136 t
= strchr(buffer
, ' ');
145 while ((t
= strchr(u
, '\r')) != NULL
) {
147 if (*(t
+ 1) == '\n') {
153 } else if (*(t
+ 1) == '\n') {
161 removed
= (u
- buffer
) + 1;
162 memmove(buffer
, u
, (sizeof(buffer
) - left
) - removed
);
168 t
= strchr(buffer
, '\n');
173 for (u
= buffer
; isdigit((unsigned char)*u
) ||
181 while (isdigit((unsigned char)*u
))
183 if (!isspace((unsigned char)*u
) && *u
)
188 a
= alist_new(4, buffer
);
197 removed
= t
- buffer
;
198 memmove(buffer
, t
, sizeof(buffer
) - left
- removed
);