Week 10 Laboratory Exercises
Objectives
- understand how a web server works
- see a TCP/IP stream in action
- Become familiar with the environment used for the final exam
- practice coding under exam conditions
Getting Started
MyExperience
The first 10 minutes of the lab is set aside for you to complete the myExperience survey for COMP1521. Your tutors will leave the room to ensure your answers stay confidential.
Exam Environment Practice
The first two lab exercises must be completed and submitted in the exam environment.
They can not be submitted outside the exam environment.
Logging Into the Exam environment
Log out of your regular account after completing the myExperience survey.
Your tutors will log your lab machine into the exam environment,
Practice Exam Part 1
First enter answers to Part 1
You run "Enter Part 1 Answers" from the right mouse button menu to enter the answers to part 1.
In the final exam you will have 30 minutes to complete part 1 and during that time you are not permitted to run xterms, shells, editors, etc. You can view online documentation.
The real exam will have more part 1 questions!
Your answers for part 1 questions in the practice exam will not be marked.
Practice Exam Part 2
Two of this weeks lab exercises are the Part 2 questions for the practice exam.
You run "View Part 2 Questions" from the right mouse button menu to see part 2 questions.
Part 2 answers are entered into the file specified by each question and submitted using the "give" command inside the exam environment. The questions include submission instructions.
We are not enforcing exam conditions but try to do the questions under simulated exam conditions.
If you can't complete the questions, you can talk to your tutors and your lab partner.
Your two answers will be automarked in the usual way for lab exercises.
Logging Out Of The Exam environment
When you have finished the two part 2 questions log out of the exam environment and log into your account.
The remaining lab exercises are completed the usual way.
Exercise — in pairs:
Serve A Web Page
It implements a very simple webserver which you can compile and run:
dcc web_server.c serve_web_page.c -o web_server ./web_server From your web browser try one of these URLs http://localhost:32517/example.html http://localhost:32517/bird.htmlNote
localhost
is effectively a networking synonym for
the current machine so you web browser will have to be running on the
same machine for the above URLs to work.
If you access one of these URLs, they'll tell you that function
serve_web_page
is unimplemented. Your task in this
exercise is to implement this function:
Your task is to add code to this function in serve_web_page.c:
// pathname is the pathname component of a URL
// if a corresponding file exists the client is sent
// a 200 header assuming file contains HTML
// followed by the file contents
// if a the file can't be opened
// a 404 header is sent with no body
void serve_web_page(char *pathname, FILE *client_stream) {
// REPLACE WITH YOUR CODE
fprintf(client_stream, HEADER_404);
fprintf(client_stream, "You requested %s but function serve_web_page is unimplemented\n", pathname);
}
serve_web_page
should
write a 200 header to client_stream
followed by the file
contents.
If the request file does not existsserve_web_page
should write a 404 header to client_stream
(and nothing
else).
When you've done this the URL for example.html should show a very simple web page in your browser and and the one for bird.html should show a flying bird.
Use make(1) to build your code:
make # or 'make serve_web_page'
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest serve_web_page
When you are finished working on this exercise, you and your lab
partner must both submit your work by running give
:
give cs1521 lab10_serve_web_page serve_web_page.c
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
Challenge Exercise — individual:
Serve A Web Page With the Correct Mime Type
text/html
in the header as the
mime type.
When serving a file web servers when servers commonly, in the absence of special directives, infer the content type to return from a files extension.
For example, give the pathname "picture.jpg", the "jpg" extension would be used to determine the content type (in the absence of other directives).
On Unix-like systems the file /etc/mime.types
lists for
each mime-type one or more extensions that should be assumed to
correspond to this mime-type.
Your task in this exercise is to implement this function:
Your task is to add code to this function in mime_web_page.c:
// pathname is the pathname component of a URL
// if a corresponding file exists the client is sent
// a 200 header with a mime-type based on the files suffix
// with /etc/mime.types used to convert this suffix to a mime-type
// if the pathname doesn't have a suffix or this suffix can't be found
// in /etc/mime.types DEFAULT_MIME_TYPE is assumed
// the header is followed by the file contents
//
// if a the file can't be opened
// a 404 header is sent with no body
void mime_web_page(char *pathname, FILE *client_stream) {
// REPLACE WITH YOUR CODE
fprintf(client_stream, HEADER_404);
fprintf(client_stream, "You requested %s but function mime_web_page is unimplemented\n", pathname);
}
Compile it with :
dcc mime_web_server.c serve_web_page.c -o web_server ./web_server From your web browser test these URLs: http://localhost:32517/xkcd_303.jpg http://localhost:32517/xkcd_742.png http://localhost:32517/bird.html
The first should should you relevant XKCD cartoons.
Use make(1) to build your code:
make # or 'make mime_web_page'
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest mime_web_page
When you are finished working on this exercise, you must submit your
work by running give
:
give cs1521 lab10_mime_web_page mime_web_page.c
You must run give
before Wednesday 01 January 00:00 to obtain the
marks for this lab exercise. Note that this is an individual
exercise, the work you submit with give
must be
entirely your own.
Exercise — in pairs:
Practice Exam Q2
Your task is to add code to this function in practice_exam_q2.c:
// return how many 1 bits value contains
int practice_exam_q2(uint64_t value) {
// PUT YOUR CODE HERE
return 42;
}
Add code to the function practice_exam_q2
so that,
given a uint64_t
value, it returns how many 1 bits it
contains. You should use bitwise operators to do this.
For example:
./practice_exam_q2 0x123456789abcdef0 practice_exam_q2(0x123456789abcdef0) returned 32 ./practice_exam_q2 0xffffffffffffffff practice_exam_q2(0xffffffffffffffff) returned 64 ./practice_exam_q2 0x0000000000000000 practice_exam_q2(0x0000000000000000) returned 0 ./practice_exam_q2 0x0000000400000000 practice_exam_q2(0x0000000400000000) returned 1 ./practice_exam_q2 0x8000000000000001 practice_exam_q2(0x8000000000000001) returned 2
Use make(1) to build your code:
make # or 'make practice_exam_q2'
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest practice_exam_q2
When you are finished working on this exercise, you and your lab
partner must both submit your work by running give
:
give cs1521 lab10_practice_exam_q2 q2.c
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Practice Exam Q3
In the files for this lab, you have been given
practice_exam_q3.s
, a MIPS assembler program that reads 2 numbers and then prints 42.
Add code to practice_exam_q3.s
to make it equivalent to this C program:
// print the integers between x and y except 13
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
int i = x + 1;
while (i < y) {
if (i != 13) {
printf("%d\n", i);
}
i = i + 1;
}
return 0;
}
For example:
1521 spim -f practice_exam_q3.s Loaded: /home/cs1521/share/spim/exceptions.s 5 8 6 7 1521 spim -f practice_exam_q3.s Loaded: /home/cs1521/share/spim/exceptions.s 10 15 11 12 14 1521 spim -f practice_exam_q3.s Loaded: /home/cs1521/share/spim/exceptions.s 5 14 6 7 8 9 10 11 12
Assumptions/Limitations/Clarifications
You can assume the first number read is not greater than the second number.
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest practice_exam_q3
When you are finished working on this exercise, you and your lab
partner must both submit your work by running give
:
give cs1521 lab10_practice_exam_q3 q3.s
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
Submission
give
.
You can run give
multiple times. Only your last
submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Wednesday 01 January 00:00 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the
submission deadline, using test cases different to those
autotest
runs for you. (Hint: do your own testing as
well as runningautotest
.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1521 classrun -sturec