1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21-module(mod_actions).
22-export([do/1,load/2, store/2]).
23
24-include("httpd.hrl").
25-include("httpd_internal.hrl").
26
27%% do
28
29do(Info) ->
30  case proplists:get_value(status, Info#mod.data) of
31    %% A status code has been generated!
32    {_StatusCode, _PhraseArgs, _Reason} ->
33      {proceed,Info#mod.data};
34    %% No status code has been generated!
35    undefined ->
36      case proplists:get_value(response, Info#mod.data) of
37	%% No response has been generated!
38	undefined ->
39	  Path = mod_alias:path(Info#mod.data,Info#mod.config_db,
40			      Info#mod.request_uri),
41	  Suffix = httpd_util:suffix(Path),
42	  MimeType = httpd_util:lookup_mime(Info#mod.config_db,Suffix,
43					  "text/plain"),
44	  Actions = httpd_util:multi_lookup(Info#mod.config_db,action),
45	  case action(Info#mod.request_uri,MimeType,Actions) of
46	    {yes, RequestURI} ->
47	      {proceed, [{new_request_uri, RequestURI} | Info#mod.data]};
48	    no ->
49	      Scripts = httpd_util:multi_lookup(Info#mod.config_db, script),
50	      case script(Info#mod.request_uri, Info#mod.method, Scripts) of
51		{yes, RequestURI} ->
52		  {proceed,[{new_request_uri, RequestURI} | Info#mod.data]};
53		no ->
54		  {proceed, Info#mod.data}
55	      end
56	  end;
57	%% A response has been generated or sent!
58	_Response ->
59	  {proceed, Info#mod.data}
60      end
61  end.
62
63action(_RequestURI, _MimeType, []) ->
64  no;
65action(RequestURI, MimeType, [{MimeType, CGIScript} | _Rest]) ->
66  {yes, CGIScript ++ RequestURI};
67action(RequestURI, MimeType, [_ | Rest]) ->
68  action(RequestURI, MimeType, Rest).
69
70script(_RequestURI, _Method, []) ->
71  no;
72script(RequestURI, Method, [{Method, CGIScript} | _Rest]) ->
73  {yes, CGIScript ++ RequestURI};
74script(RequestURI, Method, [_ | Rest]) ->
75  script(RequestURI, Method, Rest).
76
77%%
78%% Configuration
79%%
80
81%% load
82
83load("Action "++  Action, []) ->
84  case re:split(Action, " ", [{return, list}]) of
85      [MimeType, CGIScript] ->
86	  {ok,[],{action, {MimeType, CGIScript}}};
87      _ ->
88	  {error,?NICE(string:strip(Action)++" is an invalid Action")}
89  end;
90load("Script " ++ Script,[]) ->
91  case re:split(Script, " ", [{return, list}]) of
92      [Method, CGIScript] ->
93	  {ok,[],{script, {Method, CGIScript}}};
94      _ ->
95	  {error,?NICE(string:strip(Script)++" is an invalid Script")}
96  end.
97
98store({action, {MimeType, CGIScript}} = Conf, _) when is_list(MimeType),
99						      is_list(CGIScript)  ->
100    {ok, Conf};
101store({action, Value}, _) ->
102    {error, {wrong_type, {action, Value}}};
103
104store({script, {Method, CGIScript}} = Conf, _) when is_list(Method),
105						    is_list(CGIScript) ->
106    case string:to_lower(Method) of
107	"get" ->
108	    {ok, Conf};
109	"post" ->
110	    {ok, Conf};
111	_ ->
112	    {error, {wrong_type, Conf}}
113    end;
114
115store({script, Value}, _) ->
116    {error, {wrong_type, {script, Value}}}.
117
118
119
120