]> git.street.me.uk Git - andy/viking.git/blame - src/http.c
Elevation map click and bugfixes
[andy/viking.git] / src / http.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include <stdio.h>
23#include <gtk/gtk.h>
24#include <string.h>
25#include <errno.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29
30#ifdef WINDOWS
31
32#include <io.h>
33#include <winsock.h>
34#define access(a,b) _access(a,b)
35#define close(a) closesocket(a)
36
37char *dirname ( char * dir )
38{
39 char *tmp = dir + strlen(dir) - 1;
40 while ( tmp != dir && *tmp != '\\' )
41 tmp--;
42 *tmp = '\0';
43 return dir;
44}
45
46#else
47
48#include <unistd.h>
49#include <sys/types.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52#include <netdb.h>
53#include <malloc.h>
54
55/* dirname */
56#include <libgen.h>
57
58#endif
59
60#include "http.h"
61
62int http_connect(const char *hostname, int port)
63{
64 int sock;
65 struct sockaddr_in server;
66 struct hostent *host_addr;
67
68 /* create a socket of type AF_INET, and SOCK_STREAM (TCP) */
69 sock = socket(AF_INET, SOCK_STREAM, 0);
70
71 /* get an IP from a domain name -- essential */
72 host_addr = gethostbyname(hostname);
73 if (host_addr == NULL)
74 return(-1);
75
76 server.sin_family = AF_INET;
77 /* 110 is the standard POP port. Host TO Network order. */
78 server.sin_port = htons(port);
79 /* get the IP address. */
80 server.sin_addr = *((struct in_addr *) host_addr->h_addr);
81 /* padding unused in sockaddr_in */
82#ifndef WINDOWS
83 bzero(&(server.sin_zero), 8);
84#endif
85
86 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
87 return(-2);
88
89 return(sock);
90}
91
92int http_get_line(int sock, char *buf, int len)
93{
94 static char lilbuf;
95 int size, count;
96
97 count = 1;
98 size = 1;
99 lilbuf = 'a';
100 while (size != 0 && lilbuf != '\n' && count < len)
101 {
102 size = recv(sock, &lilbuf, 1, 0);
103 if (size == 0 && count == 1 )
104 return 0;
105
106 if (size > 0)
107 *buf++ = lilbuf;
108 count++;
109 }
110 *buf = '\0';
111
112 return 1;
113}
114
115/* makes directory if neccessary */
116int http_download_get_url ( const char *hostname, const char *uri, const char *fn, int already_redirected, int sendhostname )
117{
118 static char input_buffer[1024];
119 int sock;
120 int len;
121 FILE *f, *tmp_f;
122 /* int hnlen = strlen ( hostname ); */
123
124 if ( access ( fn, F_OK ) == 0 )
125 {
126 return -3;
127 } else {
128 if ( errno == ENOENT)
129 {
130 char *tmp = g_strdup ( fn );
131#ifdef WINDOWS
132 mkdir( dirname ( dirname ( tmp ) ) );
133 g_free ( tmp ); tmp = g_strdup ( fn );
134 mkdir( dirname ( tmp ) );
135#else
136 mkdir( dirname ( dirname ( tmp ) ), 0777 );
137 g_free ( tmp ); tmp = g_strdup ( fn );
138 mkdir( dirname ( tmp ), 0777 );
139#endif
140 g_free ( tmp );
141 }
142 if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
143 return -4;
144 }
145#ifdef WINDOWS
146 WSADATA usadata;
147 WSAStartup ( MAKEWORD(2,2), &usadata );
148#endif
149
150 sock = http_connect ( hostname, 80 );
151 if (sock < 0)
152 {
153 fclose ( f );
154 remove ( fn );
155 return -1;
156 }
157
158
159 if ( sendhostname ) {
160 send ( sock, "GET http://", 11, 0);
161 send ( sock, hostname, strlen(hostname), 0 );
162 send ( sock, uri, strlen ( uri ), 0 );
163 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
164 } else {
165 send ( sock, "GET ", 4, 0 );
166 send ( sock, uri, strlen ( uri ), 0 );
167 send ( sock, "\r\n\r\n", 4, 0 );
168 }
169
170 /* next, skip through all headers EXCEPT content length.,
171 that is, if it begins with "Content-Length: " (strncasecmp),
172 atoi that line from +16 (+17 ?), read that many bytes directly
173 into file (IF we can open it, else return error) and we're done.
174 */
175
176 /* "HTTP/1.x 200 OK" check */
177 if ( recv ( sock, input_buffer, 12, 0 ) < 12 || input_buffer[9] != '2' || input_buffer[10] != '0' || input_buffer[11] != '0' )
178 {
179 /* maybe it's a redirect */
180 if ( ! already_redirected )
181 do
182 {
183 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
184 break;
185
186 /* Location: http://abc.def/bla */
187 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
188 {
189 char *uri_start;
190
191 int rv;
192 uri_start = strchr(input_buffer+17,'/');
193
194 if ( uri_start )
195 {
196 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
197 char *newuri = strdup ( uri_start );
198 fclose ( f );
199 remove ( fn );
200 close ( sock );
201
202 rv = http_download_get_url ( newhost, newuri, fn, 1, sendhostname );
203
204 free ( newhost );
205 free ( newuri );
206 return rv;
207 }
208 }
209 } while (input_buffer[0] != '\r' );
210
211 fclose ( f );
212 remove ( fn );
213 return 1;
214 }
215
216 do
217 {
218 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
219 {
220 fclose ( f );
221 remove ( fn );
222 close ( sock );
223 return -2;
224 }
225 } while (input_buffer[0] != '\r' );
226
227 tmp_f = tmpfile();
228
229 do {
230 len = recv ( sock, input_buffer, 1024, 0 );
231 if ( len > 0 )
232 fwrite ( input_buffer, 1, len, tmp_f );
233 } while ( len > 0 );
234
235 rewind(tmp_f);
236
237 while ( ! feof(tmp_f) )
238 {
239 len = fread ( input_buffer, 1, 1024, tmp_f );
240 fwrite ( input_buffer, 1, len, f);
241 }
242 fclose ( tmp_f );
243 fclose ( f );
244
245 close ( sock );
246#ifdef WINDOWS
247 WSACleanup(); /* they sure make winsock programming easy. */
248#endif
249 return 0;
250}
251
252/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
253/* uri: like "/uri.html?whatever" */
254/* only reason for the "wrapper" is so we can do redirects. */
255int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn )
256{
257 return http_download_get_url ( hostname, uri, fn, 0, 1 );
258}
259
260int a_http_download_get_url_nohostname ( const char *hostname, const char *uri, const char *fn )
261{
262 return http_download_get_url ( hostname, uri, fn, 0, 0 );
263}
264
265
266int usgs_hack ( const char *scale_factor, const char *uri, const char *fn )
267{
268 static char input_buffer[1024];
269 int sock;
270 /* int hnlen = strlen ( scale_factor ); */
271
272#ifdef WINDOWS
273 WSADATA usadata;
274 WSAStartup ( MAKEWORD(2,2), &usadata );
275#endif
276
277 sock = http_connect ( scale_factor, 80 );
278 if (sock < 0)
279 return -1;
280
281 send ( sock, "GET /", 5, 0);
282 send ( sock, uri, strlen ( uri ), 0 );
283 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
284
285 /* next, skip through all headers EXCEPT content length.,
286 that is, if it begins with "Content-Length: " (strncasecmp),
287 atoi that line from +16 (+17 ?), read that many bytes directly
288 into file (IF we can open it, else return error) and we're done.
289 */
290
291 /* "HTTP/1.x 200 OK" check */
292 for (;;) {
293 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
294 break;
295
296 if ( strncmp(input_buffer, "\t\t\t<img src=\"/g", 15) == 0 && strlen(input_buffer) > 28 )
297 {
298 char *uri = input_buffer + 13;
299 close(sock);
300 uri[strlen(uri)-4] = '\0';
301 return a_http_download_get_url_nohostname ( scale_factor, uri, fn );
302 }
303 }
304 close ( sock );
305#ifdef WINDOWS
306 WSACleanup(); /* they sure make winsock programming easy. */
307#endif
308 return -10;
309}