Objective |
The objective of this homework is to: Recollect (from CSE-278) the use of HTTP protocol for communication Develop a web-server that can process HTTP GET requests o Your web-server should be able to run a program with command-line arguments and return its outputs back to the client Continue to gain familiarity with I/O streams & string processing |
Background
In this homework you will be developing a simple program to process HTTP requests and generate responses. Recollect that HTTP is a multi-line text protocol that is used by World Wide Web (WWW), an application running on the Internet. In this homework you will be developing a program to respond to HTTP requests. Overall this program is essentially just string processing from I/O streams with most of the required code copy-pasted from different exercises/lecture slides.
Testing via a browser
The starter code enables the program to run as a web-server on an available port number. The starter code prints the port number, say 3456. Each time the program is run, the port number will change. Given a port number, say 3456, you must test the program via a web-server by suitably changing the command and arguments in the following URL:
- http://os1.csi.miamioh.edu:3456/cgi-bin/exec?cmd=ls&args=-l
- http://os1.csi.miamioh.edu:3456/cgi-bin/exec?cmd=echo&args=hello
- http://os1.csi.miamioh.edu:3456/cgi-bin/exec?cmd=ls&args=/blah
- http://os1.csi.miamioh.edu:3456/blah.txt
Chunked Response Requirements [Review from lecture slides]
The response/output from the program/server should be in the following format, with each line terminated with a r
:
- The first line should be HTTP/1.1 200 OK or HTTP/1.1 404 Not Found (if the GET request was not in expected format). See example inputs
- The second line should be Content-Type: text/plain .
- The next line must be Transfer-Encoding: chunked (a fixed constant string)
- The next line must be Connection: Close (a fixed constant string)
- The header section must be terminated by r
Rest of the contents of the output of a program are printed line-by-line with line size (in hex on a separate line) followed by the data as shown below:
// Send chunk size to client. os << std::hex << line.size() << r
; // Write the actual data for the line.
os << line << r
;
The end of data must be sent to the client via the following trailer:
// Send trailer out to denote end of data os << 0r
r
;
Request Processing Requirements
You are required to implement the serveClient method in the starter code. This method must be implemented (using additional helper methods to be implemented by you) to process multiple lines of one (and only one) HTTP GET request in the following manner:
- Base case Run program & return its outputs [15 points]: If input line starts with GET /cgi-bin/exec then this line corresponds to the case where a program is to be run. In this case, this line will be in the format: GET /cgi-
bin/exec?cmd=ls&args=1.txt hello.cpp HTTP/1.1 (one line), where
cmd contains the command to run and args has list of space separated command-line arguments to the program. Use std::quoted to correctly handle double quoted words in args. Note that you need to url_decode cmd and args prior to using them as they will be encoded (for an example see request in test_files/ cgi_bin_test_inputs1.txt). URL decoding method is included in starter code.
Appropriately process the GET request, run the specified command, obtain its outputs using pipes (see slide #19, #20 in 07_IPC.pdf, you will need to suitably use simple pipes example but run 1 process and read its outputs using a pipe) and generate the HTTP response using chunked encoding.
The last line of the output should include the exit code from the program being run. The response payload is finally terminated with a 0r
r
.
For example, given the request http://os1.csi.miamioh.edu:3456/cgibin/exec/cmd=echo&args=hello\n world, your program should generate the following output. Note: The r
is shown to help with non-printable characters. The
is what causes output to appear in next line. Some of the commands print their own newlines and those are not explicitly shown (as your program should not be printing those anyways).
HTTP/1.1 200 OKr Content-Type: text/plainr Transfer-Encoding: chunkedr Connection: Closer r 6r hellor 7r world r cr Exit code: 0r 0r r |
- Error case [5 points]: If the path in the GET request is not GET /cgi-bin/exec then the program should generate an HTTP 404 error response with message Invalid request: <path>. For example, given the request
http://os1.csi.miamioh.edu:3456/blah.txt, the following output should be generated:
HTTP/1.1 404 Not Foundr
Content-Type: text/plainr
Transfer-Encoding: chunkedr
Connection: Closer
r
1ar
Invalid request: blah.txt
r
0r
for good overall program structure, organization, | conciseness |
If your methods are not concise or undocumented points will be |
- 5 points are reserved , documentation.
deducted.
Sample inputs & outputs
Sample inputs and outputs are not shown because the HTTP headers contain line endings in the form r
which are not preserved in documents and leads to incorrect results if copypasted. Instead use the supplied input files and copy-paste from those input files.
Functional testing
When testing your program during development, simply copy-paste the inputs to your program (mimicking as if you manually typed the inputs). Once you have done the basic testing you may redirect input and output for testing as shown below. Several test inputs and expected output files are also supplied to streamline your testing as shown below:
$ ./hw5 test_files/base_case1_inputs.txt > my_base_case1_output.txt
$ diff my_base_case1_output.txt test_files/base_case1_expected_outputs.txt
Note: If your solution is correct then the above diff command will not print any output.
Ensure you test with all input files and corresponding expected outputs to check all features of your program.
Submit to Canvas
This homework assignment must be turned-in electronically via Canvas CODE plug-in. Ensure your program compiles (without any warnings or style errors) successfully. Ensure you have tested operations of your program as indicated. Once you have tested your implementation, upload the following onto Canvas:
The 1 C++ source file you developed for this homework
Reviews
There are no reviews yet.