| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Net.Http.Headers; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | internal static class CookieHelper |
| | | 11 | | { |
| | | 12 | | public static void ProcessReceivedCookies(HttpResponseMessage response, CookieContainer cookieContainer) |
| | 0 | 13 | | { |
| | 0 | 14 | | if (response.Headers.TryGetValues(KnownHeaders.SetCookie.Descriptor, out IEnumerable<string>? values)) |
| | 0 | 15 | | { |
| | | 16 | | // The header values are always a string[] |
| | 0 | 17 | | var valuesArray = (string[])values; |
| | 0 | 18 | | Debug.Assert(valuesArray.Length > 0, "No values for header??"); |
| | 0 | 19 | | Debug.Assert(response.RequestMessage != null && response.RequestMessage.RequestUri != null); |
| | | 20 | | |
| | 0 | 21 | | Uri requestUri = response.RequestMessage.RequestUri; |
| | 0 | 22 | | for (int i = 0; i < valuesArray.Length; i++) |
| | 0 | 23 | | { |
| | | 24 | | try |
| | 0 | 25 | | { |
| | 0 | 26 | | cookieContainer.SetCookies(requestUri, valuesArray[i]); |
| | 0 | 27 | | } |
| | 0 | 28 | | catch (CookieException) |
| | 0 | 29 | | { |
| | | 30 | | // Ignore invalid Set-Cookie header and continue processing. |
| | 0 | 31 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 32 | | { |
| | 0 | 33 | | NetEventSource.Error(response, $"Invalid Set-Cookie '{valuesArray[i]}' ignored."); |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | 0 | 36 | | } |
| | 0 | 37 | | } |
| | 0 | 38 | | } |
| | | 39 | | } |
| | | 40 | | } |