Fix bug in Google search that caused viking jump to wrong location when
[viking/guyou.git] / src / http.c
blob56bd16475edb313a5287590aa94879ffaaecb91c
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 int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, DownloadOptions *options )
93 static char input_buffer[1024];
94 int sock;
95 int len;
96 FILE *tmp_f;
97 /* int hnlen = strlen ( hostname ); */
99 #ifdef WINDOWS
100 WSADATA usadata;
101 WSAStartup ( MAKEWORD(2,2), &usadata );
102 #endif
104 sock = http_connect ( hostname, 80 );
105 if (sock < 0)
107 return -1;
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 );
116 } else {
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 )
136 break;
138 /* Location: http://abc.def/bla */
139 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
141 char *uri_start;
143 int rv;
144 uri_start = strchr(input_buffer+17,'/');
146 if ( uri_start )
148 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
149 char *newuri = strdup ( uri_start );
150 close ( sock );
152 if ( options != NULL )
153 rv = http_download_get_url ( newhost, newuri, f, 1, options->sendhostname );
154 else
155 rv = http_download_get_url ( newhost, newuri, f, 1, 0 );
157 free ( newhost );
158 free ( newuri );
159 return rv;
162 } while (input_buffer[0] != '\r' );
164 /* Something went wrong */
165 return 1;
170 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
172 close ( sock );
173 return -2;
175 } while (input_buffer[0] != '\r' );
177 tmp_f = tmpfile();
179 do {
180 len = recv ( sock, input_buffer, 1024, 0 );
181 if ( len > 0 )
182 fwrite ( input_buffer, 1, len, tmp_f );
183 } while ( len > 0 );
185 rewind(tmp_f);
187 while ( ! feof(tmp_f) )
189 len = fread ( input_buffer, 1, 1024, tmp_f );
190 fwrite ( input_buffer, 1, len, f);
192 fclose ( tmp_f );
194 close ( sock );
195 #ifdef WINDOWS
196 WSACleanup(); /* they sure make winsock programming easy. */
197 #endif
198 return 0;