@ -10,30 +10,30 @@
"""
"""
import os
import os
import sys
import sys
from threading import Lock
from datetime import timedelta
from datetime import timedelta
from itertools import chain
from functools import update_wrapper
from functools import update_wrapper
from itertools import chain
from threading import Lock
from werkzeug . datastructures import ImmutableDict
from werkzeug . datastructures import ImmutableDict , Headers
from werkzeug . routing import Map , Rule , RequestRedirect , BuildError
from werkzeug . exceptions import BadRequest , HTTPException , \
from werkzeug . exceptions import HTTPException , InternalServerError , \
InternalServerError , MethodNotAllowed , default_exceptions
MethodNotAllowed , BadRequest , default_exceptions
from werkzeug . routing import BuildError , Map , RequestRedirect , Rule
from . helpers import _PackageBoundObject , url_for , get_flashed_messages , \
from . import cli , json
locked_cached_property , _endpoint_from_view_func , find_package , \
from . _compat import integer_types , reraise , string_types , text_type
get_debug_flag
from . config import Config , ConfigAttribute
from . import json , cli
from . ctx import AppContext , RequestContext , _AppCtxGlobals
from . wrappers import Request , Response
from . globals import _request_ctx_stack , g , request , session
from . config import ConfigAttribute , Config
from . helpers import _PackageBoundObject , \
from . ctx import RequestContext , AppContext , _AppCtxGlobals
_endpoint_from_view_func , find_package , get_debug_flag , \
from . globals import _request_ctx_stack , request , session , g
get_flashed_messages , locked_cached_property , url_for
from . sessions import SecureCookieSessionInterface
from . sessions import SecureCookieSessionInterface
from . signals import appcontext_tearing_down , got_request_exception , \
request_finished , request_started , request_tearing_down
from . templating import DispatchingJinjaLoader , Environment , \
from . templating import DispatchingJinjaLoader , Environment , \
_default_template_ctx_processor
_default_template_ctx_processor
from . signals import request_started , request_finished , got_request_exception , \
from . wrappers import Request , Response
request_tearing_down , appcontext_tearing_down
from . _compat import reraise , string_types , text_type , integer_types
# a lock used for logger initialization
# a lock used for logger initialization
_logger_lock = Lock ( )
_logger_lock = Lock ( )
@ -1715,62 +1715,106 @@ class Flask(_PackageBoundObject):
return False
return False
def make_response ( self , rv ) :
def make_response ( self , rv ) :
""" Converts the return value from a view function to a real
""" Convert the return value from a view function to an instance of
response object that is an instance of : attr : ` response_class ` .
: attr : ` response_class ` .
The following types are allowed for ` rv ` :
: param rv : the return value from the view function . The view function
must return a response . Returning ` ` None ` ` , or the view ending
. . tabularcolumns : : | p { 3.5 cm } | p { 9.5 cm } |
without returning , is not allowed . The following types are allowed
for ` ` view_rv ` ` :
== == == == == == == == == == == = == == == == == == == == == == == == == == == == == == == == == =
: attr : ` response_class ` the object is returned unchanged
` ` str ` ` ( ` ` unicode ` ` in Python 2 )
: class : ` str ` a response object is created with the
A response object is created with the string encoded to UTF - 8
string as body
as the body .
: class : ` unicode ` a response object is created with the
string encoded to utf - 8 as body
` ` bytes ` ` ( ` ` str ` ` in Python 2 )
a WSGI function the function is called as WSGI application
A response object is created with the bytes as the body .
and buffered as response object
: class : ` tuple ` A tuple in the form ` ` ( response , status ,
` ` tuple ` `
headers ) ` ` or ` ` ( response , headers ) ` `
Either ` ` ( body , status , headers ) ` ` , ` ` ( body , status ) ` ` , or
where ` response ` is any of the
` ` ( body , headers ) ` ` , where ` ` body ` ` is any of the other types
types defined here , ` status ` is a string
allowed here , ` ` status ` ` is a string or an integer , and
or an integer and ` headers ` is a list or
` ` headers ` ` is a dictionary or a list of ` ` ( key , value ) ` `
a dictionary with header values .
tuples . If ` ` body ` ` is a : attr : ` response_class ` instance ,
== == == == == == == == == == == = == == == == == == == == == == == == == == == == == == == == == =
` ` status ` ` overwrites the exiting value and ` ` headers ` ` are
extended .
: param rv : the return value from the view function
: attr : ` response_class `
The object is returned unchanged .
other : class : ` ~ werkzeug . wrappers . Response ` class
The object is coerced to : attr : ` response_class ` .
: func : ` callable `
The function is called as a WSGI application . The result is
used to create a response object .
. . versionchanged : : 0.9
. . versionchanged : : 0.9
Previously a tuple was interpreted as the arguments for the
Previously a tuple was interpreted as the arguments for the
response object .
response object .
"""
"""
status_or_headers = headers = None
if isinstance ( rv , tuple ) :
rv , status_or_headers , headers = rv + ( None , ) * ( 3 - len ( rv ) )
if rv is None :
status = headers = None
raise ValueError ( ' View function did not return a response ' )
# unpack tuple returns
if isinstance ( rv , ( tuple , list ) ) :
len_rv = len ( rv )
if isinstance ( status_or_headers , ( dict , list ) ) :
# a 3-tuple is unpacked directly
headers , status_or_headers = status_or_headers , None
if len_rv == 3 :
rv , status , headers = rv
# decide if a 2-tuple has status or headers
elif len_rv == 2 :
if isinstance ( rv [ 1 ] , ( Headers , dict , tuple , list ) ) :
rv , headers = rv
else :
rv , status = rv
# other sized tuples are not allowed
else :
raise TypeError (
' The view function did not return a valid response tuple. '
' The tuple must have the form (body, status, headers), '
' (body, status), or (body, headers). '
)
# the body must not be None
if rv is None :
raise TypeError (
' The view function did not return a valid response. The '
' function either returned None or ended without a return '
' statement. '
)
# make sure the body is an instance of the response class
if not isinstance ( rv , self . response_class ) :
if not isinstance ( rv , self . response_class ) :
# When we create a response object directly, we let the constructor
# set the headers and status. We do this because there can be
# some extra logic involved when creating these objects with
# specific values (like default content type selection).
if isinstance ( rv , ( text_type , bytes , bytearray ) ) :
if isinstance ( rv , ( text_type , bytes , bytearray ) ) :
rv = self . response_class ( rv , headers = headers ,
# let the response class set the status and headers instead of
status = status_or_headers )
# waiting to do it manually, so that the class can handle any
headers = status_or_headers = None
# special logic
rv = self . response_class ( rv , status = status , headers = headers )
status = headers = None
else :
else :
rv = self . response_class . force_type ( rv , request . environ )
# evaluate a WSGI callable, or coerce a different response
# class to the correct type
if status_or_headers is not None :
try :
if isinstance ( status_or_headers , string_types ) :
rv = self . response_class . force_type ( rv , request . environ )
rv . status = status_or_headers
except TypeError as e :
new_error = TypeError (
' {e} \n The view function did not return a valid '
' response. The return type must be a string, tuple, '
' Response instance, or WSGI callable, but it was a '
' {rv.__class__.__name__} . ' . format ( e = e , rv = rv )
)
reraise ( TypeError , new_error , sys . exc_info ( ) [ 2 ] )
# prefer the status if it was provided
if status is not None :
if isinstance ( status , ( text_type , bytes , bytearray ) ) :
rv . status = status
else :
else :
rv . status_code = status_or_headers
rv . status_code = status
# extend existing headers with provided headers
if headers :
if headers :
rv . headers . extend ( headers )
rv . headers . extend ( headers )