4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * $Id: load_http.c,v 1.5.2.5 2012/07/22 08:04:24 darren_r Exp $
15 * Because the URL can be included twice into the buffer, once as the
16 * full path for the "GET" and once as the "Host:", the buffer it is
17 * put in needs to be larger than 512*2 to make room for the supporting
18 * text. Why not just use snprintf and truncate? The warning about the
19 * URL being too long tells you something is wrong and does not fetch
20 * any data - just truncating the URL (with snprintf, etc) and sending
21 * that to the server is allowing an unknown and unintentioned action
24 #define MAX_URL_LEN 512
25 #define LOAD_BUFSIZE (MAX_URL_LEN * 2 + 128)
28 * Format expected is one addres per line, at the start of each line.
33 int fd, len, left, port, endhdr, removed, linenum = 0;
34 char *s, *t, *u, buffer[LOAD_BUFSIZE], *myurl;
35 alist_t *a, *rtop, *rbot;
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",
52 avail = sizeof(buffer);
53 error = snprintf(buffer, avail, "GET %s HTTP/1.0\r\n", url);
56 * error is always less then avail due to the constraint on
57 * the url length above.
65 s = myurl + 7; /* http:// */
68 fprintf(stderr, "load_http has a malformed URL '%s'\n", url);
75 * 10 is the length of 'Host: \r\n\r\n' below.
77 if (strlen(s) + strlen(buffer) + 10 > sizeof(buffer)) {
78 fprintf(stderr, "load_http has a malformed URL '%s'\n", url);
87 error = snprintf(buffer + strlen(buffer), avail, "Host: %s\r\n\r\n", s);
89 fprintf(stderr, "URL is too large: %s\n", url);
97 if (port < 0 || port > 65535)
104 fd = connecttcp(s, port);
109 len = strlen(buffer);
110 if (write(fd, buffer, len) != len)
115 left = sizeof(buffer) - 1;
117 while ((len = read(fd, s, left)) > 0) {
124 t = strchr(buffer, ' ');
133 while ((t = strchr(u, '\r')) != NULL) {
135 if (*(t + 1) == '\n') {
141 } else if (*(t + 1) == '\n') {
149 removed = (u - buffer) + 1;
150 memmove(buffer, u, (sizeof(buffer) - left) - removed);
156 t = strchr(buffer, '\n');
164 * Remove comment and continue to the next line if
165 * the comment is at the start of the line.
167 u = strchr(buffer, '#');
175 * Trim off tailing white spaces, will include \r
177 for (u = t - 1; (u >= buffer) && ISSPACE(*u); u--)
180 a = alist_new(AF_UNSPEC, buffer);
189 "%s:%d unrecognised content:%s\n",
190 url, linenum, buffer);
194 removed = t - buffer;
195 memmove(buffer, t, sizeof(buffer) - left - removed);