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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
|
/* HTGET (C) J Whitham 1998
** based on snarf, the Simple Non-interactive All-purpose Resource Fetcher
** snarf is copyright (C) 1995, 1996 Zachary Beane
**
** 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
** MERCHANTABILIY 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
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** The author of this program may be reached via email at
** jwhitham@globalnet.co.uk
*/
/* htget version 0.91 */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
#define MAXLEN 256
#define BIG_BUFFER_SIZE 4096
#define BASE64_END '='
const char base64_table[64]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#define HEADER_INDICATOR "TTP"
#define USER_PASS "anonymous:altrn@"
#define MY_NAME "alternate 1"
#define CONTENT_LENGTH "Content-Length:"
#define MAX_URLS 16
#define OPTIONS 15
/* The HTGet function can return these errors: */
#define ERROR_TIMEOUT 1
#define ERROR_WRITE 2
#define ERROR_ABORT 3
#define ERROR_RESUME_IMPOSSIBLE 4
#define ERROR_HTTP 5
#define ERROR_NOT_NEEDED 6 /* file is already complete */
#define ERROR_TCP 7
#define DOWNLOAD_OK 8
#define ERROR_PARSE 9
#define ERROR_LIST 10
#define ERROR_FOUR_OH_FOUR 11
char ShowHelp ;
long Timeout ;
long Redial ;
long Resume ;
char Verbose ;
long MinRate ;
char ShowBytesRemaining ;
char ShowTimeRemaining ;
char ShowPercentage ;
char ShowBytesPerSecond ;
char DownloadsDir [ 128 ] ;
char UserAgent [ 128 ] ;
char ShowBanner ;
char * MakeHTTPRequest ( char * Hostname , char * Filename ) ;
int TCPConnect ( char * Hostname , unsigned int Port ) ;
int ReadLine ( int Socket , char * ReceiveBuffer ) ;
char * ToBase64(unsigned char *bin, int len ) ;
int ProcessURL ( char * TheURL , char * Hostname , char * Filename , char * ActualFilename , unsigned * Port ) ;
int GetHeaderForRequest ( unsigned socket , char * request , char * Header ) ;
void PrintReport ( long ArrivedSize , long RangeSize ,
time_t CurrentTime , time_t StartTime , time_t TimeoutTime ) ;
int main ( int argc , char * argv [] )
{
char Temp [ 130 ] ;
int I , J , URLs , Ok , CurrentURL , Success ;
FILE * ResourceFile ;
char * Home ;
char RetrieveURL [ MAX_URLS ][ 128 ] ;
/* open the resource file. */
/* first search cmd line params to see if one has been specified. */
for ( I = 1 ; I < argc ; I ++ )
{
if ( strncasecmp ( "--rcfile=" , argv [ I ] , 9 ) == 0 )
{
ResourceFile = fopen ( & argv [ I ][ 9 ] , "rt" ) ;
if ( ResourceFile == NULL )
{
fprintf ( stderr , "%s: couldn't open rc file %s\n" , argv [ 0 ] , & argv [ I ][ 9 ] ) ;
exit ( 1 ) ;
}
}
}
if ( ResourceFile == NULL )
{
/* try a few other locations */
ResourceFile = fopen ( "htgetrc" , "rt" ) ;
if ( ResourceFile == NULL )
{
Home = (char *) getenv ( "HOME" ) ;
if ( Home == NULL )
{
strcpy ( Temp , ".htgetrc" ) ;
} else {
strcpy ( Temp , Home ) ;
strcat ( Temp , "/.htgetrc" ) ;
}
ResourceFile = fopen ( Temp , "rt" ) ;
if ( ResourceFile == NULL)
{
strcpy ( Temp, "/etc/htget.conf" );
ResourceFile = fopen ( Temp, "rt" );
}
if ( ResourceFile == NULL )
{
fprintf ( stderr , "%s: couldn't find a htget resource file in the current\n"
"directory or your home directory. Please read\n"
"the documentation that came with htget for more details\n" ,
argv [ 0 ] ) ;
}
}
}
/* set up some default values */
Timeout = 30 ;
Redial = 10 ;
Resume = 1 ;
MinRate = 1000 ;
Verbose = 0 ;
ShowBytesRemaining = 1 ;
ShowTimeRemaining = 1 ;
ShowPercentage = 1 ;
ShowBytesPerSecond = 1 ;
strcpy ( DownloadsDir , "./" ) ;
strcpy ( UserAgent , "linux htget 0.91" ) ;
ShowBanner = 1 ;
if ( ResourceFile != NULL )
{
while ( ! feof ( ResourceFile ) )
{
fgets ( Temp , 128 , ResourceFile ) ;
/* Remove terminating newline/junk */
for ( I = 0 ; I < strlen ( Temp ) ; I ++ )
{
if ( ! isprint ( Temp [ I ] ))
{
Temp [ I ] = '\0' ;
break ;
}
}
if ( Temp [ 0 ] != '#' )
{
if ( strncasecmp ( Temp , "Timeout=" , 8 ) == 0 )
{
Timeout = atoi ( & Temp [ 8 ] ) ;
if ( Timeout < 0 )
{
fprintf ( stderr , "Timeout value cannot be negative!\n" ) ;
return ( 1 ) ;
}
}
if ( strncasecmp ( Temp , "Redial=" , 7 ) == 0 )
{
Redial = atoi ( & Temp [ 7 ] ) ;
if ( Redial < 0 )
{
fprintf ( stderr , "Redial delay value cannot be negative!\n" ) ;
return ( 1 ) ;
}
}
if ( strncasecmp ( Temp , "MinRate=" , 8 ) == 0 )
{
MinRate = atoi ( & Temp [ 8 ] ) ;
if ( MinRate < 0 )
{
fprintf ( stderr , "MinRate value cannot be negative!\n" ) ;
return ( 1 ) ;
}
}
if ( strncasecmp ( Temp , "Resume=" , 7 ) == 0 )
{
Resume = ( atoi ( & Temp [ 7 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "Verbose=" , 8 ) == 0 )
{
Verbose = ( atoi ( & Temp [ 8 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "ShowBytesRemaining=" , 19 ) == 0 )
{
ShowBytesRemaining = ( atoi ( & Temp [ 19 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "ShowPercentage=" , 15 ) == 0 )
{
ShowPercentage = ( atoi ( & Temp [ 15 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "ShowBytesPerSecond=" , 19 ) == 0 )
{
ShowBytesPerSecond = ( atoi ( & Temp [ 19 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "ShowBanner=" , 11 ) == 0 )
{
ShowBanner = ( atoi ( & Temp [ 11 ] ) == 1 ) ;
}
if ( strncasecmp ( Temp , "DownloadsDir=" , 13 ) == 0 )
{
strcpy ( DownloadsDir , & Temp [ 13 ] ) ;
}
if ( strncasecmp ( Temp , "UserAgent=" , 10 ) == 0 )
{
strcpy ( UserAgent , & Temp [ 10 ] ) ;
}
}
}
fclose ( ResourceFile ) ;
}
if ( ShowBanner )
{
fprintf ( stderr , "HTGET (c) J Whitham 1998 <jwhitham@globalnet.co.uk> version 0.91\n"
"See README. This program comes with NO WARRANTY of any kind.\n\n" ) ;
}
/* parse cmd line */
URLs = 0 ;
for ( I = 1 ; I < argc ; I ++ )
{
if ( argv [ I ][ 0 ] == '-' )
{
if (( strncasecmp ( argv [ I ] , "--help" , 6 ) == 0 )
|| ( strncasecmp ( argv [ I ] , "-h" , 2 ) == 0 )
|| ( strncasecmp ( argv [ I ] , "-?" , 2 ) == 0 ))
{
ShowHelp = 1 ;
}
else if ( strncasecmp ( argv [ I ] , "--verbose" , 9 ) == 0 )
{
Verbose = 1 ;
}
else if ( strncasecmp ( argv [ I ] , "--noverbose" , 11 ) == 0 )
{
Verbose = 0 ;
}
else if ( strncasecmp ( argv [ I ] , "--resume" , 8 ) == 0 )
{
Resume = 1 ;
}
else if (( strncasecmp ( argv [ I ] , "--noresume" , 10 ) == 0 )
|| ( strncasecmp ( argv [ I ] , "-n" , 2 ) == 0 ))
{
Resume = 0 ;
}
else if ( strncasecmp ( argv [ I ] , "--downloadsdir=" , 15 ) == 0 )
{
strcpy ( DownloadsDir , & argv [ I ][ 15 ] ) ;
}
else if ( strncasecmp ( argv [ I ] , "--currentdir" , 12 ) == 0 )
{
strcpy ( DownloadsDir , "./" ) ;
}
else if ( strncasecmp ( argv [ I ] , "--timeout=" , 10 ) == 0 )
{
Timeout = atoi ( & argv [ I ][ 10 ] ) ;
if ( Timeout < 0 )
{
fprintf ( stderr , "Timeout must be a positive integer\n" ) ;
return ( 1 ) ;
}
}
else if ( strncasecmp ( argv [ I ] , "--redial=" , 9 ) == 0 )
{
Redial = atoi ( & argv [ I ][ 9 ] ) ;
if ( Redial < 0 )
{
fprintf ( stderr , "Redial must be a positive integer\n" ) ;
return ( 1 ) ;
}
}
else if ( strncasecmp ( argv [ I ] , "--minrate=" , 10 ) == 0 )
{
MinRate = atoi ( & argv [ I ][ 10 ] ) ;
if ( MinRate < 0 )
{
fprintf ( stderr , "MinRate must be a positive integer\n" ) ;
return ( 1 ) ;
}
}
else if ( strncasecmp ( argv [ I ] , "--rcfile=" , 9 ) == 0 )
{
/* already dealt with this one */
} else {
fprintf ( stderr , "Unrecognised parameter: %s\n" , argv [ I ] ) ;
}
} else {
if ( strlen ( argv [ I ] ) > 0 )
{
strcpy ( RetrieveURL [ URLs ] , argv [ I ] ) ;
URLs ++ ;
if ( URLs >= MAX_URLS )
{
fprintf ( stderr , "The maximum number of URLs that can be specified at the command line is %d.\n" , MAX_URLS ) ;
return ( 0 ) ;
}
}
}
}
if ( ( URLs < 1 ) || ShowHelp )
{
fprintf ( stderr , "Usage: htget <urls..> [options]\n\n"
"Options include:\n"
"--help -h This text\n"
"--verbose Verbose download mode on\n"
"--noverbose Verbose download mode off\n"
"--resume D/load resuming on\n"
"--noresume D/load resuming off\n"
"--timeout=t Set timeout to t seconds\n"
"--rcfile=rc Specify alternative resource file rc\n"
"\n\n"
"For more information, see the htget README file\n"
"or enter: man htget\n" ) ;
return ( 1 ) ;
}
Ok = 1 ;
CurrentURL = 0 ;
Success = 0 ;
while (( Ok ) && ( CurrentURL < URLs ))
{
fprintf ( stderr , "\n" ) ;
Ok = 1 ;
switch ( HTGet ( RetrieveURL [ CurrentURL ] , Resume ) )
{
case ERROR_WRITE :
fprintf ( stderr , "\nError writing to local file!\n" ) ;
CurrentURL ++ ;
break ;
case ERROR_NOT_NEEDED :
CurrentURL ++ ;
break ;
/* All of these are unrecoverable errors */
case ERROR_PARSE : CurrentURL ++ ; /* next please */
break ;
case DOWNLOAD_OK : Success ++ ;
CurrentURL ++ ;
break ;
case ERROR_RESUME_IMPOSSIBLE : /* I hate this! */
break ;
case ERROR_TCP :
case ERROR_HTTP :
if ( Redial == 0 )
{
CurrentURL ++ ;
} else {
fprintf ( stderr , "\nRedial: delaying for %d seconds before retry.." , Redial ) ;
sleep ( Redial ) ;
fprintf ( stderr , "\n" ) ;
}
break ;
case ERROR_FOUR_OH_FOUR :
fprintf ( stderr , "\nFile not found on server.\n" ) ;
CurrentURL ++ ;
break ;
case ERROR_ABORT :
Ok = 0 ;
break ;
case ERROR_TIMEOUT : /* just try again */
if ( Redial > 0 )
{
fprintf ( stderr , "\nRedial: delaying for %d seconds before retry.." , Redial ) ;
sleep ( Redial ) ;
fprintf ( stderr , "\n" ) ;
}
break ;
}
}
fprintf ( stderr , "\n\nhtget: Successfully downloaded %d of %d URLs\n" , Success , URLs ) ;
return ( 0 ) ;
}
int HTGet ( char * RetrieveURL , int AllowResume )
{
/* request-header */
char Header [ BIG_BUFFER_SIZE ] ;
char Request [ BIG_BUFFER_SIZE ] ;
/* tcp/ip */
unsigned socket ;
/* URL */
int I , Port ;
char ch ;
char Hostname [ MAXLEN ] ;
char Filename [ MAXLEN ] ;
char ActualFilename [ MAXLEN ] ;
char Parameter [ MAXLEN ] ;
int Resuming = 0 ;
/* output */
FILE * File ;
long ActualSize , ArrivedSize ;
char recv_buf [ BIG_BUFFER_SIZE ] ;
int Percentage ;
long CurrentSize ;
long RangeSize = 0 , RangeStart = 0 ;
/* authenticate */
char * UserPass ;
struct stat ConnectionStatistics ;
/* timing */
time_t StartTime , CurrentTime , TimeoutTime ;
time_t RedundantReportsTimer ;
UserPass = (char *) malloc ( strlen ( USER_PASS ) + 1 ) ;
strcpy ( UserPass , USER_PASS ) ;
if ( ! ProcessURL ( RetrieveURL , Hostname , Filename , ActualFilename , & Port ) )
{
fprintf ( stderr , "Couldn't parse URL %s\n" , RetrieveURL ) ;
return ( ERROR_PARSE ) ;
}
/* apply alternate directory stuff */
for ( I = strlen ( DownloadsDir ) - 1 ; I >= 0 ; I -- )
{
if ( DownloadsDir [ I ] == '/' )
{
DownloadsDir [ I ] == '\0' ;
} else {
break ;
}
}
strcat ( DownloadsDir , "/" ) ;
strcpy ( recv_buf , ActualFilename ) ;
strcpy ( ActualFilename , DownloadsDir ) ;
strcat ( ActualFilename , recv_buf ) ;
CurrentSize = 0 ;
/* read/resume check */
File = fopen ( ActualFilename , "rb" ) ;
if ( File != NULL )
{
fseek ( File , 0 , SEEK_END ) ;
CurrentSize = ftell ( File ) ;
fclose ( File ) ;
}
/* write check */
File = fopen ( ActualFilename , "ab" ) ;
if ( File == NULL )
{
fprintf ( stderr , "Error: attempt to open file %s for writing failed.\n" , ActualFilename ) ;
return ( ERROR_WRITE ) ;
}
fclose ( File ) ;
ActualSize = -1 ;
if (( CurrentSize > 0 ) && AllowResume )
{
/* See if resume is possible by finding out the file length,
time and date */
socket = TCPConnect ( Hostname , Port ) ;
if ( socket == 0 )
{
return ( ERROR_TCP ) ; /* error messages already generated by TCPConnect */
}
fprintf ( stderr , "Getting header for %s.." , RetrieveURL ) ;
sprintf ( Request , "HEAD %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-agent: %s\r\n"
"Authorization: Basic %s\r\n\r\n" ,
Filename , Hostname , UserAgent ,
ToBase64 ( UserPass , strlen ( UserPass ) ) ) ;
if ( Verbose )
{
fprintf ( stderr , "\n** Sent: %s" , Request ) ;
}
I = GetHeaderForRequest ( socket , Request , Header ) ;
if ( I > 0 )
{
if ( I == 404 )
{
return ( ERROR_FOUR_OH_FOUR ) ;
} else {
return ( ERROR_HTTP ) ;
}
}
if ( Verbose )
{
fprintf ( stderr , "\n** Received: %s" , Header ) ;
}
/* get some data from the header..
* the actual size for file resumes */
Resuming = 0 ;
if ( GetHeaderData ( Header , "Content-Length:" , recv_buf ) )
{
ActualSize = atol ( recv_buf ) ;
if ( CurrentSize > 0 )
{
if ( CurrentSize < ActualSize )
{
Resuming = 1 ;
fprintf ( stderr , "Resuming download from %ld bytes\n" , CurrentSize ) ;
}
if ( CurrentSize == ActualSize )
{
fprintf ( stderr , "Remote and local size is the same!\n" ) ;
return ( ERROR_NOT_NEEDED ) ;
}
}
}
/* next bit: is it really necessary to reopen the socket?? */
} else {
CurrentSize = 0 ;
/* Avoid bugs in the next bit */
}
socket = TCPConnect ( Hostname , Port ) ;
if ( socket == 0 )
{
return ( ERROR_TCP ) ;
}
fprintf ( stderr , "Getting data for %s.." , RetrieveURL ) ;
if ( Resuming )
{
sprintf ( Request , "GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"User-agent: %s\r\n"
"Range: bytes=%ld-%ld\r\n"
"Authorization: Basic %s\r\n\r\n" ,
Filename , Hostname , UserAgent ,
CurrentSize , ActualSize ,
ToBase64 ( UserPass , strlen ( UserPass ) ) ) ;
} else {
sprintf ( Request , "GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-agent: %s\r\n"
"Authorization: Basic %s\r\n\r\n" ,
Filename , Hostname , UserAgent ,
ToBase64 ( UserPass , strlen ( UserPass ) ) ) ;
}
if ( Verbose )
{
fprintf ( stderr , "\n** Sent: %s" , Request ) ;
}
I = GetHeaderForRequest ( socket , Request , Header );
if ( I > 0 )
{
if ( I == 404 )
{
return ( ERROR_FOUR_OH_FOUR ) ;
} else {
return ( ERROR_HTTP ) ;
}
}
if ( Verbose )
{
fprintf ( stderr , "\n** Received: %s" , Header ) ;
}
if ( Resuming )
{
RangeSize = ActualSize - CurrentSize ;
} else {
RangeSize = ActualSize ;
}
if ( GetHeaderData ( Header , "Content-Length:" , recv_buf ) )
{
RangeSize = atol ( recv_buf ) ;
if ( ActualSize < 0 )
{
ActualSize = RangeSize ;
}
}
RangeStart = 0 ;
if ( GetHeaderData ( Header , "Content-Range:" , recv_buf ) )
{
/* example from RFC:
* Content-Range: bytes 21010-47021/47022
* add 6 bytes */
RangeStart = atol ( & recv_buf [ 6 ] ) ;
}
if (( Resuming )
&& (( RangeStart > CurrentSize ) || ( RangeStart < 128 )) )
{
fprintf ( stderr , "This server does not support resume\n" ) ;
return ( ERROR_RESUME_IMPOSSIBLE ) ;
}
if ( CurrentSize > 0 )
{
File = fopen ( ActualFilename , "ab" ) ;
} else {
File = fopen ( ActualFilename , "wb" ) ;
}
if ( File == NULL )
{
return ( ERROR_WRITE ) ;
}
if ( RangeStart > CurrentSize )
{
fprintf ( stderr , "The HTTP range is past the end of file\n" ) ;
return ( ERROR_RESUME_IMPOSSIBLE ) ;
/* well actually it isn't but this will force the
gizmo to try again from 0 bytes */
}
fseek ( File , RangeStart , SEEK_SET ) ;
fprintf ( stderr , "\nReceiving HTTP data.." ) ;
I = read ( socket , recv_buf , sizeof ( recv_buf ) ) ;
fcntl ( socket , F_SETFL , O_NONBLOCK ) ;
ArrivedSize = 0 ;
fprintf ( stderr , "..\nTotal size: %ld bytes\n" , ActualSize ) ;
StartTime = time ( NULL ) ;
TimeoutTime = StartTime + Timeout ;
while ( I > 0 )
{
if ( fwrite ( recv_buf , I , 1 , File ) != 1 )
{
fprintf ( stderr , "\nError writing to file %s\n" , ActualFilename ) ;
fclose ( File ) ;
return ( ERROR_WRITE ) ;
}
ArrivedSize += I ;
CurrentTime = time ( NULL ) ;
if ( MinRate < 1 )
{
TimeoutTime = CurrentTime + Timeout ;
} else {
/* BytesPerSecond = ArrivedSize / ( CurrentTime - StartTime ) ; */
if (( ArrivedSize > 0 )
&& ( CurrentTime > StartTime ))
{
if (( ArrivedSize / ( CurrentTime - StartTime )) < MinRate )
{
/* just not good enough */
} else {
TimeoutTime = CurrentTime + Timeout ;
}
} else {
TimeoutTime = CurrentTime + Timeout ;
}
}
I = -1 ;
RedundantReportsTimer = 0 ;
while ( I < 0 )
{
I = read ( socket , recv_buf , sizeof ( recv_buf ) ) ;
if ( RedundantReportsTimer != CurrentTime )
{
PrintReport ( ArrivedSize , RangeSize , CurrentTime , StartTime , TimeoutTime ) ;
RedundantReportsTimer = CurrentTime ;
}
CurrentTime = time ( NULL ) ;
if ( Timeout < 1 )
{
TimeoutTime = CurrentTime + 120 ;
}
if ( CurrentTime > TimeoutTime )
{
fclose ( File ) ;
return ( ERROR_TIMEOUT ) ;
}
}
}
fclose ( File ) ;
fprintf ( stderr , "\nFinished downloading.\n" ) ;
return ( DOWNLOAD_OK ) ;
}
/* MakeHTTPRequest
* Makes a HTTP Request header for Hostname/Filename
* [ZB] */
char * MakeHTTPRequest ( char * Hostname , char * Filename )
{
char tmp [ MAXLEN ] ;
char *request ;
char *pwstring ;
/* size of the two strings, plus ':' and trailing null */
pwstring = (char *) malloc ( strlen ( USER_PASS ) + 1 ) ;
strcpy ( pwstring , USER_PASS ) ;
sprintf(tmp, "GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-agent: snarf %s\r\n"
"Authorization: Basic %s\r\n\r\n",
Filename , Hostname , MY_NAME ,
ToBase64 ( pwstring , strlen ( pwstring ) ) ) ;
request = (char *) malloc ( sizeof ( tmp ) + 1 ) ;
strncpy ( request , tmp , MAXLEN ) ;
return ( request ) ;
}
/* TCPConnect
* Gets a socket number for the host after finding it's address
* [ZB] */
int TCPConnect ( char * Hostname , unsigned int Port )
{
struct hostent * host ;
struct sockaddr_in sin ;
int sock_fd ;
if( ( host = gethostbyname ( Hostname ) ) == NULL )
{
fprintf ( stderr , "Error finding host %s\n" , Hostname ) ;
return ( 0 ) ;
}
/* get the socket */
if( ( sock_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
{
fprintf ( stderr , "Error creating TCP/IP socket\n" ) ;
return ( 0 ) ;
}
/* connect the socket, filling in the important stuff */
sin . sin_family = AF_INET ;
sin . sin_port = htons ( Port ) ;
memcpy ( & sin . sin_addr , host -> h_addr , host -> h_length ) ;
if ( connect ( sock_fd , ( struct sockaddr * ) & sin , sizeof ( sin ) ) < 0 )
{
fprintf ( stderr , "Error connecting to %s\n" , Hostname ) ;
return ( 0 ) ;
}
return ( sock_fd ) ;
}
/* ReadLine
* Reads a \n terminated line (includes the \n)
* [ZB] heavily modified by jw.*/
int ReadLine ( int Socket , char * ReceiveBuffer )
{
int rc = 0 ;
char ch ;
int I = 0 ;
rc = read ( Socket , & ch , 1 ) ;
while ( rc == 1 )
{
ReceiveBuffer [ I ] = ch ;
I ++ ;
if ( ch == '\n' )
{
break ;
}
rc = read ( Socket , & ch , 1 ) ;
}
if ( rc == 0 )
{
if ( I == 0 )
{
/* nothing read.. */
return ( 0 ) ;
}
/* unexpected EOF */
}
if (( rc != 0 ) && ( rc != 1 ))
{
/* something has gone very wrong!? */
return ( -1 ) ;
}
ReceiveBuffer [ I ] = '\0' ;
return ( I ) ;
}
/* ToBase64 [ZB]
* lauri alanko, i thank you */
char * ToBase64(unsigned char *bin, int len)
{
char *buf= (char *)malloc((len+2)/3*4+1);
int i=0, j=0;
while(j<len-2){
buf[i++]=base64_table[bin[j]>>2];
buf[i++]=base64_table[((bin[j]&3)<<4)|(bin[j+1]>>4)];
buf[i++]=base64_table[((bin[j+1]&15)<<2)|(bin[j+2]>>6)];
buf[i++]=base64_table[bin[j+2]&63];
j+=3;
}
switch(len-j){
case 1:
buf[i++]=base64_table[bin[j]>>2];
buf[i++]=base64_table[(bin[j]&3)<<4];
buf[i++]=BASE64_END;
buf[i++]=BASE64_END;
break;
case 2:
buf[i++]=base64_table[bin[j]>>2];
buf[i++]=base64_table[((bin[j]&3)<<4)|(bin[j+1]>>4)];
buf[i++]=base64_table[(bin[j+1]&15)<<2];
buf[i++]=BASE64_END;
break;
case 0:
}
buf[i]='\0';
return buf;
}
/* ProcessURL
* Converts TheURL to it's component parts. Uses defaults if necessary.
* [JW] returns 0 if error. 1 if OK. */
int ProcessURL ( char * TheURL , char * Hostname , char * Filename , char * ActualFilename , unsigned * Port )
{
char BufferURL [ MAXLEN ] ;
char NormalURL [ MAXLEN ] ;
int I ;
/* clear everything. Is this necessary? not sure */
memset ( Hostname , '\0' , strlen ( TheURL ) ) ;
memset ( Filename , '\0' , strlen ( TheURL ) ) ;
memset ( ActualFilename , '\0' , strlen ( TheURL ) ) ;
/* remove "http://" */
strcpy ( BufferURL , TheURL ) ;
/* BUGFIX: strlwr ( BufferURL )? */
if ( strncmp ( BufferURL , "http://" , 7 ) == 0 )
{
strcpy ( NormalURL , & TheURL [ 7 ] ) ;
} else {
strcpy ( NormalURL , TheURL ) ;
}
if ( strlen ( NormalURL ) < 1 )
{
return ( 0 ) ;
}
/* scan for first /. */
for ( I = 0 ; I < strlen ( NormalURL ) ; I ++ )
{
if ( NormalURL [ I ] == '/' )
{
break ;
}
}
/* whatever, I will be the end of the hostname */
strncpy ( Hostname , NormalURL , I ) ;
if ( I < 1 )
{
return ( 0 ) ; /* gone wrong -- too short */
}
strcpy ( BufferURL , & NormalURL [ I ] ) ;
if ( strlen ( BufferURL ) < 1 )
{
/* no filename error! */
/* Bugfix: this is still legal, but we must specify root */
strcat ( BufferURL , "/" ) ;
}
(* Port) = 80 ; /* default HTTP port */
for ( I = strlen ( BufferURL ) - 1 ; I >= 0 ; I -- )
{
if ( BufferURL [ I ] == ':' )
{
/* everything after I is a port number */
(* Port) = (unsigned int) atol ( & BufferURL [ I + 1 ] ) ;
BufferURL [ I ] = '\0' ;
break ;
}
}
/* that means that BufferURL is the Filename */
strcpy ( Filename , BufferURL ) ;
if ( strlen ( BufferURL ) < 1 )
{
/* no filename error, again */
return ( 0 ) ;
}
/* now the ActualFilename, everything after the last / */
for ( I = strlen ( Filename ) - 1 ; I >= 0 ; I -- )
{
if ( Filename [ I ] == '/' )
{
strcpy ( ActualFilename , & Filename [ I + 1 ] ) ;
break ;
}
}
if ( strlen ( ActualFilename ) < 1 )
{
/* This is probably because user asked for URL like: www.stuff.com/
Make up a filename based on URL. */
strcpy ( ActualFilename , Hostname ) ;
}
return ( 1 ) ;
}
/* GetHeaderForRequest .. gets the HTTP header for
* request @ socket into Header */
int GetHeaderForRequest ( unsigned socket , char * request , char * Header )
{
int Timeout , I ;
char line [ MAXLEN ] ;
char status [ MAXLEN ] ;
char token [ MAXLEN ] ;
int in_header;
char ch ;
Header [ 0 ] = '\0' ;
send ( socket , request , strlen ( request ) , 0 ) ;
/* hmm...let's do a bit of status checking by reading the numeric
response code from the server */
Timeout = 0 ;
/* I don't know how much of this waiting stuff is really needed,
but I put it in after some problems connecting to my local server,
there are possibly some latency issues here? */
fprintf ( stderr , ".." ) ;
ch = '\0' ;
I = read ( socket , & ch , 1 ) ;
while ( I == 0 )
{
fprintf ( stderr , "." ) ;
sleep ( 1 ) ;
I = read ( socket , & ch , 1 ) ;
}
if ( I == -1 )
{
fprintf ( stderr , "\nUnknown TCP/IP error\n" ) ;
return ( 1 ) ;
}
strcpy ( status , "" ) ;
if ( ! ReadLine ( socket , status ) )
{
fprintf ( stderr , "no response\n" ) ;
return ( 1 ) ;
} else {
fprintf ( stderr , "\n" ) ;
}
/* this will re-append the check character read above to the 1st line */
line [ 0 ] = ch ;
line [ 1 ] = '\0' ;
strcat ( line , status ) ;
strcpy ( status , line ) ;
/* scan line for "HTTP" to indicate this is the header */
for ( I = 0 ; I < ( strlen ( line ) - strlen ( HEADER_INDICATOR ) ) ; I ++ )
{
if ( strncmp ( & line [ I ] , HEADER_INDICATOR , strlen ( HEADER_INDICATOR ) ) == 0 )
{
in_header = 1 ;
break ;
}
}
if ( in_header )
{
strtok(line, " ");
strcpy(token, (char *)strtok(NULL, " "));
if ( token [ 0 ] == '4' || token [ 0 ] == '5' )
{
fprintf ( stderr , "http error from server: %s" , status ) ;
if ( strncmp ( token , "404" , 3 ) == 0 )
{
return ( 404 ) ;
} else {
return ( 1 ) ;
}
}
}
while ( in_header )
{
if ( ! ReadLine(socket, line) )
{
in_header = 0;
break;
}
if ( line [ 0 ] == '\n' || line [ 1 ] == '\n' )
{
in_header = 0;
}
strcat ( Header , line ) ;
}
return ( 0 ) ;
}
int NoCaseStrNCmp ( char * String1 , char * String2 , int Length )
{
int I ;
for ( I = 0 ; I < Length ; I ++ )
{
if ( toupper ( String1 [ I ] ) != toupper ( String2 [ I ] ))
{
return ( 0 ) ;
}
}
return ( 1 ) ;
}
int GetHeaderData ( char * Header , char * FieldName , char * Record )
{
int I , J ;
for ( I = 0 ; I < strlen ( Header ) ; I ++ )
{
if ( NoCaseStrNCmp ( & Header [ I ] , FieldName , strlen ( FieldName ) ) )
{
I += strlen ( FieldName ) ;
strcpy ( Record , & Header [ I ] ) ;
for ( J = 0 ; J < strlen ( Record ) ; J ++ )
{
if ( Record [ J ] == '\n' )
{
Record [ J ] = '\0' ;
break ;
}
}
return ( 1 ) ;
}
}
return ( 0 ) ;
}
void PrintReport ( long ArrivedSize , long RangeSize ,
time_t CurrentTime , time_t StartTime , time_t TimeoutTime )
{
long Percentage ;
long BytesPerSecond ;
time_t TimeLeft ;
long BytesLeft ;
fprintf ( stderr , "\r\033[K\r.." ) ;
if ( ShowBytesRemaining )
{
if ( RangeSize > 0 )
{
fprintf ( stderr , " %ld/%ld" , ArrivedSize , RangeSize ) ;
} else {
fprintf ( stderr , " %ld/unknown" , ArrivedSize ) ;
}
}
if (( ShowPercentage ) && ( RangeSize > 0 ) )
{
Percentage = ( ArrivedSize * 100 ) / RangeSize ;
fprintf ( stderr , " %d%%" , Percentage ) ;
}
if ( ( CurrentTime - StartTime ) > 0 )
{
BytesPerSecond = ArrivedSize / ( CurrentTime - StartTime ) ;
if ( ShowBytesPerSecond )
{
fprintf ( stderr , " %ld bytes/sec" , BytesPerSecond ) ;
}
if (( RangeSize > 0 ) && ShowTimeRemaining )
{
/* time left may be predicted. */
BytesLeft = RangeSize - ArrivedSize ;
TimeLeft = BytesLeft / BytesPerSecond ;
if ( TimeLeft > 3599 )
{
/* hours and minutes */
fprintf ( stderr , "%4dh %02dm" ,
( TimeLeft / 3600 ) , (( TimeLeft / 60 ) % 60 ) ) ;
} else {
fprintf ( stderr , "%4dm %02ds" ,
(( TimeLeft / 60 ) % 60 ) ,
( TimeLeft % 60 ) ) ;
}
}
}
if (( CurrentTime < TimeoutTime )
&& ( CurrentTime > ( TimeoutTime - 30 )))
{
fprintf ( stderr , " Timeout in %lds" , ( TimeoutTime - CurrentTime ) ) ;
}
}
|