// File: /workspace/github_c_makefile_star2_O2/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 0x2db8(%rip), %r14      # 0x41b0
movl $0x1fa0, %edx           # imm = 0x1FA0
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
incq %rax
cmpq $0x1, %rax
ja 0x142d
leaq 0xe9b(%rip), %rsi       # 0x22b3
leaq 0xfab(%rip), %rdx       # 0x23ca
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
jmp 0x1440
leaq -0x1(%r15), %rax
cmpq $0x1f9e, %rax           # imm = 0x1F9E
ja 0x1440
movb $0x0, (%r15,%r14)
jmp 0x1447
movb $0x0, 0x2d69(%rip)      # 0x41b0
xorl %eax, %eax
testq %r15, %r15
cmovleq %rax, %r15
cmpq %rax, %r15
je 0x146e
movzbl (%rax,%r14), %ecx
cmpl $0xd, %ecx
je 0x1464
cmpl $0xa, %ecx
jne 0x1469
movb $0x2a, (%rax,%r14)
incq %rax
jmp 0x1450
leaq 0xe55(%rip), %rsi       # 0x22ca
leaq 0x2d34(%rip), %r14      # 0x41b0
pushq $0x2c
popq %rdi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x12ac
movl 0x2d21(%rip), %eax      # 0x41b0
cmpl $0x20544547, %eax       # imm = 0x20544547
je 0x14b7
cmpl $0x20746567, %eax       # imm = 0x20746567
je 0x14b7
leaq 0xe2e(%rip), %rsi       # 0x22d2
leaq 0x2d05(%rip), %rdx      # 0x41b0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
pushq $0x4
popq %r12
cmpq $0x1fa0, %r12           # imm = 0x1FA0
je 0x14d0
cmpb $0x20, (%r12,%r14)
je 0x14d8
incq %r12
jmp 0x14bb
movl $0x1f9f, %r12d          # imm = 0x1F9F
jmp 0x14e0
movb $0x0, (%r12,%r14)
decq %r12
xorl %r13d, %r13d
leaq 0xe0c(%rip), %r15       # 0x22f6
cmpq %r13, %r12
je 0x1516
cmpb $0x2e, (%r13,%r14)
jne 0x1511
cmpb $0x2e, 0x1(%r13,%r14)
jne 0x1511
movl $0x193, %edi            # imm = 0x193
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x12ac
incq %r13
jmp 0x14ea
leaq 0x2c93(%rip), %r14      # 0x41b0
leaq 0x1182(%rip), %rsi      # 0x26a6
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x1100
testl %eax, %eax
je 0x154d
leaq 0x2c76(%rip), %rdi      # 0x41b0
leaq 0x116c(%rip), %rsi      # 0x26ad
pushq $0x6
popq %rdx
callq 0x1100
testl %eax, %eax
jne 0x155b
movups 0xdd1(%rip), %xmm0      # 0x2325
movaps %xmm0, 0x2c55(%rip)     # 0x41b0
movq %r14, %rdi
callq 0x1080
movslq %eax, %r13
leaq 0x2b83(%rip), %r12      # 0x40f0
movq (%r12), %r15
testq %r15, %r15
je 0x15a4
movq %r15, %rdi
callq 0x1080
movq %r13, %rdi
subq %rax, %rdi
addq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1040
addq $0x10, %r12
testl %eax, %eax
jne 0x156d
movq -0x8(%r12), %r15
testq %r15, %r15
jne 0x15c1
leaq 0xd8a(%rip), %rsi       # 0x2335
leaq 0x2bfe(%rip), %rdx      # 0x41b0
movl $0x193, %edi            # imm = 0x193
movl %ebx, %ecx
callq 0x12ac
xorl %r15d, %r15d
leaq 0x2bed(%rip), %rdi      # 0x41b5
xorl %esi, %esi
xorl %eax, %eax
callq 0x1120
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x15f3
leaq 0xd77(%rip), %rsi       # 0x2357
leaq 0x2bce(%rip), %rdx      # 0x41b5
movl $0x194, %edi            # imm = 0x194
movl %ebx, %ecx
callq 0x12ac
leaq 0xd71(%rip), %rsi       # 0x236b
leaq 0x2bb4(%rip), %rdx      # 0x41b5
pushq $0x2c
popq %rdi
movl %ebp, %ecx
callq 0x12ac
leaq 0x2b9e(%rip), %r12      # 0x41b0
pushq $0x2
popq %rdx
movl %r14d, %edi
xorl %esi, %esi
callq 0x10b0
movl %ebp, %r13d
movq %rax, %rbp
movl %r14d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
leaq 0xd38(%rip), %rsi       # 0x2370
pushq $0x17
popq %rdx
movq %r12, %rdi
movq %rbp, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1150
leaq 0xd79(%rip), %rsi       # 0x23cb
pushq $0x2c
popq %rdi
movq %r12, %rdx
movl %r13d, %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
jg 0x1668
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x1170
movl %ebx, %edi
callq 0x10c0
movl %ebp, %edi
callq 0x1160