// File: /workspace/github_c_makefile_star2_O1/shifengbin[P]bishe/Download/Download.cpp
// Function: download
void download( void * args)
{
	
	URL *tURL = (URL*)args;
	char header[1024] = { 0 };
	int sock;
	struct sockaddr_in *saddr = NULL;
	char *host = NULL;
	char *port = NULL;
	char *res = NULL;
	int tRet = 0;
	int ffd;
	char filename[128] = { 0 };
	Model *model = NULL;	
	HTTP *respond = NULL;
	ConfigParser *config = ConfigParser::getConfigParser();

	tURL->setStateToDB(1);

	DNSCache *dnsch = DNSCache::getDNSCache();
	if ( tURL == NULL )
	{
		return;
	}
	
/*	if ( config->getMode(tURL->getType()) )
	{
		puts(config->getMode(tURL->getType())->getMode() );
	}
*/
	host = URLParser::getHost( tURL->getURLStr() );
	port = URLParser::getPort( tURL->getURLStr() );
	res =  URLParser::getRes( tURL->getURLStr() );
	if ( host == NULL || port == NULL || res == NULL )
	{
		puts("host or port or res is NULL");
		goto ret;
	}
	//puts(header);
//	printf("%s:%d\n",host,tURL->getDeep());
	sprintf( header,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",res,host);
	saddr = dnsch->DNS( host, port );

	if (saddr == NULL )
	{
		puts("DNS ERROR");
		goto ret;
	}
//	puts(inet_ntoa(saddr->sin_addr));	
	sock = SockTool::Socket();
	if ( sock == -1 )
	{
		puts("create socket error");
		goto ret;
	}
		
	tRet = SockTool::connectTimeout( sock , saddr , config->getTimeout());
	if ( tRet == -1 )
	{
		//puts("timeout");
		perror("Connect");
		goto retc;
	}

	SockTool::writeN( sock , header , strlen( header) );
	respond = new HTTP(sock);
	if ( respond == NULL )
	{
		puts("get respond error");
		goto retc;
	}
//	puts(respond->getBody());
	tURL->setStateToDB(respond->getState());
//	printf("State:%d\n",respond->getState());

	model = Model::getModel();

	if ( respond->getState() / 100 == 2 )
	{
/*		sprintf(filename,"file%d.html",aaaa);
		aaaa++;
		//puts(filename);
		ffd = open( filename, O_CREAT|O_WRONLY,0666);
		write( ffd , respond->getBody(),strlen(respond->getBody()));
		close(ffd);
*/

		if ( config->getMode(tURL->getType())  )
	        {
			if( config->getMethod()  )
			{
//				puts("Method=1");
				if ( tURL->getDeep() < config->getDeepth() )
				{
               				GetURLFromBody( config->getMode(tURL->getType()),host,respond->getBody(),tURL->getDeep());
				}
			}
			else
			{
//				puts("Method=0");
				GetURLFromBody( config->getMode(tURL->getType()),host,respond->getBody(),tURL->getDeep());
			}
       		}
		//puts(respond->getBody());
		
		model->init();
		if ( model->output != NULL )
		{
	//		puts("Output`````````````````");
			model->output(host,respond->getBody(),tURL->getType());
		}
		else
		{
			puts("Output is NULL");
		}
	}
	if ( respond != NULL )
	{
		delete respond;
	}
	
retc:
	SockTool::Close(sock);
ret:
	if ( host != NULL )
	{
		URLParser::deleteMemery((void*)host);
	}
	if ( port != NULL )
	{
		URLParser::deleteMemery((void*) port );
	}
	if ( res != NULL )
	{
		URLParser::deleteMemery((void*) res );
	}
	if ( tURL != NULL )
	{
		URLParser::deleteMemery((void*) tURL );
	}
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rdi, %rbx
leaq -0x450(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x8160
callq 0x9c6a
movq %rax, -0x40(%rbp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8570
callq 0x89a2
movq %rax, %r13
movq 0x8(%rbx), %rdi
callq 0x9a30
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0x9a64
movq %rax, %r15
movq 0x8(%rbx), %rdi
callq 0x9a98
movq %rax, %r14
testq %r12, %r12
setne %al
testq %r15, %r15
setne %cl
andb %al, %cl
testq %r14, %r14
setne %al
testb %al, %cl
jne 0x8b35
leaq 0x7050b(%rip), %rdi     # 0x7903e
jmp 0x8b80
leaq 0x7051e(%rip), %rsi     # 0x7905a
leaq -0x450(%rbp), %rdi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x8080
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x8a0c
testq %rax, %rax
je 0x8b79
movq %rax, %r13
callq 0x9580
cmpl $-0x1, %eax
jne 0x8bc6
leaq 0x7050b(%rip), %rdi     # 0x79082
jmp 0x8b80
leaq 0x704f8(%rip), %rdi     # 0x79078
callq 0x84a0
testq %r12, %r12
je 0x8b92
movq %r12, %rdi
callq 0x9acc
testq %r15, %r15
je 0x8b9f
movq %r15, %rdi
callq 0x9acc
testq %r14, %r14
je 0x8bac
movq %r14, %rdi
callq 0x9acc
movq %rbx, %rdi
callq 0x9acc
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
movl %eax, -0x2c(%rbp)
callq 0x9c58
movl -0x2c(%rbp), %edi
movq %r13, %rsi
movl %eax, %edx
callq 0x961a
cmpl $-0x1, %eax
jne 0x8bf5
leaq 0x704ab(%rip), %rdi     # 0x79096
callq 0x8240
jmp 0x8d1f
leaq -0x450(%rbp), %rdi
callq 0x8120
movl -0x2c(%rbp), %r13d
movl %r13d, %edi
leaq -0x450(%rbp), %rsi
movl %eax, %edx
callq 0x962e
movl $0x10, %edi
callq 0x82f0
movq %rax, -0x38(%rbp)
movq %rax, %rdi
movl %r13d, %esi
callq 0x8d90
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x9000
movq %rbx, %rdi
movl %eax, %esi
callq 0x8570
callq 0x9290
movq %rax, -0x48(%rbp)
movq %r13, %rdi
callq 0x9000
addl $0xffffff38, %eax       # imm = 0xFFFFFF38
cmpl $0x63, %eax
movq -0x40(%rbp), %rdi
ja 0x8d0a
movl 0x10(%rbx), %esi
movq %rdi, %r13
callq 0x9c2c
testq %rax, %rax
je 0x8cc7
movq %r13, %rdi
callq 0x9c3a
testb %al, %al
je 0x8c9f
movl 0x14(%rbx), %r13d
movq -0x40(%rbp), %rdi
callq 0x9c44
cmpl %eax, %r13d
movq -0x40(%rbp), %rdi
jge 0x8cc7
movl 0x10(%rbx), %esi
jmp 0x8ca5
movl 0x10(%rbx), %esi
movq %r13, %rdi
callq 0x9c2c
movq %rax, %r13
movq -0x38(%rbp), %rdi
callq 0x9012
movl 0x14(%rbx), %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x90a8
movq -0x48(%rbp), %r13
movq %r13, %rdi
callq 0x9368
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x8cfa
movq -0x38(%rbp), %r13
movq %r13, %rdi
movq %rax, -0x40(%rbp)
callq 0x9012
movl 0x10(%rbx), %edx
movq %r12, %rdi
movq %rax, %rsi
callq *-0x40(%rbp)
jmp 0x8d0a
leaq 0x7039d(%rip), %rdi     # 0x7909e
callq 0x84a0
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x8fca
movl $0x10, %esi
movq %r13, %rdi
callq 0x8300
movl -0x2c(%rbp), %edi
callq 0x95ba
jmp 0x8b85
movq %rax, %rbx
movl $0x10, %esi
movq -0x38(%rbp), %rdi
callq 0x8300
movq %rbx, %rdi
callq 0x8510
nopl (%rax)