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