Commit 489f9d57 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

wip

parent 187b19df
import os
import sys
import argparse
from gevent import pywsgi, threadpool
from tempfile import TemporaryFile
from io import BytesIO
import socket
import Zope2
from Zope2.Startup.run import make_wsgi_app
import ZConfig
from Products.ERP5Type.patches.WSGIPublisher import publish_module
def app_wrapper(large_file_threshold):
def app(environ, start_response):
original_wsgi_input = environ['wsgi.input']
if not hasattr(original_wsgi_input, 'seek'):
# Convert environ['wsgi.input'] to a file-like object.
cl = environ.get('CONTENT_LENGTH')
cl = int(cl) if cl else 0
if cl > large_file_threshold:
new_wsgi_input = environ['wsgi.input'] = TemporaryFile('w+b')
else:
new_wsgi_input = environ['wsgi.input'] = BytesIO()
rest = cl
chunksize = 1<<20
try:
while rest:
if rest <= chunksize:
chunk = original_wsgi_input.read(rest)
rest = 0
else:
chunk = original_wsgi_input.read(chunksize)
rest = rest - chunksize
new_wsgi_input.write(chunk)
except (socket.error, IOError):
msg = b'Not enough data in request or socket error'
start_response('400 Bad Request', [
('Content-Type', 'text/plain'),
('Content-Length', str(len(msg))),
]
)
return [msg]
new_wsgi_input.seek(0)
return publish_module(environ, start_response)
return app
def runwsgi():
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--webdav', action='store_true')
parser.add_argument('address', help='<ip>:<port>')
parser.add_argument('zope_conf', help='path to zope.conf')
args = parser.parse_args()
# We read some "old" values used by the Medusa HTTP Server in order to reuse
# them for the WSGI mode.
startup = os.path.dirname(Zope2.Startup.__file__)
schema = ZConfig.loadSchema(os.path.join(startup, 'zopeschema.xml'))
conf, _ = ZConfig.loadConfig(schema, args.zope_conf)
make_wsgi_app({}, zope_conf=args.zope_conf)
pywsgi.WSGIServer(
listener=args.address,
application=app_wrapper(conf.large_file_threshold),
spawn=threadpool.ThreadPool(conf.zserver_threads),
log=conf.access(), # Z2.log
).serve_forever()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment