<-
Apache > HTTP 伺服器 > 文件 > 版本 2.4 > 模組

Apache 模組 mod_ext_filter

可用的語言: en  |  fr  |  ja  |  ko 

說明在傳遞給用戶端之前,透過外部程式傳遞回應主體
狀態擴充功能
模組識別碼ext_filter_module
原始檔mod_ext_filter.c

摘要

mod_ext_filter過濾器 提供一個簡單又熟悉的程式設計模型。使用這個模組,讀取 stdin 並寫入 stdout 的程式 (例如,Unix 型式的過濾命令) 可以成為 Apache 的過濾器。這種過濾機制比使用專門寫給 Apache API 並在 Apache 伺服器程序內執行的過濾器還慢許多,但它有以下優點

即使效能特性不適用於正式營運使用,mod_ext_filter 仍然可用作過濾器的原型環境。

Support Apache!

主題

指令

錯誤修正清單

請參閱

top

範例

從其他類型的回應產生 HTML

# mod_ext_filter directive to define a filter
# to HTML-ize text/c files using the external
# program /usr/bin/enscript, with the type of
# the result set to text/html
ExtFilterDefine c-to-html mode=output \
    intype=text/c outtype=text/html \
    cmd="/usr/bin/enscript --color -w html -Ec -o -"

<Directory "/export/home/trawick/apacheinst/htdocs/c">
    # core directive to cause the new filter to
    # be run on output
    SetOutputFilter c-to-html
    
    # mod_mime directive to set the type of .c
    # files to text/c
    AddType text/c .c
</Directory>

實作內容編碼過濾器

注意: 這個 gzip 範例僅供說明用途。如需實用的實作方式,請參考 mod_deflate

# mod_ext_filter directive to define the external filter
ExtFilterDefine gzip mode=output cmd=/bin/gzip

<Location "/gzipped">
    
    # core directive to cause the gzip filter to be
    # run on output
    SetOutputFilter gzip
    
    # mod_headers directive to add
    # "Content-Encoding: gzip" header field
    Header set Content-Encoding gzip
</Location>

讓伺服器慢下來

# mod_ext_filter directive to define a filter
# which runs everything through cat; cat doesn't
# modify anything; it just introduces extra pathlength
# and consumes more resources
ExtFilterDefine slowdown mode=output cmd=/bin/cat \
    preservescontentlength

<Location "/">
    # core directive to cause the slowdown filter to
    # be run several times on output
    #
    SetOutputFilter slowdown;slowdown;slowdown
</Location>

使用 sed 取代回應中的文字

# mod_ext_filter directive to define a filter which
# replaces text in the response
#
ExtFilterDefine fixtext mode=output intype=text/html \
    cmd="/bin/sed s/verdana/arial/g"

<Location "/">
    # core directive to cause the fixtext filter to
    # be run on output
    SetOutputFilter fixtext
</Location>

使用 mod_substitute 也可以執行相同動作,而無需呼叫外部程序。

追蹤其他過濾器

# Trace the data read and written by mod_deflate
# for a particular client (IP 192.168.1.31)
# experiencing compression problems.
# This filter will trace what goes into mod_deflate.
ExtFilterDefine tracebefore \
    cmd="/bin/tracefilter.pl /tmp/tracebefore" \
    EnableEnv=trace_this_client

# This filter will trace what goes after mod_deflate.
# Note that without the ftype parameter, the default
# filter type of AP_FTYPE_RESOURCE would cause the
# filter to be placed *before* mod_deflate in the filter
# chain.  Giving it a numeric value slightly higher than
# AP_FTYPE_CONTENT_SET will ensure that it is placed
# after mod_deflate.
ExtFilterDefine traceafter \
    cmd="/bin/tracefilter.pl /tmp/traceafter" \
    EnableEnv=trace_this_client ftype=21

<Directory "/usr/local/docs">
    SetEnvIf Remote_Addr 192.168.1.31 trace_this_client
    SetOutputFilter tracebefore;deflate;traceafter
</Directory>

以下是追蹤資料的過濾器

#!/usr/local/bin/perl -w
use strict;

open(SAVE, ">$ARGV[0]")
    or die "can't open $ARGV[0]: $?";

while (<STDIN>) {
    print SAVE $_;
    print $_;
}

close(SAVE);
top

ExtFilterDefine 指令

說明定義外部過濾器
語法ExtFilterDefine 過濾器名稱 參數
內容伺服器設定
狀態擴充功能
模組mod_ext_filter

ExtFilterDefine 指令定義外部過濾器的特性,包括要執行的程式及其參數。

過濾器名稱 指定要定義的過濾器名稱。此名稱隨後可在 SetOutputFilter 指令中使用。它必須在所有已註冊的過濾器中是唯一的。目前,註冊過濾器 API 沒有回報錯誤,因此系統不會將重複名稱的問題回報給使用者。

後續參數可以以任何順序出現並定義要執行的外部指令和某些其他特性。唯一必需參數為 cmd=。這些參數為

cmd=cmd
cmd= 關鍵字讓您可以指定要執行的外部指令。如果程式名稱後有參數,指令列應以引號包圍(例如 cmd="/bin/mypgm arg1 arg2")。無需一般 shell 引號,因為程式是透過繞過 shell 直接執行的。程式參數以空白分隔。反斜線可用來跳脫空白,以成為程式參數的一部分。屬於參數一部分的任何反斜線自身都必須以反斜線跳脫。除了標準 CGI 環境變數以外,DOCUMENT_URI、DOCUMENT_PATH_INFO 和 QUERY_STRING_UNESCAPED 也會為程式設定。
mode=模式
對處理回應的過濾器使用 mode=output(預設)。對處理請求的過濾器使用 mode=input。Apache 2.1 和更新版本支援 mode=input
intype=imt
此參數指定應過濾的文件的網際網路媒體類型( MIME 類型)。預設情況下,所有文件都會經過過濾。如果指定 intype=,過濾器將對其他類型的文件停用。
outtype=imt
此參數指定過濾文件的網際網路媒體類型( MIME 類型)。當過濾器作為過濾操作的一部分變更網際網路媒體類型時,這項功能很有用。預設情況下,網際網路媒體類型不變。
PreservesContentLength
PreservesContentLength 關鍵字指定過濾器會保留內容長度。這不是預設設定,因為大多數過濾器會變更內容長度。如果過濾器未修改長度,應指定此關鍵字。
ftype=過濾器類型
此參數指定過濾器應註冊為的過濾器類型的數值。在多數情況下,預設值 AP_FTYPE_RESOURCE 已足夠使用。如果過濾器需要在過濾器鏈中的不同點運作而非資源過濾器,則需要此參數。參閱 util_filter.h 中的 AP_FTYPE_foo 定義以取得合適的數值。
disableenv=env
此參數指定環境變數的名稱,此變數預設為關閉篩選。
enableenv=env
此參數指定環境變數的名稱,此變數為必設,否則篩選將會被關閉。
top

ExtFilterOptions 指令

說明設定 mod_ext_filter 選項
語法ExtFilterOptions 選項 [選項] ...
預設ExtFilterOptions NoLogStderr
內容目錄
狀態擴充功能
模組mod_ext_filter

ExtFilterOptions 指令為 mod_ext_filter 指定特殊的處理選項。 選項 可以為:

LogStderr | NoLogStderr
LogStderr 關鍵字指定由外部篩選程式寫入標準錯誤的訊息,會儲存在 Apache 錯誤日誌中。 NoLogStderr 會停用此功能。
Onfail=[abort|remove]
決定在無法啟動外部篩選程式時,該如何進行。使用 abort(預設值),請求會被終止。使用 remove,會移除篩選,而請求會繼續執行而不使用篩選。
ExtFilterOptions LogStderr

寫入至篩選標準錯誤的訊息,會儲存在 Apache 錯誤日誌中。

可用的語言: en  |  fr  |  ja  |  ko 

top

意見

注意
這不是問答區。張貼於此的意見應以改善文件或伺服器為建議,而且如果已執行或被認為無效/與主題無關,我們的管理員可能會將其移除。有關如何管理 Apache HTTP 伺服器的問題,應轉至我們的 IRC 頻道 #httpd(位於 Libera.chat),或寄送至我們的 郵寄清單