// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movl %edi, 0x44(%rsp)
movl %esi, 0x40(%rsp)
movl 0x44(%rsp), %edi
leaq 0x2cb9(%rip), %rsi      # 0x41c0
movl $0x1fa0, %edx           # imm = 0x1FA0
callq 0x10f0
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x1526
cmpq $-0x1, 0x20(%rsp)
jne 0x1542
movl 0x44(%rsp), %ecx
movl $0x193, %edi            # imm = 0x193
leaq 0xd7d(%rip), %rsi       # 0x22b3
leaq 0xe97(%rip), %rdx       # 0x23d4
callq 0x12d0
cmpq $0x0, 0x20(%rsp)
jle 0x1567
cmpq $0x1fa0, 0x20(%rsp)     # imm = 0x1FA0
jge 0x1567
movq 0x20(%rsp), %rcx
leaq 0x2c5f(%rip), %rax      # 0x41c0
movb $0x0, (%rax,%rcx)
jmp 0x156e
movb $0x0, 0x2c52(%rip)      # 0x41c0
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
jge 0x15cf
movq 0x28(%rsp), %rcx
leaq 0x2c31(%rip), %rax      # 0x41c0
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
je 0x15ad
movq 0x28(%rsp), %rcx
leaq 0x2c1c(%rip), %rax      # 0x41c0
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x15bd
movq 0x28(%rsp), %rcx
leaq 0x2c07(%rip), %rax      # 0x41c0
movb $0x2a, (%rax,%rcx)
jmp 0x15bf
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x1577
movl 0x40(%rsp), %ecx
movl $0x2c, %edi
leaq 0xceb(%rip), %rsi       # 0x22ca
leaq 0x2bda(%rip), %rdx      # 0x41c0
callq 0x12d0
leaq 0x2bce(%rip), %rdi      # 0x41c0
leaq 0xcd9(%rip), %rsi       # 0x22d2
movl $0x4, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1641
leaq 0x2bb1(%rip), %rdi      # 0x41c0
leaq 0xcc1(%rip), %rsi       # 0x22d7
movl $0x4, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1641
movl 0x44(%rsp), %ecx
movl $0x193, %edi            # imm = 0x193
leaq 0xca7(%rip), %rsi       # 0x22dc
leaq 0x2b84(%rip), %rdx      # 0x41c0
callq 0x12d0
movq $0x4, 0x28(%rsp)
cmpq $0x1fa0, 0x28(%rsp)     # imm = 0x1FA0
jge 0x168e
movq 0x28(%rsp), %rcx
leaq 0x2b5f(%rip), %rax      # 0x41c0
movsbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jne 0x167c
movq 0x28(%rsp), %rcx
leaq 0x2b4a(%rip), %rax      # 0x41c0
movb $0x0, (%rax,%rcx)
jmp 0x168e
jmp 0x167e
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x164a
movl $0x0, 0x3c(%rsp)
movslq 0x3c(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1703
movslq 0x3c(%rsp), %rcx
leaq 0x2b0b(%rip), %rax      # 0x41c0
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0x16f4
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x2af1(%rip), %rax      # 0x41c0
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0x16f4
movl 0x44(%rsp), %ecx
movl $0x193, %edi            # imm = 0x193
leaq 0xc18(%rip), %rsi       # 0x2300
leaq 0x2ad1(%rip), %rdx      # 0x41c0
callq 0x12d0
jmp 0x16f6
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1696
leaq 0x2ab6(%rip), %rdi      # 0x41c0
leaq 0xfa5(%rip), %rsi       # 0x26b6
movl $0x6, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x173d
leaq 0x2a99(%rip), %rdi      # 0x41c0
leaq 0xf8f(%rip), %rsi       # 0x26bd
movl $0x6, %edx
callq 0x1040
cmpl $0x0, %eax
jne 0x1750
leaq 0x2a7c(%rip), %rdi      # 0x41c0
leaq 0xbe4(%rip), %rsi       # 0x232f
callq 0x1050
leaq 0x2a69(%rip), %rdi      # 0x41c0
callq 0x1080
movl %eax, 0x34(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rcx
leaq 0x2982(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x181b
movq 0x28(%rsp), %rcx
leaq 0x2965(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1080
movq %rax, 0x18(%rsp)
movslq 0x34(%rsp), %rax
subq 0x18(%rsp), %rax
leaq 0x2a00(%rip), %rdi      # 0x41c0
addq %rax, %rdi
movq 0x28(%rsp), %rcx
leaq 0x2931(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq 0x18(%rsp), %rdx
callq 0x1040
cmpl $0x0, %eax
jne 0x1806
movq 0x28(%rsp), %rcx
leaq 0x290c(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x181b
jmp 0x1808
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x1772
cmpq $0x0, 0x10(%rsp)
jne 0x183f
movl 0x44(%rsp), %ecx
movl $0x193, %edi            # imm = 0x193
leaq 0xb0c(%rip), %rsi       # 0x233f
leaq 0x2986(%rip), %rdx      # 0x41c0
callq 0x12d0
leaq 0x297a(%rip), %rdi      # 0x41c0
addq $0x5, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1140
movl %eax, 0x38(%rsp)
cmpl $-0x1, %eax
jne 0x187c
movl 0x44(%rsp), %ecx
movl $0x194, %edi            # imm = 0x194
leaq 0xaf5(%rip), %rsi       # 0x2361
leaq 0x294d(%rip), %rdx      # 0x41c0
addq $0x5, %rdx
callq 0x12d0
movl 0x40(%rsp), %ecx
movl $0x2c, %edi
leaq 0xae9(%rip), %rsi       # 0x2375
leaq 0x292d(%rip), %rdx      # 0x41c0
addq $0x5, %rdx
callq 0x12d0
movl 0x38(%rsp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10c0
movq %rax, 0x18(%rsp)
movl 0x38(%rsp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
leaq 0x28ed(%rip), %rdi      # 0x41c0
leaq 0xaa0(%rip), %rsi       # 0x237a
movl $0x17, %edx
movb $0x0, %al
callq 0x1170
movl 0x40(%rsp), %ecx
movl $0x2c, %edi
leaq 0xadf(%rip), %rsi       # 0x23d5
leaq 0x28c3(%rip), %rdx      # 0x41c0
callq 0x12d0
movl 0x44(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x28af(%rip), %rdi      # 0x41c0
callq 0x1080
movl 0xc(%rsp), %edi
movq %rax, %rdx
leaq 0x289c(%rip), %rsi      # 0x41c0
callq 0x1060
movl 0x38(%rsp), %edi
leaq 0x288c(%rip), %rsi      # 0x41c0
movl $0x1fa0, %edx           # imm = 0x1FA0
callq 0x10f0
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
jle 0x1960
movl 0x44(%rsp), %edi
movq 0x20(%rsp), %rdx
leaq 0x2867(%rip), %rsi      # 0x41c0
callq 0x1060
jmp 0x1929
movl $0x1, %edi
callq 0x1190
movl 0x44(%rsp), %edi
callq 0x10e0
movl $0x1, %edi
callq 0x1180
nopl (%rax)