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
7 changes: 7 additions & 0 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<input type="text" name="name" style="width: 100%" placeholder="Name" /> <br>
<textarea name="message" rows="10" style="width: 100%" placeholder="Message"></textarea> <br>
<input type="submit"/>


</form>
<form enctype="multipart/form-data" action="/upload" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="file" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
55 changes: 51 additions & 4 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import wsgiref.validate
from tempfile import TemporaryFile
import cgi
import os

from utils import parse_http_x_www_form_urlencoded_post_data, \
get_first_element, parse_http_get_data, parse_http_headers, \
Expand All @@ -12,7 +15,21 @@
b'Name: user<br>Message: hi!',
b'Name: user<br>Message: hi!',
]
#import wsgiref.util
#wsgiref.util.FileWrapper().filelike()

def read(environ):
length = int(environ.get('CONTENT_LENGTH', 0))
stream = environ['wsgi.input']
body = TemporaryFile(mode='w+b')
while length > 0:
part = stream.read(min(length, 1024*200)) # 200KB buffer size
if not part: break
body.write(part)
length -= len(part)
body.seek(0)
environ['wsgi.input'] = body
return body

@wsgiref.validate.validator
def application(environ, start_response):
Expand All @@ -34,10 +51,7 @@ def application(environ, start_response):
status = '404 Not Found'
start_response(status, headers)
return [b'']

if URI_PATH.startswith(STATIC_URL):
print('STATIC FILE DETECTED!')

print(environ)
if DEBUG:
print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
"CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
Expand All @@ -46,6 +60,39 @@ def application(environ, start_response):
":HEADERS:\n{HEADERS}\n"
.format(**locals()))

if URI_PATH == "/upload":
status = '303 See Other'
headers.append(('Location', '/'))
body = read(environ)
form = cgi.FieldStorage(fp=body, environ=environ, keep_blank_values=True)
try:
fileitem = form['file']
except KeyError:
fileitem = None
if fileitem is not None and fileitem.file is not None:
fn = os.path.basename(fileitem.filename)
with open("data/" + fn, 'wb') as f:
data = fileitem.file.read(1024)
while data:
f.write(data)
data = fileitem.file.read(1024)

start_response(status, headers)
return [b'']

if URI_PATH.startswith(STATIC_URL):
path = URI_PATH.split(STATIC_URL)[-1]
Copy link
Owner

Choose a reason for hiding this comment

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

I have a file data/somelib/static/logo.png.

if "../" in path:
status = '404 Not Found'
start_response(status, headers)
return [b'']

with open(STATIC_ROOT + "/" + path, 'rb') as f:
start_response(status, [('Content-type', 'application/octet-stream; charset=utf-8')])
return [f.read()]



with open('main.html', 'rb') as f:
template_bytes = f.read()

Expand Down