📄 mod_include.erl
字号:
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %% $Id$%%-module(mod_include).-export([do/1,parse/2,config/6,include/6,echo/6,fsize/6,flastmod/6,exec/6]).-include("httpd.hrl").-define(VMODULE,"INCLUDE").%% dodo(Info) -> case Info#mod.method of "GET" -> case httpd_util:key1search(Info#mod.data,status) of %% A status code has been generated! {_StatusCode, _PhraseArgs, _Reason} -> {proceed,Info#mod.data}; %% No status code has been generated! undefined -> case httpd_util:key1search(Info#mod.data, response) of %% No response has been generated! undefined -> do_include(Info); %% A response has been generated or sent! _Response -> {proceed,Info#mod.data} end end; %% Not a GET method! _ -> {proceed,Info#mod.data} end.do_include(Info) -> Path = mod_alias:path(Info#mod.data,Info#mod.config_db, Info#mod.request_uri), Suffix = httpd_util:suffix(Path), case httpd_util:lookup_mime_default(Info#mod.config_db,Suffix) of "text/x-server-parsed-html" -> HeaderStart = [{content_type, "text/html"}], case send_in(Info, Path, HeaderStart, file:read_file_info(Path)) of {ok, ErrorLog, Size} -> {proceed, [{response, {already_sent, 200, Size}}, {mime_type, "text/html"} | lists:append(ErrorLog, Info#mod.data)]}; {error, Reason} -> {proceed, [{status,send_error(Reason,Info,Path)}|Info#mod.data]} end; _ -> %% Unknown mime type, ignore {proceed,Info#mod.data} end.%%%% config directive%%config(_Info, Context, ErrorLog, TagList, ValueList, R) -> case verify_tags("config",[errmsg,timefmt,sizefmt], TagList,ValueList) of ok -> {ok,update_context(TagList,ValueList,Context),ErrorLog,"",R}; {error,Reason} -> {ok,Context,[{internal_info,Reason}|ErrorLog], httpd_util:key1search(Context,errmsg,""),R} end.update_context([],[],Context) -> Context;update_context([Tag|R1],[Value|R2],Context) -> update_context(R1,R2,[{Tag,Value}|Context]).verify_tags(Command,ValidTags,TagList,ValueList) when length(TagList)==length(ValueList) -> verify_tags(Command, ValidTags, TagList);verify_tags(Command, _ValidTags, _TagList, _ValueList) -> {error, ?NICE(Command ++ " directive has spurious tags")}.verify_tags(_Command, _ValidTags, []) -> ok;verify_tags(Command, ValidTags, [Tag|Rest]) -> case lists:member(Tag, ValidTags) of true -> verify_tags(Command, ValidTags, Rest); false -> {error, ?NICE(Command++" directive has a spurious tag ("++ atom_to_list(Tag)++")")} end.%%%% include directive%%include(Info,Context,ErrorLog,[virtual],[VirtualPath],R) -> Aliases = httpd_util:multi_lookup(Info#mod.config_db,alias), {_, Path, _AfterPath} = mod_alias:real_name(Info#mod.config_db, VirtualPath, Aliases), include(Info,Context,ErrorLog,R,Path);include(Info, Context, ErrorLog, [file], [FileName], R) -> Path = file(Info#mod.config_db, Info#mod.request_uri, FileName), include(Info, Context, ErrorLog, R, Path);include(_Info, Context, ErrorLog, _TagList, _ValueList, R) -> {ok, Context, [{internal_info,?NICE("include directive has a spurious tag")}| ErrorLog], httpd_util:key1search(Context, errmsg, ""), R}.include(Info, Context, ErrorLog, R, Path) -> case file:read_file(Path) of {ok, Body} -> {ok, NewContext, NewErrorLog, Result} = parse(Info, binary_to_list(Body), Context, ErrorLog, []), {ok, NewContext, NewErrorLog, Result, R}; {error, _Reason} -> {ok, Context, [{internal_info, ?NICE("Can't open "++Path)}|ErrorLog], httpd_util:key1search(Context, errmsg, ""), R} end.file(ConfigDB, RequestURI, FileName) -> Aliases = httpd_util:multi_lookup(ConfigDB, alias), {_, Path, _AfterPath} = mod_alias:real_name(ConfigDB, RequestURI, Aliases), Pwd = filename:dirname(Path), filename:join(Pwd, FileName).%%%% echo directive%%echo(Info,Context,ErrorLog,[var],["DOCUMENT_NAME"],R) -> {ok,Context,ErrorLog,document_name(Info#mod.data,Info#mod.config_db, Info#mod.request_uri),R};echo(Info,Context,ErrorLog,[var],["DOCUMENT_URI"],R) -> {ok,Context,ErrorLog,document_uri(Info#mod.config_db, Info#mod.request_uri),R};echo(Info,Context,ErrorLog,[var],["QUERY_STRING_UNESCAPED"],R) -> {ok,Context,ErrorLog,query_string_unescaped(Info#mod.request_uri),R};echo(_Info,Context,ErrorLog,[var],["DATE_LOCAL"],R) -> {ok,Context,ErrorLog,date_local(),R};echo(_Info,Context,ErrorLog,[var],["DATE_GMT"],R) -> {ok,Context,ErrorLog,date_gmt(),R};echo(Info,Context,ErrorLog,[var],["LAST_MODIFIED"],R) -> {ok,Context,ErrorLog,last_modified(Info#mod.data,Info#mod.config_db, Info#mod.request_uri),R};echo(_Info, Context, ErrorLog, _TagList, _ValueList, R) -> {ok,Context, [{internal_info,?NICE("echo directive has a spurious tag")}| ErrorLog],"(none)",R}.document_name(Data,ConfigDB,RequestURI) -> Path = mod_alias:path(Data,ConfigDB,RequestURI), case regexp:match(Path,"[^/]*\$") of {match,Start,Length} -> string:substr(Path,Start,Length); nomatch -> "(none)" end.document_uri(ConfigDB, RequestURI) -> Aliases = httpd_util:multi_lookup(ConfigDB, alias), {_, Path, AfterPath} = mod_alias:real_name(ConfigDB, RequestURI, Aliases), VirtualPath = string:substr(RequestURI, 1, length(RequestURI)-length(AfterPath)), {match, Start, Length} = regexp:match(Path,"[^/]*\$"), FileName = string:substr(Path,Start,Length), case regexp:match(VirtualPath, FileName++"\$") of {match, _, _} -> httpd_util:decode_hex(VirtualPath)++AfterPath; nomatch -> string:strip(httpd_util:decode_hex(VirtualPath),right,$/)++ "/"++FileName++AfterPath end.query_string_unescaped(RequestURI) -> case regexp:match(RequestURI,"[\?].*\$") of {match,Start,Length} -> %% Escape all shell-special variables with \ escape(string:substr(RequestURI,Start+1,Length-1)); nomatch -> "(none)" end.escape([]) -> [];escape([$;|R]) -> [$\\,$;|escape(R)];escape([$&|R]) -> [$\\,$&|escape(R)];escape([$(|R]) -> [$\\,$(|escape(R)];escape([$)|R]) -> [$\\,$)|escape(R)];escape([$||R]) -> [$\\,$||escape(R)];escape([$^|R]) -> [$\\,$^|escape(R)];escape([$<|R]) -> [$\\,$<|escape(R)];escape([$>|R]) -> [$\\,$>|escape(R)];escape([$\n|R]) -> [$\\,$\n|escape(R)];escape([$ |R]) -> [$\\,$ |escape(R)];escape([$\t|R]) -> [$\\,$\t|escape(R)];escape([C|R]) -> [C|escape(R)].date_local() -> {{Year,Month,Day},{Hour,Minute,Second}}=calendar:local_time(), %% Time format hard-wired to: "%a %b %e %T %Y" according to strftime(3) io_lib:format("~s ~s ~2w ~2.2.0w:~2.2.0w:~2.2.0w ~w", [httpd_util:day(calendar:day_of_the_week(Year,Month,Day)), httpd_util:month(Month),Day,Hour,Minute,Second,Year]).date_gmt() -> {{Year,Month,Day},{Hour,Minute,Second}}=calendar:universal_time(), %% Time format hard-wired to: "%a %b %e %T %Z %Y" according to strftime(3) io_lib:format("~s ~s ~2w ~2.2.0w:~2.2.0w:~2.2.0w GMT ~w", [httpd_util:day(calendar:day_of_the_week(Year,Month,Day)), httpd_util:month(Month),Day,Hour,Minute,Second,Year]).last_modified(Data,ConfigDB,RequestURI) -> {ok,FileInfo}=file:read_file_info(mod_alias:path(Data,ConfigDB,RequestURI)), {{Year,Month,Day},{Hour,Minute,Second}}=FileInfo#file_info.mtime, io_lib:format("~s ~s ~2w ~2.2.0w:~2.2.0w:~2.2.0w ~w", [httpd_util:day(calendar:day_of_the_week(Year,Month,Day)), httpd_util:month(Month),Day,Hour,Minute,Second,Year]).%%%% fsize directive%%fsize(Info,Context,ErrorLog,[virtual],[VirtualPath],R) -> Aliases = httpd_util:multi_lookup(Info#mod.config_db,alias), {_,Path, _AfterPath}= mod_alias:real_name(Info#mod.config_db,VirtualPath,Aliases), fsize(Info, Context, ErrorLog, R, Path);fsize(Info,Context,ErrorLog,[file],[FileName],R) -> Path = file(Info#mod.config_db,Info#mod.request_uri,FileName), fsize(Info,Context,ErrorLog,R,Path);fsize(_Info, Context, ErrorLog, _TagList, _ValueList, R) -> {ok,Context,[{internal_info,?NICE("fsize directive has a spurious tag")}| ErrorLog],httpd_util:key1search(Context,errmsg,""),R}.fsize(_Info, Context, ErrorLog, R, Path) -> case file:read_file_info(Path) of {ok,FileInfo} -> case httpd_util:key1search(Context,sizefmt) of "bytes" -> {ok,Context,ErrorLog, integer_to_list(FileInfo#file_info.size),R}; "abbrev" -> Size = integer_to_list(trunc(FileInfo#file_info.size/1024+1))++"k", {ok,Context,ErrorLog,Size,R}; Value-> {ok,Context, [{internal_info, ?NICE("fsize directive has a spurious tag value ("++ Value++")")}| ErrorLog], httpd_util:key1search(Context, errmsg, ""), R} end; {error, _Reason} -> {ok,Context,[{internal_info,?NICE("Can't open "++Path)}|ErrorLog], httpd_util:key1search(Context,errmsg,""),R} end.%%%% flastmod directive%%flastmod(#mod{config_db = Db} = Info, Context, ErrorLog, [virtual], [VirtualPath],R) -> Aliases = httpd_util:multi_lookup(Db,alias), {_,Path, _AfterPath} = mod_alias:real_name(Db, VirtualPath, Aliases), flastmod(Info,Context,ErrorLog,R,Path);flastmod(#mod{config_db = Db, request_uri = RequestUri} = Info, Context, ErrorLog, [file], [FileName], R) -> Path = file(Db, RequestUri, FileName), flastmod(Info, Context, ErrorLog, R, Path);flastmod(_Info, Context, ErrorLog, _TagList, _ValueList, R) -> {ok,Context, [{internal_info,?NICE("flastmod directive has a spurious tag")}| ErrorLog],httpd_util:key1search(Context,errmsg,""),R}.flastmod(_Info, Context, ErrorLog, R, File) -> case file:read_file_info(File) of {ok, FileInfo} -> {{Yr,Mon,Day},{Hour,Minute,Second}}=FileInfo#file_info.mtime, Result = io_lib:format("~s ~s ~2w ~w:~w:~w ~w",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -