270 likes | 446 Views
CS 241 Section (12/3/10). Announcements. No class on Monday TA review session: Dec. 8, 2010 – in class HW2 due date: Dec. 8, 2010 - 11:00 am No late submissions MP8 due date: Dec. 8, 2010 – 11:59 pm Finals: Dec. 16, 2010 – 8:00 am. In section today. MP8 Overview UNIX File Systems
E N D
CS 241 Section (12/3/10)
Announcements • No class on Monday • TA review session: Dec. 8, 2010 – in class • HW2 due date: Dec. 8, 2010 - 11:00 am • No late submissions • MP8 due date: Dec. 8, 2010 – 11:59 pm • Finals: Dec. 16, 2010 – 8:00 am
In section today • MP8 Overview • UNIX File Systems • Directories • Links
What is the MP about? • Implement a multi-threaded web server • Web client: browser / wget / telnet • Server should respond to client requests with local pages (from ./pages/ folder)
Running the server • Run the web server • The server listens on the port specified • Choose a port between 1024 and 32000 Port #
Testing the server • Run the web client • wget • Browser • Go to address: • Server name (linux3.ews.illinois.edu) is obtained from the hostname command
MP Task I - Listen • Your server should listen to the port# (specified in argv[1]) for incoming connections • Helper function: newServerSocket(), acceptSocket() • Your server must handle multiple connections at the same time • Every incoming connection spawns a new thread to handle the request
Task II – Receive HTTP Request • HTTP request sent by wget • Function for extracting file name from request • char* getFileNameFromHTTPRequest(void *vptrRequest, size_t length); • All files served from web server root directory • ./pages/ directory
Task III – HTTP 1.1 Response • If the file exists the response starts with: • Function for building the response • HTTPResponse getResponseString(char *sContentType, void *vptrContent, size_t iContentLength);
Task III – HTTP 1.1 Response • If the file does NOT exist the response starts with: • Function for building the response • HTTPResponse getFileNotFoundResponseString()
Task III – HTTP 1.1 Response • If the request is NOT GET the response starts with: • Function for building the response • HTTPResponse getNotImplementedResponseString()
Task III – HTTP 1.1 Response • If the browser request for / • The web server should list all the files in the root directory (./pages/ directory) • ONLY the files • Function for building an HTML page with list of files • char *getHTMLOfDirectoryList(char **fileNames); • Build Response with getResponseString()
Last Task – Sending the Response • We have a HTTP response ready in a HTTPResponse struct: • Use send() to send the vptrResponse back • free vptrResponse after send()
Directory reading functions #include <dirent.h> Open the directory DIR *opendir(const char *dirname); Close the directory int closedir(DIR *dirp); Read the directory struct dirent *readdir(DIR *dirp);
What’s in a directory entry? struct dirent Member Fields char d_name[256] Null-terminated file name ino_t d_ino inode number unsigned char d_reclen Length of this record unsigned char d_type Type of file (DT_REG, DT_DIR, DT_FIFO, DT_SOCK, DT_CHR, DT_BLK, DT_UNKNOWN)
Example • Use opendir and readdir to print all the filenames in the current directory: #include <dirent.h> … DIR *dir; struct dirent *entry; dir = opendir(“.”); while(entry = readdir(dir)){ printf(“%s\n”,entry->d_name); } closedir(dir); • Remember to include error checking!!
Links • Hard Link • Directory Entry e.g. all regular files • Symbolic Link (Soft Link) • Special file, serves as a reference to another file
Hard Link Example Command Line ln /dirA/name1 /dirB/name2 C Code Segments if (link("/dirA/name1", "/dirB/name2") == -1) perror("Failed to make a new link in /dirB");
Hard Link Example (contd) Q: What happens if /dirA/name1 is deleted and recreated?
Hard Link Example (contd) A:/dirA/name1 and /dirB/name2 are now two distinct files.
Soft Link Example Command Line ln –s /dirA/name1 /dirB/name2 C Code Segments if (symlink("/dirA/name1", "/dirB/name2") == -1) perror("Failed to create a symbolic link in /dirB");
Soft Link Example (contd) Q: What happens if /dirA/name1 to is deleted and recreated?
Soft Link Example (contd) A:/dirA/name1 has a different inode, but /dir/name2 still links to it.
Link number • The link number (the st_nlink field in stat) tells how many directory entries link to this inode. The link number is: • Set to 1 when a file is created • Incremented when link is called • Decremented when unlink is called • The link number appears in the second column of the output of ls –l. Try it! • The link number only counts hard links, not soft links.