OWIN源碼,Katana project
katana開源許久,網上仍未搜索到對其源碼的閱讀總結,本人在工作中正好遇到數據處理流程框架設計,想來跟伺服器處理request和response差不多,遂起了閱讀katana源碼,並借鑒其設計的想法,磕磕碰碰,困難重重,所幸有一些收穫,與大家交流交流。
katana源碼 https://katanaproject.codeplex.com/
owin官網 http://owin.org/
兩個最重要的數據結構
1 Environment
IDictionary<string, object>
官方解釋:
This data structure is responsible for storing all of the state necessary for processing an HTTP request and response, as well as any relevant server state. An OWIN-compatible Web server is responsible for populating the environment dictionary with data such as the body streams and header collections for an HTTP request and response. It is then the responsibility of the application or framework components to populate or update the dictionary with additional values and write to the response body stream.
Environment是在pipeline中流動的數據,代表著一個具體的request和response,後文會介紹在每個pipeline stage中會對這個dictionary中自己關心的數據進行處理,併在進入下一個stage的時候丟棄引用,採用的是原子操作,因而每個Environment只存在一個pipeline stage中。數據舉例:
Key Name |
Value Description |
"owin.RequestBody" |
A Stream with the request body, if any. Stream.Null MAY be used as a placeholder if there is no request body. See Request Body. |
"owin.RequestHeaders" |
An IDictionary<string, string[]> of request headers. See Headers. |
"owin.RequestMethod" |
A string containing the HTTP request method of the request (e.g., "GET", "POST"). |
"owin.RequestPath" |
A string containing the request path. The path MUST be relative to the "root" of the application delegate; see Paths. |
"owin.RequestPathBase" |
A string containing the portion of the request path corresponding to the "root" of the application delegate; see Paths. |
"owin.RequestProtocol" |
A string containing the protocol name and version (e.g. "HTTP/1.0" or "HTTP/1.1"). |
"owin.RequestQueryString" |
A string containing the query string component of the HTTP request URI, without the leading “?” (e.g., "foo=bar&baz=quux"). The value may be an empty string. |
"owin.RequestScheme" |
A string containing the URI scheme used for the request (e.g., "http", "https"); see URI Scheme. |
2 AppFunc
Func<IDictionary<string, object>, Task>;
官方解釋:
The second key element of OWIN is the application delegate. This is a function signature which serves as the primary interface between all components in an OWIN application. The definition for the application delegate is as follows:
The application delegate then is simply an implementation of the Func delegate type where the function accepts the environment dictionary as input and returns a Task. This design has several implications for developers:
- There are a very small number of type dependencies required in order to write OWIN components. This greatly increases the accessibility of OWIN to developers.
- The asynchronous design enables the abstraction to be efficient with its handling of computing resources, particularly in more I/O intensive operations.
- Because the application delegate is an atomic unit of execution and because the environment dictionary is carried as a parameter on the delegate, OWIN components can be easily chained together to create complex HTTP processing pipelines.
這就是middleware,也是每個pipeline stage中具體的處理方法,採用非同步調用的方式,由StartUp類進行註冊,並生成一條鏈,實際上就是壓進一個List中。
源碼閱讀,能學到很多東西,肯定有很多理解有偏差的地方,歡迎指正,我將從一個具體的middleware註冊和StartUp的執行切入,大致勾勒一個pipeline的構造和流動過程。