Upload
carys
View
47
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
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
MP8 Overview
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
./myserver 10000
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
wget –d http://linux3.ews.illinois.edu:10000/index.html
http://linux3.ews.illinois.edu:10000/index.html
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
GET /index.html HTTP/1.0User-Agent: Wget/1.11.4 Red Hat modifiedAccept: */*Host: linux3.ews.illinois.edu:10000Connection: Keep-Alive
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);
HTTP/1.1 200 OKServer: CS/241MIME-version: 1.0Content-type: text/htmlContent-Length: 1697
Task III – HTTP 1.1 Response
• If the file does NOT exist the response starts with:
• Function for building the response• HTTPResponse getFileNotFoundResponseString()
HTTP/1.1 404 Not FoundServer: CS/241Connection: closeContent-Length: 300
Task III – HTTP 1.1 Response
• If the request is NOT GET the response starts with:
• Function for building the response• HTTPResponse getNotImplementedResponseString()
HTTP/1.1 501 Not ImplementedServer: CS/241Connection: closeContent-Length: 300
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()
typedef struct __HTTPResponse { void *vptrResponse; size_t length;} HTTPResponse;
Reading a Directory
Directory reading functions#include <dirent.h>
Open the directoryDIR *opendir(const char *dirname);
Close the directoryint closedir(DIR *dirp);
Read the directorystruct dirent *readdir(DIR *dirp);
What’s in a directory entry?struct dirent
Member Fieldschar d_name[256]
Null-terminated file nameino_t d_ino
inode numberunsigned char d_reclen
Length of this recordunsigned 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
Links
• Hard Link• Directory Entry e.g. all regular files
• Symbolic Link (Soft Link)
• Special file, serves as a reference to another file
#include <unistd.h>
int link(const char *oldpath, const char *newpath);int unlink(const char *path);
int symlink(const char *oldpath, const char *newpath);
Hard Link ExampleCommand Lineln /dirA/name1 /dirB/name2
C Code Segmentsif (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 ExampleCommand Lineln –s /dirA/name1 /dirB/name2
C Code Segmentsif (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.