diff --git a/modules/mod_base/dispatch/dispatch b/modules/mod_base/dispatch/dispatch index 1cbf2c29a8..5f2f60a3b6 100644 --- a/modules/mod_base/dispatch/dispatch +++ b/modules/mod_base/dispatch/dispatch @@ -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"}} ]}, diff --git a/modules/mod_base/resources/resource_api.erl b/modules/mod_base/resources/resource_api.erl index df5e2e28a8..7ac33d3fab 100644 --- a/modules/mod_base/resources/resource_api.erl +++ b/modules/mod_base/resources/resource_api.erl @@ -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 diff --git a/modules/mod_base/resources/resource_postback.erl b/modules/mod_base/resources/resource_postback.erl index 7f8af55e2e..ef3784bf11 100644 --- a/modules/mod_base/resources/resource_postback.erl +++ b/modules/mod_base/resources/resource_postback.erl @@ -21,6 +21,7 @@ -export([ init/1, + service_available/2, forbidden/2, malformed_request/2, allowed_methods/2, @@ -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 -> @@ -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), diff --git a/src/support/z_context.erl b/src/support/z_context.erl index a20a9338e7..6489e13aee 100644 --- a/src/support/z_context.erl +++ b/src/support/z_context.erl @@ -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, @@ -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; @@ -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 @@ -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