// File: /workspace/github_c_makefile_star2_O3/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      # 0x41a0
movl $0x1fa0, %edx           # imm = 0x1FA0
movq %r14, %rsi
callq 0x10d0
leaq 0x1(%rax), %rcx
cmpq $0x1, %rcx
ja 0x1431
leaq 0xe9e(%rip), %rsi       # 0x22b3
leaq 0xfae(%rip), %rdx       # 0x23ca
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x129c
movb $0x0, 0x2d71(%rip)      # 0x41a0
jmp 0x146f
leaq -0x1(%rax), %rcx
cmpq $0x1f9e, %rcx           # imm = 0x1F9E
ja 0x1445
movb $0x0, (%rax,%r14)
jmp 0x1451
movb $0x0, 0x2d54(%rip)      # 0x41a0
testq %rax, %rax
jle 0x146f
xorl %ecx, %ecx
movzbl (%rcx,%r14), %edx
cmpl $0xd, %edx
je 0x1462
cmpl $0xa, %edx
jne 0x1467
movb $0x2a, (%rcx,%r14)
incq %rcx
cmpq %rcx, %rax
jne 0x1453
leaq 0xe54(%rip), %rsi       # 0x22ca
leaq 0x2d23(%rip), %r14      # 0x41a0
movl $0x2c, %edi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x129c
movl 0x2d0e(%rip), %eax      # 0x41a0
cmpl $0x20544547, %eax       # imm = 0x20544547
je 0x14ba
cmpl $0x20746567, %eax       # imm = 0x20746567
je 0x14ba
leaq 0xe2b(%rip), %rsi       # 0x22d2
leaq 0x2cf2(%rip), %rdx      # 0x41a0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x129c
movl $0x4, %r12d
cmpb $0x20, (%r12,%r14)
je 0x14db
incq %r12
cmpq $0x1fa0, %r12           # imm = 0x1FA0
jne 0x14c0
movl $0x1f9f, %r12d          # imm = 0x1F9F
jmp 0x14e3
movb $0x0, (%r12,%r14)
decq %r12
xorl %r13d, %r13d
leaq 0xe09(%rip), %r15       # 0x22f6
cmpb $0x2e, (%r13,%r14)
jne 0x150f
cmpb $0x2e, 0x1(%r13,%r14)
jne 0x150f
movl $0x193, %edi            # imm = 0x193
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x129c
incq %r13
cmpq %r13, %r12
jne 0x14ed
movl $0x20544547, %eax       # imm = 0x20544547
xorl 0x2c7e(%rip), %eax      # 0x41a0
movzwl 0x2c7b(%rip), %ecx      # 0x41a4
xorl $0x2f, %ecx
orl %eax, %ecx
je 0x1549
movl $0x20746567, %eax       # imm = 0x20746567
xorl 0x2c65(%rip), %eax      # 0x41a0
movzwl 0x2c62(%rip), %ecx      # 0x41a4
xorl $0x2f, %ecx
orl %eax, %ecx
jne 0x1557
movups 0xdd5(%rip), %xmm0      # 0x2325
movaps %xmm0, 0x2c49(%rip)     # 0x41a0
movq 0x2b82(%rip), %r15      # 0x40e0
testq %r15, %r15
je 0x15c9
movq %r14, %rdi
callq 0x1080
movslq %eax, %r12
movq %r15, %rdi
callq 0x1080
movq %r12, %rdi
subq %rax, %rdi
addq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1040
leaq 0x2b4f(%rip), %r13      # 0x40e0
testl %eax, %eax
je 0x15c0
movq 0x10(%r13), %r15
testq %r15, %r15
je 0x15c9
movq %r15, %rdi
callq 0x1080
movq %r12, %rdi
subq %rax, %rdi
addq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1040
addq $0x10, %r13
jmp 0x1591
movq 0x8(%r13), %r15
testq %r15, %r15
jne 0x15e6
leaq 0xd65(%rip), %rsi       # 0x2335
leaq 0x2bc9(%rip), %rdx      # 0x41a0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x129c
xorl %r15d, %r15d
leaq 0x2bb8(%rip), %rdi      # 0x41a5
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x1618
leaq 0xd52(%rip), %rsi       # 0x2357
leaq 0x2b99(%rip), %rdx      # 0x41a5
movl $0x194, %edi            # imm = 0x194
movl %ebx, %ecx
callq 0x129c
leaq 0xd4c(%rip), %rsi       # 0x236b
leaq 0x2b7f(%rip), %rdx      # 0x41a5
movl $0x2c, %edi
movl %ebp, %ecx
callq 0x129c
leaq 0x2b67(%rip), %r12      # 0x41a0
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 0xd12(%rip), %rsi       # 0x2370
movq %r12, %rdi
movl $0x17, %edx
movq %r13, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1140
leaq 0xd51(%rip), %rsi       # 0x23cb
movl $0x2c, %edi
movq %r12, %rdx
movl %ebp, %ecx
callq 0x129c
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 0x16dc
leaq 0x2ae6(%rip), %r15      # 0x41a0
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 0x16ba
movl $0x1, %edi
callq 0x1160
movl %ebx, %edi
callq 0x10c0
movl $0x1, %edi
callq 0x1150