oscn-cw-09-10

download oscn-cw-09-10

of 2

Transcript of oscn-cw-09-10

  • 8/2/2019 oscn-cw-09-10

    1/2

    CS2 Operating Systems and Computer Networks 200910 1

    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.

    1 Managing the practical work

    It is necessary to have access to a POSIX (Unix) programming envi-

    ronment 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 pro-

    gramming environment within a Windows command prompt.

    Visit http://www.cygwin.com for full details.

    5. use a remote Unix system, on which you have an account, via

    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:

    louise.feis.herts.ac.uk and thelma.feis.herts.ac.uk.

    2 Description of the assignment

    There is one main task: to design, implement and test a web serverwritten in C++, running on a POSIX system, that acts as a minimal

    WWW Server and can respond appropriately to requests from web

    browsers such as Internet Explorer or Firefox.

    This is a description of the required functionality. The task in-

    volves 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 sum-

    mary 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,

    4. it will use its own PORT number, not 80,

    5. it will use a sub-directory to keep its own Web pages, called

    pages/

    6. it must be a concurrent server using Posix pro-

    cesses. To help you do this you should download a

    skeleton server program server-skeleton-procs.cpp from

    http://homepages.feis.herts.ac.uk/2com0088/ the download

    directory, then cw-stuff. You should base your work on this

    program (this is a requirement, not an option).

    7. all socket I/O should use system calls (not iostream or stdio li-

    brary calls), however error messages and log file writing can use

    library calls. Reading files can be done with iostream calls or

    system calls and will depend on which is easier.

    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 file. Dealing with HEAD is the same, all the same

    response lines, BUT it doesnt send the file,

    9. it must provide correct protocol response messages (as well asany 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 (file name) does not start with / you

    send 400 Bad request response,

    if the command is GET or HEAD but the requested resource

    (file) 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 file exists send the 200 OK re-

    sponse, the correct option lines and the file if the reuest is

    GET.

    10. it will use a log file to record all transactions, there should only

    be one line per transaction,

    11. your server should always send a:

    Connection: close

    option line with every response.

    12. in addition to the first, primary response line it should re-

    turn an additional line containing the Content-Type:. The

    Content-Type: line is always the last before the blank line and

    the file. You can usually determine the content type by exam-

    ining the extension of the name of the requested file. It should

    correctly identify the extensions:

    .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

    file (note, this does not apply to the output of CGI or PHP pro-

    grams ). This requires use of the system all stat, see the ex-ample in the appendix, Using stat,

    14. an extra task: a Date: line showing the current date and

    time (in the correct formatsee some server output or read the

    HTTP/1.1 specification in RFC2616),

    15. another extra task that might be harder to implement: if the file

    name in a GET request has the extension .php then you should

    execute php and give it the named file as input. The standard

    output of the program to be redirected to the network connec-tion. see the example in the appendix, Executing PHP,

    3 Hand in

    Hand in a complete, commented, listing of the final C++ server pro-

    gram to Studynet, in plain text form NOT zipped or in RAR form.

  • 8/2/2019 oscn-cw-09-10

    2/2

    CS2 Operating Systems and Computer Networks 200910 2

    4 Mark allocation and assessment

    Marks will be allocated under the following headings:

    Achieving the required basic functionality for the server: read-

    ing requests, returning the requested file 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

    (ie. no inappropriateuses of data structuresor control structures,

    no use of uninitialized variables, sensible use of parameters to

    functions, use of local variables, etc.). (3 marks)

    Checking if there is a Date: option line for HTTP/1.1(2 marks)

    Executing PHP programs with the extension .php. (2 marks)

    5 APPENDIX: testing HTTP requests

    Firstly you must test your server using a browser, either firefox,chrome or Internet Explorer. However this is not enough, firstly, 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

    used. It allows direct text access to any and from any TCP or

    UDP port.

    download the file called t1-ok.tst:

    GET /vtiny.html HTTP/1.0

    Connection: close

    NB there is a blank line at the end. Now send it to a web server

    with a file called vtiny.html, do this by running nc to connect to

    port 80 on host tink and re-directing the file t1-ok.tst

    nc tink.feis.herts.ac.uk 80 < t1-ok.tst

    you should see all the response lines from the server program aswell as the file contents.

    change the file 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 find out what server they run.

    Now try sending an illegal request using t2-bad-res.tst

    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 file and

    the name you give to nc.

    If you have started work on your own server you can try using t1-

    ok.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.

    6 APPENDIX: using statIn order to get the last modified time of a file, and the length ofthe file, you should use the stat system call. Here is an exampleprogram, eg4-stat.cpp

    ...

    #include

    #include

    using namespace std;

    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