// File: /workspace/github_c_makefile_star2_O3/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 0x9c76
movq %rax, -0x40(%rbp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8580
callq 0x89b2
movq %rax, %r13
movq 0x8(%rbx), %rdi
callq 0x9a3c
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0x9a70
movq %rax, %r15
movq 0x8(%rbx), %rdi
callq 0x9aa4
movq %rax, %r14
testq %r12, %r12
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
testq %r14, %r14
sete %al
orb %cl, %al
je 0x8b4f
leaq 0x6e4fb(%rip), %rdi     # 0x7703e
callq 0x84b0
testq %r12, %r12
jne 0x8b9f
jmp 0x8ba7
leaq 0x6e504(%rip), %rsi     # 0x7705a
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 0x8a1e
testq %rax, %rax
je 0x8b93
movq %rax, %r13
callq 0x9594
cmpl $-0x1, %eax
jne 0x8bdb
leaq 0x6e4f1(%rip), %rdi     # 0x77082
jmp 0x8b9a
leaq 0x6e4de(%rip), %rdi     # 0x77078
callq 0x84b0
movq %r12, %rdi
callq 0x9ad8
testq %r15, %r15
je 0x8bb4
movq %r15, %rdi
callq 0x9ad8
testq %r14, %r14
je 0x8bc1
movq %r14, %rdi
callq 0x9ad8
movq %rbx, %rdi
callq 0x9ad8
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 0x9c64
movl -0x2c(%rbp), %edi
movq %r13, %rsi
movl %eax, %edx
callq 0x962e
cmpl $-0x1, %eax
jne 0x8c0a
leaq 0x6e496(%rip), %rdi     # 0x77096
callq 0x8250
jmp 0x8d34
leaq -0x450(%rbp), %rdi
callq 0x8120
movl -0x2c(%rbp), %r13d
movl %r13d, %edi
leaq -0x450(%rbp), %rsi
movl %eax, %edx
callq 0x9642
movl $0x10, %edi
callq 0x8300
movq %rax, -0x38(%rbp)
movq %rax, %rdi
movl %r13d, %esi
callq 0x8da4
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x9008
movq %rbx, %rdi
movl %eax, %esi
callq 0x8580
callq 0x92a8
movq %rax, -0x48(%rbp)
movq %r13, %rdi
callq 0x9008
addl $0xffffff38, %eax       # imm = 0xFFFFFF38
cmpl $0x63, %eax
movq -0x40(%rbp), %rdi
ja 0x8d1f
movl 0x10(%rbx), %esi
movq %rdi, %r13
callq 0x9c38
testq %rax, %rax
je 0x8cdc
movq %r13, %rdi
callq 0x9c46
testb %al, %al
je 0x8cb4
movl 0x14(%rbx), %r13d
movq -0x40(%rbp), %rdi
callq 0x9c50
cmpl %eax, %r13d
movq -0x40(%rbp), %rdi
jge 0x8cdc
movl 0x10(%rbx), %esi
jmp 0x8cba
movl 0x10(%rbx), %esi
movq %r13, %rdi
callq 0x9c38
movq %rax, %r13
movq -0x38(%rbp), %rdi
callq 0x901a
movl 0x14(%rbx), %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x90ac
movq -0x48(%rbp), %r13
movq %r13, %rdi
callq 0x937e
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x8d0f
movq -0x38(%rbp), %r13
movq %r13, %rdi
movq %rax, -0x40(%rbp)
callq 0x901a
movl 0x10(%rbx), %edx
movq %r12, %rdi
movq %rax, %rsi
callq *-0x40(%rbp)
jmp 0x8d1f
leaq 0x6e388(%rip), %rdi     # 0x7709e
callq 0x84b0
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x8fd2
movl $0x10, %esi
movq %r13, %rdi
callq 0x8310
movl -0x2c(%rbp), %edi
callq 0x95ce
jmp 0x8b9f
movq %rax, %rbx
movl $0x10, %esi
movq -0x38(%rbp), %rdi
callq 0x8310
movq %rbx, %rdi
callq 0x8520
nop