]> git.street.me.uk Git - andy/viking.git/blame - src/http.c
Add referer to DownloadOptions
[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>
932f8f22 23#include <stdlib.h>
50a14534
EB
24#include <gtk/gtk.h>
25#include <string.h>
50a14534
EB
26
27#ifdef WINDOWS
50a14534 28#include <winsock.h>
50a14534 29#else
50a14534 30#include <unistd.h>
50a14534
EB
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <netdb.h>
932f8f22 34#endif
50a14534
EB
35
36#include "http.h"
37
38int http_connect(const char *hostname, int port)
39{
40 int sock;
41 struct sockaddr_in server;
42 struct hostent *host_addr;
43
44 /* create a socket of type AF_INET, and SOCK_STREAM (TCP) */
45 sock = socket(AF_INET, SOCK_STREAM, 0);
46
47 /* get an IP from a domain name -- essential */
48 host_addr = gethostbyname(hostname);
49 if (host_addr == NULL)
50 return(-1);
51
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
61
62 if ((connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr))) == -1)
63 return(-2);
64
65 return(sock);
66}
67
68int http_get_line(int sock, char *buf, int len)
69{
70 static char lilbuf;
71 int size, count;
72
73 count = 1;
74 size = 1;
75 lilbuf = 'a';
76 while (size != 0 && lilbuf != '\n' && count < len)
77 {
78 size = recv(sock, &lilbuf, 1, 0);
79 if (size == 0 && count == 1 )
80 return 0;
81
82 if (size > 0)
83 *buf++ = lilbuf;
84 count++;
85 }
86 *buf = '\0';
87
88 return 1;
89}
90
a3188993 91int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, DownloadOptions *options )
50a14534
EB
92{
93 static char input_buffer[1024];
94 int sock;
95 int len;
932f8f22 96 FILE *tmp_f;
50a14534
EB
97 /* int hnlen = strlen ( hostname ); */
98
50a14534
EB
99#ifdef WINDOWS
100 WSADATA usadata;
101 WSAStartup ( MAKEWORD(2,2), &usadata );
102#endif
103
104 sock = http_connect ( hostname, 80 );
105 if (sock < 0)
106 {
50a14534
EB
107 return -1;
108 }
109
110
a3188993 111 if ( options != NULL && options->sendhostname ) {
50a14534
EB
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 );
120 }
121
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.
126 */
127
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' )
130 {
131 /* maybe it's a redirect */
132 if ( ! already_redirected )
133 do
134 {
135 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
136 break;
137
138 /* Location: http://abc.def/bla */
139 if ( strncmp(input_buffer, "Location: ", 10) == 0 && strlen(input_buffer) > 17 )
140 {
141 char *uri_start;
142
143 int rv;
144 uri_start = strchr(input_buffer+17,'/');
145
146 if ( uri_start )
147 {
148 char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 );
149 char *newuri = strdup ( uri_start );
50a14534
EB
150 close ( sock );
151
932f8f22 152 rv = http_download_get_url ( newhost, newuri, f, 1, sendhostname );
50a14534
EB
153
154 free ( newhost );
155 free ( newuri );
156 return rv;
157 }
158 }
159 } while (input_buffer[0] != '\r' );
160
932f8f22 161 /* Something went wrong */
50a14534
EB
162 return 1;
163 }
164
165 do
166 {
167 if ( http_get_line ( sock, input_buffer, 1024 ) == 0 )
168 {
50a14534
EB
169 close ( sock );
170 return -2;
171 }
172 } while (input_buffer[0] != '\r' );
173
174 tmp_f = tmpfile();
175
176 do {
177 len = recv ( sock, input_buffer, 1024, 0 );
178 if ( len > 0 )
179 fwrite ( input_buffer, 1, len, tmp_f );
180 } while ( len > 0 );
181
182 rewind(tmp_f);
183
184 while ( ! feof(tmp_f) )
185 {
186 len = fread ( input_buffer, 1, 1024, tmp_f );
187 fwrite ( input_buffer, 1, len, f);
188 }
189 fclose ( tmp_f );
50a14534
EB
190
191 close ( sock );
192#ifdef WINDOWS
193 WSACleanup(); /* they sure make winsock programming easy. */
194#endif
195 return 0;
196}