@@ -704,13 +704,6 @@ def main(environ, start_response, sp):
704
704
)
705
705
body .append ("<br><a href='/logout'>logout</a>" )
706
706
707
- body = [
708
- item
709
- if isinstance (item , six .binary_type )
710
- else item .encode ("utf-8" )
711
- for item in body
712
- ]
713
-
714
707
resp = Response (body )
715
708
return resp (environ , start_response )
716
709
@@ -882,6 +875,28 @@ def application(environ, start_response):
882
875
return resp (environ , start_response )
883
876
884
877
878
+ class ToBytesMiddleware (object ):
879
+ """Converts a message to bytes to be sent by WSGI server."""
880
+
881
+ def __init__ (self , app ):
882
+ self .app = app
883
+
884
+ def __call__ (self , environ , start_response ):
885
+ data = self .app (environ , start_response )
886
+
887
+ if isinstance (data , list ):
888
+ return (
889
+ d
890
+ if isinstance (d , bytes )
891
+ else d .encode ("utf-8" )
892
+ for d in data
893
+ )
894
+ elif isinstance (data , str ):
895
+ return data .encode ("utf-8" )
896
+
897
+ return data
898
+
899
+
885
900
if __name__ == "__main__" :
886
901
try :
887
902
from cheroot .wsgi import Server as WSGIServer
@@ -966,7 +981,7 @@ def application(environ, start_response):
966
981
pass
967
982
ds .DefaultSignature (sign_alg , digest_alg )
968
983
969
- SRV = WSGIServer ((HOST , PORT ), application )
984
+ SRV = WSGIServer ((HOST , PORT ), ToBytesMiddleware ( application ) )
970
985
971
986
_https = ""
972
987
if service_conf .HTTPS :
0 commit comments