// File: /workspace/github_c_makefile_star2_O2/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 0x9bca
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x8c66
callq 0x88b6
movq %rax, %r13
movq 0x8(%rbx), %rdi
callq 0x99d8
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0x9a0c
movq %rax, %r15
movq 0x8(%rbx), %rdi
callq 0x9a40
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 0x8a58
leaq 0x5f5f9(%rip), %rdi     # 0x6803e
callq 0x84b0
testq %r12, %r12
jne 0x8c01
jmp 0x8c09
leaq 0x5f5fb(%rip), %rsi     # 0x6805a
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 0x8922
testq %rax, %rax
je 0x8bbf
movq %rax, %r13
callq 0x94ec
cmpl $-0x1, %eax
je 0x8bc8
movq %r14, -0x48(%rbp)
movl %eax, %r14d
movq -0x30(%rbp), %rdi
callq 0x9bb8
movl %r14d, %edi
movq %r13, %rsi
movl %eax, %edx
callq 0x9580
cmpl $-0x1, %eax
movl %r14d, -0x40(%rbp)
je 0x8c3d
leaq -0x450(%rbp), %r13
movq %r13, %rdi
callq 0x8120
movl %r14d, %edi
movq %r13, %rsi
movl %eax, %edx
callq 0x9594
pushq $0x10
popq %rdi
callq 0x8300
movq %rax, %r13
movq %rax, %rdi
movl %r14d, %esi
callq 0x8d02
movq %r13, %rdi
callq 0x8f1a
movq %rbx, %rdi
movl %eax, %esi
callq 0x8c66
callq 0x9204
movq %rax, -0x38(%rbp)
movq %r13, %rdi
callq 0x8f1a
addl $0xffffff38, %eax       # imm = 0xFFFFFF38
cmpl $0x63, %eax
movq -0x48(%rbp), %r14
ja 0x8be6
movl 0x10(%rbx), %esi
movq -0x30(%rbp), %rdi
callq 0x9b8c
testq %rax, %rax
je 0x8b88
movq -0x30(%rbp), %rdi
callq 0x9b9a
testb %al, %al
je 0x8b5e
movl 0x14(%rbx), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
callq 0x9ba4
cmpl %eax, -0x3c(%rbp)
jge 0x8b88
movl 0x10(%rbx), %esi
movq -0x30(%rbp), %rdi
callq 0x9b8c
movq %rax, -0x30(%rbp)
movq %r13, %rdi
callq 0x8f2c
movl 0x14(%rbx), %ecx
movq -0x30(%rbp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8fd4
movq %r13, -0x30(%rbp)
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x92d6
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x8bd6
movq -0x30(%rbp), %r13
movq %r13, %rdi
movq %rax, -0x38(%rbp)
callq 0x8f2c
movl 0x10(%rbx), %edx
movq %r12, %rdi
movq %rax, %rsi
callq *-0x38(%rbp)
jmp 0x8be6
leaq 0x5f4b2(%rip), %rdi     # 0x68078
jmp 0x8bcf
leaq 0x5f4b3(%rip), %rdi     # 0x68082
callq 0x84b0
jmp 0x8c01
leaq 0x5f4c1(%rip), %rdi     # 0x6809e
callq 0x84b0
movq -0x30(%rbp), %r13
movq %r13, %rdi
callq 0x8ee4
pushq $0x10
popq %rsi
movq %r13, %rdi
callq 0x8310
movl -0x40(%rbp), %edi
callq 0x9522
movq %r12, %rdi
callq 0x9a74
testq %r15, %r15
je 0x8c16
movq %r15, %rdi
callq 0x9a74
testq %r14, %r14
je 0x8c23
movq %r14, %rdi
callq 0x9a74
movq %rbx, %rdi
callq 0x9a74
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5f452(%rip), %rdi     # 0x68096
callq 0x8250
movq -0x48(%rbp), %r14
jmp 0x8bf9
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r13, %rdi
callq 0x8310
movq %rbx, %rdi
callq 0x8520
nop