|
| 1 | +Session 2 Homework |
| 2 | +================== |
| 3 | + |
| 4 | +Required Tasks: |
| 5 | +--------------- |
| 6 | + |
| 7 | +in assignments/session02/http_server.py: |
| 8 | + |
| 9 | +* Update the parse_request function to return the URI it parses from the |
| 10 | + request. |
| 11 | + |
| 12 | +* Update the response_ok function so that it accepts a body and mimetype |
| 13 | + argument and properly includes these in the response it generates. |
| 14 | + |
| 15 | +* Write a new function resolve_uri that handles looking up resources on |
| 16 | + disk using the URI. |
| 17 | + |
| 18 | + * It should take a URI as the sole argument |
| 19 | + |
| 20 | + * It should map the pathname represented by the URI to a filesystem |
| 21 | + location. |
| 22 | + |
| 23 | + * It should have a 'home directory', and look only in that location. |
| 24 | + |
| 25 | + * If the URI is a directory, it should return a plain-text listing and the |
| 26 | + mimetype ``text/plain``. |
| 27 | + |
| 28 | + * If the URI is a file, it should return the contents of that file and its |
| 29 | + correct mimetype. |
| 30 | + |
| 31 | + * If the URI does not map to a real location, it should raise an exception |
| 32 | + that the server can catch to return a 404 response. |
| 33 | + |
| 34 | +* Write a new function response_not_found that returns a 404 response if the |
| 35 | + resource does not exist. |
| 36 | + |
| 37 | +When you have successfully completed these tasks as described, all the tests |
| 38 | +in assignments/session02/tests.py will pass as written. If you have to update |
| 39 | +the tests to get them to pass, think again about how you are implementing the |
| 40 | +feature under test. |
| 41 | + |
| 42 | +To run the tests: |
| 43 | + |
| 44 | +* Open one terminal while in this folder and execute this command: |
| 45 | + |
| 46 | + $ python http_server.py |
| 47 | + |
| 48 | +* Open a second terminal in this same folder and execute this command: |
| 49 | + |
| 50 | + $ python tests.py |
| 51 | + |
| 52 | +Make sure to run the tests early and often during your work. Remember, TDD |
| 53 | +means that as soon as a test passes you are finished working. |
| 54 | + |
| 55 | + |
| 56 | +Optional Tasks: |
| 57 | +--------------- |
| 58 | + |
| 59 | +* Update all error responses so that they return something that can be seen in |
| 60 | + a web browser. |
| 61 | + |
| 62 | +* Format directory listings as HTML, so you can link to files. Update the |
| 63 | + mimetype appropriately. |
| 64 | + |
| 65 | +* Add a GMT ``Date:`` header in the proper format (RFC-1123) to responses. |
| 66 | + *hint: see email.utils.formatdate in the python standard library* |
| 67 | + |
| 68 | +* Add a ``Content-Length:`` header for ``OK`` responses that provides a |
| 69 | + correct value. |
| 70 | + |
| 71 | +* Protect your server against errors by providing, and using, a function that |
| 72 | + returns a ``500 Internal Server Error`` response. |
| 73 | + |
| 74 | +* Instead of returning the python script in ``webroot`` as plain text, execute |
| 75 | + the file and return the results as HTML. |
| 76 | + |
| 77 | +If you choose to take on any of these optional tasks, try start by writing |
| 78 | +tests in tests.py that demostrate what the task should accomplish. Then write |
| 79 | +code that makes the tests pass. |
0 commit comments