Skip to content

Commit

Permalink
updates for eptic-1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
elena-bobrova committed Feb 22, 2010
2 parents c6efd1f + 2a26f9b commit 13b19c0
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 88 deletions.
1 change: 1 addition & 0 deletions lib/eptic-1.4/src/e_annotation.erl
Expand Up @@ -83,3 +83,4 @@ save_annotations(Hrl, []) ->
-spec(generate_define_name/1 :: (atom()) -> (string())).
generate_define_name(FunName) ->
string:to_upper(atom_to_list(FunName)).

2 changes: 1 addition & 1 deletion lib/eptic-1.4/src/e_cache_ets.erl
Expand Up @@ -93,7 +93,7 @@ cache(File, wpart_xs) ->
XML;
cache(File, erlydtl_expander) ->
Mod = list_to_atom(string:join(string:tokens(File, "/.-"), "")),
case erlydtl:compile(File, Mod) of
case erlydtl:compile(File, Mod, [{doc_root, e_conf:template_root()}]) of
ok ->
ets:insert(?MODULE, {File, {date(), time()}, Mod}),
Mod;
Expand Down
66 changes: 33 additions & 33 deletions lib/eptic-1.4/src/e_mod_inets.erl
Expand Up @@ -43,7 +43,7 @@ do(#mod{parsed_header = Headers} = A) ->

e_logger:log({?MODULE, {url, URL}}),

case A#mod.socket_type of
case A#mod.socket_type of
{ssl, _} ->
e_dict:fset("__https", true),
e_dict:fset("__ip", get_ip(ssl:peername(A#mod.socket)));
Expand Down Expand Up @@ -80,8 +80,8 @@ do(#mod{parsed_header = Headers} = A) ->
e_logger:unregister_pid(self()),
{proceed, [{response, {response, [CookieHeader | NewHeaders], Result}}]};
enoent ->
cookie_bind(ClientCookie),
cleanup(),
cookie_bind(ClientCookie),
cleanup(),

e_logger:unregister_pid(self()),
{proceed, A#mod.data}
Expand All @@ -93,16 +93,16 @@ do(#mod{parsed_header = Headers} = A) ->
fe_request(#mod{parsed_header = Headers} = A, Session) ->
Cookie = proplists:get_value(?COOKIE, get_cookies(Headers), []),
e_session:update_session(Cookie, Session),

Ret = do(A),

{ok, NewSession} = e_session:get_session(Cookie),
{Ret, NewSession}.

%%====================================================================
%% Internal functions
%%====================================================================
-spec(handle_args/1 :: (tuple()) -> {ok, list(tuple())}).
-spec(handle_args/1 :: (tuple()) -> {ok, list(tuple())}).
handle_args(#mod{method = Method, entity_body = Post} = Mod) ->
Result = case Method of
"POST" ->
Expand All @@ -114,7 +114,7 @@ handle_args(#mod{method = Method, entity_body = Post} = Mod) ->
e_logger:log({?MODULE, {handle_args, Result}}),
Result.

-spec(controller_exec/2 :: (e_mod_gen:controller_response(), string()) -> {list(tuple()), string()}).
-spec(controller_exec/2 :: (e_mod_gen:controller_response(), string()) -> {list(tuple()), string()}).
controller_exec(Ret, View) ->
case Ret of
template ->
Expand Down Expand Up @@ -154,7 +154,7 @@ controller_exec(Ret, View) ->
format_response(e_mod_gen:error_page(Code, e_dict:fget("__path")))
end.

-spec(format_response/1 :: (term()) -> term()).
-spec(format_response/1 :: (term()) -> term()).
format_response({html, HTML}) ->
Length = {content_length, integer_to_list(erlang:iolist_size(HTML))},
{[{content_type, "text/html"}, {code, 200}, Length], HTML};
Expand All @@ -164,7 +164,7 @@ format_response([{status, Code}, {html, HTML}]) ->
format_response(Else) ->
Else.

-spec(with_formatted_error/1 :: (fun()) -> term()).
-spec(with_formatted_error/1 :: (fun()) -> term()).
with_formatted_error(F) ->
case catch F() of
{'EXIT', Reason} ->
Expand All @@ -173,7 +173,7 @@ with_formatted_error(F) ->
format_response(Response)
end.

-spec(create_headers/2 :: (list(tuple()), list(tuple())) -> list(tuple())).
-spec(create_headers/2 :: (list(tuple()), list(tuple())) -> list(tuple())).
create_headers([], Acc) ->
Acc;
create_headers([{cookie, CookieName, CookieVal} | Rest], Acc) ->
Expand All @@ -190,7 +190,7 @@ create_headers([_ | Rest], Acc) ->
create_headers(Rest, Acc).

%% @hidden
-spec(parse_get/1 :: (string()) -> list(tuple())).
-spec(parse_get/1 :: (string()) -> list(tuple())).
parse_get(URL) ->
case string:chr(URL, $?) of
0 ->
Expand All @@ -207,7 +207,7 @@ parse_get(URL) ->
end, Get)
end.

-spec(parse_post/1 :: (string()) -> list(tuple())).
-spec(parse_post/1 :: (string()) -> list(tuple())).
parse_post(String) ->
case fetch_boundary(String) of
{simple, Data} ->
Expand All @@ -217,41 +217,41 @@ parse_post(String) ->
end.

%% @hidden
-spec(fetch_boundary/1 :: (string()) -> {simple, string()} | {multipart, string()}).
-spec(fetch_boundary/1 :: (string()) -> {simple, string()} | {multipart, string()}).
fetch_boundary(Data) ->
case string:str(Data, "\r\n") of
0 ->
0 ->
{simple, Data};
Pos ->
Pos ->
{multipart, string:substr(Data, 1, Pos-1)}
end.

%% @hidden
-spec(cookie_up/1 :: (list(tuple())) -> term()).
-spec(cookie_up/1 :: (list(tuple())) -> term()).
cookie_up(Headers) ->
Cookies = get_cookies(Headers),
eptic:fset("__cookies", Cookies),

case proplists:get_value(?COOKIE, Cookies) of
undefined ->
ClientCookie = e_session:new_session([]),
e_mod_gen:restore_session(ClientCookie),
ClientCookie;
ClientCookie ->
{ok, Session} = e_session:get_session(ClientCookie),
if
Session == undefined ->
NewCookie = e_session:new_session([]),
e_mod_gen:restore_session(NewCookie),
NewCookie;
true ->
e_mod_gen:restore_session(ClientCookie),
ClientCookie
end
undefined ->
ClientCookie = e_session:new_session([]),
e_mod_gen:restore_session(ClientCookie),
ClientCookie;
ClientCookie ->
{ok, Session} = e_session:get_session(ClientCookie),
if
Session == undefined ->
NewCookie = e_session:new_session([]),
e_mod_gen:restore_session(NewCookie),
NewCookie;
true ->
e_mod_gen:restore_session(ClientCookie),
ClientCookie
end
end.

%% @hidden
-spec(cookie_bind/1 :: (string()) -> {string(), string()}).
-spec(cookie_bind/1 :: (string()) -> {string(), string()}).
cookie_bind(ClientCookie) ->
e_mod_gen:bind_session(ClientCookie),
{"Set-cookie", ?COOKIE ++ [$= | ClientCookie ++ "; path=/"]}.
Expand All @@ -261,7 +261,7 @@ cleanup() ->
e_multipart_inets:terminate(),
e_dict:terminate_state().

-spec(get_cookies/1 :: (list(tuple())) -> list(tuple())).
-spec(get_cookies/1 :: (list(tuple())) -> list(tuple())).
get_cookies(Headers) ->
case lists:keysearch("cookie", 1, Headers) of
false ->
Expand Down
2 changes: 2 additions & 0 deletions lib/eptic-1.4/src/e_mod_yaws.erl
Expand Up @@ -163,6 +163,8 @@ controller_exec(Ret, View) ->
{content, "text/plain", Data};
{content, pdf, Data} ->
{content, "application/pdf", Data};
{content, force_download, Data} ->
{content, "application/force-download", Data};
{json, Data} ->
{content, "text/plain", e_json:encode(Data)};
{template, Template} ->
Expand Down
33 changes: 19 additions & 14 deletions lib/eptic-1.4/src/e_multipart_inets.erl
Expand Up @@ -48,21 +48,26 @@ retrive_data(["Content-Disposition: form-data; " ++ Element | Rest]) ->
Name = string:strip(SName, both, 34),
Content = string:substr(Element, HeaderLength,
length(Element)-1-HeaderLength),

case lists:keysearch(filename, 1, Header) of
{value, {filename, "\"\""}} ->
[{Name, []} | retrive_data(Rest)];
{value, {filename, SFilename}} ->
Filename0 = string:strip(SFilename, both, 34),
Filename = case regexp:first_match(Content, "\r\n\r\n") of
{match, ContentS, _} ->
save_file(Filename0, string:substr(Content, ContentS+4));
nomatch ->
save_file(Filename0, Content)
end,
[{Name, Filename} | retrive_data(Rest)];

case e_conf:get_conf(upload_to_disk, true) of
false ->
[{Name, Content} | retrive_data(Rest)]
[{Name, Content} | retrive_data(Rest)];
true ->
case lists:keysearch(filename, 1, Header) of
{value, {filename, "\"\""}} ->
[{Name, []} | retrive_data(Rest)];
{value, {filename, SFilename}} ->
Filename0 = string:strip(SFilename, both, 34),
Filename = case regexp:first_match(Content, "\r\n\r\n") of
{match, ContentS, _} ->
save_file(Filename0, string:substr(Content, ContentS+4));
nomatch ->
save_file(Filename0, Content)
end,
[{Name, Filename} | retrive_data(Rest)];
false ->
[{Name, Content} | retrive_data(Rest)]
end
end.

save_file(SFilename, Content) ->
Expand Down
44 changes: 32 additions & 12 deletions lib/eptic-1.4/src/e_multipart_yaws.erl
Expand Up @@ -31,13 +31,13 @@
%%====================================================================
terminate() ->
Dirname = dirname(),
lists:foreach(fun file:delete/1,
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).

terminate(Pid) ->
Dirname = filename:join([e_conf:upload_dir(), pid_to_list(Pid)]),
lists:foreach(fun file:delete/1,
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).

Expand All @@ -48,7 +48,7 @@ is_multipart(#arg{req = R, headers = H}) ->
end.

%%--------------------------------------------------------------------
%% Function:
%% Function:
%% Description:
%%--------------------------------------------------------------------
parse(#arg{} = A) when A#arg.state == undefined ->
Expand All @@ -58,15 +58,35 @@ parse(A) when is_record(A, arg) ->
{cont, Cont, Res} ->
{get_more, Cont, add_chunk(Res, A#arg.state)};
{result, Res} ->
Handler = add_chunk(Res, A#arg.state),
case e_conf:get_conf(upload_to_disk, true) of
false -> Handler = add_body(Res,[]),
{ok, Handler};
_ -> Handler = add_chunk(Res, A#arg.state),
%% we cast {file, Filename} to Filename for
%% compatibility with Inets' multipart
{ok, lists:map(fun({Key, {file, Val}}) ->
{Key, Val};
(Else) ->
Else
end, element(2, Handler))}
{ok, lists:map(fun({Key, {file, Val}}) ->
{Key, Val};
(Else) ->
Else
end, element(2, Handler))}
end
end.
%%====================================================================
%% Internal functions
%%====================================================================
% head {"this_is_file",[{filename,"review.log"},{name,"this_is_file"}]}
add_body([{head, {Name, Opt}}|Res], SoFar) ->
add_body(Res, {Name, SoFar});
add_body([{body, Data}|Res], {Name, SoFar}) ->
if
length(Data) == 0 ->
add_body(Res, lists:append(SoFar, [{Name, []}]));
true ->
add_body(Res, lists:append(SoFar, [{Name, Data}]))
end;
add_body([], State) ->
State.


%%====================================================================
%% Internal functions
Expand All @@ -78,7 +98,7 @@ add_chunk([{head, {Name, Opt}}|Res], {_, SoFar}) ->
BaseDir = dirname(),

FolderCreator = fun(Element, Acc) ->
Next =
Next =
if Acc =/= "" ->
filename:join(Acc, Element);
true ->
Expand All @@ -87,9 +107,9 @@ add_chunk([{head, {Name, Opt}}|Res], {_, SoFar}) ->
case file:make_dir(Next) of
ok ->
Next;
{error, eexist} ->
{error, eexist} ->
Next;
%% special case of Mac Os - it returns
%% special case of Mac Os - it returns
%% eisdir instead of eexist
{error, eisdir} ->
Next;
Expand Down
26 changes: 13 additions & 13 deletions lib/eptic-1.4/src/e_session.erl
Expand Up @@ -17,8 +17,8 @@
%%% File : e_session.erl
%%% @author Lukas Larsson <lukas@erlang-consulting.com>
%%% @doc Handling of sessions for the eptic webplatform.<br/>
%%% A lot of ideas and some code is stolen from yaws_session_server which are
%%% copyrighted (c) 2006, Claes Wikstrom, klacke@hyber.org.
%%% A lot of ideas and some code is stolen from yaws_session_server which are
%%% copyrighted (c) 2006, Claes Wikstrom, klacke@hyber.org.
%%% All rights reserved.
%%% @end
%%% Created : 25 Jun 2008 by Lukas Larsson <lukas@erlang-consulting.com>
Expand Down Expand Up @@ -67,10 +67,10 @@ stop() ->
%% @doc Initializes the new session with the <i>SessionData</i> and generates the cookie.
%% The randomly generated cookie is returned.
%%
-spec(new_session/1 :: (term()) -> string()).
-spec(new_session/1 :: (term()) -> string()).
new_session(Data) ->
new_session(Data,?ExtendTime_default).
-spec(new_session/2 :: (term(), integer()) -> string()).
new_session(Data, e_conf:get_conf(extend_time, ?ExtendTime_default)).
-spec(new_session/2 :: (term(), integer()) -> string()).
new_session(Data,ExpireTime) ->
gen_server:call(?SESSION_SERVER,{new_session,Data,ExpireTime}).

Expand All @@ -80,7 +80,7 @@ new_session(Data,ExpireTime) ->
%% If no session with the <i>Cookie</i> has been found,
%% <i>{error, no_prev_session}</i> is returned.
%%
-spec(update_session/2 :: (term(), term()) -> true | {error, no_prev_session}).
-spec(update_session/2 :: (term(), term()) -> true | {error, no_prev_session}).
update_session(Cookie,NewData) ->
case ets:lookup(?SESSION_TABLE,Cookie) of
[Val] ->
Expand All @@ -99,7 +99,7 @@ update_session(Cookie,NewData) ->
%% Returns the previous session state or the tuple <i>{ok, undefined}</i>
%% if no previous session has been found.
%%
-spec(get_session/1 :: (term()) -> {ok, undefined} | {ok, term()}).
-spec(get_session/1 :: (term()) -> {ok, undefined} | {ok, term()}).
get_session(Cookie) ->
case ets:lookup(?SESSION_TABLE,Cookie) of
[#e_session{ data = Data } = E] ->
Expand All @@ -115,20 +115,20 @@ get_session(Cookie) ->
%% @doc Removes the session from the session table.
%% Returns new, empty cookie.
%%
-spec(delete_session/1 :: (term()) -> nil()).
-spec(delete_session/1 :: (term()) -> nil()).
delete_session(Cookie) ->
ets:delete(?SESSION_TABLE,Cookie),
"". %% Return a new empty cookie.

%%
%% @spec print_sessions() -> ok
%% @doc Prints all kept sessions on the Erlang system shell.
%%
-spec(print_sessions/0 :: () -> ok).
%%
-spec(print_sessions/0 :: () -> ok).
print_sessions() ->
lists:foreach(
fun(#e_session{ cookie = C,
created = Created,
fun(#e_session{ cookie = C,
created = Created,
expires = Expires,
extend_time = Extend,
data = Data }) ->
Expand Down Expand Up @@ -242,7 +242,7 @@ trav_ets(N, Key) ->
Next = ets:next(?SESSION_TABLE, Key),
ets:delete(?SESSION_TABLE, Key),
trav_ets(N, Next)

end;
[] ->
trav_ets(N, ets:next(?SESSION_TABLE, Key))
Expand Down

0 comments on commit 13b19c0

Please sign in to comment.