// File: /workspace/github_c_makefile_star2_O0/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
subq $0x590, %rsp            # imm = 0x590
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x410(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x8160
movq $0x0, -0x420(%rbp)
movq $0x0, -0x428(%rbp)
movq $0x0, -0x430(%rbp)
movq $0x0, -0x438(%rbp)
movl $0x0, -0x43c(%rbp)
leaq -0x4c0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x8160
movq $0x0, -0x4c8(%rbp)
movq $0x0, -0x4d0(%rbp)
callq 0xaed0
movq %rax, -0x4d8(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x9170
callq 0x89e0
movq %rax, -0x4e0(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x8c9b
jmp 0x9156
movq -0x10(%rbp), %rdi
callq 0x91f0
movq %rax, %rdi
callq 0xab50
movq %rax, -0x428(%rbp)
movq -0x10(%rbp), %rdi
callq 0x91f0
movq %rax, %rdi
callq 0xaba0
movq %rax, -0x430(%rbp)
movq -0x10(%rbp), %rdi
callq 0x91f0
movq %rax, %rdi
callq 0xabf0
movq %rax, -0x438(%rbp)
cmpq $0x0, -0x428(%rbp)
je 0x8d01
cmpq $0x0, -0x430(%rbp)
je 0x8d01
cmpq $0x0, -0x438(%rbp)
jne 0x8d12
leaq 0x99336(%rip), %rdi     # 0xa203e
callq 0x8470
jmp 0x9102
leaq -0x410(%rbp), %rdi
movq -0x438(%rbp), %rdx
movq -0x428(%rbp), %rcx
leaq 0x9932c(%rip), %rsi     # 0xa205a
movb $0x0, %al
callq 0x8080
movq -0x4e0(%rbp), %rdi
movq -0x428(%rbp), %rsi
movq -0x430(%rbp), %rdx
callq 0x8a70
movq %rax, -0x420(%rbp)
cmpq $0x0, -0x420(%rbp)
jne 0x8d71
leaq 0x99311(%rip), %rdi     # 0xa2078
callq 0x8470
jmp 0x9102
callq 0xa170
movl %eax, -0x414(%rbp)
cmpl $-0x1, -0x414(%rbp)
jne 0x8d96
leaq 0x992f6(%rip), %rdi     # 0xa2082
callq 0x8470
jmp 0x9102
movl -0x414(%rbp), %eax
movl %eax, -0x4fc(%rbp)
movq -0x420(%rbp), %rax
movq %rax, -0x4f8(%rbp)
movq -0x4d8(%rbp), %rdi
callq 0xaea0
movl -0x4fc(%rbp), %edi
movq -0x4f8(%rbp), %rsi
movl %eax, %edx
callq 0xa2a0
movl %eax, -0x43c(%rbp)
cmpl $-0x1, -0x43c(%rbp)
jne 0x8df0
leaq 0x992b0(%rip), %rdi     # 0xa2096
callq 0x8240
jmp 0x90f7
movl -0x414(%rbp), %eax
movl %eax, -0x514(%rbp)
leaq -0x410(%rbp), %rdi
movq %rdi, -0x510(%rbp)
callq 0x8120
movl -0x514(%rbp), %edi
movq -0x510(%rbp), %rsi
movl %eax, %edx
callq 0xa300
movl $0x10, %edi
callq 0x82d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x508(%rbp)
movl -0x414(%rbp), %esi
callq 0x92f0
jmp 0x8e47
movq -0x508(%rbp), %rax
movq %rax, -0x4d0(%rbp)
cmpq $0x0, -0x4d0(%rbp)
jne 0x8e98
leaq 0x99238(%rip), %rdi     # 0xa209e
callq 0x8470
jmp 0x90f7
movq -0x508(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4e8(%rbp)
movl %eax, -0x4ec(%rbp)
movl $0x10, %esi
callq 0x82e0
jmp 0x915f
movq -0x10(%rbp), %rax
movq %rax, -0x520(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x96a0
movq -0x520(%rbp), %rdi
movl %eax, %esi
callq 0x9170
callq 0x9cd0
movq %rax, -0x4c8(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x96a0
movl $0x64, %ecx
cltd
idivl %ecx
cmpl $0x2, %eax
jne 0x90b8
movq -0x4d8(%rbp), %rax
movq %rax, -0x528(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9210
movq -0x528(%rbp), %rdi
movl %eax, %esi
callq 0xae20
cmpq $0x0, %rax
je 0x9039
movq -0x4d8(%rbp), %rdi
callq 0xae40
testb $0x1, %al
jne 0x8f2a
jmp 0x8fc5
movq -0x10(%rbp), %rdi
callq 0x9230
movl %eax, -0x52c(%rbp)
movq -0x4d8(%rbp), %rdi
callq 0xae60
movl %eax, %ecx
movl -0x52c(%rbp), %eax
cmpl %ecx, %eax
jge 0x8fc3
movq -0x4d8(%rbp), %rax
movq %rax, -0x550(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9210
movq -0x550(%rbp), %rdi
movl %eax, %esi
callq 0xae20
movq %rax, -0x548(%rbp)
movq -0x428(%rbp), %rax
movq %rax, -0x540(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x96d0
movq %rax, -0x538(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9230
movq -0x548(%rbp), %rdi
movq -0x540(%rbp), %rsi
movq -0x538(%rbp), %rdx
movl %eax, %ecx
callq 0x9850
jmp 0x9037
movq -0x4d8(%rbp), %rax
movq %rax, -0x570(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9210
movq -0x570(%rbp), %rdi
movl %eax, %esi
callq 0xae20
movq %rax, -0x568(%rbp)
movq -0x428(%rbp), %rax
movq %rax, -0x560(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x96d0
movq %rax, -0x558(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9230
movq -0x568(%rbp), %rdi
movq -0x560(%rbp), %rsi
movq -0x558(%rbp), %rdx
movl %eax, %ecx
callq 0x9850
jmp 0x9039
movq -0x4c8(%rbp), %rdi
callq 0x9e60
movq -0x4c8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x90aa
movq -0x4c8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x578(%rbp)
movq -0x428(%rbp), %rax
movq %rax, -0x588(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x96d0
movq %rax, -0x580(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9210
movq -0x588(%rbp), %rdi
movq -0x580(%rbp), %rsi
movl %eax, %edx
movq -0x578(%rbp), %rax
callq *%rax
jmp 0x90b6
leaq 0x98fff(%rip), %rdi     # 0xa20b0
callq 0x8470
jmp 0x90b8
cmpq $0x0, -0x4d0(%rbp)
je 0x90f5
movq -0x4d0(%rbp), %rax
movq %rax, -0x590(%rbp)
cmpq $0x0, %rax
je 0x90f3
movq -0x590(%rbp), %rdi
callq 0x9650
movq -0x590(%rbp), %rdi
movl $0x10, %esi
callq 0x82e0
jmp 0x90f5
jmp 0x90f7
movl -0x414(%rbp), %edi
callq 0xa1b0
cmpq $0x0, -0x428(%rbp)
je 0x9118
movq -0x428(%rbp), %rdi
callq 0xac40
cmpq $0x0, -0x430(%rbp)
je 0x912e
movq -0x430(%rbp), %rdi
callq 0xac40
cmpq $0x0, -0x438(%rbp)
je 0x9144
movq -0x438(%rbp), %rdi
callq 0xac40
cmpq $0x0, -0x10(%rbp)
je 0x9154
movq -0x10(%rbp), %rdi
callq 0xac40
jmp 0x9156
addq $0x590, %rsp            # imm = 0x590
popq %rbp
retq
movq -0x4e8(%rbp), %rdi
callq 0x84e0
nopl (%rax,%rax)