Você está na página 1de 2

CS2 Operating Systems and Computer Networks 200910

Course work for CS2 Operating Systems and Computer Networks 200910 Subject to moderation
Set date: Tuesday 8th December 2009 Due date: 1200 hrs. Monday, 8th February 2010 Time required: 30 hours per student Value: 20 % of assessment of whole module Lecturers: Jean Baillie, Bob Dickerson & Jo Spring Learning outcomes assessed: To develop system programs in a POSIX environment. 8. it needs to deal with GET and HEAD requests. In order to deal with GET it returns all the appropriate HTTP response lines and then sends the le. Dealing with HEAD is the same, all the same response lines, BUT it doesnt send the le, 9. it must provide correct protocol response messages (as well as any HTML needed) for the following situations: if the request line does not end with either HTTP/1.0 or HTTP/1.1 you send 400 Bad request response (NB not all real web servers do this but yours should), if the resource name (le name) does not start with / you send 400 Bad request response, if the command is GET or HEAD but the requested resource (le) doesnt exist then it must send a 404 response, if the command is not GET or HEAD then it should send a 501 Not implemented response, correct requests and the le exists send the 200 OK response, the correct option lines and the le if the reuest is GET.

1 Managing the practical work


It is necessary to have access to a POSIX (Unix) programming environment in order to do this coursework. There are alternative ways to do this: 1. use Linux on the Computer Science laboratory machines, 2. install Linux on your computer, 3. install VMPlayer on your windows machine and then install Linux inside that,

4. install cygwin under Windows. cygwin provides a Unix programming environment within a Windows command prompt. 10. it will use a log le to record all transactions, there should only be one line per transaction, Visit http://www.cygwin.com for full details. 11. your server should always send a: 5. use a remote Unix system, on which you have an account, via Connection: close telnet or ssh. A good free Windows version of ssh is called putty (search for it and download it). You have accounts on all the machines in Computer Science, including the servers: option line with every response. louise.feis.herts.ac.uk and thelma.feis.herts.ac.uk. 12. in addition to the rst, primary response line it should return an additional line containing the Content-Type:. The 2 Description of the assignment Content-Type: line is always the last before the blank line and There is one main task: to design, implement and test a web server the le. You can usually determine the content type by examwritten in C++, running on a POSIX system, that acts as a minimal ining the extension of the name of the requested le. It should WWW Server and can respond appropriately to requests from web correctly identify the extensions: browsers such as Internet Explorer or Firefox. This is a description of the required functionality. The task involves designing and implementing a Web server program that can understand (part of) HTTP/1.1 and can return pages to a real client like Firefox, Konqueror or Internet Explorer. A more detailed summary of the minimal functional requirements of the server: 1. it will be in C++ running on a Unix system, 2. you do not need to use classes in the C++ that you write, 3. it will return pages to a real WWW client, .htm .html .jpg .jpeg .txt .gif .png for .jpg or .jpeg it should return Content-Type: image/jpeg, for .gif it should return Content-Type: image/gif, for .png it should return Content-Type: image/png, for .txt it should return Content-Type: text/plain, and for .htm, .html or any other extension it should return Content-Type: text/html. (You should have written some of the code for this in the task for the earlier test.)

13. every 200 response to GET and HEAD should have a Content-Length: option line with the number of bytes in the 4. it will use its own PORT number, not 80, le (note, this does not apply to the output of CGI or PHP programs ). This requires use of the system all stat, see the ex5. it will use a sub-directory to keep its own Web pages, called ample in the appendix, Using stat, pages/ 6. it must be a concurrent server using Posix pro- 14. an extra task: a Date: line showing the current date and time (in the correct formatsee some server output or read the cesses. To help you do this you should download a HTTP/1.1 specication in RFC2616), skeleton server program server-skeleton-procs.cpp from http://homepages.feis.herts.ac.uk/2com0088/ the download 15. another extra task that might be harder to implement: if the le directory, then cw-stuff. You should base your work on this name in a GET request has the extension .php then you should program (this is a requirement, not an option). execute php and give it the named le as input. The standard output of the program to be redirected to the network connec7. all socket I/O should use system calls (not iostream or stdio lition. see the example in the appendix, Executing PHP, brary calls), however error messages and log le writing can use library calls. Reading les can be done with iostream calls or 3 Hand in system calls and will depend on which is easier. Hand in a complete, commented, listing of the nal C++ server program to Studynet, in plain text form NOT zipped or in RAR form.

CS2 Operating Systems and Computer Networks 200910

2 If you have started work on your own server you can try using t1ok.tst to see what happens: g++ server-skeleton-procs.cpp ./a.out 8123 and in another window: nc localhost 8123 < t1-ok.tst If you have made any changes to the server you will get no response over the network socket but the server might print debugging output on its console.

4 Mark allocation and assessment


Marks will be allocated under the following headings: Achieving the required basic functionality for the server: reading requests, returning the requested le preceded by a suitable headers, with correct error responses where appropriate, (8 marks) Achieving the next level of functionality for the server: Content-Type:, and Content-Length: (5 marks)

The layout, commenting, choice of names and indentation of the program text the style. And how well C++ has been used 6 APPENDIX: using stat (ie. no inappropriate uses of data structures or control structures, In order to get the last modied time of a le, and the length of no use of uninitialized variables, sensible use of parameters to the le, you should use the stat system call. Here is an example functions, use of local variables, etc.). (3 marks) program, eg4-stat.cpp
... #include <sys/stat.h> Checking if there is a Date: option line for HTTP/1.1 (2 marks) #include <iostream> using namespace std;

Executing PHP programs with the extension .php.

(2 marks)
int main(int argc, char *argv[]){ struct stat statbuf; int r; if(argc!=2) { exit(1); } r = stat(argv[1], &statbuf); if(r < 0) { cerr << "stat failed\n"; exit(1); } cout << argv[1]<< " size ="<< statbuf.st_size<<"\n"; cout << argv[1]<< " owner protection " << oct << (statbuf.st_mode & S_IRWXU) << dec << "\n";

5 APPENDIX: testing HTTP requests

Firstly you must test your server using a browser, either refox, chrome or Internet Explorer. However this is not enough, rstly, it will not let you see all the option lines that your server sends, and secondly, it will not send incorrect requests that your server should test for. So you should send your own HTTP requests to your server using netcat, called nc. The following examples are using an existing Apache server, they are to show you how to use nc and to show you what correct responses should be. You will run the tests on real servers and on } your own server:

The program nc (also called, but not named netcat) will be 7 APPENDIX: executing PHP used. It allows direct text access to any and from any TCP or To deal with a request for a PHP le you must execute the php and UDP port. give it the name of the le as an argument. Here is a small example of running PHP download the le called t1-ok.tst: GET /vtiny.html HTTP/1.0 Connection: close
... #include <wait.h> #include <iostream> using namespace std;

NB there is a blank line at the end. Now send it to a web server int main(int argc, char *argv[]){ with a le called vtiny.html, do this by running nc to connect to if(argc != 2) { cerr << "need PHP filename\n"; exit(1); port 80 on host tink and re-directing the le t1-ok.tst nc tink.feis.herts.ac.uk 80 < t1-ok.tst you should see all the response lines from the server program as well as the le contents.
} int pid = fork(); if(pid == 0) { execl("/usr/bin/php", "php",argv[1]); cerr << "exec failed\n"; exit(1); } wait(0); cout << "main finished\n";

change the le t1-ok.tst so that it requests /index.html not /vtiny.html and send the request to other servers eg: www.herts.ac.uk, yahoo.com, www.google.co.uk. Apart from } seeing the responses, you can nd out what server they run. What the example does not show is that you must reconnect the Now try sending an illegal request using t2-bad-res.tst standard output to the socket before doing the exec, see the notes on system programming: 03-sysprog.pdf section 8.1. nc tink.feis.herts.ac.uk 80 < t2-bad-res.tst and found out what response you get try more sites, each time change the Host: line in the le and the name you give to nc.

Você também pode gostar