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
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "globalincs/pstypes.h"
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#define WSAGetLastError() (errno)
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "inetfile/inetgetfile.h"
#include "inetfile/chttpget.h"
#define NW_AGHBN_CANCEL 1
#define NW_AGHBN_LOOKUP 2
#define NW_AGHBN_READ 3
#ifdef WIN32
void __cdecl http_gethostbynameworker(void *parm);
#else
int http_gethostbynameworker(void *parm);
#endif
int http_Asyncgethostbyname(unsigned int *ip,int command, char *hostname);
#ifdef WIN32
void HTTPObjThread( void *obj )
#else
int HTTPObjThread( void *obj )
#endif
{
((ChttpGet *)obj)->WorkerThread();
((ChttpGet *)obj)->m_Aborted = true;
//OutputDebugString("http transfer exiting....\n");
#ifdef SCP_UNIX
return 0;
#endif
}
void ChttpGet::AbortGet()
{
#ifdef WIN32
OutputDebugString("Aborting....\n");
#endif
m_Aborting = true;
while(!m_Aborted) Sleep(50); //Wait for the thread to end
#ifdef WIN32
OutputDebugString("Aborted....\n");
#endif
}
ChttpGet::ChttpGet(char *URL,char *localfile,char *proxyip,unsigned short proxyport)
{
m_ProxyEnabled = true;
m_ProxyIP = proxyip;
m_ProxyPort = proxyport;
GetFile(URL,localfile);
}
ChttpGet::ChttpGet(char *URL,char *localfile)
{
m_ProxyEnabled = false;
GetFile(URL,localfile);
}
void ChttpGet::GetFile(char *URL,char *localfile)
{
m_DataSock = INVALID_SOCKET;
m_iBytesIn = 0;
m_iBytesTotal = 0;
m_State = HTTP_STATE_STARTUP;
m_Aborting = false;
m_Aborted = false;
#ifdef SCP_UNIX
thread_id = NULL;
#endif
strncpy(m_URL,URL,MAX_URL_LEN-1);
m_URL[MAX_URL_LEN-1] = 0;
LOCALFILE = fopen(localfile,"wb");
if(NULL == LOCALFILE)
{
m_State = HTTP_STATE_CANT_WRITE_FILE;
return;
}
m_DataSock = socket(AF_INET, SOCK_STREAM, 0);
if(INVALID_SOCKET == m_DataSock)
{
m_State = HTTP_STATE_SOCKET_ERROR;
return;
}
// uint arg = 1;
// ioctlsocket( m_DataSock, FIONBIO, &arg );
char *pURL = URL;
if(strnicmp(URL,"http:",5)==0)
{
pURL +=5;
while(*pURL == '/')
{
pURL++;
}
}
//There shouldn't be any : in this string
if(strchr(pURL,':'))
{
m_State = HTTP_STATE_URL_PARSING_ERROR;
return;
}
//read the filename by searching backwards for a /
//then keep reading until you find the first /
//when you found it, you have the host and dir
char *filestart = NULL;
char *dirstart = NULL;
for(int i = strlen(pURL);i>=0;i--)
{
if(pURL[i]== '/')
{
if(!filestart)
{
filestart = pURL+i+1;
dirstart = pURL+i+1;
strcpy_s(m_szFilename,filestart);
}
else
{
dirstart = pURL+i+1;
}
}
}
if((dirstart==NULL) || (filestart==NULL))
{
m_State = HTTP_STATE_URL_PARSING_ERROR;
return;
}
else
{
strcpy_s(m_szDir,dirstart);//,(filestart-dirstart));
//m_szDir[(filestart-dirstart)] = NULL;
strncpy(m_szHost,pURL,(dirstart-pURL));
m_szHost[(dirstart-pURL)-1] = '\0';
}
#ifdef WIN32
if ( _beginthread(HTTPObjThread,0,this) == NULL )
#else
if ( (thread_id = SDL_CreateThread(HTTPObjThread, this)) == NULL )
#endif
{
m_State = HTTP_STATE_INTERNAL_ERROR;
return;
}
}
ChttpGet::~ChttpGet()
{
#ifdef WIN32
_endthread();
#else
if (thread_id)
SDL_WaitThread(thread_id, NULL);
#endif
fclose(LOCALFILE);
if (m_DataSock != INVALID_SOCKET) {
shutdown(m_DataSock, 2);
closesocket(m_DataSock);
}
}
int ChttpGet::GetStatus()
{
return m_State;
}
unsigned int ChttpGet::GetBytesIn()
{
return m_iBytesIn;
}
unsigned int ChttpGet::GetTotalBytes()
{
return m_iBytesTotal;
}
void ChttpGet::WorkerThread()
{
char szCommand[1000];
char *p;
int irsp = 0;
ConnectSocket();
if(m_Aborting)
{
fclose(LOCALFILE);
return;
}
if(m_State != HTTP_STATE_CONNECTED)
{
fclose(LOCALFILE);
return;
}
sprintf(szCommand,"GET %s%s HTTP/1.1\nAccept: */*\nAccept-Encoding: deflate\nHost: %s\n\n\n",m_ProxyEnabled?"":"/",m_ProxyEnabled?m_URL:m_szDir,m_szHost);
send(m_DataSock,szCommand,strlen(szCommand),0);
p = GetHTTPLine();
if (!p) return;
if(strnicmp("HTTP/",p,5)==0)
{
char *pcode;
pcode = strchr(p,' ')+1;
if(!pcode)
{
m_State = HTTP_STATE_UNKNOWN_ERROR;
fclose(LOCALFILE);
return;
}
pcode[3] = '\0';
irsp = atoi(pcode);
if(irsp == 0)
{
m_State = HTTP_STATE_UNKNOWN_ERROR;
fclose(LOCALFILE);
return;
}
if(irsp==200)
{
int idataready=0;
do
{
p = GetHTTPLine();
if(p==NULL)
{
m_State = HTTP_STATE_UNKNOWN_ERROR;
fclose(LOCALFILE);
return;
}
if(*p=='\0')
{
idataready = 1;
break;
}
if(strnicmp(p,"Content-Length:",strlen("Content-Length:"))==0)
{
char *s = strchr(p,' ')+1;
p = s;
if(s)
{
while(*s)
{
if(!isdigit(*s))
{
*s='\0';
}
s++;
};
m_iBytesTotal = atoi(p);
}
}
Sleep(1);
}while(!idataready);
ReadDataChannel();
return;
}
m_State = HTTP_STATE_FILE_NOT_FOUND;
fclose(LOCALFILE);
return;
}
else
{
m_State = HTTP_STATE_UNKNOWN_ERROR;
fclose(LOCALFILE);
return;
}
}
int ChttpGet::ConnectSocket()
{
//HOSTENT *he;
unsigned int ip;
SERVENT *se;
SOCKADDR_IN hostaddr;
if(m_Aborting){
return 0;
}
ip = inet_addr((const char *)m_szHost);
int rcode = 0;
if(ip==INADDR_NONE)
{
http_Asyncgethostbyname(&ip,NW_AGHBN_LOOKUP,m_szHost);
do
{
if(m_Aborting)
{
http_Asyncgethostbyname(&ip,NW_AGHBN_CANCEL,m_szHost);
return 0;
}
rcode = http_Asyncgethostbyname(&ip,NW_AGHBN_READ,m_szHost);
Sleep(1);
}while(rcode==0);
}
if(rcode == -1)
{
m_State = HTTP_STATE_HOST_NOT_FOUND;
return 0;
}
//m_ControlSock
if(m_Aborting)
return 0;
se = getservbyname("http", NULL);
if(m_Aborting)
return 0;
if(se == NULL)
{
hostaddr.sin_port = htons(80);
}
else
{
hostaddr.sin_port = se->s_port;
}
hostaddr.sin_family = AF_INET;
//ip = htonl(ip);
memcpy(&hostaddr.sin_addr, &ip, 4);
if(m_ProxyEnabled)
{
//This is on a proxy, so we need to make sure to connect to the proxy machine
ip = inet_addr((const char *)m_ProxyIP);
if(ip==INADDR_NONE)
{
http_Asyncgethostbyname(&ip,NW_AGHBN_LOOKUP,m_ProxyIP);
rcode = 0;
do
{
if(m_Aborting)
{
http_Asyncgethostbyname(&ip,NW_AGHBN_CANCEL,m_ProxyIP);
return 0;
}
rcode = http_Asyncgethostbyname(&ip,NW_AGHBN_READ,m_ProxyIP);
Sleep(1);
}while(rcode==0);
if(rcode == -1)
{
m_State = HTTP_STATE_HOST_NOT_FOUND;
return 0;
}
}
//Use either the proxy port or 80 if none specified
hostaddr.sin_port = htons((ushort)(m_ProxyPort ? m_ProxyPort : 80));
//Copy the proxy address...
memcpy(&hostaddr.sin_addr,&ip,4);
}
memset(&hostaddr.sin_zero, 0, sizeof(hostaddr.sin_zero));
//Now we will connect to the host
fd_set wfds;
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int serr = connect(m_DataSock, (SOCKADDR *)&hostaddr, sizeof(SOCKADDR));
int cerr = WSAGetLastError();
//printf("cerr: %s\n", strerror(cerr));
if (serr) {
while ( (cerr == WSAEALREADY) || (cerr == WSAEINVAL) || (cerr == WSAEWOULDBLOCK) ) {
FD_ZERO(&wfds);
FD_SET( m_DataSock, &wfds );
#ifdef WIN32
if ( select(0, NULL, &wfds, NULL, &timeout) )
#else
if ( select(m_DataSock+1, NULL, &wfds, NULL, &timeout) )
#endif
{
serr = 0;
break;
}
if (m_Aborting)
return 0;
serr = connect(m_DataSock, (SOCKADDR *)&hostaddr, sizeof(SOCKADDR));
if (serr == 0)
break;
cerr = WSAGetLastError();
if (cerr == WSAEISCONN) {
serr = 0;
break;
}
Sleep(1);
}
}
if (serr) {
//printf("1-serr: %i\n", serr);
m_State = HTTP_STATE_CANT_CONNECT;
return 0;
}
m_State = HTTP_STATE_CONNECTED;
return 1;
}
char *ChttpGet::GetHTTPLine()
{
int iBytesRead;
char chunk[2];
uint igotcrlf = 0;
memset(recv_buffer,0,1000);
do
{
chunk[0]='\0';
bool gotdata = false;
do
{
iBytesRead = recv(m_DataSock,chunk,1,0);
if(SOCKET_ERROR == iBytesRead)
{
int error = WSAGetLastError();
//printf("0 - iBytesRead: %i ==> (%i) %s\n", iBytesRead, error, strerror(error));
if(WSAEWOULDBLOCK==error)
{
gotdata = false;
continue;
}
else
return NULL;
}
else
{
gotdata = true;
}
Sleep(1);
}while(!gotdata);
if(chunk[0]==0x0d)
{
//This should always read a 0x0a
do
{
iBytesRead = recv(m_DataSock,chunk,1,0);
if(SOCKET_ERROR == iBytesRead)
{
int error = WSAGetLastError();
if(WSAEWOULDBLOCK==error)
{
//printf("1 - iBytesRead: %i ==> %s\n", iBytesRead, strerror(error));
gotdata = false;
continue;
}
else
return NULL;
}
else
{
gotdata = true;
}
Sleep(1);
}while(!gotdata);
igotcrlf = 1;
}
else
{ chunk[1] = '\0';
strcat_s(recv_buffer,chunk);
}
Sleep(1);
}while(igotcrlf==0);
return recv_buffer;
}
uint ChttpGet::ReadDataChannel()
{
char sDataBuffer[4096]; // Data-storage buffer for the data channel
int nBytesRecv = 0; // Bytes received from the data channel
fd_set wfds;
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 500;
m_State = HTTP_STATE_RECEIVING;
do {
FD_ZERO(&wfds);
FD_SET( m_DataSock, &wfds );
if ( (m_iBytesTotal) && (m_iBytesIn == m_iBytesTotal) )
break;
#ifdef WIN32
select(0, &wfds, NULL, NULL, &timeout);
#else
select(m_DataSock+1, &wfds, NULL, NULL, &timeout);
#endif
if (m_Aborting) {
fclose(LOCALFILE);
return 0;
}
nBytesRecv = recv(m_DataSock, (char *)&sDataBuffer, sizeof(sDataBuffer), 0);
if (m_Aborting) {
fclose(LOCALFILE);
return 0;
}
if (SOCKET_ERROR == nBytesRecv) {
int error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
nBytesRecv = 1;
continue;
}
}
m_iBytesIn += nBytesRecv;
if (nBytesRecv > 0 ) {
fwrite(sDataBuffer, nBytesRecv, 1, LOCALFILE);
//Write sDataBuffer, nBytesRecv
}
Sleep(1);
} while (nBytesRecv > 0);
fclose(LOCALFILE);
// Close the file and check for error returns.
if (nBytesRecv == SOCKET_ERROR) {
//Ok, we got a socket error -- xfer aborted?
m_State = HTTP_STATE_RECV_FAILED;
return 0;
} else {
//OutputDebugString("HTTP File complete!\n");
//done!
m_State = HTTP_STATE_FILE_RECEIVED;
return 1;
}
}
typedef struct _async_dns_lookup
{
unsigned int ip; //resolved host. Write only to worker thread.
char * host;//host name to resolve. read only to worker thread
bool done; //write only to the worker thread. Signals that the operation is complete
bool error; //write only to worker thread. Thread sets this if the name doesn't resolve
bool abort; //read only to worker thread. If this is set, don't fill in the struct.
}async_dns_lookup;
async_dns_lookup httpaslu;
async_dns_lookup *http_lastaslu = NULL;
#ifdef WIN32
void __cdecl http_gethostbynameworker(void *parm);
#else
int http_gethostbynameworker(void *parm);
#endif
int http_Asyncgethostbyname(unsigned int *ip,int command, char *hostname)
{
if(command==NW_AGHBN_LOOKUP)
{
if(http_lastaslu)
http_lastaslu->abort = true;
async_dns_lookup *newaslu;
newaslu = (async_dns_lookup *)vm_malloc(sizeof(async_dns_lookup));
memset(&newaslu->ip,0,sizeof(unsigned int));
newaslu->host = hostname;
newaslu->done = false;
newaslu->error = false;
newaslu->abort = false;
http_lastaslu = newaslu;
httpaslu.done = false;
#ifdef WIN32
_beginthread(http_gethostbynameworker, 0, newaslu);
#else
HOSTENT *he = gethostbyname(hostname);
if (he == NULL) {
newaslu->error = true;
} else if ( !newaslu->abort ) {
memcpy( &newaslu->ip, he->h_addr_list[0], sizeof(uint) );
newaslu->done = true;
memcpy( &httpaslu,newaslu, sizeof(async_dns_lookup) );
}
// struct hostent *he;
// he = gethostbyname(hostname);
// newaslu->ip = (uint)((in_addr *)(he->h_addr))->s_addr;
// newaslu->done = true;
// httpaslu.done = true;
// thread_id
#endif
return 1;
}
else if(command==NW_AGHBN_CANCEL)
{
if(http_lastaslu)
http_lastaslu->abort = true;
http_lastaslu = NULL;
}
else if(command==NW_AGHBN_READ)
{
if(!http_lastaslu)
return -1;
if(httpaslu.done)
{
//vm_free(http_lastaslu);
http_lastaslu = NULL;
memcpy(ip,&httpaslu.ip,sizeof(unsigned int));
return 1;
}
else if(httpaslu.error)
{
vm_free(http_lastaslu);
http_lastaslu = NULL;
return -1;
}
else return 0;
}
return -2;
}
// This is the worker thread which does the lookup.
#ifdef WIN32
void __cdecl http_gethostbynameworker(void *parm)
#else
int http_gethostbynameworker(void *parm)
#endif
{
#ifdef SCP_UNIX
// df_pthread_detach(df_pthread_self());
#endif
async_dns_lookup *lookup = (async_dns_lookup *)parm;
HOSTENT *he = gethostbyname(lookup->host);
if(he==NULL)
{
lookup->error = true;
#ifdef WIN32
return;
#else
return 0;
#endif
}
else if(!lookup->abort)
{
memcpy(&lookup->ip,he->h_addr_list[0],sizeof(unsigned int));
lookup->done = true;
memcpy(&httpaslu,lookup,sizeof(async_dns_lookup));
}
vm_free(lookup);
#ifdef SCP_UNIX
return 0;
#endif
}
|