From 2f2df7de922e171b743400c890870032cc3434f0 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Tue, 12 Apr 2022 13:59:24 -0400 Subject: [PATCH] [Foundation] Ensure that post requests are not cached by the native code. The behaviour from apple is wrong, PUT and POST are differnet in that PUT is idempotent. Calling PUT several times successively has the same effect (that is no side effect), where successive identical POST may have additional effects We should not let the native code cache the calls. --- src/Foundation/NSUrlSessionHandler.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Foundation/NSUrlSessionHandler.cs b/src/Foundation/NSUrlSessionHandler.cs index acb7ccd68726..93e6eb3b4a01 100644 --- a/src/Foundation/NSUrlSessionHandler.cs +++ b/src/Foundation/NSUrlSessionHandler.cs @@ -857,7 +857,13 @@ void SetResponse (InflightData inflight) [Preserve (Conditional = true)] public override void WillCacheResponse (NSUrlSession session, NSUrlSessionDataTask dataTask, NSCachedUrlResponse proposedResponse, Action completionHandler) { - completionHandler (sessionHandler.DisableCaching ? null! : proposedResponse); + var inflight = GetInflightData (dataTask); + + if (inflight is null) + return; + // apple caches post request with a body, which should not happen. https://github.com/xamarin/maccore/issues/2571 + var disableCache = sessionHandler.DisableCaching || (inflight.Request.Method == HttpMethod.Post && inflight.Request.Content is not null); + completionHandler (disableCache ? null! : proposedResponse); } [Preserve (Conditional = true)]