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
31 #include <sys/socket.h>
32 #include <netinet/in.h>
38 int http_connect(const char *hostname
, int port
)
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
)
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 */
59 bzero(&(server
.sin_zero
), 8);
62 if ((connect(sock
, (struct sockaddr
*) &server
, sizeof(struct sockaddr
))) == -1)
68 int http_get_line(int sock
, char *buf
, int len
)
76 while (size
!= 0 && lilbuf
!= '\n' && count
< len
)
78 size
= recv(sock
, &lilbuf
, 1, 0);
79 if (size
== 0 && count
== 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];
98 /* int hnlen = strlen ( hostname ); */
102 WSAStartup ( MAKEWORD(2,2), &usadata
);
105 sock
= http_connect ( hostname
, 80 );
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 );
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 )
139 /* Location: http://abc.def/bla */
140 if ( strncmp(input_buffer
, "Location: ", 10) == 0 && strlen(input_buffer
) > 17 )
145 uri_start
= strchr(input_buffer
+17,'/');
149 char *newhost
= g_strndup ( input_buffer
+ 17, uri_start
- input_buffer
- 17 );
150 char *newuri
= strdup ( uri_start
);
153 rv
= http_download_get_url ( newhost
, newuri
, f
, 1, sendhostname
);
160 } while (input_buffer
[0] != '\r' );
162 /* Something went wrong */
168 if ( http_get_line ( sock
, input_buffer
, 1024 ) == 0 )
173 } while (input_buffer
[0] != '\r' );
178 len
= recv ( sock
, input_buffer
, 1024, 0 );
180 fwrite ( input_buffer
, 1, len
, tmp_f
);
185 while ( ! feof(tmp_f
) )
187 len
= fread ( input_buffer
, 1, 1024, tmp_f
);
188 fwrite ( input_buffer
, 1, len
, f
);
194 WSACleanup(); /* they sure make winsock programming easy. */