From 983b2b9af3a85c44c852faa5410ce376847455f5 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Wed, 23 May 2012 10:00:11 +0200 Subject: [PATCH] Revert "User agent probe via a post request. Redirect via response code and causes no caching problems." Reason: iOS doesn't handle 204 responses correctly This reverts commit 3dce9bb9f4ef2b2328d411d9232096d20cf780f7. --- modules/mod_base/dispatch/dispatch | 2 +- modules/mod_base/lib/js/apps/zotonic-1.0.js | 20 ------ .../resources/resource_user_agent_probe.erl | 61 ++++++------------- modules/mod_base/templates/_ua_probe.tpl | 18 +++--- 4 files changed, 30 insertions(+), 71 deletions(-) diff --git a/modules/mod_base/dispatch/dispatch b/modules/mod_base/dispatch/dispatch index 3da4d845c8..789b74efce 100644 --- a/modules/mod_base/dispatch/dispatch +++ b/modules/mod_base/dispatch/dispatch @@ -69,7 +69,7 @@ ]}, %% User Agent handling - {ua_probe, ["useragent","probe"], resource_user_agent_probe, []}, + {ua_probe, ["useragent","probe.js"], resource_user_agent_probe, []}, {ua_select, ["useragent","select", ua_class], resource_user_agent_select, []}, {ua_select, ["useragent","select"], resource_user_agent_select, []}, diff --git a/modules/mod_base/lib/js/apps/zotonic-1.0.js b/modules/mod_base/lib/js/apps/zotonic-1.0.js index be3821713b..d7d842a5cc 100644 --- a/modules/mod_base/lib/js/apps/zotonic-1.0.js +++ b/modules/mod_base/lib/js/apps/zotonic-1.0.js @@ -394,26 +394,6 @@ function z_reload(args) } } -/* Send a post request to a server ----------------------------------------------------------- */ - -function z_post(path, args) -{ - var form = $(document.createElement('form')) - .attr({action: path, method: "POST", style: "display: none"}); - for(var key in args) { - if(args.hasOwnProperty(key)) { - var field = $(document.createElement("input")) - .attr({type: "hidden", name: key, value: args[key]}); - form.append(field); - } - } - form.appendTo(document.body); - form.submit(); - form.remove(); -} - - /* translations ---------------------------------------------------------- */ diff --git a/modules/mod_base/resources/resource_user_agent_probe.erl b/modules/mod_base/resources/resource_user_agent_probe.erl index 316989c9d0..05a35f35a6 100644 --- a/modules/mod_base/resources/resource_user_agent_probe.erl +++ b/modules/mod_base/resources/resource_user_agent_probe.erl @@ -17,16 +17,15 @@ %% limitations under the License. -module(resource_user_agent_probe). --author("Marc Worrell "). --author("Maas-Maarten Zeeman "). +-author("Marc Worrell "). -export([ init/1, service_available/2, allowed_methods/2, malformed_request/2, - post_is_create/2, - process_post/2 + content_types_provided/2, + to_js/2 ]). -include_lib("webmachine_resource.hrl"). @@ -42,13 +41,11 @@ service_available(ReqData, DispatchArgs) -> ?WM_REPLY(true, ContextQs). allowed_methods(ReqData, Context) -> - {['POST'], ReqData, Context}. + {['GET'], ReqData, Context}. -%% Check all parameters -%% malformed_request(ReqData, Context) -> - C1 = ?WM_REQ(ReqData, Context), try + C1 = ?WM_REQ(ReqData, Context), C2 = z_context:set(ua_args, [ {width, list_to_integer(z_context:get_q("w", C1))}, {height, list_to_integer(z_context:get_q("h", C1))}, @@ -57,40 +54,22 @@ malformed_request(ReqData, Context) -> C1), ?WM_REPLY(false, C2) catch - _:_ -> {true, ReqData, C1} + _:_ -> {true, ReqData, Context} end. -%% The probe does not create a new resource. -%% -post_is_create(ReqData, Context) -> - {false, ReqData, Context}. - -%% Process the probe. Send back a 204 when the user agent class does not -%% change. Force a reload of the page with a 303 See Other when a reload -%% is requested. -%% -process_post(ReqData, Context) -> - Context1 = ?WM_REQ(ReqData, Context), - UaArgs = z_context:get(ua_args, Context1), - ReplyContext = case z_user_agent:ua_probe(UaArgs, Context1) of - {reload, ReloadContext} -> - to_referer(ReloadContext); - {ok, CookieContext} -> - no_reply(CookieContext); - ok -> - no_reply(Context1) - end, - ?WM_REPLY(true, ReplyContext). - -to_referer(Context) -> - RD = z_context:get_reqdata(Context), - Referer = wrq:get_req_header_lc("referer", RD), - RD1 = wrq:do_redirect(true, - wrq:set_resp_header("location", Referer, RD)), - z_context:set_reqdata(RD1, Context). +content_types_provided(ReqData, Context) -> + {[ + {"application/x-javascript", to_js} + ], ReqData, Context }. -no_reply(Context) -> - RD = z_context:get_reqdata(Context), - RD1 = wrq:set_resp_body(<<>>, RD), - z_context:set_reqdata(RD1, Context). +to_js(ReqData, Context) -> + CRD = z_context:set_nocache_headers(?WM_REQ(ReqData, Context)), + case z_user_agent:ua_probe(z_context:get(ua_args, CRD), CRD) of + {reload, CR} -> + ?WM_REPLY(<<"window.location.href = window.location.protocol+'//'+window.location.host+page.val();">>, CR); + {ok, CR} -> + ?WM_REPLY(<<>>, CR); + ok -> + ?WM_REPLY(<<>>, CRD) + end. diff --git a/modules/mod_base/templates/_ua_probe.tpl b/modules/mod_base/templates/_ua_probe.tpl index fb7ab4adb9..798aaafaa4 100644 --- a/modules/mod_base/templates/_ua_probe.tpl +++ b/modules/mod_base/templates/_ua_probe.tpl @@ -1,12 +1,12 @@ {# Javascript that probes the user-agent and performs a callback with the results. #} -{% with m.req.ua_props as ua_props %} -{% endwith %}