2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/socket.h>
32 #include <netinet/in.h>
38 int http_connect(const char *hostname, int port)
41 struct sockaddr_in server;
42 struct hostent *host_addr;
44 /* create a socket of type AF_INET, and SOCK_STREAM (TCP) */
45 sock = socket(AF_INET, SOCK_STREAM, 0);
47 /* get an IP from a domain name -- essential */
48 host_addr = gethostbyname(hostname);
49 if (host_addr == NULL)
52 server.sin_family = AF_INET;
53 /* 110 is the standard POP port. Host TO Network order. */
54 server.sin_port = htons(port);
55 /* get the IP address. */
56 server.sin_addr = *((struct in_addr *) host_addr->h_addr);
57 /* padding unused in sockaddr_in */
59 bzero(&(server.sin_zero), 8);
62 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
68 int http_get_line(int sock, char *buf, int len)
76 while (size != 0 && lilbuf != '\n' && count < len)
78 size = recv(sock, &lilbuf, 1, 0);
79 if (size == 0 && count == 1 )
91 int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, DownloadOptions *options )
93 static char input_buffer[1024];
97 /* int hnlen = strlen ( hostname ); */
101 WSAStartup ( MAKEWORD(2,2), &usadata );
104 sock = http_connect ( hostname, 80 );
111 if ( options != NULL && options->sendhostname ) {
112 send ( sock, "GET http://", 11, 0);
113 send ( sock, hostname, strlen(hostname), 0 );
114 send ( sock, uri, strlen ( uri ), 0 );
115 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
117 send ( sock, "GET ", 4, 0 );
118 send ( sock, uri, strlen ( uri ), 0 );
119 send ( sock, "\r\n\r\n", 4, 0 );
122 /* next, skip through all headers EXCEPT content length.,
123 that is, if it begins with "Content-Length: " (strncasecmp),
124 atoi that line from +16 (+17 ?), read that many bytes directly
125 into file (IF we can open it, else return error) and we're done.
128 /* "HTTP/1.x 200 OK" check */
129 if ( recv ( sock, input_buffer, 12, 0 ) < 12 || input_buffer[9] != '2' || input_buffer[10] != '0' || input_buffer[11] != '0' )
131 /* maybe it's a redirect */
132 if ( ! already_redirected )
135 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
138 /* Location: http://abc.def/bla */
139 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
144 uri_start = strchr(input_buffer+17,'/');
148 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
149 char *newuri = strdup ( uri_start );
152 rv = http_download_get_url ( newhost, newuri, f, 1, sendhostname );
159 } while (input_buffer[0] != '\r' );
161 /* Something went wrong */
167 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
172 } while (input_buffer[0] != '\r' );
177 len = recv ( sock, input_buffer, 1024, 0 );
179 fwrite ( input_buffer, 1, len, tmp_f );
184 while ( ! feof(tmp_f) )
186 len = fread ( input_buffer, 1, 1024, tmp_f );
187 fwrite ( input_buffer, 1, len, f);
193 WSACleanup(); /* they sure make winsock programming easy. */