Apache HTTP 伺服器版本 2.4
說明 | 在傳遞給用戶端之前,透過外部程式傳遞回應主體 |
---|---|
狀態 | 擴充功能 |
模組識別碼 | ext_filter_module |
原始檔 | mod_ext_filter.c |
mod_ext_filter
為 過濾器 提供一個簡單又熟悉的程式設計模型。使用這個模組,讀取 stdin 並寫入 stdout 的程式 (例如,Unix 型式的過濾命令) 可以成為 Apache 的過濾器。這種過濾機制比使用專門寫給 Apache API 並在 Apache 伺服器程序內執行的過濾器還慢許多,但它有以下優點
即使效能特性不適用於正式營運使用,mod_ext_filter
仍然可用作過濾器的原型環境。
# 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>
# 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);
說明 | 定義外部過濾器 |
---|---|
語法 | 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
intype=
,過濾器將對其他類型的文件停用。outtype=imt
PreservesContentLength
PreservesContentLength
關鍵字指定過濾器會保留內容長度。這不是預設設定,因為大多數過濾器會變更內容長度。如果過濾器未修改長度,應指定此關鍵字。ftype=過濾器類型
disableenv=env
enableenv=env
說明 | 設定 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 錯誤日誌中。