Graphics Programs Reference
In-Depth Information
free(ptr); // Free file memory.
}
close(fd); // Close the file.
} // End if block for file found/not found.
} // End if block for valid request.
} // End if block for valid HTTP.
shutdown(sockfd, SHUT_RDWR); // Close the socket gracefully.
}
/* This function accepts an open file descriptor and returns
* the size of the associated file. Returns -1 on failure.
*/
int get_file_size(int fd) {
struct stat stat_struct;
if(fstat(fd, &stat_struct) == -1)
return -1;
return (int) stat_struct.st_size;
}
The handle_connection function uses the strstr() function to look for the
substring HTTP/ in the request buffer. The strstr() function returns a pointer
to the substring, which will be right at the end of the request. The string is
terminated here, and the requests HEAD and GET are recognized as processable
requests. A HEAD request will just return the headers, while a GET request will
also return the requested resource (if it can be found).
The files index.html and image.jpg have been put into the directory
webroot, as shown in the output below, and then the tinyweb program is
compiled. Root privileges are needed to bind to any port below 1024, so the
program is setuid root and executed. The server's debugging output shows
the results of a web browser's request of http://127.0.0.1:
reader@hacking:~/booksrc $ ls -l webroot/
total 52
-rwxr--r-- 1 reader reader 46794 2007-05-28 23:43 image.jpg
-rw-r--r-- 1 reader reader 261 2007-05-28 23:42 index.html
reader@hacking:~/booksrc $ cat webroot/index.html
<html>
<head><title>A sample webpage</title></head>
<body bgcolor="#000000" text="#ffffffff">
<center>
<h1>This is a sample webpage</h1>
...and here is some sample text<br>
<br>
..and even a sample image:<br>
<img src="image.jpg"><br>
</center>
</body>
</html>
reader@hacking:~/booksrc $ gcc -o tinyweb tinyweb.c
reader@hacking:~/booksrc $ sudo chown root ./tinyweb
reader@hacking:~/booksrc $ sudo chmod u+s ./tinyweb
reader@hacking:~/booksrc $ ./tinyweb
Search WWH ::




Custom Search