xref: /unit/test/python/upload/wsgi.py (revision 1971:3410f9d2a662)
1*1971Szelenkov@nginx.comimport cgi
21356St.nateldemoura@f5.comfrom tempfile import TemporaryFile
31356St.nateldemoura@f5.com
41848Szelenkov@nginx.com
51356St.nateldemoura@f5.comdef read(environ):
61356St.nateldemoura@f5.com    length = int(environ.get('CONTENT_LENGTH', 0))
71356St.nateldemoura@f5.com
81356St.nateldemoura@f5.com    body = TemporaryFile(mode='w+b')
91356St.nateldemoura@f5.com    body.write(bytes(environ['wsgi.input'].read(length)))
101356St.nateldemoura@f5.com    body.seek(0)
111356St.nateldemoura@f5.com
121356St.nateldemoura@f5.com    environ['wsgi.input'] = body
131356St.nateldemoura@f5.com    return body
141356St.nateldemoura@f5.com
151848Szelenkov@nginx.com
161356St.nateldemoura@f5.comdef application(environ, start_response):
171356St.nateldemoura@f5.com    file = read(environ)
181356St.nateldemoura@f5.com
191356St.nateldemoura@f5.com    form = cgi.FieldStorage(fp=file, environ=environ, keep_blank_values=True)
201356St.nateldemoura@f5.com
211356St.nateldemoura@f5.com    filename = form['file'].filename
221356St.nateldemoura@f5.com    data = filename.encode() + form['file'].file.read()
231356St.nateldemoura@f5.com
241848Szelenkov@nginx.com    start_response(
251848Szelenkov@nginx.com        '200 OK',
261848Szelenkov@nginx.com        [('Content-Type', 'text/plain'), ('Content-Length', str(len(data)))],
271848Szelenkov@nginx.com    )
281356St.nateldemoura@f5.com
291356St.nateldemoura@f5.com    return data
30