Unused function
[viking/gosmore.git] / src / http.c
blob349d6ec121928c942b4ecfb3a77952d5606b92d7
1 /*
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
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>
30 #ifdef WINDOWS
32 #include <io.h>
33 #include <winsock.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 != '\\' )
41 tmp--;
42 *tmp = '\0';
43 return dir;
46 #else
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netdb.h>
54 /* dirname */
55 #include <libgen.h>
57 #endif
59 #include "http.h"
61 int http_connect(const char *hostname, int port)
63 int sock;
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)
73 return(-1);
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 */
81 #ifndef WINDOWS
82 bzero(&(server.sin_zero), 8);
83 #endif
85 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
86 return(-2);
88 return(sock);
91 int http_get_line(int sock, char *buf, int len)
93 static char lilbuf;
94 int size, count;
96 count = 1;
97 size = 1;
98 lilbuf = 'a';
99 while (size != 0 && lilbuf != '\n' && count < len)
101 size = recv(sock, &lilbuf, 1, 0);
102 if (size == 0 && count == 1 )
103 return 0;
105 if (size > 0)
106 *buf++ = lilbuf;
107 count++;
109 *buf = '\0';
111 return 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];
118 int sock;
119 int len;
120 FILE *f, *tmp_f;
121 /* int hnlen = strlen ( hostname ); */
123 if ( access ( fn, F_OK ) == 0 )
125 return -3;
126 } else {
127 if ( errno == ENOENT)
129 char *tmp = g_strdup ( fn );
130 #ifdef WINDOWS
131 mkdir( dirname ( dirname ( tmp ) ) );
132 g_free ( tmp ); tmp = g_strdup ( fn );
133 mkdir( dirname ( tmp ) );
134 #else
135 mkdir( dirname ( dirname ( tmp ) ), 0777 );
136 g_free ( tmp ); tmp = g_strdup ( fn );
137 mkdir( dirname ( tmp ), 0777 );
138 #endif
139 g_free ( tmp );
141 if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
142 return -4;
144 #ifdef WINDOWS
145 WSADATA usadata;
146 WSAStartup ( MAKEWORD(2,2), &usadata );
147 #endif
149 sock = http_connect ( hostname, 80 );
150 if (sock < 0)
152 fclose ( f );
153 remove ( fn );
154 return -1;
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 );
163 } else {
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 )
183 break;
185 /* Location: http://abc.def/bla */
186 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
188 char *uri_start;
190 int rv;
191 uri_start = strchr(input_buffer+17,'/');
193 if ( uri_start )
195 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
196 char *newuri = strdup ( uri_start );
197 fclose ( f );
198 remove ( fn );
199 close ( sock );
201 rv = http_download_get_url ( newhost, newuri, fn, 1, sendhostname );
203 free ( newhost );
204 free ( newuri );
205 return rv;
208 } while (input_buffer[0] != '\r' );
210 fclose ( f );
211 remove ( fn );
212 return 1;
217 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
219 fclose ( f );
220 remove ( fn );
221 close ( sock );
222 return -2;
224 } while (input_buffer[0] != '\r' );
226 tmp_f = tmpfile();
228 do {
229 len = recv ( sock, input_buffer, 1024, 0 );
230 if ( len > 0 )
231 fwrite ( input_buffer, 1, len, tmp_f );
232 } while ( len > 0 );
234 rewind(tmp_f);
236 while ( ! feof(tmp_f) )
238 len = fread ( input_buffer, 1, 1024, tmp_f );
239 fwrite ( input_buffer, 1, len, f);
241 fclose ( tmp_f );
242 fclose ( f );
244 close ( sock );
245 #ifdef WINDOWS
246 WSACleanup(); /* they sure make winsock programming easy. */
247 #endif
248 return 0;
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 );