Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

modified patch by Robert David to add a hook to yaws session server w…

…hen a session is gone

git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@1264 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information...
commit 6964fff7ec9cad5fbc461cfa7ddaa5cdd84f161c 1 parent c4030ff
Claes Wikstrom authored
Showing with 53 additions and 14 deletions.
  1. +11 −1 man/yaws_api.5
  2. +4 −1 src/yaws_api.erl
  3. +38 −12 src/yaws_session_server.erl
12 man/yaws_api.5
View
@@ -293,7 +293,17 @@ such as username and password
.TP
\fBnew_cookie_session(Opaque, TTL)\fR
As above, but allows to set a session specific time-out value,
-overriding teh system specified time-out value.
+overriding the system specified time-out value.
+
+.TP
+\fBnew_cookie_session(Opaque, TTL, CleanupPid)\fR
+As above, but also send a message
+\fI{yaws_session_end, Reason, Cookie, Opaque}\fR to the provided CleanuPid where
+Reason can be either of \fItimeout\fR or \fInormal\fR. The \fICookie\fR
+is the HTTP cookie as returned by \fInew_session()\fR and the Opaque
+is the user provided Opaque parameter to \fInew_session()\fR.
+The purpose of the feature is to cleanup resources assigned to the session.
+
.TP
\fBcookieval_to_opaque(CookieVal)\fR
5 src/yaws_api.erl
View
@@ -33,7 +33,7 @@
-export([get_line/1, mime_type/1]).
-export([stream_chunk_deliver/2, stream_chunk_deliver_blocking/2,
stream_chunk_end/1]).
--export([new_cookie_session/1, new_cookie_session/2,
+-export([new_cookie_session/1, new_cookie_session/2, new_cookie_session/3,
cookieval_to_opaque/1, request_url/1,
print_cookie_sessions/0,
replace_cookie_session/2, delete_cookie_session/1]).
@@ -848,6 +848,9 @@ new_cookie_session(Opaque) ->
new_cookie_session(Opaque, TTL) ->
yaws_session_server:new_session(Opaque, TTL).
+new_cookie_session(Opaque, TTL, Cleanup) ->
+ yaws_session_server:new_session(Opaque, TTL, Cleanup).
+
%% as returned in #ysession.cookie
cookieval_to_opaque(CookieVal) ->
yaws_session_server:cookieval_to_opaque(CookieVal).
50 src/yaws_session_server.erl
View
@@ -18,7 +18,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-include("../include/yaws_api.hrl").
--export([new_session/1,new_session/2,
+-export([new_session/1,new_session/2,new_session/3,
cookieval_to_opaque/1,
print_sessions/0,
replace_session/2,
@@ -32,14 +32,12 @@
to, %% greg secs untill timeout death
ttl, %% default time to live
starttime, %% When calendar:local_time() did sess start
+ cleanup, %% PID to notify of session end
opaque %% any data the user supplies
}).
-
-
-
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
@@ -55,10 +53,20 @@ stop() ->
%% will return a new cookie as a string
new_session(Opaque) ->
- gen_server:call(?MODULE, {new_session, Opaque, ?TTL}, infinity).
+ gen_server:call(?MODULE, {new_session, Opaque, ?TTL, undefined}, infinity).
new_session(Opaque, TTL) ->
- gen_server:call(?MODULE, {new_session, Opaque, TTL}, infinity).
+ gen_server:call(?MODULE, {new_session, Opaque, TTL, undefined}, infinity).
+
+new_session(Opaque, TTL, Cleanup) ->
+ case TTL of
+ undefined ->
+ gen_server:call(?MODULE,
+ {new_session, Opaque, ?TTL, Cleanup}, infinity);
+ _ ->
+ gen_server:call(?MODULE,
+ {new_session, Opaque, TTL, Cleanup}, infinity)
+ end.
cookieval_to_opaque(CookieString) ->
case ets:lookup(?MODULE, CookieString) of
@@ -96,7 +104,14 @@ replace_session(Cookie, NewOpaque) ->
delete_session(CookieVal) ->
- ets:delete(?MODULE, CookieVal).
+ case ets:lookup(?MODULE, CookieVal) of
+ [Y] ->
+ ets:delete(?MODULE, CookieVal),
+ report_deleted_sess(Y);
+ [] ->
+ true
+ end.
+
%%%----------------------------------------------------------------------
@@ -152,7 +167,7 @@ seed() ->
%%----------------------------------------------------------------------
-handle_call({new_session, Opaque, TTL}, _From, _State) ->
+handle_call({new_session, Opaque, TTL, Cleanup}, _From, _State) ->
Now = gnow(),
N = random:uniform(16#ffffffffffffffff), %% 64 bits
TS = calendar:local_time(),
@@ -161,7 +176,8 @@ handle_call({new_session, Opaque, TTL}, _From, _State) ->
starttime = TS,
opaque = Opaque,
to = Now + TTL,
- ttl = TTL},
+ ttl = TTL,
+ cleanup = Cleanup},
ets:insert(?MODULE, NS),
{reply, C, undefined, to()};
@@ -169,11 +185,21 @@ handle_call(stop, _From, State) ->
{stop, stopped, State}.
+send_cleanup_message(Sess,Msg) ->
+ case Sess#ysession.cleanup of
+ undefined ->
+ nocleanup;
+ Pid ->
+ Pid ! Msg
+ end.
-report_timedout_sess(_S) ->
- %%error_logger:info_msg("Session timedout: ~p ", [S#ysession.opaque]).
- silence.
+report_timedout_sess(S) ->
+ send_cleanup_message(S,{yaws_session_end,timeout,
+ S#ysession.cookie, S#ysession.opaque}).
+report_deleted_sess(S) ->
+ send_cleanup_message(S,{yaws_session_end,normal,
+ S#ysession.cookie, S#ysession.opaque}).
%%----------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.