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
27 #include <sys/types.h>
34 #define access(a,b) _access(a,b)
35 #define close(a) closesocket(a)
37 char *dirname ( char * dir )
39 char *tmp = dir + strlen(dir) - 1;
40 while ( tmp != dir && *tmp != '\\' )
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
61 int http_connect(const char *hostname, int port)
64 struct sockaddr_in server;
65 struct hostent *host_addr;
67 /* create a socket of type AF_INET, and SOCK_STREAM (TCP) */
68 sock = socket(AF_INET, SOCK_STREAM, 0);
70 /* get an IP from a domain name -- essential */
71 host_addr = gethostbyname(hostname);
72 if (host_addr == NULL)
75 server.sin_family = AF_INET;
76 /* 110 is the standard POP port. Host TO Network order. */
77 server.sin_port = htons(port);
78 /* get the IP address. */
79 server.sin_addr = *((struct in_addr *) host_addr->h_addr);
80 /* padding unused in sockaddr_in */
82 bzero(&(server.sin_zero), 8);
85 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
91 int http_get_line(int sock, char *buf, int len)
99 while (size != 0 && lilbuf != '\n' && count < len)
101 size = recv(sock, &lilbuf, 1, 0);
102 if (size == 0 && count == 1 )
114 /* makes directory if neccessary */
115 int http_download_get_url ( const char *hostname, const char *uri, const char *fn, int already_redirected, int sendhostname )
117 static char input_buffer[1024];
121 /* int hnlen = strlen ( hostname ); */
123 if ( access ( fn, F_OK ) == 0 )
127 if ( errno == ENOENT)
129 char *tmp = g_strdup ( fn );
131 mkdir( dirname ( dirname ( tmp ) ) );
132 g_free ( tmp ); tmp = g_strdup ( fn );
133 mkdir( dirname ( tmp ) );
135 mkdir( dirname ( dirname ( tmp ) ), 0777 );
136 g_free ( tmp ); tmp = g_strdup ( fn );
137 mkdir( dirname ( tmp ), 0777 );
141 if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
146 WSAStartup ( MAKEWORD(2,2), &usadata );
149 sock = http_connect ( hostname, 80 );
158 if ( sendhostname ) {
159 send ( sock, "GET http://", 11, 0);
160 send ( sock, hostname, strlen(hostname), 0 );
161 send ( sock, uri, strlen ( uri ), 0 );
162 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
164 send ( sock, "GET ", 4, 0 );
165 send ( sock, uri, strlen ( uri ), 0 );
166 send ( sock, "\r\n\r\n", 4, 0 );
169 /* next, skip through all headers EXCEPT content length.,
170 that is, if it begins with "Content-Length: " (strncasecmp),
171 atoi that line from +16 (+17 ?), read that many bytes directly
172 into file (IF we can open it, else return error) and we're done.
175 /* "HTTP/1.x 200 OK" check */
176 if ( recv ( sock, input_buffer, 12, 0 ) < 12 || input_buffer[9] != '2' || input_buffer[10] != '0' || input_buffer[11] != '0' )
178 /* maybe it's a redirect */
179 if ( ! already_redirected )
182 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
185 /* Location: http://abc.def/bla */
186 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
191 uri_start = strchr(input_buffer+17,'/');
195 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
196 char *newuri = strdup ( uri_start );
201 rv = http_download_get_url ( newhost, newuri, fn, 1, sendhostname );
208 } while (input_buffer[0] != '\r' );
217 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
224 } while (input_buffer[0] != '\r' );
229 len = recv ( sock, input_buffer, 1024, 0 );
231 fwrite ( input_buffer, 1, len, tmp_f );
236 while ( ! feof(tmp_f) )
238 len = fread ( input_buffer, 1, 1024, tmp_f );
239 fwrite ( input_buffer, 1, len, f);
246 WSACleanup(); /* they sure make winsock programming easy. */
251 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
252 /* uri: like "/uri.html?whatever" */
253 /* only reason for the "wrapper" is so we can do redirects. */
254 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn )
256 return http_download_get_url ( hostname, uri, fn, 0, 1 );
259 int a_http_download_get_url_nohostname ( const char *hostname, const char *uri, const char *fn )
261 return http_download_get_url ( hostname, uri, fn, 0, 0 );
265 int usgs_hack ( const char *scale_factor, const char *uri, const char *fn )
267 static char input_buffer[1024];
269 /* int hnlen = strlen ( scale_factor ); */
273 WSAStartup ( MAKEWORD(2,2), &usadata );
276 sock = http_connect ( scale_factor, 80 );
280 send ( sock, "GET /", 5, 0);
281 send ( sock, uri, strlen ( uri ), 0 );
282 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
284 /* next, skip through all headers EXCEPT content length.,
285 that is, if it begins with "Content-Length: " (strncasecmp),
286 atoi that line from +16 (+17 ?), read that many bytes directly
287 into file (IF we can open it, else return error) and we're done.
290 /* "HTTP/1.x 200 OK" check */
292 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
295 if ( strncmp(input_buffer, "\t\t\t<img src=\"/g", 15) == 0 && strlen(input_buffer) > 28 )
297 char *uri = input_buffer + 13;
299 uri[strlen(uri)-4] = '\0';
300 return a_http_download_get_url_nohostname ( scale_factor, uri, fn );
305 WSACleanup(); /* they sure make winsock programming easy. */