Skip to content

Commit

Permalink
Add 'no session' dispatch rule option to prevent starting a session o…
Browse files Browse the repository at this point in the history
…n a page.
  • Loading branch information
Arjan Scherpenisse committed Jan 3, 2012
1 parent a1d79b5 commit 7a7395a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
8 changes: 4 additions & 4 deletions modules/mod_base/dispatch/dispatch
Expand Up @@ -4,19 +4,19 @@
{home, [], resource_template, [ {template, "home.tpl"} ]},

%% Comet connection, used with long polls from the browser.
{comet, ["comet"], resource_comet, [{ssl, any}, {statz, [no_duration]}]},
{comet, ["comet"], resource_comet, [{ssl, any}, {statz, [no_duration]}, {no_session, true}]},

%% Comet sub-domain connection, used with long polls from the browser.
{comet, ["comet", "subdomain"], resource_template, [{template, "comet_subdomain.tpl"}]},

%% WebSocket connection.
{websocket, ["websocket"], resource_websocket, [{ssl, any}]},
{websocket, ["websocket"], resource_websocket, [{ssl, any}, {no_session, true}]},

%% Postback of events from the browser to the server, dispatched from the postback resource.
{postback, ["postback"], resource_postback, [{ssl, any}]},
{postback, ["postback"], resource_postback, [{ssl, any}, {no_session, true}]},

%% Used in Ajax file upload, fixes a problem in Safari. Just closes the connection.
{close_connection, ["close-connection"], resource_close_connection, [{ssl, any}]},
{close_connection, ["close-connection"], resource_close_connection, [{ssl, any}, {no_session, true}]},

%% Normal page to show a resource.
{page, ["page", id], resource_page, [ {template, {cat, "page.tpl"}} ]},
Expand Down
3 changes: 2 additions & 1 deletion modules/mod_base/resources/resource_api.erl
Expand Up @@ -45,7 +45,8 @@ service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->

allowed_methods(ReqData, Context) ->
Context0 = ?WM_REQ(ReqData, Context),
Context1 = z_context:ensure_all(Context0),
Context1 = z_context:ensure_qs(z_context:continue_session(Context0)),

%% 'ping' the service to ensure we loaded all the existing services.
z_service:all(Context1),
TheMod = case z_context:get_q("module", Context1) of
Expand Down
18 changes: 14 additions & 4 deletions modules/mod_base/resources/resource_postback.erl
Expand Up @@ -21,6 +21,7 @@

-export([
init/1,
service_available/2,
forbidden/2,
malformed_request/2,
allowed_methods/2,
Expand All @@ -33,10 +34,16 @@
-include_lib("webmachine_resource.hrl").
-include_lib("include/zotonic.hrl").

init(_Args) -> {ok, []}.
init(DispatchArgs) -> {ok, DispatchArgs}.

malformed_request(ReqData, _Context) ->
Context1 = z_context:new(ReqData, ?MODULE),
service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
Context = z_context:new(ReqData, ?MODULE),
Context1 = z_context:set(DispatchArgs, Context),
?WM_REPLY(true, Context1).


malformed_request(ReqData, Context) ->
Context1 = ?WM_REQ(ReqData, Context),
Context2 = z_context:ensure_qs(Context1),
case z_context:get_q("postback", Context2) of
undefined ->
Expand All @@ -61,7 +68,10 @@ content_types_provided(ReqData, Context) ->
process_post(ReqData, Context) ->
Context1 = ?WM_REQ(ReqData, Context),
{Script, EventContext} = process_postback(Context1),
CometScript = z_session_page:get_scripts(EventContext#context.page_pid),
CometScript = case z_context:has_session_page(EventContext) of
true -> z_session_page:get_scripts(EventContext#context.page_pid);
false -> []
end,

% Send back all the javascript.
RD = z_context:get_reqdata(EventContext),
Expand Down
32 changes: 25 additions & 7 deletions src/support/z_context.erl
Expand Up @@ -46,10 +46,10 @@

continue_session/1,
has_session/1,
has_session_page/1,

ensure_all/1,
ensure_session/1,
ensure_page_session/1,
ensure_qs/1,

get_reqdata/1,
Expand Down Expand Up @@ -456,6 +456,12 @@ continue_session(Context) ->
end.


%% @doc Check if the current context has a session page attached
has_session_page(#context{page_pid=PagePid}) when is_pid(PagePid) ->
true;
has_session_page(_) ->
false.

%% @doc Check if the current context has a session attached
has_session(#context{session_pid=SessionPid}) when is_pid(SessionPid) ->
true;
Expand All @@ -465,9 +471,13 @@ has_session(_) ->

%% @doc Ensure session and page session and fetch and parse the query string
ensure_all(Context) ->
ensure_page_session(
ensure_session(
ensure_qs(Context))).
case get(no_session, Context, false) of
false ->
ensure_page_session(ensure_session(ensure_qs(Context)));
true ->
continue_page_session(continue_session(ensure_qs(Context)))
end.



%% @doc Ensure that we have a session, start a new session process when needed
Expand All @@ -482,16 +492,24 @@ ensure_session(Context) ->
Context
end.

%% @doc Ensure that we have a page session, used for comet and postback requests
%% @doc Ensure that we have a page session, used for comet and postback requests.
ensure_page_session(Context) ->
case Context#context.page_pid of
undefined ->
Context1 = ensure_session(Context),
z_session:ensure_page_session(Context1);
z_session:ensure_page_session(Context);
_ ->
Context
end.

continue_page_session(Context) ->
case Context#context.session_pid of
undefined ->
Context;
_ ->
z_session:ensure_page_session(Context)
end.


%% @doc Ensure that we have parsed the query string, fetch body if necessary
ensure_qs(Context) ->
case proplists:lookup('q', Context#context.props) of
Expand Down

0 comments on commit 7a7395a

Please sign in to comment.