6.flask 源碼解析:響應(yīng)
response 簡(jiǎn)介
在 flask 應(yīng)用中剩辟,我們只需要編寫(xiě) view 函數(shù)端铛,并不需要直接和響應(yīng)(response)打交道鲸阻,flask 會(huì)自動(dòng)生成響應(yīng)返回給客戶端六荒。
The return value from a view function is automatically converted into a response object for you.
—— Flask docs
我們知道 HTTP 響應(yīng)分為三個(gè)部分:
狀態(tài)欄(HTTP 版本养距、狀態(tài)碼和說(shuō)明)、頭部(以冒號(hào)隔開(kāi)的字符對(duì)汇荐,用于各種控制和協(xié)商)洞就、body(服務(wù)端返回的數(shù)據(jù))。比如下面訪問(wèn)一個(gè)地址的響應(yīng):
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: max-age=600
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Wed, 15 Feb 2017 07:50:41 GMT
Expires: Wed, 15 Feb 2017 08:00:41 GMT
Last-Modified: Wed, 15 Feb 2017 07:46:56 GMT
Server: GitHub.com
Transfer-Encoding: chunked
X-GitHub-Request-Id: D2A7:7B6B:33C0628:47C44B9:58A40851
<BODY>
flask 自然也會(huì)提供所有這些數(shù)據(jù)的操作掀淘,視圖函數(shù)就支持返回三個(gè)值:第一個(gè)是返回的數(shù)據(jù)旬蟋,第二個(gè)是狀態(tài)碼,第三個(gè)是頭部字典革娄。比如:
@app.route('/')
def hello_world():
return 'Hello, World!', 201, {'X-Foo': 'bar'}
這篇文章就講講這背后的魔法倾贰。
flask 響應(yīng)(response)
在 flask 源碼解析:應(yīng)用啟動(dòng)流程 的最后,我們講到 full_dispatch_request
在調(diào)用路由的視圖函數(shù)之后拦惋,會(huì)調(diào)用 finalize_request
進(jìn)行最后的處理匆浙,在這個(gè)方法里就包含了 response 對(duì)象的生成和處理邏輯。
finalize_request
的代碼如下:
def finalize_request(self, rv, from_error_handler=False):
"""Given the return value from a view function this finalizes
the request by converting it into a response and invoking the
postprocessing functions. This is invoked for both normal
request dispatching as well as error handlers.
"""
response = self.make_response(rv)
try:
response = self.process_response(response)
request_finished.send(self, response=response)
except Exception:
if not from_error_handler:
raise
self.logger.exception('Request finalizing failed with an '
'error while handling an error')
return response
里面有兩個(gè)方法調(diào)用:make_response
根據(jù)視圖函數(shù)的返回值生成 response 對(duì)象厕妖,process_response
對(duì) response 做一些后續(xù)的處理(比如執(zhí)行 hooks 函數(shù))首尼。我們先來(lái)看看 make_response
:
def make_response(self, rv):
"""Converts the return value from a view function to a real
response object that is an instance of :attr:`response_class`.
"""
status_or_headers = headers = None
if isinstance(rv, tuple):
rv, status_or_headers, headers = rv + (None,) * (3 - len(rv))
if isinstance(status_or_headers, (dict, list)):
headers, status_or_headers = status_or_headers, None
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)):
rv = self.response_class(rv, headers=headers,
status=status_or_headers)
headers = status_or_headers = None
if status_or_headers is not None:
if isinstance(status_or_headers, string_types):
rv.status = status_or_headers
else:
rv.status_code = status_or_headers
if headers:
rv.headers.extend(headers)
return rv
make_response
是視圖函數(shù)能返回多個(gè)不同數(shù)量和類型值的關(guān)鍵,因?yàn)樗芴幚磉@些情況,統(tǒng)一把它們轉(zhuǎn)換成 response软能。
如果返回值本身就是 Response 實(shí)例迎捺,就直接使用它;如果返回值是字符串類型查排,就把它作為響應(yīng)的 body凳枝,并自動(dòng)設(shè)置狀態(tài)碼和頭部信息;
如果返回值是 tuple雹嗦,會(huì)嘗試用 (response, status, headers) 或者 (response, headers) 去解析。
NOTE:因?yàn)橐晥D函數(shù)可以返回 Response
對(duì)象合是,因此我們可以直接操作 Response
了罪。
不管視圖函數(shù)返回的是什么,最終都會(huì)變成 Response
對(duì)象聪全,那么我們就來(lái)看看 Response
的定義:
from werkzeug.wrappers import Response as ResponseBase
class Response(ResponseBase):
"""The response object that is used by default in Flask. Works like the
response object from Werkzeug but is set to have an HTML mimetype by
default. Quite often you don't have to create this object yourself because
:meth:`~flask.Flask.make_response` will take care of that for you.
If you want to replace the response object used you can subclass this and
set :attr:`~flask.Flask.response_class` to your subclass.
"""
default_mimetype = 'text/html'
Flask 的 Response
類非常簡(jiǎn)單泊藕,它只是繼承了 werkzeug.wrappers:Response
,然后設(shè)置默認(rèn)返回類型為 html难礼。
不過(guò)從注釋中娃圆,我們得到兩條很有用的信息:
- 一般情況下不要直接操作
Response
對(duì)象,而是使用make_response
方法來(lái)生成它 - 如果需要使用自定義的響應(yīng)對(duì)象蛾茉,可以覆蓋 flask app 對(duì)象的
response_class
屬性讼呢。
繼續(xù),下面就要分析 werkzeug 對(duì)應(yīng)的代碼了谦炬。
werkzeug response
werkzeug 實(shí)現(xiàn)的 response 定義在 werkzeug/wrappers.py
文件中:
class Response(BaseResponse, ETagResponseMixin, ResponseStreamMixin,
CommonResponseDescriptorsMixin,
WWWAuthenticateMixin):
"""Full featured response object implementing the following mixins:
- :class:`ETagResponseMixin` for etag and cache control handling
- :class:`ResponseStreamMixin` to add support for the `stream` property
- :class:`CommonResponseDescriptorsMixin` for various HTTP descriptors
- :class:`WWWAuthenticateMixin` for HTTP authentication support
"""
和我們?cè)?flask 請(qǐng)求分析的 Request 類一樣悦屏,這里使用了 Mixin 機(jī)制。BaseResponse
精簡(jiǎn)后的大概框架如下:
class BaseResponse(object):
"""Base response class. The most important fact about a response object
is that it's a regular WSGI application. It's initialized with a couple
of response parameters (headers, body, status code etc.) and will start a
valid WSGI response when called with the environ and start response
callable.
"""
charset = 'utf-8'
default_status = 200
default_mimetype = 'text/plain'
automatically_set_content_length = True
def __init__(self, response=None, status=None, headers=None,
mimetype=None, content_type=None, direct_passthrough=False):
pass
BaseResponse
有一些類屬性键思,定義了默認(rèn)的值础爬,比如默認(rèn)字符編碼是 utf-8,默認(rèn)狀態(tài)碼是 200 等吼鳞。實(shí)例化的時(shí)候接受的參數(shù)有:
- response: 字符串或者其他 iterable 對(duì)象看蚜,作為響應(yīng)的 body
- status: 狀態(tài)碼,可以是整數(shù)赔桌,也可以是字符串
- headers: 響應(yīng)的頭部供炎,可以是個(gè)列表,也可以是
werkzeug.datastructures.Headers
對(duì)象 - mimetype: mimetype 類型疾党,告訴客戶端響應(yīng) body 的格式碱茁,默認(rèn)是文本格式
- content_type: 響應(yīng)頭部的
Content-Type
內(nèi)容
所有這些參數(shù)都是可選的,默認(rèn)情況下會(huì)生成一個(gè)狀態(tài)碼為 200仿贬,沒(méi)有任何 body 的響應(yīng)纽竣。status、status_code 作為 Response
的屬性,可以直接讀取和修改蜓氨。body 數(shù)據(jù)在內(nèi)部保存為 iterable 的類型聋袋,
但是對(duì)外也提供了直接讀寫(xiě)的接口 self.data
:
def get_data(self, as_text=False):
"""The string representation of the request body. Whenever you call
this property the request iterable is encoded and flattened.
"""
self._ensure_sequence()
rv = b''.join(self.iter_encoded())
if as_text:
rv = rv.decode(self.charset)
return rv
def set_data(self, value):
"""Sets a new string as response. The value set must either by a
unicode or bytestring.
"""
if isinstance(value, text_type):
value = value.encode(self.charset)
else:
value = bytes(value)
self.response = [value]
if self.automatically_set_content_length:
self.headers['Content-Length'] = str(len(value))
data = property(get_data, set_data, doc='''
A descriptor that calls :meth:`get_data` and :meth:`set_data`. This
should not be used and will eventually get deprecated.
''')
body 字符的編碼和長(zhǎng)度都是自動(dòng)設(shè)置的,用戶不需要手動(dòng)處理穴吹。
至于頭部的存儲(chǔ)幽勒,werkzeug 使用的是類似于字典的 werkzeug.datastructures:Headers
類。在flask 源碼解析:請(qǐng)求這篇文章中港令,我們沒(méi)有詳細(xì)
解釋頭部的存儲(chǔ)啥容,那么這篇文章就具體分析一下吧。
Headers
這個(gè)類的提供了很多和字典相同的接口:keys顷霹、values咪惠、iterms,但是和字典的區(qū)別在于它保存的值是有序的淋淀,而且允許相同 key 的值存在遥昧。
為什么這么設(shè)計(jì)呢?因?yàn)橹?HTTP 頭部的特性朵纷。先來(lái)看看有序炭臭,在 HTTP 傳送的過(guò)程中,如果頭部各個(gè) key-value 鍵值對(duì)順序發(fā)生變化袍辞,有些代理或者客戶端等組件會(huì)認(rèn)為請(qǐng)求被篡改而丟棄或者拒絕請(qǐng)求的處理鞋仍,所以最好把頭部設(shè)置為有序的,用戶按照什么順序設(shè)置的搅吁,就按照什么順序存儲(chǔ)凿试;再說(shuō)說(shuō)相同 key 的問(wèn)題,這是因?yàn)?HTTP 頭部同一個(gè) key 可能有多個(gè) value(比如 Accept似芝、SetCookie頭部)那婉。那么這個(gè)看起比較特殊的字典是怎么實(shí)現(xiàn)的呢?來(lái)看代碼:
class Headers(object):
"""An object that stores some headers. It has a dict-like interface
but is ordered and can store the same keys multiple times.
"""
def __init__(self, defaults=None):
self._list = []
if defaults is not None:
if isinstance(defaults, (list, Headers)):
self._list.extend(defaults)
else:
self.extend(defaults)
def __getitem__(self, key, _get_mode=False):
if not _get_mode:
if isinstance(key, integer_types):
return self._list[key]
elif isinstance(key, slice):
return self.__class__(self._list[key])
if not isinstance(key, string_types):
raise exceptions.BadRequestKeyError(key)
ikey = key.lower()
for k, v in self._list:
if k.lower() == ikey:
return v
if _get_mode:
raise KeyError()
raise exceptions.BadRequestKeyError(key)
可以看到党瓮,頭部信息在內(nèi)部存儲(chǔ)為二元組構(gòu)成的列表详炬,這樣就能同時(shí)保證它的有序性和重復(fù)性。一個(gè)核心的方法是 __getitem__
寞奸,它定義了如何獲取頭部中的信息:
- 通過(guò)下標(biāo)
header[3]
呛谜,直接返回對(duì)應(yīng)未知存儲(chǔ)的鍵值對(duì)元組 - 通過(guò) key,返回 value
header['Accept']
枪萄,返回匹配的第一個(gè) value 值 - 通過(guò) slice
header[3:7]
隐岛,返回另外一個(gè)Headers
對(duì)象,保存了 slice 中所有的數(shù)據(jù)
然后實(shí)現(xiàn) keys()
瓷翻、items()
聚凹、pop()
割坠、setdefault()
等方法讓它表現(xiàn)出來(lái)字典的特性,除此之外還有 add()
妒牙、extend()
彼哼、add_header()
等和字典無(wú)關(guān)的方法方便操作。
自定義 response
如果需要擴(kuò)展 flask Response
的功能湘今,或者干脆把它替換掉敢朱,只要修改 flask app 的 response_class
屬性就可以了,比如:
from flask import Flask, Response
class MyResponse(Response):
pass
app = Flask(__name__)
app.response_class = MyResponse