Moving up cache management
[viking/gosmore.git] / src / http.c
blobd9e935bb636729ba2210ac5b44161e7df17514be
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 <stdlib.h>
24 #include <gtk/gtk.h>
25 #include <string.h>
27 #ifdef WINDOWS
28 #include <winsock.h>
29 #else
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #endif
36 #include "http.h"
38 int http_connect(const char *hostname, int port)
40 int sock;
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)
50 return(-1);
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 */
58 #ifndef WINDOWS
59 bzero(&(server.sin_zero), 8);
60 #endif
62 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
63 return(-2);
65 return(sock);
68 int http_get_line(int sock, char *buf, int len)
70 static char lilbuf;
71 int size, count;
73 count = 1;
74 size = 1;
75 lilbuf = 'a';
76 while (size != 0 && lilbuf != '\n' && count < len)
78 size = recv(sock, &lilbuf, 1, 0);
79 if (size == 0 && count == 1 )
80 return 0;
82 if (size > 0)
83 *buf++ = lilbuf;
84 count++;
86 *buf = '\0';
88 return 1;
91 /* makes directory if neccessary */
92 int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, int sendhostname )
94 static char input_buffer[1024];
95 int sock;
96 int len;
97 FILE *tmp_f;
98 /* int hnlen = strlen ( hostname ); */
100 #ifdef WINDOWS
101 WSADATA usadata;
102 WSAStartup ( MAKEWORD(2,2), &usadata );
103 #endif
105 sock = http_connect ( hostname, 80 );
106 if (sock < 0)
108 return -1;
112 if ( sendhostname ) {
113 send ( sock, "GET http://", 11, 0);
114 send ( sock, hostname, strlen(hostname), 0 );
115 send ( sock, uri, strlen ( uri ), 0 );
116 send ( sock, " HTTP/1.0\r\n\r\n", 13, 0 );
117 } else {
118 send ( sock, "GET ", 4, 0 );
119 send ( sock, uri, strlen ( uri ), 0 );
120 send ( sock, "\r\n\r\n", 4, 0 );
123 /* next, skip through all headers EXCEPT content length.,
124 that is, if it begins with "Content-Length: " (strncasecmp),
125 atoi that line from +16 (+17 ?), read that many bytes directly
126 into file (IF we can open it, else return error) and we're done.
129 /* "HTTP/1.x 200 OK" check */
130 if ( recv ( sock, input_buffer, 12, 0 ) < 12 || input_buffer[9] != '2' || input_buffer[10] != '0' || input_buffer[11] != '0' )
132 /* maybe it's a redirect */
133 if ( ! already_redirected )
136 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
137 break;
139 /* Location: http://abc.def/bla */
140 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
142 char *uri_start;
144 int rv;
145 uri_start = strchr(input_buffer+17,'/');
147 if ( uri_start )
149 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
150 char *newuri = strdup ( uri_start );
151 close ( sock );
153 rv = http_download_get_url ( newhost, newuri, f, 1, sendhostname );
155 free ( newhost );
156 free ( newuri );
157 return rv;
160 } while (input_buffer[0] != '\r' );
162 /* Something went wrong */
163 return 1;
168 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
170 close ( sock );
171 return -2;
173 } while (input_buffer[0] != '\r' );
175 tmp_f = tmpfile();
177 do {
178 len = recv ( sock, input_buffer, 1024, 0 );
179 if ( len > 0 )
180 fwrite ( input_buffer, 1, len, tmp_f );
181 } while ( len > 0 );
183 rewind(tmp_f);
185 while ( ! feof(tmp_f) )
187 len = fread ( input_buffer, 1, 1024, tmp_f );
188 fwrite ( input_buffer, 1, len, f);
190 fclose ( tmp_f );
192 close ( sock );
193 #ifdef WINDOWS
194 WSACleanup(); /* they sure make winsock programming easy. */
195 #endif
196 return 0;