// File: /workspace/github_c_makefile_star2_O1/vincenthsu[P]nweb/nweb23.c
// Function: web
void web(int fd, int hit)
{
	int j, file_fd, buflen;
	long i, ret, len;
	char * fstr;
	static char buffer[BUFSIZE+1]; /* static so zero filled */

	ret =read(fd,buffer,BUFSIZE); 	/* read Web request in one go */
	if(ret == 0 || ret == -1) {	/* read failure stop now */
		logger(FORBIDDEN,"failed to read browser request","",fd);
	}
	if(ret > 0 && ret < BUFSIZE)	/* return code is valid chars */
		buffer[ret]=0;		/* terminate the buffer */
	else buffer[0]=0;
	for(i=0;i<ret;i++)	/* remove CF and LF characters */
		if(buffer[i] == '\r' || buffer[i] == '\n')
			buffer[i]='*';
	logger(LOG,"request",buffer,hit);
	if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) ) {
		logger(FORBIDDEN,"Only simple GET operation supported",buffer,fd);
	}
	for(i=4;i<BUFSIZE;i++) { /* null terminate after the second space to ignore extra stuff */
		if(buffer[i] == ' ') { /* string is "GET URL " +lots of other stuff */
			buffer[i] = 0;
			break;
		}
	}
	for(j=0;j<i-1;j++) 	/* check for illegal parent directory use .. */
		if(buffer[j] == '.' && buffer[j+1] == '.') {
			logger(FORBIDDEN,"Parent directory (..) path names not supported",buffer,fd);
		}
	if( !strncmp(&buffer[0],"GET /\0",6) || !strncmp(&buffer[0],"get /\0",6) ) /* convert no filename to index file */
		(void)strcpy(buffer,"GET /index.html");

	/* work out the file type and check we support it */
	buflen=strlen(buffer);
	fstr = (char *)0;
	for(i=0;extensions[i].ext != 0;i++) {
		len = strlen(extensions[i].ext);
		if( !strncmp(&buffer[buflen-len], extensions[i].ext, len)) {
			fstr =extensions[i].filetype;
			break;
		}
	}
	if(fstr == 0) logger(FORBIDDEN,"file extension type not supported",buffer,fd);

	if(( file_fd = open(&buffer[5],O_RDONLY)) == -1) {  /* open the file for reading */
		logger(NOTFOUND, "failed to open file",&buffer[5],fd);
	}
	logger(LOG,"SEND",&buffer[5],hit);
	len = (long)lseek(file_fd, (off_t)0, SEEK_END); /* lseek to the file end to find the length */
	      (void)lseek(file_fd, (off_t)0, SEEK_SET); /* lseek back to the file start ready for reading */
          (void)sprintf(buffer,"HTTP/1.1 200 OK\nServer: nweb/%d.0\nContent-Length: %ld\nConnection: close\nContent-Type: %s\n\n", VERSION, len, fstr); /* Header + a blank line */
	logger(LOG,"Header",buffer,hit);
	(void)write(fd,buffer,strlen(buffer));

	/* send file in 8KB block - last block may be smaller */
	while (	(ret = read(file_fd, buffer, BUFSIZE)) > 0 ) {
		(void)write(fd,buffer,ret);
	}
	sleep(1);	/* allow socket to drain before signalling the socket is closed */
	close(fd);
	exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movl %edi, %ebx
leaq 0x2da9(%rip), %r14      # 0x41b0
movl $0x1fa0, %edx           # imm = 0x1FA0
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
incq %rax
cmpq $0x1, %rax
ja 0x143a
leaq 0xe8c(%rip), %rsi       # 0x22b3
leaq 0xf9c(%rip), %rdx       # 0x23ca
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
leaq -0x1(%r15), %rax
cmpq $0x1f9e, %rax           # imm = 0x1F9E
ja 0x144d
movb $0x0, (%r15,%r14)
jmp 0x1454
movb $0x0, 0x2d5c(%rip)      # 0x41b0
testq %r15, %r15
jle 0x1477
xorl %eax, %eax
movzbl (%rax,%r14), %ecx
cmpl $0xd, %ecx
je 0x146a
cmpl $0xa, %ecx
jne 0x146f
movb $0x2a, (%rax,%r14)
incq %rax
cmpq %rax, %r15
jne 0x145b
leaq 0xe4c(%rip), %rsi       # 0x22ca
leaq 0x2d2b(%rip), %r14      # 0x41b0
movl $0x2c, %edi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x12ac
movl 0x2d16(%rip), %eax      # 0x41b0
cmpl $0x20544547, %eax       # imm = 0x20544547
je 0x14c2
cmpl $0x20746567, %eax       # imm = 0x20746567
je 0x14c2
leaq 0xe23(%rip), %rsi       # 0x22d2
leaq 0x2cfa(%rip), %rdx      # 0x41b0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
movl $0x4, %r12d
cmpb $0x20, (%r12,%r14)
je 0x14e3
incq %r12
cmpq $0x1fa0, %r12           # imm = 0x1FA0
jne 0x14c8
movl $0x1f9f, %r12d          # imm = 0x1F9F
jmp 0x14eb
movb $0x0, (%r12,%r14)
decq %r12
testq %r12, %r12
jle 0x1524
xorl %r13d, %r13d
leaq 0xdfc(%rip), %r15       # 0x22f6
cmpb $0x2e, (%r13,%r14)
jne 0x151c
cmpb $0x2e, 0x1(%r13,%r14)
jne 0x151c
movl $0x193, %edi            # imm = 0x193
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x12ac
incq %r13
cmpq %r13, %r12
jne 0x14fa
movl $0x20544547, %eax       # imm = 0x20544547
xorl 0x2c81(%rip), %eax      # 0x41b0
movzwl 0x2c7e(%rip), %ecx      # 0x41b4
xorl $0x2f, %ecx
orl %eax, %ecx
je 0x1556
movl $0x20746567, %eax       # imm = 0x20746567
xorl 0x2c68(%rip), %eax      # 0x41b0
movzwl 0x2c65(%rip), %ecx      # 0x41b4
xorl $0x2f, %ecx
orl %eax, %ecx
jne 0x1564
movups 0xdc8(%rip), %xmm0      # 0x2325
movaps %xmm0, 0x2c4c(%rip)     # 0x41b0
movq 0x2b85(%rip), %r15      # 0x40f0
testq %r15, %r15
jne 0x1575
xorl %r15d, %r15d
jmp 0x15e3
movl %ebp, 0x4(%rsp)
movq %r14, %rdi
callq 0x1080
movslq %eax, %r13
movq %r15, %rdi
callq 0x1080
movq %r13, %rdi
subq %rax, %rdi
addq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1040
leaq 0x2b49(%rip), %rbp      # 0x40f0
testl %eax, %eax
je 0x15db
xorl %r15d, %r15d
movq 0x10(%rbp), %r12
testq %r12, %r12
je 0x15df
movq %r12, %rdi
callq 0x1080
movq %r13, %rdi
subq %rax, %rdi
addq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1040
addq $0x10, %rbp
testl %eax, %eax
jne 0x15ae
movq 0x8(%rbp), %r15
movl 0x4(%rsp), %ebp
testq %r15, %r15
jne 0x1602
leaq 0xd46(%rip), %rsi       # 0x2335
leaq 0x2bba(%rip), %rdx      # 0x41b0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
leaq 0x2bac(%rip), %rdi      # 0x41b5
xorl %esi, %esi
xorl %eax, %eax
callq 0x1120
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x1634
leaq 0xd36(%rip), %rsi       # 0x2357
leaq 0x2b8d(%rip), %rdx      # 0x41b5
movl $0x194, %edi            # imm = 0x194
movl %ebx, %ecx
callq 0x12ac
leaq 0xd30(%rip), %rsi       # 0x236b
leaq 0x2b73(%rip), %rdx      # 0x41b5
movl $0x2c, %edi
movl %ebp, %ecx
callq 0x12ac
leaq 0x2b5b(%rip), %r12      # 0x41b0
movl %r14d, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10b0
movq %rax, %r13
movl %r14d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
leaq 0xcf6(%rip), %rsi       # 0x2370
movq %r12, %rdi
movl $0x17, %edx
movq %r13, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1150
leaq 0xd35(%rip), %rsi       # 0x23cb
movl $0x2c, %edi
movq %r12, %rdx
movl %ebp, %ecx
callq 0x12ac
movq %r12, %rdi
callq 0x1080
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1060
movl $0x1fa0, %edx           # imm = 0x1FA0
movl %r14d, %edi
movq %r12, %rsi
callq 0x10d0
testq %rax, %rax
jle 0x16f8
leaq 0x2ada(%rip), %r15      # 0x41b0
movl %ebx, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1060
movl $0x1fa0, %edx           # imm = 0x1FA0
movl %r14d, %edi
movq %r15, %rsi
callq 0x10d0
testq %rax, %rax
jg 0x16d6
movl $0x1, %edi
callq 0x1170
movl %ebx, %edi
callq 0x10c0
movl $0x1, %edi
callq 0x1160