-
Notifications
You must be signed in to change notification settings - Fork 26
Homework #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import wsgiref.validate | ||
|
|
||
| from conteroller import index | ||
| import mimetypes | ||
| import os.path | ||
|
|
||
| from conteroller import index, add | ||
| from router import Router | ||
| from utils import parse_http_x_www_form_urlencoded_post_data, \ | ||
| parse_http_get_data, parse_http_headers, \ | ||
|
|
@@ -11,8 +14,15 @@ | |
| STATIC_URL = '/static/' | ||
| STATIC_ROOT = 'data' | ||
|
|
||
| os.curdir | ||
| startdir = os.path.abspath(STATIC_ROOT) | ||
| # print(startdir) | ||
|
|
||
|
|
||
| router = Router() | ||
| router.register_controller('/', index) | ||
| router.register_controller('/add/', add) | ||
| # router.register_controller('/static/logo.png', logo) | ||
|
|
||
|
|
||
| @wsgiref.validate.validator | ||
|
|
@@ -35,14 +45,30 @@ def application(environ, start_response): | |
|
|
||
| if URI_PATH.startswith(STATIC_URL): | ||
| print('STATIC FILE DETECTED!') | ||
| print(URI_PATH) | ||
| file = str(URI_PATH).replace(STATIC_URL, "", 1) | ||
| requested_path = os.path.join(startdir, file) | ||
| requested_path = os.path.abspath(requested_path) | ||
| print(requested_path) | ||
| if not requested_path.startswith(startdir): | ||
| raise OSError("Hack") | ||
| headers[0] = ('Content-type', mimetypes.guess_type(URI_PATH, strict=True)[0]+'; charset=utf-8') | ||
| try: | ||
| f = open(requested_path, 'rb') | ||
| except FileExistsError: | ||
| status = '404 Not Found' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If FileExistsError be raised then you change status to 404, after that you change status to 200 and do |
||
| status = '200 OK' | ||
| body = f.read() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put all file data in memory. If file too large? |
||
| f.close() | ||
|
|
||
|
|
||
| if DEBUG: | ||
| print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n" | ||
| "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n" | ||
| "POST: {POST}\n" | ||
| "GET: {GET}\n" | ||
| ":HEADERS:\n{HEADERS}\n" | ||
| .format(**locals())) | ||
| # if DEBUG: | ||
| # print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n" | ||
| # "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n" | ||
| # "POST: {POST}\n" | ||
| # "GET: {GET}\n" | ||
| # ":HEADERS:\n{HEADERS}\n" | ||
| # .format(**locals())) | ||
|
|
||
| start_response(status, headers) | ||
| return [body] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug code!