Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions conteroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,22 @@ def index(method, get, post, headers):
manager.save(message)

return status, body


def add(method, get, post, headers):
a = get_first_element(get, 'a', '')
b = get_first_element(get, 'b', '')
print(a, b)
status = '200 OK'
summ = int(a) + int(b)
body = b'<p>' + str(summ).encode() + b'</p>'
return status, body


# def static(method, get, post, headers):
# headers[0] = ('Content-type', 'image/png; charset=utf-8')
# status = '200 OK'
# f = open('data/logo.png', 'rb')
# body = f.read()
# f.close()
# return status, body
Binary file added data/Audi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/BMW_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
</head>
<body>
<img src="/static/logo.png" alt="logo"/> <hr>
<img src="/static/BMW_logo.png" alt="logo"/> <hr>
<img src="/static/Audi.jpg" alt="audi"/> <hr>
{{messages}}
<hr>
<form action="/" method="post" enctype="application/x-www-form-urlencoded">
Expand Down
42 changes: 34 additions & 8 deletions wsgi.py
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, \
Expand All @@ -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
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug code!

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'
Copy link
Owner

Choose a reason for hiding this comment

The 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 body = f.read() file. I think this is the wrong behavior.

status = '200 OK'
body = f.read()
Copy link
Owner

Choose a reason for hiding this comment

The 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]