2009年7月3日 星期五

Apache Filter Module過濾器模組

從應用模組程式開發的角度來看,Apache 2.x的最重要改進與創新就是過濾器(filter)的架構,過濾器使得處理不同的資料可以隨心所欲的運作。

從Apache 2的架構來看,可以分為兩個正交軸,一為處理軸(Processing Axis),另一為資料軸(Data Axis)。每一次的請求皆由處理軸來接受,其中會經過許許多多的掛勾(hook),於是進行一些處理(handler)的呼叫。

過濾器依據過濾資料的處理階段有這幾種不同類型:
  • Content Filter:在接受請求中,用來處理文件的資料。一般來說我們都式設計這類的過濾器居多,例如過濾HTML中特定的文字。
  • Protocol Filter:用來處理通訊協定(protocol)的資料。在Apache中,Protocol Filter用在將HTTP通訊協定轉換request_rec的資料結構。
  • Connection Filter:處理TCP連線的資料。用來處理conn_rec的資料結構,跟HTTP和request_recr皆無關係,你可以看成是操作在傳輸層(Transport Layer)的部分,而Content Filter和Protocol Filter是操作在應用層(Application Layer)。
過濾器依據放置的位置則有這幾種不同類型:
  • Input Filter:處理來自client請求(request)的資料,故稱為輸入過濾器(Input Filter)。
  • Output Filter:處理將回應(response)給client的資料,故稱為輸出過濾器(Output Filter)。
此外,過濾器也有點像掛勾的樣子,有著另一種資料型態來處理過濾器之間的傳遞,是要依靠ap_filter_t的資料結構



/**
* @brief The representation of a filter chain.
*
* Each request has a list
* of these structures which are called in turn to filter the data. Sub
* requests get an exact copy of the main requests filter chain.
*/
struct ap_filter_t {
/** The internal representation of this filter. This includes
* the filter's name, type, and the actual function pointer.
*/
ap_filter_rec_t *frec;

/** A place to store any data associated with the current filter */
void *ctx;

/** The next filter in the chain */
ap_filter_t *next;

/** The request_rec associated with the current filter. If a sub-request
* adds filters, then the sub-request is the request associated with the
* filter.
*/
request_rec *r;

/** The conn_rec associated with the current filter. This is analogous
* to the request_rec, except that it is used for input filtering.
*/
conn_rec *c;
};

如果需要在多個過濾器之間傳送資料,你就需要用ctx這個資料欄位。