/[axel]/trunk/http.c
ViewVC logotype

Contents of /trunk/http.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Jan 11 11:57:58 2008 UTC (5 years, 5 months ago) by appaji-guest
File MIME type: text/plain
File size: 5530 byte(s)
Import 1.0a
1 /********************************************************************\
2 * Axel -- A lighter download accelerator for Linux and other Unices. *
3 * *
4 * Copyright 2001 Wilmer van der Gaast *
5 \********************************************************************/
6
7 /* HTTP control file */
8
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
22 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include "axel.h"
27
28 int http_connect( http_t *conn, int proto, char *proxy, char *host, int port, char *user, char *pass )
29 {
30 char base64_encode[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31 "abcdefghijklmnopqrstuvwxyz0123456789+/";
32 char auth[MAX_STRING];
33 conn_t tconn[1];
34 int i;
35
36 strncpy( conn->host, host, MAX_STRING );
37 conn->proto = proto;
38
39 if( proxy != NULL ) { if( *proxy != 0 )
40 {
41 sprintf( conn->host, "%s:%i", host, port );
42 if( !conn_set( tconn, proxy ) )
43 {
44 sprintf( conn->request, _("Invalid proxy string: %s\n"), proxy );
45 return( 0 );
46 }
47 host = tconn->host;
48 port = tconn->port;
49 conn->proxy = 1;
50 }
51 else
52 {
53 conn->proxy = 0;
54 } }
55
56 if( ( conn->fd = tcp_connect( host, port, conn->local_if ) ) == -1 )
57 {
58 sprintf( conn->request, _("Unable to connect to server %s:%i\n"), host, port );
59 return( 0 );
60 }
61
62 if( *user == 0 )
63 {
64 *conn->auth = 0;
65 }
66 else
67 {
68 memset( auth, 0, MAX_STRING );
69 snprintf( auth, MAX_STRING, "%s:%s", user, pass );
70 for( i = 0; auth[i*3]; i ++ )
71 {
72 conn->auth[i*4] = base64_encode[(auth[i*3]>>2)];
73 conn->auth[i*4+1] = base64_encode[((auth[i*3]&3)<<4)|(auth[i*3+1]>>4)];
74 conn->auth[i*4+2] = base64_encode[((auth[i*3+1]&15)<<2)|(auth[i*3+2]>>6)];
75 conn->auth[i*4+3] = base64_encode[auth[i*3+2]&63];
76 if( auth[i*3+2] == 0 ) conn->auth[i*4+3] = '=';
77 if( auth[i*3+1] == 0 ) conn->auth[i*4+2] = '=';
78 }
79 }
80
81 return( 1 );
82 }
83
84 void http_disconnect( http_t *conn )
85 {
86 if( conn->fd > 0 )
87 close( conn->fd );
88 conn->fd = -1;
89 }
90
91 void http_get( http_t *conn, char *lurl )
92 {
93 *conn->request = 0;
94 if( conn->proxy )
95 {
96 http_addheader( conn, "GET %s://%s%s HTTP/1.0",
97 conn->proto == PROTO_HTTP ? "http" : "ftp", conn->host, lurl );
98 }
99 else
100 {
101 http_addheader( conn, "GET %s HTTP/1.0", lurl );
102 http_addheader( conn, "Host: %s", conn->host );
103 }
104 http_addheader( conn, "User-Agent: %s", USER_AGENT );
105 if( *conn->auth )
106 http_addheader( conn, "Authorization: Basic %s", conn->auth );
107 if( conn->firstbyte )
108 {
109 if( conn->lastbyte )
110 http_addheader( conn, "Range: bytes=%i-%i", conn->firstbyte, conn->lastbyte );
111 else
112 http_addheader( conn, "Range: bytes=%i-", conn->firstbyte );
113 }
114 }
115
116 void http_addheader( http_t *conn, char *format, ... )
117 {
118 char s[MAX_STRING];
119 va_list params;
120
121 va_start( params, format );
122 vsnprintf( s, MAX_STRING - 3, format, params );
123 strcat( s, "\r\n" );
124 va_end( params );
125
126 strncat( conn->request, s, MAX_QUERY );
127 }
128
129 int http_exec( http_t *conn )
130 {
131 int i = 0;
132 char s[2] = " ", *s2;
133
134 #ifdef DEBUG
135 fprintf( stderr, "--- Sending request ---\n%s--- End of request ---\n", conn->request );
136 #endif
137
138 http_addheader( conn, "" );
139 write( conn->fd, conn->request, strlen( conn->request ) );
140
141 *conn->headers = 0;
142 /* Read the headers byte by byte to make sure we don't touch the
143 actual data */
144 while( 1 )
145 {
146 if( read( conn->fd, s, 1 ) <= 0 )
147 {
148 sprintf( conn->request, _("Connection gone.\n") );
149 return( 0 );
150 }
151 if( *s == '\r' )
152 {
153 continue;
154 }
155 else if( *s == '\n' )
156 {
157 if( i == 0 )
158 break;
159 i = 0;
160 }
161 else
162 {
163 i ++;
164 }
165 strncat( conn->headers, s, MAX_QUERY );
166 }
167
168 #ifdef DEBUG
169 fprintf( stderr, "--- Reply headers ---\n%s--- End of headers ---\n", conn->headers );
170 #endif
171
172 sscanf( conn->headers, "%*s %3i", &conn->status );
173 s2 = strchr( conn->headers, '\n' ); *s2 = 0;
174 strcpy( conn->request, conn->headers );
175 *s2 = '\n';
176
177 return( 1 );
178 }
179
180 char *http_header( http_t *conn, char *header )
181 {
182 char s[32];
183 int i;
184
185 for( i = 1; conn->headers[i]; i ++ )
186 if( conn->headers[i-1] == '\n' )
187 {
188 sscanf( &conn->headers[i], "%31s", s );
189 if( strcasecmp( s, header ) == 0 )
190 return( &conn->headers[i+strlen(header)] );
191 }
192
193 return( NULL );
194 }
195
196 int http_size( http_t *conn )
197 {
198 char *i;
199 int j;
200
201 if( ( i = http_header( conn, "Content-Length:" ) ) == NULL )
202 return( -2 );
203
204 sscanf( i, "%i", &j );
205 return( j );
206 }
207
208 /* Decode%20a%20file%20name */
209 void http_decode( char *s )
210 {
211 char t[MAX_STRING];
212 int i, j, k;
213
214 for( i = j = 0; s[i]; i ++, j ++ )
215 {
216 t[j] = s[i];
217 if( s[i] == '%' )
218 if( sscanf( s + i + 1, "%2x", &k ) )
219 {
220 t[j] = k;
221 i += 2;
222 }
223 }
224 t[j] = 0;
225
226 strcpy( s, t );
227 }
228
229 void http_encode( char *s )
230 {
231 char t[MAX_STRING];
232 int i, j;
233
234 for( i = j = 0; s[i]; i ++, j ++ )
235 {
236 t[j] = s[i];
237 if( s[i] == ' ' )
238 {
239 strcpy( t + j, "%20" );
240 j += 2;
241 }
242 }
243 t[j] = 0;
244
245 strcpy( s, t );
246 }

  ViewVC Help
Powered by ViewVC 1.1.5