< Summary

Class:Azure.Core.HttpMessageSanitizer
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\HttpMessageSanitizer.cs
Covered lines:52
Uncovered lines:0
Coverable lines:52
Total lines:134
Line coverage:100% (52 of 52)
Covered branches:34
Total branches:34
Branch coverage:100% (34 of 34)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
SanitizeHeader(...)-100%100%
SanitizeUrl(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\HttpMessageSanitizer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Text;
 7using System.Linq;
 8
 9namespace Azure.Core
 10{
 11    internal class HttpMessageSanitizer
 12    {
 13        private const string LogAllValue = "*";
 14        private readonly bool _logAllHeaders;
 15        private readonly bool _logFullQueries;
 16        private readonly string[] _allowedQueryParameters;
 17        private readonly string _redactedPlaceholder;
 18        private readonly HashSet<string> _allowedHeaders;
 19
 16420        public HttpMessageSanitizer(string[] allowedQueryParameters, string[] allowedHeaders, string redactedPlaceholder
 21        {
 16422            _logAllHeaders = allowedHeaders.Contains(LogAllValue);
 16423            _logFullQueries = allowedQueryParameters.Contains(LogAllValue);
 24
 16425            _allowedQueryParameters = allowedQueryParameters;
 16426            _redactedPlaceholder = redactedPlaceholder;
 16427            _allowedHeaders = new HashSet<string>(allowedHeaders, StringComparer.InvariantCultureIgnoreCase);
 16428        }
 29
 30        public string SanitizeHeader(string name, string value)
 31        {
 1239232            if (_logAllHeaders || _allowedHeaders.Contains(name))
 33            {
 1228834                return value;
 35            }
 36
 10437            return _redactedPlaceholder;
 38        }
 39
 40        public string SanitizeUrl(string url)
 41        {
 216042            if (_logFullQueries)
 43            {
 444                return url;
 45            }
 46
 215647            int indexOfQuerySeparator = url.IndexOf('?');
 215648            if (indexOfQuerySeparator == -1)
 49            {
 212250                return url;
 51            }
 52
 3453            StringBuilder stringBuilder = new StringBuilder(url.Length);
 3454            stringBuilder.Append(url, 0, indexOfQuerySeparator);
 55
 3456            string query = url.Substring(indexOfQuerySeparator);
 57
 3458            int queryIndex = 1;
 3459            stringBuilder.Append('?');
 60
 61            do
 62            {
 7663                int endOfParameterValue = query.IndexOf('&', queryIndex);
 7664                int endOfParameterName = query.IndexOf('=', queryIndex);
 7665                bool noValue = false;
 66
 67                // Check if we have parameter without value
 7668                if ((endOfParameterValue == -1 && endOfParameterName == -1) ||
 7669                    (endOfParameterValue != -1 && (endOfParameterName == -1 || endOfParameterName > endOfParameterValue)
 70                {
 3671                    endOfParameterName = endOfParameterValue;
 3672                    noValue = true;
 73                }
 74
 7675                if (endOfParameterName == -1)
 76                {
 1077                    endOfParameterName = query.Length;
 78                }
 79
 7680                if (endOfParameterValue == -1)
 81                {
 2282                    endOfParameterValue = query.Length;
 83                }
 84                else
 85                {
 86                    // include the separator
 5487                    endOfParameterValue++;
 88                }
 89
 7690                ReadOnlySpan<char> parameterName = query.AsSpan(queryIndex, endOfParameterName - queryIndex);
 91
 7692                bool isAllowed = false;
 42293                foreach (string name in _allowedQueryParameters)
 94                {
 15295                    if (parameterName.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
 96                    {
 3497                        isAllowed = true;
 3498                        break;
 99                    }
 100                }
 101
 76102                int valueLength = endOfParameterValue - queryIndex;
 76103                int nameLength = endOfParameterName - queryIndex;
 104
 76105                if (isAllowed)
 106                {
 34107                    stringBuilder.Append(query, queryIndex, valueLength);
 108                }
 109                else
 110                {
 42111                    if (noValue)
 112                    {
 24113                        stringBuilder.Append(query, queryIndex, valueLength);
 114                    }
 115                    else
 116                    {
 18117                        stringBuilder.Append(query, queryIndex, nameLength);
 18118                        stringBuilder.Append("=");
 18119                        stringBuilder.Append(_redactedPlaceholder);
 18120                        if (query[endOfParameterValue - 1] == '&')
 121                        {
 10122                            stringBuilder.Append("&");
 123                        }
 124                    }
 125                }
 126
 76127                queryIndex += valueLength;
 128
 76129            } while (queryIndex < query.Length);
 130
 34131            return stringBuilder.ToString();
 132        }
 133    }
 134}