diff --git a/RuntimePortable.sln b/RuntimePortable.sln
index bbe26d3cf..6838937f7 100644
--- a/RuntimePortable.sln
+++ b/RuntimePortable.sln
@@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C40883CD-3
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.TestCommon", "test\Microsoft.TestCommon\Microsoft.TestCommon.csproj", "{FCCC4CB7-BAF7-4A57-9F89-E5766FE536C0}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Net.Http.Formatting.NetCore", "src\System.Net.Http.Formatting.NetCore\System.Net.Http.Formatting.NetCore.csproj", "{C7060639-719B-4BD2-8A37-2F146B5A0668}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.Http.Formatting.NetCore", "src\System.Net.Http.Formatting.NetCore\System.Net.Http.Formatting.NetCore.csproj", "{C7060639-719B-4BD2-8A37-2F146B5A0668}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Net.Http.Formatting.NetCore.Test", "test\System.Net.Http.Formatting.NetCore.Test\System.Net.Http.Formatting.NetCore.Test.csproj", "{8DA61DAC-FF7E-4CA1-93A0-6148DB66FD08}"
EndProject
diff --git a/packages/repositories.config b/packages/repositories.config
index 22e5c94b2..18d08a3de 100644
--- a/packages/repositories.config
+++ b/packages/repositories.config
@@ -3,7 +3,6 @@
-
diff --git a/src/Common/Error.cs b/src/Common/Error.cs
index b6013c5dc..f25b3d9b2 100644
--- a/src/Common/Error.cs
+++ b/src/Common/Error.cs
@@ -225,11 +225,7 @@ internal static OperationCanceledException OperationCanceled(string messageForma
/// The logged .
internal static ArgumentException InvalidEnumArgument(string parameterName, int invalidValue, Type enumClass)
{
-#if NETFX_CORE
- return new ArgumentException(Error.Format(CommonWebApiResources.InvalidEnumArgument, parameterName, invalidValue, enumClass.Name), parameterName);
-#else
return new InvalidEnumArgumentException(parameterName, invalidValue, enumClass);
-#endif
}
///
@@ -265,5 +261,24 @@ internal static NotSupportedException NotSupported(string messageFormat, params
{
return new NotSupportedException(Error.Format(messageFormat, messageArgs));
}
+
+#if NETFX_CORE // InvalidEnumArgumentException not available in netstandard1.3.
+ internal class InvalidEnumArgumentException : ArgumentException
+ {
+ public InvalidEnumArgumentException() : this(null)
+ { }
+
+ public InvalidEnumArgumentException(string message) : base(message)
+ { }
+
+ public InvalidEnumArgumentException(string message, Exception innerException) : base(message, innerException)
+ { }
+
+ public InvalidEnumArgumentException(string argumentName, int invalidValue, Type enumClass) : base(
+ Error.Format(CommonWebApiResources.InvalidEnumArgument, argumentName, invalidValue, enumClass.Name),
+ argumentName)
+ { }
+ }
+#endif
}
}
diff --git a/src/Common/UriQueryUtility.cs b/src/Common/UriQueryUtility.cs
deleted file mode 100644
index 53675e11a..000000000
--- a/src/Common/UriQueryUtility.cs
+++ /dev/null
@@ -1,278 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System.Diagnostics.Contracts;
-using System.Net;
-using System.Text;
-
-namespace System.Web.Http
-{
- ///
- /// Helpers for encoding, decoding, and parsing URI query components. In .Net 4.5
- /// please use the WebUtility class.
- ///
- internal static class UriQueryUtility
- {
- public static string UrlEncode(string str)
- {
- if (str == null)
- {
- return null;
- }
-
-#if NETFX_CORE
- return WebUtility.UrlEncode(str);
-#else
- byte[] bytes = Encoding.UTF8.GetBytes(str);
- return Encoding.ASCII.GetString(UrlEncode(bytes, 0, bytes.Length, alwaysCreateNewReturnValue: false));
-#endif
- }
-
- public static string UrlDecode(string str)
- {
- if (str == null)
- {
- return null;
- }
-
-#if NETFX_CORE
- return WebUtility.UrlDecode(str);
-#else
- return UrlDecodeInternal(str, Encoding.UTF8);
-#endif
- }
-
-#if !NETFX_CORE
- private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue)
- {
- byte[] encoded = UrlEncode(bytes, offset, count);
-
- return (alwaysCreateNewReturnValue && (encoded != null) && (encoded == bytes))
- ? (byte[])encoded.Clone()
- : encoded;
- }
-
- private static byte[] UrlEncode(byte[] bytes, int offset, int count)
- {
- if (!ValidateUrlEncodingParameters(bytes, offset, count))
- {
- return null;
- }
-
- int cSpaces = 0;
- int cUnsafe = 0;
-
- // count them first
- for (int i = 0; i < count; i++)
- {
- char ch = (char)bytes[offset + i];
-
- if (ch == ' ')
- cSpaces++;
- else if (!IsUrlSafeChar(ch))
- cUnsafe++;
- }
-
- // nothing to expand?
- if (cSpaces == 0 && cUnsafe == 0)
- return bytes;
-
- // expand not 'safe' characters into %XX, spaces to +s
- byte[] expandedBytes = new byte[count + cUnsafe * 2];
- int pos = 0;
-
- for (int i = 0; i < count; i++)
- {
- byte b = bytes[offset + i];
- char ch = (char)b;
-
- if (IsUrlSafeChar(ch))
- {
- expandedBytes[pos++] = b;
- }
- else if (ch == ' ')
- {
- expandedBytes[pos++] = (byte)'+';
- }
- else
- {
- expandedBytes[pos++] = (byte)'%';
- expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);
- expandedBytes[pos++] = (byte)IntToHex(b & 0x0f);
- }
- }
-
- return expandedBytes;
- }
-
- private static string UrlDecodeInternal(string value, Encoding encoding)
- {
- if (value == null)
- {
- return null;
- }
-
- int count = value.Length;
- UrlDecoder helper = new UrlDecoder(count, encoding);
-
- // go through the string's chars collapsing %XX and %uXXXX and
- // appending each char as char, with exception of %XX constructs
- // that are appended as bytes
-
- for (int pos = 0; pos < count; pos++)
- {
- char ch = value[pos];
-
- if (ch == '+')
- {
- ch = ' ';
- }
- else if (ch == '%' && pos < count - 2)
- {
- int h1 = HexToInt(value[pos + 1]);
- int h2 = HexToInt(value[pos + 2]);
-
- if (h1 >= 0 && h2 >= 0)
- {
- // valid 2 hex chars
- byte b = (byte)((h1 << 4) | h2);
- pos += 2;
-
- // don't add as char
- helper.AddByte(b);
- continue;
- }
- }
-
- if ((ch & 0xFF80) == 0)
- helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
- else
- helper.AddChar(ch);
- }
-
- return helper.GetString();
- }
-
- private static int HexToInt(char h)
- {
- return (h >= '0' && h <= '9') ? h - '0' :
- (h >= 'a' && h <= 'f') ? h - 'a' + 10 :
- (h >= 'A' && h <= 'F') ? h - 'A' + 10 :
- -1;
- }
-
- private static char IntToHex(int n)
- {
- Contract.Assert(n < 0x10);
-
- if (n <= 9)
- return (char)(n + (int)'0');
- else
- return (char)(n - 10 + (int)'a');
- }
-
- // Set of safe chars, from RFC 1738.4 minus '+'
- private static bool IsUrlSafeChar(char ch)
- {
- if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
- return true;
-
- switch (ch)
- {
- case '-':
- case '_':
- case '.':
- case '!':
- case '*':
- case '(':
- case ')':
- return true;
- }
-
- return false;
- }
-
- private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
- {
- if (bytes == null && count == 0)
- return false;
- if (bytes == null)
- {
- throw Error.ArgumentNull("bytes");
- }
- if (offset < 0 || offset > bytes.Length)
- {
- throw new ArgumentOutOfRangeException("offset");
- }
- if (count < 0 || offset + count > bytes.Length)
- {
- throw new ArgumentOutOfRangeException("count");
- }
-
- return true;
- }
-
- // Internal class to facilitate URL decoding -- keeps char buffer and byte buffer, allows appending of either chars or bytes
- private class UrlDecoder
- {
- private int _bufferSize;
-
- // Accumulate characters in a special array
- private int _numChars;
- private char[] _charBuffer;
-
- // Accumulate bytes for decoding into characters in a special array
- private int _numBytes;
- private byte[] _byteBuffer;
-
- // Encoding to convert chars to bytes
- private Encoding _encoding;
-
- private void FlushBytes()
- {
- if (_numBytes > 0)
- {
- _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
- _numBytes = 0;
- }
- }
-
- internal UrlDecoder(int bufferSize, Encoding encoding)
- {
- _bufferSize = bufferSize;
- _encoding = encoding;
-
- _charBuffer = new char[bufferSize];
- // byte buffer created on demand
- }
-
- internal void AddChar(char ch)
- {
- if (_numBytes > 0)
- FlushBytes();
-
- _charBuffer[_numChars++] = ch;
- }
-
- internal void AddByte(byte b)
- {
- if (_byteBuffer == null)
- _byteBuffer = new byte[_bufferSize];
-
- _byteBuffer[_numBytes++] = b;
- }
-
- internal String GetString()
- {
- if (_numBytes > 0)
- FlushBytes();
-
- if (_numChars > 0)
- return new String(_charBuffer, 0, _numChars);
- else
- return String.Empty;
- }
- }
-#endif
- }
-}
\ No newline at end of file
diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index c81c39ff4..c5c258a90 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -4,6 +4,6 @@
truefalse
- v4.5
+ v4.5
diff --git a/src/System.Net.Http.Formatting.NetCore/GlobalSuppressions.cs b/src/System.Net.Http.Formatting.NetCore/GlobalSuppressions.cs
deleted file mode 100644
index 778d614cd..000000000
--- a/src/System.Net.Http.Formatting.NetCore/GlobalSuppressions.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System.Diagnostics.CodeAnalysis;
-
-[assembly: SuppressMessage("Microsoft.Design", "CA2210:AssembliesShouldHaveValidStrongNames")]
-[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Net.Http.Headers", Justification = "We follow the layout of System.Net.Http.")]
-[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Net.Http.Handlers", Justification = "Handlers provide an extensibility hook which we want to keep in a separate namespace.")]
-[assembly: SuppressMessage("Microsoft.Design", "CA1014:MarkAssembliesWithClsCompliant", Scope = "module", Target = "system.net.http.formatting.dll", Justification = "CLSCompliant is not applicable to the portable version of the assembly.")]
-[assembly: SuppressMessage("Microsoft.Web.FxCop", "MW1000:UnusedResourceUsageRule", Scope = "module", Target = "system.net.http.formatting.dll", Justification = "The resources are only used in the non-portable version of the assembly.")]
diff --git a/src/System.Net.Http.Formatting.NetCore/ICloneable.cs b/src/System.Net.Http.Formatting.NetCore/ICloneable.cs
new file mode 100644
index 000000000..1fc5b88c5
--- /dev/null
+++ b/src/System.Net.Http.Formatting.NetCore/ICloneable.cs
@@ -0,0 +1,9 @@
+// No ICloneable interface in .NET Standard 1.3.
+
+namespace System
+{
+ internal interface ICloneable
+ {
+ object Clone();
+ }
+}
diff --git a/src/System.Net.Http.Formatting.NetCore/Internal/ConcurrentDictionary.cs b/src/System.Net.Http.Formatting.NetCore/Internal/ConcurrentDictionary.cs
deleted file mode 100644
index 3df580802..000000000
--- a/src/System.Net.Http.Formatting.NetCore/Internal/ConcurrentDictionary.cs
+++ /dev/null
@@ -1,192 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-#if NETFX_CORE // This file should only be included by the NetCore version of the formatting project, but adding a guard here just in case.
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace System.Net.Http.Internal
-{
- // TODO: Remove this class after BCL makes their portable library version.
- internal sealed class ConcurrentDictionary : IDictionary
- {
- private Dictionary _dictionary = new Dictionary();
- private object _lock = new object();
-
- public ICollection Keys
- {
- get
- {
- throw new NotImplementedException();
- }
- }
-
- public ICollection Values
- {
- get
- {
- throw new NotImplementedException();
- }
- }
-
- public int Count
- {
- get
- {
- throw new NotImplementedException();
- }
- }
-
- public bool IsReadOnly
- {
- get
- {
- return ((IDictionary)_dictionary).IsReadOnly;
- }
- }
-
- public TValue this[TKey key]
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
-
- public void Add(TKey key, TValue value)
- {
- throw new NotImplementedException();
- }
-
- public bool ContainsKey(TKey key)
- {
- lock (_lock)
- {
- return _dictionary.ContainsKey(key);
- }
- }
-
- public bool Remove(TKey key)
- {
- throw new NotImplementedException();
- }
-
- public bool TryGetValue(TKey key, out TValue value)
- {
- lock (_lock)
- {
- return _dictionary.TryGetValue(key, out value);
- }
- }
-
- public void Add(KeyValuePair item)
- {
- throw new NotImplementedException();
- }
-
- public void Clear()
- {
- throw new NotImplementedException();
- }
-
- public bool Contains(KeyValuePair item)
- {
- throw new NotImplementedException();
- }
-
- public void CopyTo(KeyValuePair[] array, int arrayIndex)
- {
- throw new NotImplementedException();
- }
-
- public bool Remove(KeyValuePair item)
- {
- throw new NotImplementedException();
- }
-
- public IEnumerator> GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- // ConcurrentDictionary members
- public bool TryRemove(TKey key, out TValue removedValue)
- {
- lock (_lock)
- {
- if (_dictionary.TryGetValue(key, out removedValue))
- {
- return _dictionary.Remove(key);
- }
-
- return false;
- }
- }
-
- public TValue GetOrAdd(TKey key, Func addValueFactory)
- {
- lock (_lock)
- {
- TValue value;
-
- if (!_dictionary.TryGetValue(key, out value))
- {
- value = addValueFactory.Invoke(key);
- _dictionary.Add(key, value);
- }
-
- return value;
- }
- }
-
- public bool TryAdd(TKey key, TValue value)
- {
- lock (_lock)
- {
- if (_dictionary.ContainsKey(key))
- {
- return false;
- }
-
- _dictionary.Add(key, value);
- return true;
- }
- }
-
- public TValue AddOrUpdate(TKey key, TValue addValue, Func updateValueFactory)
- {
- lock (_lock)
- {
- TValue value;
-
- // update
- if (_dictionary.TryGetValue(key, out value))
- {
- value = updateValueFactory.Invoke(key, value);
- _dictionary[key] = value;
- return value;
- }
-
- // add
- _dictionary.Add(key, addValue);
- return addValue;
- }
- }
- }
-}
-#endif
\ No newline at end of file
diff --git a/src/System.Net.Http.Formatting.NetCore/System.Net.Http.Formatting.NetCore.csproj b/src/System.Net.Http.Formatting.NetCore/System.Net.Http.Formatting.NetCore.csproj
index 6964b5560..615fd2326 100644
--- a/src/System.Net.Http.Formatting.NetCore/System.Net.Http.Formatting.NetCore.csproj
+++ b/src/System.Net.Http.Formatting.NetCore/System.Net.Http.Formatting.NetCore.csproj
@@ -1,279 +1,72 @@
-
-
+
- {C7060639-719B-4BD2-8A37-2F146B5A0668}
- Library
- Properties
+ netstandard1.3System.Net.HttpSystem.Net.Http.Formatting$(OutputPath)NetCore\$(OutputPath)$(AssemblyName).xml
- $(CodeAnalysis)
- ..\Strict.ruleset
- /assemblycomparemode:StrongNameIgnoringVersion
- false
- $(DefineConstants);NETFX_CORE;ASPNETMVC;NOT_CLS_COMPLIANT
- Profile259
- {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
+ false
+ $(DefineConstants);ASPNETMVC;NETFX_CORE1591
+ false
+ $(Configurations);CodeAnalysis
+ false
+
-
- Properties\CommonAssemblyInfo.cs
-
-
- Common\Error.cs
-
-
- Common\TaskHelpers.cs
-
-
- Common\TaskHelpersExtensions.cs
-
-
- Common\UriQueryUtility.cs
-
-
- Common\CollectionExtensions.cs
-
-
- Common\ListWrapperCollection.cs
-
-
- FormattingUtilities.cs
-
-
- Formatting\BaseJsonMediaTypeFormatter.cs
-
-
- Formatting\BsonMediaTypeFormatter.cs
-
-
- Formatting\DelegatingEnumerable.cs
-
-
- Formatting\FormDataCollection.cs
-
-
- Formatting\FormUrlEncodedJson.cs
-
-
- Formatting\FormUrlEncodedMediaTypeFormatter.cs
-
-
- Formatting\IFormatterLogger.cs
-
-
- Formatting\JsonMediaTypeFormatter.cs
-
-
- Formatting\MediaTypeConstants.cs
-
-
- Formatting\MediaTypeFormatter.cs
-
-
- Formatting\MediaTypeFormatterCollection.cs
-
-
- Formatting\MediaTypeHeaderValueExtensions.cs
-
-
- Formatting\MediaTypeHeaderValueRange.cs
-
-
- Formatting\ParsedMediaTypeHeaderValue.cs
-
-
- Formatting\Parsers\FormUrlEncodedParser.cs
-
-
- Formatting\Parsers\HttpRequestHeaderParser.cs
-
-
- Formatting\Parsers\HttpRequestLineParser.cs
-
-
- Formatting\Parsers\HttpResponseHeaderParser.cs
-
-
- Formatting\Parsers\HttpStatusLineParser.cs
-
-
- Formatting\Parsers\InternetMessageFormatHeaderParser.cs
-
-
- Formatting\Parsers\MimeMultipartBodyPartParser.cs
-
-
- Formatting\Parsers\MimeMultipartParser.cs
-
-
- Formatting\Parsers\ParserState.cs
-
-
- Formatting\StringComparisonHelper.cs
-
-
- Formatting\XmlMediaTypeFormatter.cs
-
-
- HttpContentFormDataExtensions.cs
-
-
- HttpValueCollection.cs
-
-
- UriExtensions.cs
-
-
-
- Handlers\HttpProgressEventArgs.cs
-
-
- Handlers\ProgressContent.cs
-
-
- Handlers\ProgressMessageHandler.cs
-
-
- Handlers\ProgressStream.cs
-
-
- HttpClientExtensions.cs
-
-
- HttpClientFactory.cs
-
-
- HttpContentExtensions.cs
-
-
- HttpContentMessageExtensions.cs
-
-
- HttpContentMultipartExtensions.cs
-
-
- HttpHeaderExtensions.cs
-
-
- HttpMessageContent.cs
-
-
- HttpUnsortedHeaders.cs
-
-
- HttpUnsortedRequest.cs
-
-
- HttpUnsortedResponse.cs
-
-
- Internal\AsyncResult.cs
-
-
- Internal\DelegatingStream.cs
-
-
- Internal\ReadOnlyStreamWithEncodingPreamble.cs
-
-
- Internal\TypeExtensions.cs
-
-
- MimeBodyPart.cs
-
-
- MultipartFileData.cs
-
-
- MultipartMemoryStreamProvider.cs
-
-
- MultipartRelatedStreamProvider.cs
-
-
- MultipartStreamProvider.cs
-
-
- ObjectContent.cs
-
-
- ObjectContentOfT.cs
-
-
- PushStreamContent.cs
-
-
- Properties\AssemblyInfo.cs
-
-
- Properties\TransparentCommonAssemblyInfo.cs
- True
- True
- Resources.resx
-
-
- UnsupportedMediaTypeException.cs
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ %(RecursiveDir)\%(Filename).cs
+
+
- Properties\CommonWebApiResources.Designer.cs
True
- TrueCommonWebApiResources.resx
+ True
+ Properties\CommonWebApiResources.Designer.cs
-
-
- Properties\CommonWebApiResources.resx
ResXFileCodeGeneratorCommonWebApiResources.Designer.cs
+ Properties\CommonWebApiResources.resx
-
-
+
+
+ True
+ Resources.resx
+ True
+ Properties\Resources.Designer.cs
+
- Properties\Resources.resx
ResXFileCodeGeneratorResources.Designer.cs
+ Properties\Resources.resx
Designer
+
+
-
-
- CodeAnalysisDictionary.xml
-
-
-
-
-
-
-
- ..\..\packages\Newtonsoft.Json.13.0.1\lib\netstandard1.0\Newtonsoft.Json.dll
-
-
- ..\..\packages\Microsoft.Net.Http.2.2.13\lib\portable-net40+sl4+win8+wp71\System.Net.Http.dll
-
-
- ..\..\packages\Microsoft.Net.Http.2.2.13\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Extensions.dll
-
-
- ..\..\packages\Microsoft.Net.Http.2.2.13\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Primitives.dll
-
-
-
-
-
-
- This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
-
-
-
-
\ No newline at end of file
+
diff --git a/src/System.Net.Http.Formatting.NetCore/packages.config b/src/System.Net.Http.Formatting.NetCore/packages.config
deleted file mode 100644
index ba452cd0f..000000000
--- a/src/System.Net.Http.Formatting.NetCore/packages.config
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj b/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj
index 2021cff78..ba3a97a38 100644
--- a/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj
+++ b/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj
@@ -1,4 +1,4 @@
-
+netstandard2.0
@@ -13,67 +13,52 @@
$(Configurations);CodeAnalysisfalse
+
+
+
+
-
-
-
- Properties\CommonAssemblyInfo.cs
-
-
- Common\CollectionExtensions.cs
-
-
- Common\Error.cs
-
-
- Common\ListWrapperCollection.cs
-
-
- Common\TaskHelpers.cs
-
-
- Common\TaskHelpersExtensions.cs
-
-
- Common\UriQueryUtility.cs
-
+
+
+
+
+
+
-
+
%(RecursiveDir)\%(Filename).cs
-
-
+
- Properties\CommonWebApiResources.Designer.cs
True
- TrueCommonWebApiResources.resx
+ True
+ Properties\CommonWebApiResources.Designer.cs
- Properties\CommonWebApiResources.resx
ResXFileCodeGeneratorCommonWebApiResources.Designer.cs
+ Properties\CommonWebApiResources.resx
-
-
-
+
+ True
- TrueResources.resx
+ True
+ Properties\Resources.Designer.cs
- Properties\Resources.resx
ResXFileCodeGeneratorResources.Designer.cs
+ Properties\Resources.resx
Designer
-
-
-
- CodeAnalysisDictionary.xml
-
+
+
diff --git a/src/System.Net.Http.Formatting/Formatting/BaseJsonMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/BaseJsonMediaTypeFormatter.cs
index b30a22edf..300716228 100644
--- a/src/System.Net.Http.Formatting/Formatting/BaseJsonMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/BaseJsonMediaTypeFormatter.cs
@@ -10,9 +10,7 @@
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
-#if !NETFX_CORE
using Newtonsoft.Json.Serialization;
-#endif
namespace System.Net.Http.Formatting
{
@@ -24,9 +22,7 @@ public abstract class BaseJsonMediaTypeFormatter : MediaTypeFormatter
// Though MaxDepth is not supported in portable library, we still override JsonReader's MaxDepth
private int _maxDepth = FormattingUtilities.DefaultMaxDepth;
-#if !NETFX_CORE // DataContractResolver is not supported in portable library
private readonly IContractResolver _defaultContractResolver;
-#endif
private JsonSerializerSettings _jsonSerializerSettings;
@@ -36,9 +32,7 @@ public abstract class BaseJsonMediaTypeFormatter : MediaTypeFormatter
protected BaseJsonMediaTypeFormatter()
{
// Initialize serializer settings
-#if !NETFX_CORE // DataContractResolver is not supported in portable library
_defaultContractResolver = new JsonContractResolver(this);
-#endif
_jsonSerializerSettings = CreateDefaultSerializerSettings();
// Set default supported character encodings
@@ -50,19 +44,15 @@ protected BaseJsonMediaTypeFormatter()
/// Initializes a new instance of the class.
///
/// The instance to copy settings from.
-#if !NETFX_CORE
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors",
Justification = "MaxDepth is sealed in existing subclasses and its documentation carries warnings.")]
-#endif
protected BaseJsonMediaTypeFormatter(BaseJsonMediaTypeFormatter formatter)
: base(formatter)
{
Contract.Assert(formatter != null);
SerializerSettings = formatter.SerializerSettings;
-#if !NETFX_CORE // MaxDepth is not supported in portable library and so _maxDepth never changes there
MaxDepth = formatter._maxDepth;
-#endif
}
///
@@ -82,7 +72,6 @@ public JsonSerializerSettings SerializerSettings
}
}
-#if !NETFX_CORE // MaxDepth is not supported in portable library
///
/// Gets or sets the maximum depth allowed by this formatter.
///
@@ -106,22 +95,15 @@ public virtual int MaxDepth
_maxDepth = value;
}
}
-#endif
///
/// Creates a instance with the default settings used by the .
///
-#if NETFX_CORE
- [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This could only be static half the time.")]
-#endif
public JsonSerializerSettings CreateDefaultSerializerSettings()
{
return new JsonSerializerSettings()
{
-#if !NETFX_CORE // DataContractResolver is not supported in portable library
ContractResolver = _defaultContractResolver,
-#endif
-
MissingMemberHandling = MissingMemberHandling.Ignore,
// Do not change this setting
diff --git a/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs
index 99b8cca5b..b6d0a8351 100644
--- a/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs
@@ -11,12 +11,7 @@
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
-#if NETFX_CORE
using Newtonsoft.Json.Bson;
-#else
-using BsonReader = Newtonsoft.Json.Bson.BsonDataReader;
-using BsonWriter = Newtonsoft.Json.Bson.BsonDataWriter;
-#endif
namespace System.Net.Http.Formatting
{
@@ -65,7 +60,6 @@ public static MediaTypeHeaderValue DefaultMediaType
}
}
-#if !NETFX_CORE // MaxDepth and DBNull not supported in portable library; no need to override there
///
public sealed override int MaxDepth
{
@@ -79,6 +73,7 @@ public sealed override int MaxDepth
}
}
+#if !NETFX_CORE // DBNull not supported in portable library; no need to override there
///
public override Task
public class JsonMediaTypeFormatter : BaseJsonMediaTypeFormatter
{
-#if !NETFX_CORE // DataContractJsonSerializer and MediaTypeMappings are not supported in portable library
- private ConcurrentDictionary _dataContractSerializerCache = new ConcurrentDictionary();
- private XmlDictionaryReaderQuotas _readerQuotas = FormattingUtilities.CreateDefaultReaderQuotas();
- private RequestHeaderMapping _requestHeaderMapping;
-#endif
+ private readonly ConcurrentDictionary _dataContractSerializerCache = new ConcurrentDictionary();
+ private readonly XmlDictionaryReaderQuotas _readerQuotas = FormattingUtilities.CreateDefaultReaderQuotas();
+ private readonly RequestHeaderMapping _requestHeaderMapping;
///
/// Initializes a new instance of the class.
@@ -45,10 +35,8 @@ public JsonMediaTypeFormatter()
SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType);
SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType);
-#if !NETFX_CORE // MediaTypeMappings are not supported in portable library
_requestHeaderMapping = new XmlHttpRequestHeaderMapping();
MediaTypeMappings.Add(_requestHeaderMapping);
-#endif
}
///
@@ -60,10 +48,7 @@ protected JsonMediaTypeFormatter(JsonMediaTypeFormatter formatter)
{
Contract.Assert(formatter != null);
-#if !NETFX_CORE // UseDataContractJsonSerializer is not supported in portable library
UseDataContractJsonSerializer = formatter.UseDataContractJsonSerializer;
-#endif
-
Indent = formatter.Indent;
}
@@ -84,7 +69,6 @@ public static MediaTypeHeaderValue DefaultMediaType
get { return MediaTypeConstants.ApplicationJsonMediaType; }
}
-#if !NETFX_CORE // DataContractJsonSerializer is not supported in portable library
///
/// Gets or sets a value indicating whether to use by default.
///
@@ -92,14 +76,12 @@ public static MediaTypeHeaderValue DefaultMediaType
/// true if use by default; otherwise, false. The default is false.
///
public bool UseDataContractJsonSerializer { get; set; }
-#endif
///
/// Gets or sets a value indicating whether to indent elements when writing data.
///
public bool Indent { get; set; }
-#if !NETFX_CORE // MaxDepth not supported in portable library; no need to override there
///
public sealed override int MaxDepth
{
@@ -113,7 +95,6 @@ public sealed override int MaxDepth
_readerQuotas.MaxDepth = value;
}
}
-#endif
///
public override JsonReader CreateJsonReader(Type type, Stream readStream, Encoding effectiveEncoding)
@@ -163,7 +144,6 @@ public override JsonWriter CreateJsonWriter(Type type, Stream writeStream, Encod
return jsonWriter;
}
-#if !NETFX_CORE // DataContractJsonSerializer not supported in portable library; no need to override there
///
public override bool CanReadType(Type type)
{
@@ -233,10 +213,26 @@ public override object ReadFromStream(Type type, Stream readStream, Encoding eff
if (UseDataContractJsonSerializer)
{
DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type);
- using (XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(new NonClosingDelegatingStream(readStream), effectiveEncoding, _readerQuotas, null))
+
+ // JsonReaderWriterFactory is internal, CreateTextReader only supports auto-detecting the encoding
+ // and auto-detection fails in some cases for the NETFX_CORE project. In addition, DCS encodings are
+ // limited to UTF8, UTF16BE, and UTF16LE. Convert to UTF8 as we read.
+ Stream innerStream = string.Equals(effectiveEncoding.WebName, Utf8Encoding.WebName, StringComparison.OrdinalIgnoreCase) ?
+ new NonClosingDelegatingStream(readStream) :
+ new TranscodingStream(readStream, effectiveEncoding, Utf8Encoding, leaveOpen: true);
+
+#if NETFX_CORE
+ using (innerStream)
{
- return dataContractSerializer.ReadObject(reader);
+ // Unfortunately, we're ignoring _readerQuotas.
+ return dataContractSerializer.ReadObject(innerStream);
}
+#else
+ // XmlDictionaryReader will always dispose of innerStream when we dispose of the reader.
+ using XmlDictionaryReader reader =
+ JsonReaderWriterFactory.CreateJsonReader(innerStream, Utf8Encoding, _readerQuotas, onClose: null);
+ return dataContractSerializer.ReadObject(reader);
+#endif
}
else
{
@@ -294,10 +290,18 @@ public override void WriteToStream(Type type, object value, Stream writeStream,
}
}
- DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type);
- using (XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter(writeStream, effectiveEncoding, ownsStream: false))
+ WritePreamble(writeStream, effectiveEncoding);
+ if (string.Equals(effectiveEncoding.WebName, Utf8Encoding.WebName, StringComparison.OrdinalIgnoreCase))
+ {
+ WriteObject(writeStream, type, value);
+ }
+ else
{
- dataContractSerializer.WriteObject(writer, value);
+ // JsonReaderWriterFactory is internal and DataContractJsonSerializer only writes UTF8 for the
+ // NETFX_CORE project. In addition, DCS encodings are limited to UTF8, UTF16BE, and UTF16LE.
+ // Convert to UTF8 as we write.
+ using var innerStream = new TranscodingStream(writeStream, effectiveEncoding, Utf8Encoding, leaveOpen: true);
+ WriteObject(innerStream, type, value);
}
}
else
@@ -306,6 +310,19 @@ public override void WriteToStream(Type type, object value, Stream writeStream,
}
}
+ private void WriteObject(Stream stream, Type type, object value)
+ {
+ DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type);
+
+ // Do not dispose of the stream. WriteToStream handles that where it's needed.
+#if NETFX_CORE
+ dataContractSerializer.WriteObject(stream, value);
+#else
+ using XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Utf8Encoding, ownsStream: false);
+ dataContractSerializer.WriteObject(writer, value);
+#endif
+ }
+
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Catch all is around an extensibile method")]
private DataContractJsonSerializer CreateDataContractSerializer(Type type, bool throwOnError)
{
@@ -316,8 +333,11 @@ private DataContractJsonSerializer CreateDataContractSerializer(Type type, bool
try
{
+#if !NETFX_CORE // XsdDataContractExporter is not supported in portable libraries
// Verify that type is a valid data contract by forcing the serializer to try to create a data contract
FormattingUtilities.XsdDataContractExporter.GetRootElementName(type);
+#endif
+
serializer = CreateDataContractSerializer(type);
}
catch (Exception caught)
@@ -378,6 +398,5 @@ private DataContractJsonSerializer GetDataContractSerializer(Type type)
return serializer;
}
-#endif
}
}
diff --git a/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatter.cs
index 367fba74a..20eace069 100644
--- a/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatter.cs
@@ -1,17 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-#if !NETFX_CORE // In portable library we have our own implementation of Concurrent Dictionary which is in the internal namespace
using System.Collections.Concurrent;
-#endif
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
-#if NETFX_CORE // In portable library we have our own implementation of Concurrent Dictionary which is in the internal namespace
-using System.Net.Http.Internal;
-#endif
using System.Reflection;
using System.Text;
using System.Threading;
@@ -25,6 +20,8 @@ namespace System.Net.Http.Formatting
///
public abstract class MediaTypeFormatter
{
+ private protected static readonly Encoding Utf8Encoding =
+ new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
private const int DefaultMinHttpCollectionKeys = 1;
private const int DefaultMaxHttpCollectionKeys = 1000; // same default as ASPNET
private const string IWellKnownComparerTypeName = "System.IWellKnownStringEqualityComparer, mscorlib, Version=4.0.0.0, PublicKeyToken=b77a5c561934e089";
@@ -36,10 +33,8 @@ public abstract class MediaTypeFormatter
private readonly List _supportedMediaTypes;
private readonly List _supportedEncodings;
-#if !NETFX_CORE // No MediaTypeMappings in portable library or IRequiredMemberSelector (no model state on client)
private readonly List _mediaTypeMappings;
private IRequiredMemberSelector _requiredMemberSelector;
-#endif
///
/// Initializes a new instance of the class.
@@ -50,10 +45,8 @@ protected MediaTypeFormatter()
SupportedMediaTypes = new MediaTypeHeaderValueCollection(_supportedMediaTypes);
_supportedEncodings = new List();
SupportedEncodings = new Collection(_supportedEncodings);
-#if !NETFX_CORE // No MediaTypeMappings in portable library
_mediaTypeMappings = new List();
MediaTypeMappings = new Collection(_mediaTypeMappings);
-#endif
}
///
@@ -71,11 +64,9 @@ protected MediaTypeFormatter(MediaTypeFormatter formatter)
SupportedMediaTypes = formatter.SupportedMediaTypes;
_supportedEncodings = formatter._supportedEncodings;
SupportedEncodings = formatter.SupportedEncodings;
-#if !NETFX_CORE // No MediaTypeMappings in portable library or IRequiredMemberSelector (no model state on client)
_mediaTypeMappings = formatter._mediaTypeMappings;
MediaTypeMappings = formatter.MediaTypeMappings;
_requiredMemberSelector = formatter._requiredMemberSelector;
-#endif
}
///
@@ -126,7 +117,6 @@ internal List SupportedEncodingsInternal
get { return _supportedEncodings; }
}
-#if !NETFX_CORE // No MediaTypeMappings in portable library
///
/// Gets the mutable collection of elements used
/// by this instance to determine the
@@ -138,9 +128,7 @@ internal List MediaTypeMappingsInternal
{
get { return _mediaTypeMappings; }
}
-#endif
-#if !NETFX_CORE // IRequiredMemberSelector is not in portable libraries because there is no model state on the client.
///
/// Gets or sets the used to determine required members.
///
@@ -155,7 +143,6 @@ public virtual IRequiredMemberSelector RequiredMemberSelector
_requiredMemberSelector = value;
}
}
-#endif
internal virtual bool CanWriteAnyTypes
{
@@ -504,6 +491,15 @@ public static object GetDefaultValueForType(Type type)
return null;
}
+ private protected static void WritePreamble(Stream stream, Encoding encoding)
+ {
+ byte[] bytes = encoding.GetPreamble();
+ if (bytes.Length > 0)
+ {
+ stream.Write(bytes, 0, bytes.Length);
+ }
+ }
+
///
/// Collection class that validates it contains only instances
/// that are not null and not media ranges.
diff --git a/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs b/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs
index 17a4959ba..4320d1ff6 100644
--- a/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs
+++ b/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs
@@ -198,9 +198,7 @@ public MediaTypeFormatter FindWriter(Type type, MediaTypeHeaderValue mediaType)
public static bool IsTypeExcludedFromValidation(Type type)
{
return
-#if !NETFX_CORE
typeof(XmlNode).IsAssignableFrom(type) ||
-#endif
typeof(FormDataCollection).IsAssignableFrom(type) ||
FormattingUtilities.IsJTokenType(type) ||
typeof(XObject).IsAssignableFrom(type) ||
diff --git a/src/System.Net.Http.Formatting/Formatting/Parsers/FormUrlEncodedParser.cs b/src/System.Net.Http.Formatting/Formatting/Parsers/FormUrlEncodedParser.cs
index 6a4f42024..90754c123 100644
--- a/src/System.Net.Http.Formatting/Formatting/Parsers/FormUrlEncodedParser.cs
+++ b/src/System.Net.Http.Formatting/Formatting/Parsers/FormUrlEncodedParser.cs
@@ -275,9 +275,9 @@ public StringBuilder Value
/// The collection to copy into.
public void CopyTo(ICollection> nameValuePairs)
{
- string unescapedName = UriQueryUtility.UrlDecode(_name.ToString());
+ string unescapedName = WebUtility.UrlDecode(_name.ToString());
string escapedValue = _value.ToString();
- string value = UriQueryUtility.UrlDecode(escapedValue);
+ string value = WebUtility.UrlDecode(escapedValue);
nameValuePairs.Add(new KeyValuePair(unescapedName, value));
@@ -290,7 +290,7 @@ public void CopyTo(ICollection> nameValuePairs)
/// The collection to copy into.
public void CopyNameOnlyTo(ICollection> nameValuePairs)
{
- string unescapedName = UriQueryUtility.UrlDecode(_name.ToString());
+ string unescapedName = WebUtility.UrlDecode(_name.ToString());
string value = String.Empty;
nameValuePairs.Add(new KeyValuePair(unescapedName, value));
Clear();
diff --git a/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
index 2ffd75975..9e858e5cb 100644
--- a/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
@@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-#if !NETFX_CORE // In portable library we have our own implementation of Concurrent Dictionary which is in the internal namespace
using System.Collections.Concurrent;
-#endif
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
@@ -57,9 +55,7 @@ protected XmlMediaTypeFormatter(XmlMediaTypeFormatter formatter)
{
UseXmlSerializer = formatter.UseXmlSerializer;
WriterSettings = formatter.WriterSettings;
-#if !NETFX_CORE // MaxDepth is not supported in portable libraries
MaxDepth = formatter.MaxDepth;
-#endif
}
///
@@ -108,7 +104,6 @@ public bool Indent
///
public XmlWriterSettings WriterSettings { get; private set; }
-#if !NETFX_CORE // MaxDepth is not supported in portable libraries
///
/// Gets or sets the maximum depth allowed by this formatter.
///
@@ -128,7 +123,6 @@ public int MaxDepth
_readerQuotas.MaxDepth = value;
}
}
-#endif
///
/// Registers the to use to read or write
@@ -343,12 +337,14 @@ protected internal virtual XmlReader CreateXmlReader(Stream readStream, HttpCont
{
// Get the character encoding for the content
Encoding effectiveEncoding = SelectCharacterEncoding(content == null ? null : content.Headers);
-#if NETFX_CORE
- // Force a preamble into the stream, since CreateTextReader in WinRT only supports auto-detecting encoding.
- return XmlDictionaryReader.CreateTextReader(new ReadOnlyStreamWithEncodingPreamble(readStream, effectiveEncoding), _readerQuotas);
-#else
- return XmlDictionaryReader.CreateTextReader(new NonClosingDelegatingStream(readStream), effectiveEncoding, _readerQuotas, null);
-#endif
+
+ // DCS encodings are limited to UTF8, UTF16BE, and UTF16LE. Convert to UTF8 as we read.
+ Stream innerStream = string.Equals(effectiveEncoding.WebName, Utf8Encoding.WebName, StringComparison.OrdinalIgnoreCase) ?
+ new NonClosingDelegatingStream(readStream) :
+ new TranscodingStream(readStream, effectiveEncoding, Utf8Encoding, leaveOpen: true);
+
+ // XmlDictionaryReader will always dispose of innerStream when caller disposes of the reader.
+ return XmlDictionaryReader.CreateTextReader(innerStream, Utf8Encoding, _readerQuotas, onClose: null);
}
///
@@ -439,9 +435,20 @@ protected internal virtual object GetSerializer(Type type, object value, HttpCon
protected internal virtual XmlWriter CreateXmlWriter(Stream writeStream, HttpContent content)
{
Encoding effectiveEncoding = SelectCharacterEncoding(content != null ? content.Headers : null);
+ WritePreamble(writeStream, effectiveEncoding);
+
+ // DCS encodings are limited to UTF8, UTF16BE, and UTF16LE. Convert to UTF8 as we read.
+ Stream innerStream = string.Equals(effectiveEncoding.WebName, Utf8Encoding.WebName, StringComparison.OrdinalIgnoreCase) ?
+ writeStream :
+ new TranscodingStream(writeStream, effectiveEncoding, Utf8Encoding, leaveOpen: true);
+
XmlWriterSettings writerSettings = WriterSettings.Clone();
- writerSettings.Encoding = effectiveEncoding;
- return XmlWriter.Create(writeStream, writerSettings);
+ writerSettings.Encoding = Utf8Encoding;
+
+ // Have XmlWriter dispose of innerStream when caller disposes of the writer if using a TranscodingStream.
+ writerSettings.CloseOutput = writeStream != innerStream;
+
+ return XmlWriter.Create(innerStream, writerSettings);
}
///
@@ -515,11 +522,12 @@ private object CreateDefaultSerializer(Type type, bool throwOnError)
}
else
{
-#if !NETFX_CORE
+#if !NETFX_CORE // XsdDataContractExporter is not supported in portable libraries
// REVIEW: Is there something comparable in WinRT?
// Verify that type is a valid data contract by forcing the serializer to try to create a data contract
FormattingUtilities.XsdDataContractExporter.GetRootElementName(type);
#endif
+
serializer = CreateDataContractSerializer(type);
}
}
diff --git a/src/System.Net.Http.Formatting/FormattingUtilities.cs b/src/System.Net.Http.Formatting/FormattingUtilities.cs
index a1ac95595..897bcd21f 100644
--- a/src/System.Net.Http.Formatting/FormattingUtilities.cs
+++ b/src/System.Net.Http.Formatting/FormattingUtilities.cs
@@ -166,9 +166,6 @@ public static HttpContentHeaders CreateEmptyContentHeaders()
///
public static XmlDictionaryReaderQuotas CreateDefaultReaderQuotas()
{
-#if NETFX_CORE // MaxDepth is a DOS mitigation. We don't support MaxDepth in portable libraries because it is strictly client side.
- return XmlDictionaryReaderQuotas.Max;
-#else
return new XmlDictionaryReaderQuotas()
{
MaxArrayLength = Int32.MaxValue,
@@ -177,7 +174,6 @@ public static XmlDictionaryReaderQuotas CreateDefaultReaderQuotas()
MaxNameTableCharCount = Int32.MaxValue,
MaxStringContentLength = Int32.MaxValue
};
-#endif
}
///
diff --git a/src/System.Net.Http.Formatting/HttpContentFormDataExtensions.cs b/src/System.Net.Http.Formatting/HttpContentFormDataExtensions.cs
index 4a830684d..72ebb40c7 100644
--- a/src/System.Net.Http.Formatting/HttpContentFormDataExtensions.cs
+++ b/src/System.Net.Http.Formatting/HttpContentFormDataExtensions.cs
@@ -8,9 +8,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
-#if NETFX_CORE
-using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
-#endif
namespace System.Net.Http
{
diff --git a/src/System.Net.Http.Formatting/Internal/ByteRangeStream.cs b/src/System.Net.Http.Formatting/Internal/ByteRangeStream.cs
index 33325d4f4..bf3d4371f 100644
--- a/src/System.Net.Http.Formatting/Internal/ByteRangeStream.cs
+++ b/src/System.Net.Http.Formatting/Internal/ByteRangeStream.cs
@@ -111,10 +111,12 @@ public override long Position
}
}
+#if !NETFX_CORE // BeginX and EndX are not supported on streams in portable libraries
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return base.BeginRead(buffer, offset, PrepareStreamForRangeRead(count), callback, state);
}
+#endif
public override int Read(byte[] buffer, int offset, int count)
{
@@ -172,6 +174,7 @@ public override void Write(byte[] buffer, int offset, int count)
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
+#if !NETFX_CORE // BeginX and EndX are not supported on streams in portable libraries
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
@@ -181,6 +184,7 @@ public override void EndWrite(IAsyncResult asyncResult)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
+#endif
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
diff --git a/src/System.Net.Http.Formatting/Internal/HttpValueCollection.cs b/src/System.Net.Http.Formatting/Internal/HttpValueCollection.cs
index 3197a9733..72f28d4cd 100644
--- a/src/System.Net.Http.Formatting/Internal/HttpValueCollection.cs
+++ b/src/System.Net.Http.Formatting/Internal/HttpValueCollection.cs
@@ -1,14 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-#if NETFX_CORE
-using System.Collections;
-using System.Collections.Generic;
-using System.Diagnostics.Contracts;
-using System.Linq;
-using System.Text;
-using System.Web.Http;
-#else
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.Contracts;
@@ -17,45 +9,29 @@
using System.Runtime.Serialization;
using System.Text;
using System.Web.Http;
-#endif
-#if NETFX_CORE
-namespace System.Net.Http.Formatting
-#else
namespace System.Net.Http.Formatting.Internal
-#endif
{
///
/// NameValueCollection to represent form data and to generate form data output.
///
-#if NETFX_CORE
- public class HttpValueCollection : IEnumerable>
-#else
+#if !NETFX_CORE // NameValueCollection is not serializable in netstandard1.3.
[Serializable]
- internal class HttpValueCollection : NameValueCollection
#endif
+ internal class HttpValueCollection : NameValueCollection
{
-#if NETFX_CORE
- internal readonly HashSet Names = new HashSet(StringComparer.OrdinalIgnoreCase);
- internal readonly List> List = new List>();
-
- ///
- /// Creates a new instance
- ///
- public HttpValueCollection()
- {
- }
-#else
+#if !NETFX_CORE // NameValueCollection is not serializable in netstandard1.3.
protected HttpValueCollection(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
+#endif
private HttpValueCollection()
: base(StringComparer.OrdinalIgnoreCase) // case-insensitive keys
{
}
-#endif
+
// Use a builder function instead of a ctor to avoid virtual calls from the ctor.
// The above condition is only important in the Full .NET fx implementation.
internal static HttpValueCollection Create()
@@ -75,9 +51,8 @@ internal static HttpValueCollection Create(IEnumerable
/// The name to be added as a case insensitive string.
/// The value to be added.
- public
-#if !NETFX_CORE
- override
-#endif
- void Add(string name, string value)
+ public override void Add(string name, string value)
{
ThrowIfMaxHttpCollectionKeysExceeded(Count);
name = name ?? String.Empty;
value = value ?? String.Empty;
-#if NETFX_CORE
- Names.Add(name);
- List.Add(new KeyValuePair(name, value));
-#else
base.Add(name, value);
-#endif
}
///
@@ -131,11 +97,7 @@ private string ToString(bool urlEncode)
StringBuilder builder = new StringBuilder();
bool first = true;
-#if NETFX_CORE
- foreach (string name in Names)
-#else
foreach (string name in this)
-#endif
{
string[] values = GetValues(name);
if (values == null || values.Length == 0)
@@ -157,10 +119,10 @@ private string ToString(bool urlEncode)
private static bool AppendNameValuePair(StringBuilder builder, bool first, bool urlEncode, string name, string value)
{
string effectiveName = name ?? String.Empty;
- string encodedName = urlEncode ? UriQueryUtility.UrlEncode(effectiveName) : effectiveName;
+ string encodedName = urlEncode ? WebUtility.UrlEncode(effectiveName) : effectiveName;
string effectiveValue = value ?? String.Empty;
- string encodedValue = urlEncode ? UriQueryUtility.UrlEncode(effectiveValue) : effectiveValue;
+ string encodedValue = urlEncode ? WebUtility.UrlEncode(effectiveValue) : effectiveValue;
if (first)
{
@@ -179,104 +141,5 @@ private static bool AppendNameValuePair(StringBuilder builder, bool first, bool
}
return first;
}
-
-#if NETFX_CORE
- ///
- /// Gets the values associated with the specified name
- /// combined into one comma-separated list.
- ///
- /// The name of the entry that contains the values to get. The name can be null.
- /// A that contains a comma-separated list of url encoded values associated
- /// with the specified name if found; otherwise, null. The values are Url encoded.
- public string this[string name]
- {
- get
- {
- return Get(name);
- }
- }
-
- ///
- /// Gets the number of names in the collection.
- ///
- public int Count
- {
- get
- {
- return Names.Count;
- }
- }
-
- ///
- /// Gets the values associated with the specified name
- /// combined into one comma-separated list.
- ///
- /// The name of the entry that contains the values to get. The name can be null.
- ///
- /// A that contains a comma-separated list of url encoded values associated
- /// with the specified name if found; otherwise, null. The values are Url encoded.
- ///
- public string Get(string name)
- {
- name = name ?? String.Empty;
-
- if (!Names.Contains(name))
- {
- return null;
- }
-
- List values = GetValuesInternal(name);
- Contract.Assert(values != null && values.Count > 0);
-
- return String.Join(",", values);
- }
-
- ///
- /// Gets the values associated with the specified name.
- ///
- /// The
- /// A that contains url encoded values associated with the name, or null if the name does not exist.
- public string[] GetValues(string name)
- {
- name = name ?? String.Empty;
-
- if (!Names.Contains(name))
- {
- return null;
- }
-
- return GetValuesInternal(name).ToArray();
- }
-
- // call this when only when there are values available.
- private List GetValuesInternal(string name)
- {
- List values = new List();
-
- for (int i = 0; i < List.Count; i++)
- {
- KeyValuePair kvp = List[i];
-
- if (String.Equals(kvp.Key, name, StringComparison.OrdinalIgnoreCase))
- {
- values.Add(kvp.Value);
- }
- }
-
- return values;
- }
-
- ///
- public IEnumerator> GetEnumerator()
- {
- return List.GetEnumerator();
- }
-
- ///
- IEnumerator IEnumerable.GetEnumerator()
- {
- return List.GetEnumerator();
- }
-#endif
}
}
diff --git a/src/System.Net.Http.Formatting/Internal/NonClosingDelegatingStream.cs b/src/System.Net.Http.Formatting/Internal/NonClosingDelegatingStream.cs
index 5af2385b0..625bc8536 100644
--- a/src/System.Net.Http.Formatting/Internal/NonClosingDelegatingStream.cs
+++ b/src/System.Net.Http.Formatting/Internal/NonClosingDelegatingStream.cs
@@ -8,8 +8,8 @@ namespace System.Net.Http.Internal
///
/// Stream that doesn't close the inner stream when closed. This is to work around a limitation
/// in the insisting of closing the inner stream.
- /// The regular does allow for not closing the inner stream but that
- /// doesn't have the quota that we need for security reasons. Implementations of
+ /// The regular does allow for not closing the inner stream but that
+ /// doesn't have the quota that we need for security reasons. Implementations of
///
/// should not close the input stream when reading or writing so hence this workaround.
///
@@ -20,8 +20,14 @@ public NonClosingDelegatingStream(Stream innerStream)
{
}
+#if NETFX_CORE
+ protected override void Dispose(bool disposing)
+ {
+ }
+#else
public override void Close()
{
}
+#endif
}
}
diff --git a/src/System.Net.Http.Formatting/Internal/NullableAttributes.cs b/src/System.Net.Http.Formatting/Internal/NullableAttributes.cs
new file mode 100644
index 000000000..2344a2994
--- /dev/null
+++ b/src/System.Net.Http.Formatting/Internal/NullableAttributes.cs
@@ -0,0 +1,198 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+// From https://github.com/dotnet/runtime/blob/88868b7a781f4e5b9037b8721f30440207a7aa42/src/tools/illink/src/ILLink.RoslynAnalyzer/NullableAttributes.cs
+
+namespace System.Diagnostics.CodeAnalysis
+{
+#if !NETSTANDARD2_1
+ /// Specifies that null is allowed as an input even if the corresponding type disallows it.
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class AllowNullAttribute : Attribute { }
+
+ /// Specifies that null is disallowed as an input even if the corresponding type allows it.
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class DisallowNullAttribute : Attribute { }
+
+ /// Specifies that an output may be null even if the corresponding type disallows it.
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class MaybeNullAttribute : Attribute { }
+
+ /// Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input argument was not null when the call returns.
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class NotNullAttribute : Attribute { }
+
+ /// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it.
+ [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class MaybeNullWhenAttribute : Attribute
+ {
+ /// Initializes the attribute with the specified return value condition.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter may be null.
+ ///
+ public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+ }
+
+ /// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it.
+ [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class NotNullWhenAttribute : Attribute
+ {
+ /// Initializes the attribute with the specified return value condition.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+ }
+
+ /// Specifies that the output will be non-null if the named parameter is non-null.
+ [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class NotNullIfNotNullAttribute : Attribute
+ {
+ /// Initializes the attribute with the associated parameter name.
+ ///
+ /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
+ ///
+ public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
+
+ /// Gets the associated parameter name.
+ public string ParameterName { get; }
+ }
+
+ /// Applied to a method that will never return under any circumstance.
+ [AttributeUsage(AttributeTargets.Method, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class DoesNotReturnAttribute : Attribute { }
+
+ /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value.
+ [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class DoesNotReturnIfAttribute : Attribute
+ {
+ /// Initializes the attribute with the specified parameter value.
+ ///
+ /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
+ /// the associated parameter matches this value.
+ ///
+ public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
+
+ /// Gets the condition parameter value.
+ public bool ParameterValue { get; }
+ }
+#endif
+
+ /// Specifies that the method or property will ensure that the listed field and property members have not-null values.
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class MemberNotNullAttribute : Attribute
+ {
+ /// Initializes the attribute with a field or property member.
+ ///
+ /// The field or property member that is promised to be not-null.
+ ///
+ public MemberNotNullAttribute(string member) => Members = new[] { member };
+
+ /// Initializes the attribute with the list of field and property members.
+ ///
+ /// The list of field and property members that are promised to be not-null.
+ ///
+ public MemberNotNullAttribute(params string[] members) => Members = members;
+
+ /// Gets field or property member names.
+ public string[] Members { get; }
+ }
+
+ /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ sealed class MemberNotNullWhenAttribute : Attribute
+ {
+ /// Initializes the attribute with the specified return value condition and a field or property member.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ ///
+ /// The field or property member that is promised to be not-null.
+ ///
+ public MemberNotNullWhenAttribute(bool returnValue, string member)
+ {
+ ReturnValue = returnValue;
+ Members = new[] { member };
+ }
+
+ /// Initializes the attribute with the specified return value condition and list of field and property members.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ ///
+ /// The list of field and property members that are promised to be not-null.
+ ///
+ public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
+ {
+ ReturnValue = returnValue;
+ Members = members;
+ }
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+
+ /// Gets field or property member names.
+ public string[] Members { get; }
+ }
+}
diff --git a/src/System.Net.Http.Formatting/Internal/ReadOnlyStreamWithEncodingPreamble.cs b/src/System.Net.Http.Formatting/Internal/ReadOnlyStreamWithEncodingPreamble.cs
deleted file mode 100644
index 923e4bd37..000000000
--- a/src/System.Net.Http.Formatting/Internal/ReadOnlyStreamWithEncodingPreamble.cs
+++ /dev/null
@@ -1,175 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System.Diagnostics.Contracts;
-using System.IO;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace System.Net.Http.Internal
-{
- ///
- /// This implements a read-only, forward-only stream around another readable stream, to ensure
- /// that there is an appropriate encoding preamble in the stream.
- ///
- internal class ReadOnlyStreamWithEncodingPreamble : Stream
- {
- private static Task _cancelledTask = GetCancelledTask();
- private Stream _innerStream;
- private ArraySegment _remainingBytes;
-
- public ReadOnlyStreamWithEncodingPreamble(Stream innerStream, Encoding encoding)
- {
- Contract.Assert(innerStream != null);
- Contract.Assert(innerStream.CanRead);
- Contract.Assert(encoding != null);
-
- _innerStream = innerStream;
-
- // Determine whether we even have a preamble to be concerned about
- byte[] preamble = encoding.GetPreamble();
- int preambleLength = preamble.Length;
- if (preambleLength <= 0)
- {
- return;
- }
-
- // Create a double sized buffer, and read enough bytes from the stream to know
- // whether we have a preamble present already or not.
- int finalBufferLength = preambleLength * 2;
- byte[] finalBuffer = new byte[finalBufferLength];
- int finalCount = preambleLength;
- preamble.CopyTo(finalBuffer, 0);
-
- // Read the first bytes of the stream and see if they already contain a preamble
- for (; finalCount < finalBufferLength; finalCount++)
- {
- int b = innerStream.ReadByte();
- if (b == -1)
- {
- break;
- }
- finalBuffer[finalCount] = (byte)b;
- }
-
- // Did we read enough bytes to do the comparison?
- if (finalCount == finalBufferLength)
- {
- bool foundPreamble = true;
- for (int idx = 0; idx < preambleLength; idx++)
- {
- if (finalBuffer[idx] != finalBuffer[idx + preambleLength])
- {
- foundPreamble = false;
- break;
- }
- }
-
- // If we found the preamble, then just exclude it from the data that we return
- if (foundPreamble)
- {
- finalCount = preambleLength;
- }
- }
-
- _remainingBytes = new ArraySegment(finalBuffer, 0, finalCount);
- }
-
- public override bool CanRead
- {
- get { return true; }
- }
-
- public override bool CanSeek
- {
- get { return false; }
- }
-
- public override bool CanWrite
- {
- get { return false; }
- }
-
- public override long Length
- {
- get { throw new NotImplementedException(); }
- }
-
- public override long Position
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
-
- public override void Flush()
- {
- throw new NotImplementedException();
- }
-
- private static Task GetCancelledTask()
- {
- var tcs = new TaskCompletionSource();
- tcs.SetCanceled();
- return tcs.Task;
- }
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- byte[] remainingArray = _remainingBytes.Array;
- if (remainingArray == null)
- {
- return _innerStream.Read(buffer, offset, count);
- }
-
- int remainingCount = _remainingBytes.Count;
- int remainingOffset = _remainingBytes.Offset;
- int result = Math.Min(count, remainingCount);
-
- for (int idx = 0; idx < result; ++idx)
- {
- buffer[offset + idx] = remainingArray[remainingOffset + idx];
- }
-
- if (result == remainingCount)
- {
- _remainingBytes = default(ArraySegment);
- }
- else
- {
- _remainingBytes = new ArraySegment(remainingArray, remainingOffset + result, remainingCount - result);
- }
-
- return result;
- }
-
- public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- if (_remainingBytes.Array == null)
- {
- return _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
- }
- if (cancellationToken.IsCancellationRequested)
- {
- return _cancelledTask;
- }
-
- return Task.FromResult(Read(buffer, offset, count));
- }
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotImplementedException();
- }
-
- public override void SetLength(long value)
- {
- throw new NotImplementedException();
- }
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- throw new NotImplementedException();
- }
- }
-}
diff --git a/src/System.Net.Http.Formatting/Internal/TranscodingStream.cs b/src/System.Net.Http.Formatting/Internal/TranscodingStream.cs
new file mode 100644
index 000000000..9f89eeee8
--- /dev/null
+++ b/src/System.Net.Http.Formatting/Internal/TranscodingStream.cs
@@ -0,0 +1,828 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+// From https://github.com/dotnet/runtime/blob/88868b7a781f4e5b9037b8721f30440207a7aa42/src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs
+
+using System.Buffers;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Web.Http;
+using Properties = System.Net.Http.Properties;
+
+#nullable enable
+
+namespace System.Text
+{
+ internal sealed class TranscodingStream : Stream
+ {
+ private const int DefaultReadByteBufferSize = 4 * 1024; // lifted from StreamReader.cs (FileStream)
+
+ // We optimistically assume 1 byte ~ 1 char during transcoding. This is a good rule of thumb
+ // but isn't always appropriate: transcoding between single-byte and multi-byte encodings
+ // will violate this, as will any invalid data fixups performed by the transcoder itself.
+ // To account for these unknowns we have a minimum scratch buffer size we use during the
+ // transcoding process. This should be generous enough to account for even the largest
+ // fallback mechanism we're likely to see in the real world.
+
+ private const int MinWriteRentedArraySize = 4 * 1024;
+ private const int MaxWriteRentedArraySize = 1024 * 1024;
+
+ internal static readonly byte[] EmptyByteBuffer = new byte[0];
+ internal static readonly char[] EmptyCharBuffer = new char[0];
+
+ private readonly Encoding _innerEncoding;
+ private readonly Encoding _thisEncoding;
+ private Stream _innerStream; // null if the wrapper has been disposed
+ private readonly bool _leaveOpen;
+ private readonly byte[] _singleByteBuffer = new byte[1];
+
+ /*
+ * Fields used for writing bytes [this] -> chars -> bytes [inner]
+ * Lazily initialized the first time we need to write
+ */
+
+ private Encoder? _innerEncoder;
+ private Decoder? _thisDecoder;
+
+ /*
+ * Fields used for reading bytes [inner] -> chars -> bytes [this]
+ * Lazily initialized the first time we need to read
+ */
+
+ private Encoder? _thisEncoder;
+ private Decoder? _innerDecoder;
+ private int _readCharBufferMaxSize; // the maximum number of characters _innerDecoder.ReadChars can return
+ private byte[]? _readBuffer; // contains the data that Read() should return
+ private int _readBufferOffset;
+ private int _readBufferCount;
+
+ internal TranscodingStream(Stream innerStream, Encoding innerEncoding, Encoding thisEncoding, bool leaveOpen = false)
+ {
+ _innerStream = innerStream ?? throw Error.ArgumentNull(nameof(innerStream));
+ _leaveOpen = leaveOpen;
+
+ _innerEncoding = innerEncoding ?? throw Error.ArgumentNull(nameof(innerEncoding));
+ _thisEncoding = thisEncoding ?? throw Error.ArgumentNull(nameof(thisEncoding));
+ }
+
+ /*
+ * Most CanXyz methods delegate to the inner stream, returning false
+ * if this instance has been disposed. CanSeek is always false.
+ */
+
+ public override bool CanRead => _innerStream?.CanRead ?? false;
+
+ public override bool CanSeek => false;
+
+ public override bool CanWrite => _innerStream?.CanWrite ?? false;
+
+ public override long Length => throw Error.NotSupported(Properties.Resources.NotSupported_UnseekableStream);
+
+ public override long Position
+ {
+ get => throw Error.NotSupported(Properties.Resources.NotSupported_UnseekableStream);
+ set => throw Error.NotSupported(Properties.Resources.NotSupported_UnseekableStream);
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ Debug.Assert(disposing, "This type isn't finalizable.");
+ base.Dispose(disposing);
+
+ if (_innerStream is null)
+ {
+ return; // dispose called multiple times, ignore
+ }
+
+ // First, flush any pending data to the inner stream.
+
+ ArraySegment pendingData = FinalFlushWriteBuffers();
+ if (pendingData.Count != 0)
+ {
+ _innerStream.Write(pendingData.Array, pendingData.Offset, pendingData.Count);
+ }
+
+ // Mark our object as disposed
+
+ Stream innerStream = _innerStream;
+ _innerStream = null!;
+
+ // And dispose the inner stream if needed
+
+ if (!_leaveOpen)
+ {
+ innerStream.Dispose();
+ }
+ }
+
+#if NETCOREAPP3_1 || NET5_0_OR_GREATER || NETSTANDARD2_1
+ public override ValueTask DisposeAsync()
+ {
+ if (_innerStream is null)
+ {
+ return default; // dispose called multiple times, ignore
+ }
+
+ // First, get any pending data destined for the inner stream.
+
+ ArraySegment pendingData = FinalFlushWriteBuffers();
+
+ if (pendingData.Count == 0)
+ {
+ // Fast path: just dispose of the object graph.
+ // No need to write anything to the stream first.
+
+ Stream innerStream = _innerStream;
+ _innerStream = null!;
+
+ return (_leaveOpen)
+ ? default /* no work to do */
+ : innerStream.DisposeAsync();
+ }
+
+ // Slower path; need to perform an async write followed by an async dispose.
+
+ return DisposeAsyncCore(pendingData);
+ async ValueTask DisposeAsyncCore(ArraySegment pendingData)
+ {
+ Debug.Assert(pendingData.Count != 0);
+
+ Stream innerStream = _innerStream;
+ _innerStream = null!;
+
+ await innerStream.WriteAsync(pendingData.AsMemory()).ConfigureAwait(false);
+
+ if (!_leaveOpen)
+ {
+ await innerStream.DisposeAsync().ConfigureAwait(false);
+ }
+ }
+ }
+#endif
+
+#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
+
+ // Sets up the data structures that are necessary before any read operation takes place,
+ // throwing if the object is in a state where reads are not possible.
+ [MemberNotNull(nameof(_innerDecoder), nameof(_thisEncoder), nameof(_readBuffer))]
+ private void EnsurePreReadConditions()
+ {
+ ThrowIfDisposed();
+ if (_innerDecoder is null)
+ {
+ InitializeReadDataStructures();
+ }
+
+ void InitializeReadDataStructures()
+ {
+ if (!CanRead)
+ {
+ throw Error.NotSupported(Properties.Resources.NotSupported_UnreadableStream);
+ }
+
+ _innerDecoder = _innerEncoding.GetDecoder();
+ _thisEncoder = _thisEncoding.GetEncoder();
+ _readCharBufferMaxSize = _innerEncoding.GetMaxCharCount(DefaultReadByteBufferSize);
+
+ // Can't use ArrayPool for the below array since it's an instance field of this object.
+ // But since we never expose the raw array contents to our callers we can get away
+ // with skipping the array zero-init during allocation. The segment points to the
+ // data which we haven't yet read; however, we own the entire backing array and can
+ // re-create the segment as needed once the array is repopulated.
+
+#if NET5_0_OR_GREATER
+ _readBuffer = GC.AllocateUninitializedArray(_thisEncoding.GetMaxByteCount(_readCharBufferMaxSize));
+#else
+ _readBuffer = new byte[_thisEncoding.GetMaxByteCount(_readCharBufferMaxSize)];
+#endif
+ }
+ }
+
+ // Sets up the data structures that are necessary before any write operation takes place,
+ // throwing if the object is in a state where writes are not possible.
+ [MemberNotNull(nameof(_thisDecoder), nameof(_innerEncoder))]
+ private void EnsurePreWriteConditions()
+ {
+ ThrowIfDisposed();
+ if (_innerEncoder is null)
+ {
+ InitializeReadDataStructures();
+ }
+
+ void InitializeReadDataStructures()
+ {
+ if (!CanWrite)
+ {
+ throw Error.NotSupported(Properties.Resources.NotSupported_UnwritableStream);
+ }
+
+ _innerEncoder = _innerEncoding.GetEncoder();
+ _thisDecoder = _thisEncoding.GetDecoder();
+ }
+ }
+
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
+#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant
+
+ // returns any pending data that needs to be flushed to the inner stream before disposal
+ private ArraySegment FinalFlushWriteBuffers()
+ {
+ // If this stream was never used for writing, no-op.
+
+ if (_thisDecoder is null || _innerEncoder is null)
+ {
+ return default;
+ }
+
+ // convert bytes [this] -> chars
+ // Having leftover data in our buffers should be very rare since it should only
+ // occur if the end of the stream contains an incomplete multi-byte sequence.
+ // Let's not bother complicating this logic with array pool rentals or allocation-
+ // avoiding loops.
+
+ char[] chars = EmptyCharBuffer;
+ int charCount = _thisDecoder.GetCharCount(EmptyByteBuffer, 0, 0, flush: true);
+ if (charCount > 0)
+ {
+ chars = new char[charCount];
+ charCount = _thisDecoder.GetChars(EmptyByteBuffer, 0, 0, chars, 0, flush: true);
+ }
+
+ // convert chars -> bytes [inner]
+ // It's possible that _innerEncoder might need to perform some end-of-text fixup
+ // (due to flush: true), even if _thisDecoder didn't need to do so.
+
+ byte[] bytes = EmptyByteBuffer;
+ int byteCount = _innerEncoder.GetByteCount(chars, 0, charCount, flush: true);
+ if (byteCount > 0)
+ {
+ bytes = new byte[byteCount];
+ byteCount = _innerEncoder.GetBytes(chars, 0, charCount, bytes, 0, flush: true);
+ }
+
+ return new ArraySegment(bytes, 0, byteCount);
+ }
+
+ public override void Flush()
+ {
+ // Don't pass flush: true to our inner decoder + encoder here, since it could cause data
+ // corruption if a flush occurs mid-stream. Wait until the stream is being closed.
+
+ ThrowIfDisposed();
+ _innerStream.Flush();
+ }
+
+ public override Task FlushAsync(CancellationToken cancellationToken)
+ {
+ // Don't pass flush: true to our inner decoder + encoder here, since it could cause data
+ // corruption if a flush occurs mid-stream. Wait until the stream is being closed.
+
+ ThrowIfDisposed();
+ return _innerStream.FlushAsync(cancellationToken);
+ }
+
+ public override int Read(byte[] buffer, int offset, int count)
+ {
+ ValidateBufferArguments(buffer, offset, count);
+
+ return Read(new Span(buffer, offset, count));
+ }
+
+#if NETCOREAPP || NETSTANDARD2_1
+ public override
+#else
+ private
+#endif
+ int Read(Span buffer)
+ {
+ EnsurePreReadConditions();
+
+ // If there's no data in our pending read buffer, we'll need to populate it from
+ // the inner stream. We read the inner stream's bytes, decode that to chars using
+ // the 'inner' encoding, then re-encode those chars under the 'this' encoding.
+ // We've already calculated the worst-case expansions for the intermediate buffers,
+ // so we use GetChars / GetBytes instead of Convert to simplify the below code
+ // and to ensure an exception is thrown if the Encoding reported an incorrect
+ // worst-case expansion.
+
+ if (_readBufferCount == 0)
+ {
+ byte[] rentedBytes = ArrayPool.Shared.Rent(DefaultReadByteBufferSize);
+ char[] rentedChars = ArrayPool.Shared.Rent(_readCharBufferMaxSize);
+
+ try
+ {
+ int pendingReadDataPopulatedJustNow;
+ bool isEofReached;
+
+ do
+ {
+ // Beware: Use our constant value instead of 'rentedBytes.Length' for the count
+ // parameter below. The reason for this is that the array pool could've returned
+ // a larger-than-expected array, but our worst-case expansion calculations
+ // performed earlier didn't take that into account.
+
+ int innerBytesReadJustNow = _innerStream.Read(rentedBytes, 0, DefaultReadByteBufferSize);
+ isEofReached = (innerBytesReadJustNow == 0);
+
+ // Convert bytes [inner] -> chars, then convert chars -> bytes [this].
+ // We can't return 0 to our caller until inner stream EOF has been reached. But if the
+ // inner stream returns a non-empty but incomplete buffer, GetBytes may return 0 anyway
+ // since it can't yet make forward progress on the input data. If this happens, we'll
+ // loop so that we don't return 0 to our caller until we truly see inner stream EOF.
+
+ int charsDecodedJustNow = _innerDecoder.GetChars(rentedBytes, 0, innerBytesReadJustNow, rentedChars, 0, flush: isEofReached);
+ pendingReadDataPopulatedJustNow = _thisEncoder.GetBytes(rentedChars, 0, charsDecodedJustNow, _readBuffer, 0, flush: isEofReached);
+ } while (!isEofReached && pendingReadDataPopulatedJustNow == 0);
+
+ _readBufferOffset = 0;
+ _readBufferCount = pendingReadDataPopulatedJustNow;
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(rentedBytes);
+ ArrayPool.Shared.Return(rentedChars);
+ }
+ }
+
+ // At this point: (a) we've populated our pending read buffer and there's
+ // useful data to return to our caller; or (b) the pending read buffer is
+ // empty because the inner stream has reached EOF and all pending read data
+ // has already been flushed, and we should return 0.
+
+ int bytesToReturn = Math.Min(_readBufferCount, buffer.Length);
+ _readBuffer.AsSpan(_readBufferOffset, bytesToReturn).CopyTo(buffer);
+ _readBufferOffset += bytesToReturn;
+ _readBufferCount -= bytesToReturn;
+ return bytesToReturn;
+ }
+
+ public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+ {
+ ValidateBufferArguments(buffer, offset, count);
+
+ return ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask();
+ }
+
+#if NETCOREAPP || NETSTANDARD2_1
+ public override
+#else
+ private
+#endif
+ ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default)
+ {
+ EnsurePreReadConditions();
+
+ if (cancellationToken.IsCancellationRequested)
+ {
+#if NETCOREAPP || NETSTANDARD
+ return new ValueTask(Task.FromCanceled(cancellationToken));
+#else
+ // Lose track of the CancellationToken in this case.
+ return new ValueTask(TaskHelpers.Canceled());
+#endif
+ }
+
+ return ReadAsyncCore(buffer, cancellationToken);
+ async ValueTask ReadAsyncCore(Memory buffer, CancellationToken cancellationToken)
+ {
+ // If there's no data in our pending read buffer, we'll need to populate it from
+ // the inner stream. We read the inner stream's bytes, decode that to chars using
+ // the 'inner' encoding, then re-encode those chars under the 'this' encoding.
+ // We've already calculated the worst-case expansions for the intermediate buffers,
+ // so we use GetChars / GetBytes instead of Convert to simplify the below code
+ // and to ensure an exception is thrown if the Encoding reported an incorrect
+ // worst-case expansion.
+
+ if (_readBufferCount == 0)
+ {
+ byte[] rentedBytes = ArrayPool.Shared.Rent(DefaultReadByteBufferSize);
+ char[] rentedChars = ArrayPool.Shared.Rent(_readCharBufferMaxSize);
+
+ try
+ {
+ int pendingReadDataPopulatedJustNow;
+ bool isEofReached;
+
+ do
+ {
+ // Beware: Use our constant value instead of 'rentedBytes.Length' when creating
+ // the Mem struct. The reason for this is that the array pool could've returned
+ // a larger-than-expected array, but our worst-case expansion calculations
+ // performed earlier didn't take that into account.
+
+ int innerBytesReadJustNow = await _innerStream.ReadAsync(rentedBytes, 0, DefaultReadByteBufferSize, cancellationToken).ConfigureAwait(false);
+ isEofReached = (innerBytesReadJustNow == 0);
+
+ // Convert bytes [inner] -> chars, then convert chars -> bytes [this].
+ // We can't return 0 to our caller until inner stream EOF has been reached. But if the
+ // inner stream returns a non-empty but incomplete buffer, GetBytes may return 0 anyway
+ // since it can't yet make forward progress on the input data. If this happens, we'll
+ // loop so that we don't return 0 to our caller until we truly see inner stream EOF.
+
+ int charsDecodedJustNow = _innerDecoder.GetChars(rentedBytes, 0, innerBytesReadJustNow, rentedChars, 0, flush: isEofReached);
+ pendingReadDataPopulatedJustNow = _thisEncoder.GetBytes(rentedChars, 0, charsDecodedJustNow, _readBuffer, 0, flush: isEofReached);
+ } while (!isEofReached && pendingReadDataPopulatedJustNow == 0);
+
+ _readBufferOffset = 0;
+ _readBufferCount = pendingReadDataPopulatedJustNow;
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(rentedBytes);
+ ArrayPool.Shared.Return(rentedChars);
+ }
+ }
+
+ // At this point: (a) we've populated our pending read buffer and there's
+ // useful data to return to our caller; or (b) the pending read buffer is
+ // empty because the inner stream has reached EOF and all pending read data
+ // has already been flushed, and we should return 0.
+
+ int bytesToReturn = Math.Min(_readBufferCount, buffer.Length);
+ _readBuffer.AsSpan(_readBufferOffset, bytesToReturn).CopyTo(buffer.Span);
+ _readBufferOffset += bytesToReturn;
+ _readBufferCount -= bytesToReturn;
+ return bytesToReturn;
+ }
+ }
+
+ public override int ReadByte()
+ {
+ return Read(_singleByteBuffer, offset: 0, count: 1) != 0 ? _singleByteBuffer[0] : -1;
+ }
+
+ public override long Seek(long offset, SeekOrigin origin)
+ => throw Error.NotSupported(Properties.Resources.NotSupported_UnseekableStream);
+
+ public override void SetLength(long value)
+ => throw Error.NotSupported(Properties.Resources.NotSupported_UnseekableStream);
+
+#if NET6_0_OR_GREATER
+ [StackTraceHidden]
+#endif
+ private void ThrowIfDisposed()
+ {
+ if (_innerStream is null)
+ {
+ ThrowObjectDisposedException();
+ }
+ }
+
+ [DoesNotReturn]
+#if NET6_0_OR_GREATER
+ [StackTraceHidden]
+#endif
+ private void ThrowObjectDisposedException()
+ {
+ throw new ObjectDisposedException(GetType().Name, Properties.Resources.ObjectDisposed_StreamClosed);
+ }
+
+ public override void Write(byte[] buffer, int offset, int count)
+ {
+ ValidateBufferArguments(buffer, offset, count);
+
+#if NETCOREAPP || NETSTANDARD2_1
+ Write(new ReadOnlySpan(buffer, offset, count));
+#else
+ WriteCore(buffer, offset, count);
+#endif
+ }
+
+#if NETCOREAPP || NETSTANDARD2_1
+ public override void Write(ReadOnlySpan buffer)
+ {
+ EnsurePreWriteConditions();
+
+ if (buffer.IsEmpty)
+ {
+ return;
+ }
+
+ int rentalLength = buffer.Length < MinWriteRentedArraySize ? MinWriteRentedArraySize :
+ buffer.Length > MaxWriteRentedArraySize ? MaxWriteRentedArraySize :
+ buffer.Length;
+
+ char[] scratchChars = ArrayPool.Shared.Rent(rentalLength);
+ byte[] scratchBytes = ArrayPool.Shared.Rent(rentalLength);
+
+ try
+ {
+ bool decoderFinished, encoderFinished;
+ do
+ {
+ // convert bytes [this] -> chars
+
+ _thisDecoder.Convert(
+ bytes: buffer,
+ chars: scratchChars,
+ flush: false,
+ out int bytesConsumed,
+ out int charsWritten,
+ out decoderFinished);
+
+ buffer = buffer.Slice(bytesConsumed);
+
+ // convert chars -> bytes [inner]
+
+ Span decodedChars = scratchChars.AsSpan(0, charsWritten);
+
+ do
+ {
+ _innerEncoder.Convert(
+ chars: decodedChars,
+ bytes: scratchBytes,
+ flush: false,
+ out int charsConsumed,
+ out int bytesWritten,
+ out encoderFinished);
+
+ decodedChars = decodedChars.Slice(charsConsumed);
+
+ // It's more likely that the inner stream provides an optimized implementation of
+ // Write(byte[], ...) over Write(ROS), so we'll prefer the byte[]-based overloads.
+
+ _innerStream.Write(scratchBytes, 0, bytesWritten);
+ } while (!encoderFinished);
+ } while (!decoderFinished);
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(scratchChars);
+ ArrayPool.Shared.Return(scratchBytes);
+ }
+ }
+#else
+ private void WriteCore(byte[] buffer, int offset, int count)
+ {
+ EnsurePreWriteConditions();
+
+ if (count == 0)
+ {
+ return;
+ }
+
+ int rentalLength = buffer.Length < MinWriteRentedArraySize ? MinWriteRentedArraySize :
+ buffer.Length > MaxWriteRentedArraySize ? MaxWriteRentedArraySize :
+ buffer.Length;
+
+ char[] scratchChars = ArrayPool.Shared.Rent(rentalLength);
+ byte[] scratchBytes = ArrayPool.Shared.Rent(rentalLength);
+
+ try
+ {
+ bool decoderFinished, encoderFinished;
+ do
+ {
+ // convert bytes [this] -> chars
+
+ _thisDecoder.Convert(
+ bytes: buffer,
+ byteIndex: offset,
+ byteCount: count,
+ chars: scratchChars,
+ charIndex: 0,
+ charCount: rentalLength,
+ flush: false,
+ out int bytesConsumed,
+ out int charsWritten,
+ out decoderFinished);
+
+ offset += bytesConsumed;
+ count -= bytesConsumed;
+
+ // convert chars -> bytes [inner]
+
+ int scratchOffset = 0;
+ do
+ {
+ _innerEncoder.Convert(
+ chars: scratchChars,
+ charIndex: scratchOffset,
+ charCount: charsWritten,
+ bytes: scratchBytes,
+ byteIndex: 0,
+ byteCount: rentalLength,
+ flush: false,
+ out int charsConsumed,
+ out int bytesWritten,
+ out encoderFinished);
+
+ scratchOffset += charsConsumed;
+ charsWritten -= charsConsumed;
+
+ // It's more likely that the inner stream provides an optimized implementation of
+ // Write(byte[], ...) over Write(ROS), so we'll prefer the byte[]-based overloads.
+
+ _innerStream.Write(scratchBytes, 0, bytesWritten);
+ } while (!encoderFinished);
+ } while (!decoderFinished);
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(scratchChars);
+ ArrayPool.Shared.Return(scratchBytes);
+ }
+ }
+#endif
+
+ public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+ {
+ ValidateBufferArguments(buffer, offset, count);
+
+#if NETCOREAPP || NETSTANDARD2_1
+ return WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask();
+#else
+ return WriteAsyncCore(buffer, offset, count, cancellationToken).AsTask();
+#endif
+ }
+
+#if NETCOREAPP || NETSTANDARD2_1
+ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default)
+ {
+ EnsurePreWriteConditions();
+
+ if (cancellationToken.IsCancellationRequested)
+ {
+ return new ValueTask(Task.FromCanceled(cancellationToken));
+ }
+
+ if (buffer.IsEmpty)
+ {
+ // ValueTask.CompletedTask
+ return default;
+ }
+
+ return WriteAsyncCore(buffer, cancellationToken);
+ async ValueTask WriteAsyncCore(ReadOnlyMemory remainingOuterEncodedBytes, CancellationToken cancellationToken)
+ {
+ int rentalLength = remainingOuterEncodedBytes.Length < MinWriteRentedArraySize ? MinWriteRentedArraySize :
+ remainingOuterEncodedBytes.Length > MaxWriteRentedArraySize ? MaxWriteRentedArraySize:
+ remainingOuterEncodedBytes.Length;
+
+ char[] scratchChars = ArrayPool.Shared.Rent(rentalLength);
+ byte[] scratchBytes = ArrayPool.Shared.Rent(rentalLength);
+
+ try
+ {
+ bool decoderFinished, encoderFinished;
+ do
+ {
+ // convert bytes [this] -> chars
+
+ _thisDecoder.Convert(
+ bytes: buffer,
+ chars: scratchChars,
+ flush: false,
+ out int bytesConsumed,
+ out int charsWritten,
+ out decoderFinished);
+
+ buffer = buffer.Slice(bytesConsumed);
+
+ // convert chars -> bytes [inner]
+
+ Span decodedChars = scratchChars.AsSpan(0, charsWritten);
+
+ do
+ {
+ _innerEncoder.Convert(
+ chars: decodedChars,
+ bytes: scratchBytes,
+ flush: false,
+ out int charsConsumed,
+ out int bytesWritten,
+ out encoderFinished);
+
+ decodedChars = decodedChars.Slice(charsConsumed);
+
+ await _innerStream.WriteAsync(scratchBytes, 0, bytesWritten, cancellationToken).ConfigureAwait(false);
+ } while (!encoderFinished);
+ } while (!decoderFinished);
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(scratchChars);
+ ArrayPool.Shared.Return(scratchBytes);
+ }
+ }
+ }
+#else
+ private ValueTask WriteAsyncCore(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+ {
+ EnsurePreWriteConditions();
+
+ if (cancellationToken.IsCancellationRequested)
+ {
+#if NETSTANDARD
+ return new ValueTask(Task.FromCanceled(cancellationToken));
+#else
+ // Lose track of the CancellationToken in this case.
+ return new ValueTask(TaskHelpers.Canceled());
+#endif
+ }
+
+ if (count == 0)
+ {
+ // ValueTask.CompletedTask
+ return default;
+ }
+
+ return WriteAsyncCore(buffer, cancellationToken);
+ async ValueTask WriteAsyncCore(ReadOnlyMemory remainingOuterEncodedBytes, CancellationToken cancellationToken)
+ {
+ int rentalLength = remainingOuterEncodedBytes.Length < MinWriteRentedArraySize ? MinWriteRentedArraySize :
+ remainingOuterEncodedBytes.Length > MaxWriteRentedArraySize ? MaxWriteRentedArraySize :
+ remainingOuterEncodedBytes.Length;
+
+ char[] scratchChars = ArrayPool.Shared.Rent(rentalLength);
+ byte[] scratchBytes = ArrayPool.Shared.Rent(rentalLength);
+
+ try
+ {
+ bool decoderFinished, encoderFinished;
+ do
+ {
+ // convert bytes [this] -> chars
+
+ _thisDecoder.Convert(
+ bytes: buffer,
+ byteIndex: offset,
+ byteCount: count,
+ chars: scratchChars,
+ charIndex: 0,
+ charCount: rentalLength,
+ flush: false,
+ out int bytesConsumed,
+ out int charsWritten,
+ out decoderFinished);
+
+ offset += bytesConsumed;
+ count -= bytesConsumed;
+
+ // convert chars -> bytes [inner]
+
+ int scratchOffset = 0;
+ do
+ {
+ _innerEncoder.Convert(
+ chars: scratchChars,
+ charIndex: scratchOffset,
+ charCount: charsWritten,
+ bytes: scratchBytes,
+ byteIndex: 0,
+ byteCount: rentalLength,
+ flush: false,
+ out int charsConsumed,
+ out int bytesWritten,
+ out encoderFinished);
+
+ scratchOffset += charsConsumed;
+ charsWritten -= charsConsumed;
+
+ await _innerStream.WriteAsync(scratchBytes, 0, bytesWritten, cancellationToken).ConfigureAwait(false);
+ } while (!encoderFinished);
+ } while (!decoderFinished);
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(scratchChars);
+ ArrayPool.Shared.Return(scratchBytes);
+ }
+ }
+ }
+#endif
+
+ public override void WriteByte(byte value)
+ {
+ _singleByteBuffer[0] = value;
+ Write(_singleByteBuffer, offset: 0, count: 1);
+ }
+
+ // From https://github.com/dotnet/runtime/blob/88868b7a781f4e5b9037b8721f30440207a7aa42/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static void ValidateBufferArguments(byte[] buffer, int offset, int count)
+ {
+ if (buffer is null)
+ {
+ throw Error.ArgumentNull(nameof(buffer));
+ }
+
+ if (offset < 0)
+ {
+ throw Error.ArgumentMustBeGreaterThanOrEqualTo(nameof(offset), offset, minValue: 0);
+ }
+
+ if ((uint)count > buffer.Length - offset)
+ {
+ throw Error.ArgumentOutOfRange(nameof(count), count, Properties.Resources.Argument_InvalidOffLen);
+ }
+ }
+ }
+}
diff --git a/src/System.Net.Http.Formatting/InvalidByteRangeException.cs b/src/System.Net.Http.Formatting/InvalidByteRangeException.cs
index 230564dbd..7e7e677bf 100644
--- a/src/System.Net.Http.Formatting/InvalidByteRangeException.cs
+++ b/src/System.Net.Http.Formatting/InvalidByteRangeException.cs
@@ -9,7 +9,7 @@
namespace System.Net.Http
{
///
- /// An exception thrown by in case none of the requested ranges
+ /// An exception thrown by in case none of the requested ranges
/// overlap with the current extend of the selected resource. The current extend of the resource
/// is indicated in the ContentRange property.
///
@@ -35,11 +35,13 @@ public InvalidByteRangeException(ContentRangeHeaderValue contentRange, string me
Initialize(contentRange);
}
+#if !NETFX_CORE // Exception is not serializable in netstandard1.3.
public InvalidByteRangeException(ContentRangeHeaderValue contentRange, SerializationInfo info, StreamingContext context)
: base(info, context)
{
Initialize(contentRange);
}
+#endif
///
/// The current extend of the resource indicated in terms of a ContentRange header field.
diff --git a/src/System.Net.Http.Formatting/MultipartFormDataStreamProvider.cs b/src/System.Net.Http.Formatting/MultipartFormDataStreamProvider.cs
index f2c997e92..1e012106b 100644
--- a/src/System.Net.Http.Formatting/MultipartFormDataStreamProvider.cs
+++ b/src/System.Net.Http.Formatting/MultipartFormDataStreamProvider.cs
@@ -11,12 +11,12 @@
namespace System.Net.Http
{
///
- /// A implementation suited for use with HTML file uploads for writing file
- /// content to a . The stream provider looks at the Content-Disposition header
+ /// A implementation suited for use with HTML file uploads for writing file
+ /// content to a . The stream provider looks at the Content-Disposition header
/// field and determines an output based on the presence of a filename parameter.
- /// If a filename parameter is present in the Content-Disposition header field then the body
+ /// If a filename parameter is present in the Content-Disposition header field then the body
/// part is written to a , otherwise it is written to a .
- /// This makes it convenient to process MIME Multipart HTML Form data which is a combination of form
+ /// This makes it convenient to process MIME Multipart HTML Form data which is a combination of form
/// data and file content.
///
public class MultipartFormDataStreamProvider : MultipartFileStreamProvider
@@ -52,7 +52,7 @@ public MultipartFormDataStreamProvider(string rootPath, int bufferSize)
///
/// This body part stream provider examines the headers provided by the MIME multipart parser
- /// and decides whether it should return a file stream or a memory stream for the body part to be
+ /// and decides whether it should return a file stream or a memory stream for the body part to be
/// written to.
///
/// The parent MIME multipart HttpContent instance.
diff --git a/src/System.Net.Http.Formatting/Properties/AssemblyInfo.cs b/src/System.Net.Http.Formatting/Properties/AssemblyInfo.cs
index 454c8861c..331be44ef 100644
--- a/src/System.Net.Http.Formatting/Properties/AssemblyInfo.cs
+++ b/src/System.Net.Http.Formatting/Properties/AssemblyInfo.cs
@@ -3,9 +3,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
-#if !NETFX_CORE
using System.Runtime.InteropServices;
-#endif
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
@@ -13,14 +11,12 @@
[assembly: AssemblyTitle("System.Net.Http.Formatting")]
[assembly: AssemblyDescription("")]
-
-#if !NETFX_CORE // GuidAttibute is not supported in portable libraries.
[assembly: Guid("7fa1ae84-36e2-46b6-812c-c985a8e65e9a")]
-#endif
#if NETSTANDARD2_0
[assembly: InternalsVisibleTo("System.Net.Http.Formatting.NetStandard.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
#elif NETFX_CORE
+[assembly: InternalsVisibleTo("Microsoft.TestCommon, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo("System.Net.Http.Formatting.NetCore.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
#else
[assembly: InternalsVisibleTo("System.Net.Http.Formatting.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
diff --git a/src/System.Net.Http.Formatting/Properties/Resources.Designer.cs b/src/System.Net.Http.Formatting/Properties/Resources.Designer.cs
index ede0dca5e..194d4eff1 100644
--- a/src/System.Net.Http.Formatting/Properties/Resources.Designer.cs
+++ b/src/System.Net.Http.Formatting/Properties/Resources.Designer.cs
@@ -11,7 +11,8 @@
namespace System.Net.Http.Properties {
using System;
using System.Reflection;
-
+
+
///
/// A strongly-typed resource class, for looking up localized strings, etc.
///
@@ -19,19 +20,19 @@ namespace System.Net.Http.Properties {
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
-
+
private static global::System.Resources.ResourceManager resourceMan;
-
+
private static global::System.Globalization.CultureInfo resourceCulture;
-
+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
-
+
///
/// Returns the cached ResourceManager instance used by this class.
///
@@ -50,7 +51,7 @@ internal Resources() {
return resourceMan;
}
}
-
+
///
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
@@ -64,7 +65,16 @@ internal Resources() {
resourceCulture = value;
}
}
-
+
+ ///
+ /// Looks up a localized string similar to Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection..
+ ///
+ internal static string Argument_InvalidOffLen {
+ get {
+ return ResourceManager.GetString("Argument_InvalidOffLen", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to Async Callback threw an exception..
///
@@ -73,7 +83,7 @@ internal static string AsyncResult_CallbackThrewException {
return ResourceManager.GetString("AsyncResult_CallbackThrewException", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The IAsyncResult implementation '{0}' tried to complete a single operation multiple times. This could be caused by an incorrect application IAsyncResult implementation or other extensibility code, such as an IAsyncResult that returns incorrect CompletedSynchronously values or invokes the AsyncCallback multiple times..
///
@@ -82,7 +92,7 @@ internal static string AsyncResult_MultipleCompletes {
return ResourceManager.GetString("AsyncResult_MultipleCompletes", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to End cannot be called twice on an AsyncResult..
///
@@ -91,7 +101,7 @@ internal static string AsyncResult_MultipleEnds {
return ResourceManager.GetString("AsyncResult_MultipleEnds", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to An incorrect IAsyncResult was provided to an 'End' method. The IAsyncResult object passed to 'End' must be the one returned from the matching 'Begin' or passed to the callback provided to 'Begin'..
///
@@ -100,7 +110,7 @@ internal static string AsyncResult_ResultMismatch {
return ResourceManager.GetString("AsyncResult_ResultMismatch", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Found zero byte ranges. There must be at least one byte range provided..
///
@@ -109,7 +119,7 @@ internal static string ByteRangeStreamContentNoRanges {
return ResourceManager.GetString("ByteRangeStreamContentNoRanges", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The range unit '{0}' is not valid. The range must have a unit of '{1}'..
///
@@ -118,7 +128,7 @@ internal static string ByteRangeStreamContentNotBytesRange {
return ResourceManager.GetString("ByteRangeStreamContentNotBytesRange", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The stream over which '{0}' provides a range view must have a length greater than or equal to 1..
///
@@ -127,7 +137,7 @@ internal static string ByteRangeStreamEmpty {
return ResourceManager.GetString("ByteRangeStreamEmpty", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The 'From' value of the range must be less than or equal to {0}..
///
@@ -136,7 +146,7 @@ internal static string ByteRangeStreamInvalidFrom {
return ResourceManager.GetString("ByteRangeStreamInvalidFrom", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to An attempt was made to move the position before the beginning of the stream..
///
@@ -145,7 +155,7 @@ internal static string ByteRangeStreamInvalidOffset {
return ResourceManager.GetString("ByteRangeStreamInvalidOffset", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to None of the requested ranges ({0}) overlap with the current extent of the selected resource..
///
@@ -154,7 +164,7 @@ internal static string ByteRangeStreamNoneOverlap {
return ResourceManager.GetString("ByteRangeStreamNoneOverlap", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The requested range ({0}) does not overlap with the current extent of the selected resource..
///
@@ -163,7 +173,7 @@ internal static string ByteRangeStreamNoOverlap {
return ResourceManager.GetString("ByteRangeStreamNoOverlap", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The stream over which '{0}' provides a range view must be seekable..
///
@@ -172,7 +182,7 @@ internal static string ByteRangeStreamNotSeekable {
return ResourceManager.GetString("ByteRangeStreamNotSeekable", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to This is a read-only stream..
///
@@ -181,7 +191,7 @@ internal static string ByteRangeStreamReadOnly {
return ResourceManager.GetString("ByteRangeStreamReadOnly", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to A null '{0}' is not valid..
///
@@ -190,7 +200,7 @@ internal static string CannotHaveNullInList {
return ResourceManager.GetString("CannotHaveNullInList", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' of '{1}' cannot be used as a supported media type because it is a media range..
///
@@ -199,7 +209,7 @@ internal static string CannotUseMediaRangeForSupportedMediaType {
return ResourceManager.GetString("CannotUseMediaRangeForSupportedMediaType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' type cannot accept a null value for the value type '{1}'..
///
@@ -208,7 +218,7 @@ internal static string CannotUseNullValueType {
return ResourceManager.GetString("CannotUseNullValueType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The specified value is not a valid cookie name..
///
@@ -217,7 +227,7 @@ internal static string CookieInvalidName {
return ResourceManager.GetString("CookieInvalidName", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Cookie cannot be null..
///
@@ -226,7 +236,7 @@ internal static string CookieNull {
return ResourceManager.GetString("CookieNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' list is invalid because it contains one or more null items..
///
@@ -235,7 +245,7 @@ internal static string DelegatingHandlerArrayContainsNullItem {
return ResourceManager.GetString("DelegatingHandlerArrayContainsNullItem", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' list is invalid because the property '{1}' of '{2}' is not null..
///
@@ -244,7 +254,7 @@ internal static string DelegatingHandlerArrayHasNonNullInnerHandler {
return ResourceManager.GetString("DelegatingHandlerArrayHasNonNullInnerHandler", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error reading HTML form URL-encoded data stream..
///
@@ -253,7 +263,7 @@ internal static string ErrorReadingFormUrlEncodedStream {
return ResourceManager.GetString("ErrorReadingFormUrlEncodedStream", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Mismatched types at node '{0}'..
///
@@ -262,7 +272,7 @@ internal static string FormUrlEncodedMismatchingTypes {
return ResourceManager.GetString("FormUrlEncodedMismatchingTypes", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing HTML form URL-encoded data, byte {0}..
///
@@ -271,7 +281,7 @@ internal static string FormUrlEncodedParseError {
return ResourceManager.GetString("FormUrlEncodedParseError", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid HTTP status code: '{0}'. The status code must be between {1} and {2}..
///
@@ -280,7 +290,7 @@ internal static string HttpInvalidStatusCode {
return ResourceManager.GetString("HttpInvalidStatusCode", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid HTTP version: '{0}'. The version must start with the characters '{1}'..
///
@@ -289,7 +299,7 @@ internal static string HttpInvalidVersion {
return ResourceManager.GetString("HttpInvalidVersion", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' of the '{1}' has already been read..
///
@@ -298,7 +308,7 @@ internal static string HttpMessageContentAlreadyRead {
return ResourceManager.GetString("HttpMessageContentAlreadyRead", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' must be seekable in order to create an '{1}' instance containing an entity body. .
///
@@ -307,7 +317,7 @@ internal static string HttpMessageContentStreamMustBeSeekable {
return ResourceManager.GetString("HttpMessageContentStreamMustBeSeekable", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error reading HTTP message..
///
@@ -316,7 +326,7 @@ internal static string HttpMessageErrorReading {
return ResourceManager.GetString("HttpMessageErrorReading", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid '{0}' instance provided. It does not have a content type header with a value of '{1}'..
///
@@ -325,7 +335,7 @@ internal static string HttpMessageInvalidMediaType {
return ResourceManager.GetString("HttpMessageInvalidMediaType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to HTTP Request URI cannot be an empty string..
///
@@ -334,7 +344,7 @@ internal static string HttpMessageParserEmptyUri {
return ResourceManager.GetString("HttpMessageParserEmptyUri", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing HTTP message header byte {0} of message {1}..
///
@@ -343,7 +353,7 @@ internal static string HttpMessageParserError {
return ResourceManager.GetString("HttpMessageParserError", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to An invalid number of '{0}' header fields were present in the HTTP Request. It must contain exactly one '{0}' header field but found {1}..
///
@@ -352,7 +362,7 @@ internal static string HttpMessageParserInvalidHostCount {
return ResourceManager.GetString("HttpMessageParserInvalidHostCount", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid URI scheme: '{0}'. The URI scheme must be a valid '{1}' scheme..
///
@@ -361,7 +371,7 @@ internal static string HttpMessageParserInvalidUriScheme {
return ResourceManager.GetString("HttpMessageParserInvalidUriScheme", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid array at node '{0}'..
///
@@ -370,7 +380,7 @@ internal static string InvalidArrayInsert {
return ResourceManager.GetString("InvalidArrayInsert", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Traditional style array without '[]' is not supported with nested object at location {0}..
///
@@ -379,7 +389,7 @@ internal static string JQuery13CompatModeNotSupportNestedJson {
return ResourceManager.GetString("JQuery13CompatModeNotSupportNestedJson", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' method returned null. It must return a JSON serializer instance..
///
@@ -388,7 +398,7 @@ internal static string JsonSerializerFactoryReturnedNull {
return ResourceManager.GetString("JsonSerializerFactoryReturnedNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' method threw an exception when attempting to create a JSON serializer..
///
@@ -397,7 +407,7 @@ internal static string JsonSerializerFactoryThrew {
return ResourceManager.GetString("JsonSerializerFactoryThrew", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The maximum read depth ({0}) has been exceeded because the form url-encoded data being read has more levels of nesting than is allowed..
///
@@ -406,7 +416,7 @@ internal static string MaxDepthExceeded {
return ResourceManager.GetString("MaxDepthExceeded", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The number of keys in a NameValueCollection has exceeded the limit of '{0}'. You can adjust it by modifying the MaxHttpCollectionKeys property on the '{1}' class..
///
@@ -415,7 +425,7 @@ internal static string MaxHttpCollectionKeyLimitReached {
return ResourceManager.GetString("MaxHttpCollectionKeyLimitReached", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing BSON data; unable to read content as a {0}..
///
@@ -424,7 +434,7 @@ internal static string MediaTypeFormatter_BsonParseError_MissingData {
return ResourceManager.GetString("MediaTypeFormatter_BsonParseError_MissingData", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing BSON data; unexpected dictionary content: {0} entries, first key '{1}'..
///
@@ -433,7 +443,7 @@ internal static string MediaTypeFormatter_BsonParseError_UnexpectedData {
return ResourceManager.GetString("MediaTypeFormatter_BsonParseError_UnexpectedData", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' method returned null. It must return a JSON reader instance..
///
@@ -442,7 +452,7 @@ internal static string MediaTypeFormatter_JsonReaderFactoryReturnedNull {
return ResourceManager.GetString("MediaTypeFormatter_JsonReaderFactoryReturnedNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' method returned null. It must return a JSON writer instance..
///
@@ -451,7 +461,7 @@ internal static string MediaTypeFormatter_JsonWriterFactoryReturnedNull {
return ResourceManager.GetString("MediaTypeFormatter_JsonWriterFactoryReturnedNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The media type formatter of type '{0}' does not support reading because it does not implement the ReadFromStreamAsync method..
///
@@ -460,7 +470,7 @@ internal static string MediaTypeFormatterCannotRead {
return ResourceManager.GetString("MediaTypeFormatterCannotRead", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The media type formatter of type '{0}' does not support reading because it does not implement the ReadFromStream method..
///
@@ -469,7 +479,7 @@ internal static string MediaTypeFormatterCannotReadSync {
return ResourceManager.GetString("MediaTypeFormatterCannotReadSync", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The media type formatter of type '{0}' does not support writing because it does not implement the WriteToStreamAsync method..
///
@@ -478,7 +488,7 @@ internal static string MediaTypeFormatterCannotWrite {
return ResourceManager.GetString("MediaTypeFormatterCannotWrite", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The media type formatter of type '{0}' does not support writing because it does not implement the WriteToStream method..
///
@@ -487,7 +497,7 @@ internal static string MediaTypeFormatterCannotWriteSync {
return ResourceManager.GetString("MediaTypeFormatterCannotWriteSync", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to No encoding found for media type formatter '{0}'. There must be at least one supported encoding registered in order for the media type formatter to read or write content..
///
@@ -496,7 +506,7 @@ internal static string MediaTypeFormatterNoEncoding {
return ResourceManager.GetString("MediaTypeFormatterNoEncoding", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to MIME multipart boundary cannot end with an empty space..
///
@@ -505,7 +515,7 @@ internal static string MimeMultipartParserBadBoundary {
return ResourceManager.GetString("MimeMultipartParserBadBoundary", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Did not find required '{0}' header field in MIME multipart body part..
///
@@ -514,7 +524,7 @@ internal static string MultipartFormDataStreamProviderNoContentDisposition {
return ResourceManager.GetString("MultipartFormDataStreamProviderNoContentDisposition", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Could not determine a valid local file name for the multipart body part..
///
@@ -523,7 +533,7 @@ internal static string MultipartStreamProviderInvalidLocalFileName {
return ResourceManager.GetString("MultipartStreamProviderInvalidLocalFileName", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Nested bracket is not valid for '{0}' data at position {1}..
///
@@ -532,7 +542,7 @@ internal static string NestedBracketNotValid {
return ResourceManager.GetString("NestedBracketNotValid", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to A non-null request URI must be provided to determine if a '{0}' matches a given request or response message..
///
@@ -541,7 +551,7 @@ internal static string NonNullUriRequiredForMediaTypeMapping {
return ResourceManager.GetString("NonNullUriRequiredForMediaTypeMapping", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to No MediaTypeFormatter is available to read an object of type '{0}' from content with media type '{1}'..
///
@@ -550,7 +560,34 @@ internal static string NoReadSerializerAvailable {
return ResourceManager.GetString("NoReadSerializerAvailable", resourceCulture);
}
}
-
+
+ ///
+ /// Looks up a localized string similar to Stream does not support reading..
+ ///
+ internal static string NotSupported_UnreadableStream {
+ get {
+ return ResourceManager.GetString("NotSupported_UnreadableStream", resourceCulture);
+ }
+ }
+
+ ///
+ /// Looks up a localized string similar to Stream does not support seeking..
+ ///
+ internal static string NotSupported_UnseekableStream {
+ get {
+ return ResourceManager.GetString("NotSupported_UnseekableStream", resourceCulture);
+ }
+ }
+
+ ///
+ /// Looks up a localized string similar to Stream does not support writing..
+ ///
+ internal static string NotSupported_UnwritableStream {
+ get {
+ return ResourceManager.GetString("NotSupported_UnwritableStream", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to An object of type '{0}' cannot be used with a type parameter of '{1}'..
///
@@ -559,7 +596,7 @@ internal static string ObjectAndTypeDisagree {
return ResourceManager.GetString("ObjectAndTypeDisagree", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The configured formatter '{0}' cannot write an object of type '{1}'..
///
@@ -568,7 +605,16 @@ internal static string ObjectContent_FormatterCannotWriteType {
return ResourceManager.GetString("ObjectContent_FormatterCannotWriteType", resourceCulture);
}
}
-
+
+ ///
+ /// Looks up a localized string similar to Cannot access a closed stream..
+ ///
+ internal static string ObjectDisposed_StreamClosed {
+ get {
+ return ResourceManager.GetString("ObjectDisposed_StreamClosed", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to Query string name cannot be null..
///
@@ -577,7 +623,7 @@ internal static string QueryStringNameShouldNotNull {
return ResourceManager.GetString("QueryStringNameShouldNotNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Unexpected end of HTTP message stream. HTTP message is not complete..
///
@@ -586,7 +632,7 @@ internal static string ReadAsHttpMessageUnexpectedTermination {
return ResourceManager.GetString("ReadAsHttpMessageUnexpectedTermination", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid '{0}' instance provided. It does not have a '{1}' content-type header with a '{2}' parameter..
///
@@ -595,7 +641,7 @@ internal static string ReadAsMimeMultipartArgumentNoBoundary {
return ResourceManager.GetString("ReadAsMimeMultipartArgumentNoBoundary", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid '{0}' instance provided. It does not have a content-type header value. '{0}' instances must have a content-type header starting with '{1}'..
///
@@ -604,7 +650,7 @@ internal static string ReadAsMimeMultipartArgumentNoContentType {
return ResourceManager.GetString("ReadAsMimeMultipartArgumentNoContentType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Invalid '{0}' instance provided. It does not have a content type header starting with '{1}'..
///
@@ -613,7 +659,7 @@ internal static string ReadAsMimeMultipartArgumentNoMultipart {
return ResourceManager.GetString("ReadAsMimeMultipartArgumentNoMultipart", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error reading MIME multipart body part..
///
@@ -622,7 +668,7 @@ internal static string ReadAsMimeMultipartErrorReading {
return ResourceManager.GetString("ReadAsMimeMultipartErrorReading", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error writing MIME multipart body part to output stream..
///
@@ -631,7 +677,7 @@ internal static string ReadAsMimeMultipartErrorWriting {
return ResourceManager.GetString("ReadAsMimeMultipartErrorWriting", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing MIME multipart body part header byte {0} of data segment {1}..
///
@@ -640,7 +686,7 @@ internal static string ReadAsMimeMultipartHeaderParseError {
return ResourceManager.GetString("ReadAsMimeMultipartHeaderParseError", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Error parsing MIME multipart message byte {0} of data segment {1}..
///
@@ -649,7 +695,7 @@ internal static string ReadAsMimeMultipartParseError {
return ResourceManager.GetString("ReadAsMimeMultipartParseError", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The stream provider of type '{0}' threw an exception..
///
@@ -658,7 +704,7 @@ internal static string ReadAsMimeMultipartStreamProviderException {
return ResourceManager.GetString("ReadAsMimeMultipartStreamProviderException", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The stream provider of type '{0}' returned null. It must return a writable '{1}' instance..
///
@@ -667,7 +713,7 @@ internal static string ReadAsMimeMultipartStreamProviderNull {
return ResourceManager.GetString("ReadAsMimeMultipartStreamProviderNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The stream provider of type '{0}' returned a read-only stream. It must return a writable '{1}' instance..
///
@@ -676,7 +722,7 @@ internal static string ReadAsMimeMultipartStreamProviderReadOnly {
return ResourceManager.GetString("ReadAsMimeMultipartStreamProviderReadOnly", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Unexpected end of MIME multipart stream. MIME multipart message is not complete..
///
@@ -685,7 +731,7 @@ internal static string ReadAsMimeMultipartUnexpectedTermination {
return ResourceManager.GetString("ReadAsMimeMultipartUnexpectedTermination", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' method in '{1}' returned null. It must return a RemoteStreamResult instance containing a writable stream and a valid URL..
///
@@ -694,7 +740,7 @@ internal static string RemoteStreamInfoCannotBeNull {
return ResourceManager.GetString("RemoteStreamInfoCannotBeNull", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The '{0}' serializer cannot serialize the type '{1}'..
///
@@ -703,7 +749,7 @@ internal static string SerializerCannotSerializeType {
return ResourceManager.GetString("SerializerCannotSerializeType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to There is an unmatched opened bracket for the '{0}' at position {1}..
///
@@ -712,7 +758,7 @@ internal static string UnMatchedBracketNotValid {
return ResourceManager.GetString("UnMatchedBracketNotValid", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to Indentation is not supported by '{0}'..
///
@@ -721,7 +767,7 @@ internal static string UnsupportedIndent {
return ResourceManager.GetString("UnsupportedIndent", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The object of type '{0}' returned by {1} must be an instance of either XmlObjectSerializer or XmlSerializer..
///
@@ -730,7 +776,7 @@ internal static string XmlMediaTypeFormatter_InvalidSerializerType {
return ResourceManager.GetString("XmlMediaTypeFormatter_InvalidSerializerType", resourceCulture);
}
}
-
+
///
/// Looks up a localized string similar to The object returned by {0} must not be a null value..
///
diff --git a/src/System.Net.Http.Formatting/Properties/Resources.resx b/src/System.Net.Http.Formatting/Properties/Resources.resx
index d20156c5a..1b8df2cd2 100644
--- a/src/System.Net.Http.Formatting/Properties/Resources.resx
+++ b/src/System.Net.Http.Formatting/Properties/Resources.resx
@@ -1,17 +1,17 @@
-
@@ -342,4 +342,19 @@
An attempt was made to move the position before the beginning of the stream.
+
+ Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
+
+
+ Stream does not support reading.
+
+
+ Stream does not support seeking.
+
+
+ Stream does not support writing.
+
+
+ Cannot access a closed stream.
+
\ No newline at end of file
diff --git a/src/System.Net.Http.Formatting/Settings.StyleCop b/src/System.Net.Http.Formatting/Settings.StyleCop
deleted file mode 100644
index 5b387864d..000000000
--- a/src/System.Net.Http.Formatting/Settings.StyleCop
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
- UriQueryUtility.cs
-
-
- False
-
-
-
-
\ No newline at end of file
diff --git a/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj b/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj
index 793e175a8..d52549eec 100644
--- a/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj
+++ b/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj
@@ -14,173 +14,84 @@
Client1591
+
-
- ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll
+
+
+ ..\..\packages\System.Buffers.4.5.1\lib\netstandard2.0\System.Buffers.dllFalseFalse
-
- ..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll
+
+
+
+ ..\..\packages\System.Memory.4.5.5\lib\netstandard2.0\System.Memory.dllFalseFalse
-
-
-
+
+ ..\..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll
+ False
+ False
+
-
-
-
- Properties\CommonAssemblyInfo.cs
-
-
- Common\CollectionExtensions.cs
-
-
- Common\Error.cs
-
-
- Common\ListWrapperCollection.cs
-
-
- Common\TaskHelpers.cs
-
-
- Common\TaskHelpersExtensions.cs
-
-
- Common\UriQueryUtility.cs
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- True
- True
- Resources.resx
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+ ..\..\packages\NETStandard.Library.2.0.3\lib\netstandard2.0\netstandard.dll
+ False
+ False
+
+
+ ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll
+ False
+ False
+
+
+ ..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll
+ False
+ False
+
+
+
+
+
+
+
+
+
+
+
- Properties\CommonWebApiResources.Designer.cs
True
- TrueCommonWebApiResources.resx
+ True
+ Properties\CommonWebApiResources.Designer.cs
-
-
- Properties\CommonWebApiResources.resx
ResXFileCodeGeneratorCommonWebApiResources.Designer.cs
+ Properties\CommonWebApiResources.resx
-
-
+
+
+ True
+ Resources.resx
+ True
+ ResXFileCodeGeneratorResources.Designer.csDesigner
-
-
-
- CodeAnalysisDictionary.xml
-
-
-
+
+
+
+
-
\ No newline at end of file
+
diff --git a/src/System.Net.Http.Formatting/UriExtensions.cs b/src/System.Net.Http.Formatting/UriExtensions.cs
index ab83ec938..cf531431a 100644
--- a/src/System.Net.Http.Formatting/UriExtensions.cs
+++ b/src/System.Net.Http.Formatting/UriExtensions.cs
@@ -18,21 +18,12 @@ namespace System.Net.Http
[EditorBrowsable(EditorBrowsableState.Never)]
public static class UriExtensions
{
-#if NETFX_CORE
- ///
- /// Parses the query portion of the specified .
- ///
- /// The instance from which to read.
- /// A containing the parsed result.
- public static HttpValueCollection ParseQueryString(this Uri address)
-#else
///
/// Parses the query portion of the specified .
///
/// The instance from which to read.
/// A containing the parsed result.
public static NameValueCollection ParseQueryString(this Uri address)
-#endif
{
if (address == null)
{
diff --git a/src/System.Net.Http.Formatting/packages.config b/src/System.Net.Http.Formatting/packages.config
index 509dcb7d1..1ff2795f6 100644
--- a/src/System.Net.Http.Formatting/packages.config
+++ b/src/System.Net.Http.Formatting/packages.config
@@ -2,4 +2,8 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/System.Web.Http/Settings.StyleCop b/src/System.Web.Http/Settings.StyleCop
deleted file mode 100644
index ddf8ec764..000000000
--- a/src/System.Web.Http/Settings.StyleCop
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
- DynamicQueryable.cs
- UriQueryUtility.cs
-
-
- False
-
-
-
-
\ No newline at end of file
diff --git a/src/System.Web.Http/System.Web.Http.csproj b/src/System.Web.Http/System.Web.Http.csproj
index eaa991282..27041a371 100644
--- a/src/System.Web.Http/System.Web.Http.csproj
+++ b/src/System.Web.Http/System.Web.Http.csproj
@@ -102,9 +102,6 @@
Common\TypeExtensions.cs
-
- Common\UriQueryUtility.cs
-
diff --git a/test/Common/UriQueryUtilityTest.cs b/test/Common/UriQueryUtilityTest.cs
index 4b325abbd..ac0307cae 100644
--- a/test/Common/UriQueryUtilityTest.cs
+++ b/test/Common/UriQueryUtilityTest.cs
@@ -10,7 +10,7 @@
namespace System.Net.Http
{
- public class UriQueryUtilityTest
+ public class WebUtilityTest
{
public static TheoryDataSet UriQueryData
{
@@ -23,19 +23,20 @@ public static TheoryDataSet UriQueryData
[Fact]
public void TypeIsCorrect()
{
- Assert.Type.HasProperties(typeof(UriQueryUtility), TypeAssert.TypeProperties.IsClass | TypeAssert.TypeProperties.IsStatic);
+ Assert.Type.HasProperties(typeof(WebUtility),
+ TypeAssert.TypeProperties.IsStatic | TypeAssert.TypeProperties.IsPublicVisibleClass);
}
[Fact]
public void UrlEncode_ReturnsNull()
{
- Assert.Null(UriQueryUtility.UrlEncode(null));
+ Assert.Null(WebUtility.UrlEncode(null));
}
[Fact]
public void UrlDecode_ReturnsNull()
{
- Assert.Null(UriQueryUtility.UrlDecode(null));
+ Assert.Null(WebUtility.UrlDecode(null));
}
[Fact]
diff --git a/test/Microsoft.TestCommon/ExceptionAssertions.cs b/test/Microsoft.TestCommon/ExceptionAssertions.cs
index 11b36e9e6..5cca7e7c8 100644
--- a/test/Microsoft.TestCommon/ExceptionAssertions.cs
+++ b/test/Microsoft.TestCommon/ExceptionAssertions.cs
@@ -6,6 +6,9 @@
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
+#if NETFX_CORE
+using System.Web.Http;
+#endif
namespace Microsoft.TestCommon
{
@@ -494,12 +497,17 @@ public static HttpException ThrowsHttpException(Action testCode, string exceptio
/// Pass true to allow exceptions which derive from TException; pass false, otherwise
/// The exception that was thrown, when successful
/// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown
- public static InvalidEnumArgumentException ThrowsInvalidEnumArgument(Action testCode, string paramName, int invalidValue, Type enumType, bool allowDerivedExceptions = false)
+ public static ArgumentException ThrowsInvalidEnumArgument(Action testCode, string paramName, int invalidValue, Type enumType, bool allowDerivedExceptions = false)
{
string message = String.Format(CultureReplacer.DefaultCulture,
"The value of argument '{0}' ({1}) is invalid for Enum type '{2}'.{3}Parameter name: {0}",
paramName, invalidValue, enumType.Name, Environment.NewLine);
+
+#if NETFX_CORE // InvalidEnumArgumentException not available in netstandard1.3.
+ return Throws(testCode, message, allowDerivedExceptions);
+#else
return Throws(testCode, message, allowDerivedExceptions);
+#endif
}
///
diff --git a/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj b/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj
index dae43e747..4def411d9 100644
--- a/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj
+++ b/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj
@@ -1,23 +1,34 @@
- netcoreapp2.1;net452
- ..\..\bin\$(Configuration)\Test\
+ net452;net462;netcoreapp2.1$(Configurations);CodeAnalysis
+ $(DefineConstants);NETFX_COREfalse
+ ..\..\bin\$(Configuration)\Test\
+ $(OutputPath)NetCore\
+
-
+
+
+
-
+ Condition=" '$(TargetFrameworkIdentifier)' != '.NETFramework' " />
+
+
+
diff --git a/test/System.Net.Http.Formatting.NetCore.Test/System.Net.Http.Formatting.NetCore.Test.csproj b/test/System.Net.Http.Formatting.NetCore.Test/System.Net.Http.Formatting.NetCore.Test.csproj
index d60e9a90c..863474dbc 100644
--- a/test/System.Net.Http.Formatting.NetCore.Test/System.Net.Http.Formatting.NetCore.Test.csproj
+++ b/test/System.Net.Http.Formatting.NetCore.Test/System.Net.Http.Formatting.NetCore.Test.csproj
@@ -1,105 +1,38 @@
- net462
+ netcoreapp2.1;net462System.Net.Http
- System.Net.Http.Formatting.NetCore.Test..\..\bin\$(Configuration)\Test\NetCore\$(Configurations);CodeAnalysisfalse$(DefineConstants);NETFX_COREtrue
+ true
+
+
+ allruntime; build; native; contentfiles; analyzers
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+ %(RecursiveDir)\%(Filename).cs
+
+
-
-
-
+
+
-
-
-
-
-
-
- This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
-
-
-
-
-
diff --git a/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj b/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj
index 64d06cf7e..6d6405cb6 100644
--- a/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj
+++ b/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj
@@ -3,32 +3,35 @@
netcoreapp2.1;net462System.Net.Http
- System.Net.Http.Formatting.NetStandard.Test..\..\bin\$(Configuration)\Test\NetStandard\$(Configurations);CodeAnalysisfalse
+ $(DefineConstants);Testing_NetStandard2_0true
+ true
+
+
+
+ allruntime; build; native; contentfiles; analyzers
-
-
-
+
+
%(RecursiveDir)\%(Filename).cs
-
-
+
-
-
+
diff --git a/test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs b/test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs
index 84178a3a6..5e7672b2e 100644
--- a/test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs
+++ b/test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs
@@ -356,7 +356,6 @@ public static TestData StandardHttpContents
//// TODO: make this list compose from other data?
// Collection of legal instances of all standard MediaTypeMapping types
-#if !NETFX_CORE // not present in portable library version
public static TestData StandardMediaTypeMappings
{
get
@@ -375,7 +374,6 @@ public static TestData QueryStringMappings
});
}
}
-#endif
public static TestData LegalUriPathExtensions
{
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs
index e28096df8..3082b265d 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs
@@ -149,9 +149,7 @@ void CopyConstructor()
// Arrange
TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter()
{
-#if !NETFX_CORE // MaxDepth is not supported in portable library
MaxDepth = 42,
-#endif
};
// Replace serializable settings and switch one property's value
@@ -163,16 +161,13 @@ void CopyConstructor()
TestBsonMediaTypeFormatter derivedFormatter = new TestBsonMediaTypeFormatter(formatter);
// Assert
-#if !NETFX_CORE // MaxDepth is not supported in portable library
Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth);
-#endif
Assert.NotSame(oldSettings, formatter.SerializerSettings);
Assert.NotEqual(oldSettings.CheckAdditionalContent, formatter.SerializerSettings.CheckAdditionalContent);
Assert.Same(formatter.SerializerSettings, derivedFormatter.SerializerSettings);
Assert.Same(formatter.SerializerSettings.ContractResolver, derivedFormatter.SerializerSettings.ContractResolver);
}
-#if !NETFX_CORE // MaxDepth is not supported in portable library
[Fact]
public void MaxDepth_RoundTrips()
{
@@ -187,7 +182,6 @@ public void MaxDepth_RoundTrips()
illegalUpperValue: null,
roundTripTestValue: 256);
}
-#endif
[Theory]
[TestDataSet(typeof(CommonUnitTestDataSets), "RepresentativeValueAndRefTypeTestDataCollection")]
@@ -262,14 +256,8 @@ public async Task FormatterThrowsOnWriteWithInvalidContent()
BsonMediaTypeFormatter formatter = new BsonMediaTypeFormatter();
HttpContent content = new StringContent(String.Empty);
MemoryStream stream = new MemoryStream();
-#if NETFX_CORE // Separate Bson package (not yet used in NETCore project) calculates the path in exceptions differently
- string expectedPath = string.Empty;
-#else
- string expectedPath = "Value";
-#endif
- string expectedMessage = string.Format(
- "Value is too large to fit in a signed 32 bit integer. BSON does not support unsigned values. Path '{0}'.",
- expectedPath);
+ string expectedMessage =
+ "Value is too large to fit in a signed 32 bit integer. BSON does not support unsigned values. Path 'Value'.";
// Act & Assert
// Note error message is not quite correct: BSON supports byte, ushort, and smaller uint / ulong values.
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/DataContractJsonMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/DataContractJsonMediaTypeFormatterTests.cs
index e576a6924..55ad646b4 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/DataContractJsonMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/DataContractJsonMediaTypeFormatterTests.cs
@@ -87,6 +87,7 @@ public void CanReadType_ReturnsExpectedValues(Type variationType, object testDat
Assert.False(isSerializable != canSupport && isSerializable, String.Format("2nd CanReadType returned wrong value for '{0}'.", variationType));
}
+#if !NETFX_CORE // XsdDataContractExporterMethods unconditionally return true without XsdDataContractExporter to use.
[Fact]
public void CanReadType_ReturnsFalse_ForInvalidDataContracts()
{
@@ -100,6 +101,7 @@ public void CanWriteType_ReturnsFalse_ForInvalidDataContracts()
JsonMediaTypeFormatter formatter = new DataContractJsonMediaTypeFormatter();
Assert.False(formatter.CanWriteType(typeof(InvalidDataContract)));
}
+#endif
public class InvalidDataContract
{
@@ -216,12 +218,6 @@ public void UseDataContractJsonSerializer_True_Indent_Throws()
public override Task ReadFromStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- if (!isDefaultEncoding)
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
DataContractJsonMediaTypeFormatter formatter = new DataContractJsonMediaTypeFormatter();
string formattedContent = "\"" + content + "\"";
@@ -234,14 +230,6 @@ public override Task ReadFromStreamAsync_UsesCorrectCharacterEncoding(string con
public override Task WriteToStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- // DataContractJsonSerializer does not honor the value of byteOrderMark in the UnicodeEncoding ctor.
- // It doesn't include the BOM when byteOrderMark is set to true.
- if (!isDefaultEncoding || encoding != "utf-8")
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
DataContractJsonMediaTypeFormatter formatter = new DataContractJsonMediaTypeFormatter();
string formattedContent = "\"" + content + "\"";
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromContentTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromContentTests.cs
index dec45eab4..fcc01232d 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromContentTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromContentTests.cs
@@ -340,11 +340,11 @@ private static void BuildParams(string prefix, JToken jsonValue, List re
{
dateStr = dateStr.Substring(1, dateStr.Length - 2);
}
- results.Add(prefix + "=" + UriQueryUtility.UrlEncode(dateStr));
+ results.Add(prefix + "=" + WebUtility.UrlEncode(dateStr));
}
else
{
- results.Add(prefix + "=" + UriQueryUtility.UrlEncode(jsonPrimitive.Value.ToString()));
+ results.Add(prefix + "=" + WebUtility.UrlEncode(jsonPrimitive.Value.ToString()));
}
}
}
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromUriQueryTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromUriQueryTests.cs
index 4dee5366e..b56717039 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromUriQueryTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/FormUrlEncodedFromUriQueryTests.cs
@@ -279,11 +279,11 @@ private static void BuildParams(string prefix, JToken jsonValue, List re
{
dateStr = dateStr.Substring(1, dateStr.Length - 2);
}
- results.Add(prefix + "=" + UriQueryUtility.UrlEncode(dateStr));
+ results.Add(prefix + "=" + WebUtility.UrlEncode(dateStr));
}
else
{
- results.Add(prefix + "=" + UriQueryUtility.UrlEncode(jsonPrimitive.Value.ToString()));
+ results.Add(prefix + "=" + WebUtility.UrlEncode(jsonPrimitive.Value.ToString()));
}
}
}
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/JsonMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/JsonMediaTypeFormatterTests.cs
index 3d7898ddb..76a497182 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/JsonMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/JsonMediaTypeFormatterTests.cs
@@ -83,18 +83,14 @@ void CopyConstructor()
TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter()
{
Indent = true,
-#if !NETFX_CORE // MaxDepth and DCJS not supported in client portable library
MaxDepth = 42,
UseDataContractJsonSerializer = true
-#endif
};
TestJsonMediaTypeFormatter derivedFormatter = new TestJsonMediaTypeFormatter(formatter);
-#if !NETFX_CORE // MaxDepth and DCJS not supported in client portable library
Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth);
Assert.Equal(formatter.UseDataContractJsonSerializer, derivedFormatter.UseDataContractJsonSerializer);
-#endif
Assert.Equal(formatter.Indent, derivedFormatter.Indent);
Assert.Same(formatter.SerializerSettings, derivedFormatter.SerializerSettings);
Assert.Same(formatter.SerializerSettings.ContractResolver, derivedFormatter.SerializerSettings.ContractResolver);
@@ -117,7 +113,6 @@ public void Indent_RoundTrips()
expectedDefaultValue: false);
}
-#if !NETFX_CORE // MaxDepth is not supported in portable libraries
[Fact]
public void MaxDepth_RoundTrips()
{
@@ -131,7 +126,6 @@ public void MaxDepth_RoundTrips()
illegalUpperValue: null,
roundTripTestValue: 256);
}
-#endif
[Theory]
[TestDataSet(typeof(CommonUnitTestDataSets), "RepresentativeValueAndRefTypeTestDataCollection")]
@@ -230,7 +224,6 @@ public async Task FormatterThrowsOnReadWhenOverridenCreateReturnsNull()
Assert.NotNull(formatter.InnerJsonSerializer);
}
-#if !NETFX_CORE
[Fact]
public async Task DataContractFormatterThrowsOnWriteWhenOverridenCreateFails()
{
@@ -315,7 +308,6 @@ public async Task DataContractFormatterThrowsOnReadWhenOverridenCreateReturnsNul
Assert.NotNull(formatter.InnerDataContractSerializer);
Assert.Null(formatter.InnerJsonSerializer);
}
-#endif
[Fact]
public void CanReadType_ReturnsTrueOnJtoken()
@@ -499,9 +491,7 @@ public async Task UseDataContractJsonSerializer_False()
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter
{
-#if !NETFX_CORE // No JsonSerializer in portable libraries
UseDataContractJsonSerializer = false
-#endif
};
MemoryStream memoryStream = new MemoryStream();
HttpContent content = new StringContent(String.Empty);
@@ -518,9 +508,7 @@ public async Task UseDataContractJsonSerializer_False_Indent()
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter
{
-#if !NETFX_CORE // No JsonSerializer in portable libraries
UseDataContractJsonSerializer = false,
-#endif
Indent = true
};
MemoryStream memoryStream = new MemoryStream();
@@ -538,9 +526,7 @@ public async Task UseJsonFormatterWithNull(Type type)
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter
{
-#if !NETFX_CORE // No JsonSerializer in portable libraries
UseDataContractJsonSerializer = false
-#endif
};
MemoryStream memoryStream = new MemoryStream();
HttpContent content = new StringContent(String.Empty);
@@ -633,7 +619,6 @@ public override JsonSerializer CreateJsonSerializer()
return InnerJsonSerializer;
}
-#if !NETFX_CORE
public override DataContractJsonSerializer CreateDataContractSerializer(Type type)
{
InnerDataContractSerializer = base.CreateDataContractSerializer(type);
@@ -650,7 +635,6 @@ public override DataContractJsonSerializer CreateDataContractSerializer(Type typ
return InnerDataContractSerializer;
}
-#endif
}
private bool IsTypeSerializableWithJsonSerializer(Type type, object obj)
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/JsonNetValidationTest.cs b/test/System.Net.Http.Formatting.Test/Formatting/JsonNetValidationTest.cs
index eac53df04..60bad376d 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/JsonNetValidationTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/JsonNetValidationTest.cs
@@ -43,7 +43,6 @@ public static TheoryDataSet Theories
}
}
-#if !NETFX_CORE // IRequiredMemeberSelector is not in portable libraries because there is no model state on the client.
[Theory]
[PropertyData("Theories")]
public async Task ModelErrorsPopulatedWithValidationErrors(string json, Type type, int expectedErrors)
@@ -56,7 +55,6 @@ public async Task ModelErrorsPopulatedWithValidationErrors(string json, Type typ
mockLogger.Verify(mock => mock.LogError(It.IsAny(), It.IsAny()), Times.Exactly(expectedErrors));
}
-#endif
[Fact]
public async Task HittingMaxDepthRaisesOnlyOneValidationError()
@@ -81,7 +79,6 @@ public async Task HittingMaxDepthRaisesOnlyOneValidationError()
}
}
-#if !NETFX_CORE // IRequiredMemeberSelector is not in portable libraries because there is no model state on the client.
// this IRMS treats all member names that start with "Required" as required
public class SimpleRequiredMemberSelector : IRequiredMemberSelector
{
@@ -90,7 +87,6 @@ public bool IsRequiredMember(MemberInfo member)
return member.Name.StartsWith("Required");
}
}
-#endif
public class DataContractWithRequiredMembers
{
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs
index cd1c9e03b..9c8643757 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs
@@ -361,9 +361,7 @@ public void FindWriter_ReturnsFormatterOnMatch(Type variationType, object testDa
[InlineData(typeof(XAttribute))]
[InlineData(typeof(Type))]
[InlineData(typeof(byte[]))]
-#if !NETFX_CORE
[InlineData(typeof(XmlElement))]
-#endif
[InlineData(typeof(FormDataCollection))]
public void IsTypeExcludedFromValidation_ReturnsTrueForExcludedTypes(Type type)
{
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterTests.cs
index 0d75a9634..526e0734f 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterTests.cs
@@ -51,12 +51,10 @@ public void Constructor()
Collection supportedMediaTypes = formatter.SupportedMediaTypes;
Assert.NotNull(supportedMediaTypes);
Assert.Empty(supportedMediaTypes);
-#if !NETFX_CORE // No MediaTypeMapping support in portable libraries
Collection mappings = formatter.MediaTypeMappings;
Assert.NotNull(mappings);
Assert.Empty(mappings);
-#endif
}
[Fact]
@@ -65,11 +63,9 @@ void CopyConstructor()
TestMediaTypeFormatter formatter = new TestMediaTypeFormatter();
TestMediaTypeFormatter derivedFormatter = new TestMediaTypeFormatter(formatter);
-#if !NETFX_CORE // No MediaTypeMapping or RequiredMemberSelector in client libraries
Assert.Same(formatter.MediaTypeMappings, derivedFormatter.MediaTypeMappings);
Assert.Same(formatter.MediaTypeMappingsInternal, derivedFormatter.MediaTypeMappingsInternal);
Assert.Equal(formatter.RequiredMemberSelector, derivedFormatter.RequiredMemberSelector);
-#endif
Assert.Same(formatter.SupportedMediaTypes, derivedFormatter.SupportedMediaTypes);
Assert.Same(formatter.SupportedMediaTypesInternal, derivedFormatter.SupportedMediaTypesInternal);
@@ -151,7 +147,6 @@ public void SupportedMediaTypes_InsertThrowsWithMediaRange(MediaTypeHeaderValue
Assert.ThrowsArgument(() => supportedMediaTypes.Insert(0, mediaType), "item", Error.Format(Properties.Resources.CannotUseMediaRangeForSupportedMediaType, typeof(MediaTypeHeaderValue).Name, mediaType.MediaType));
}
-#if !NETFX_CORE // No MediaTypeMapping support in portable libraries
[Fact]
public void MediaTypeMappings_IsMutable()
{
@@ -165,7 +160,6 @@ public void MediaTypeMappings_IsMutable()
Assert.True(standardMappings.SequenceEqual(formatter.MediaTypeMappings));
}
-#endif
[Fact]
public void SelectCharacterEncoding_ThrowsIfNoSupportedEncodings()
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/StringComparisonHelperTest.cs b/test/System.Net.Http.Formatting.Test/Formatting/StringComparisonHelperTest.cs
index 77a8d2361..f18764086 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/StringComparisonHelperTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/StringComparisonHelperTest.cs
@@ -12,15 +12,7 @@ public StringComparisonHelperTest()
{
}
-#if NETFX_CORE // InvariantCulture and InvarianteCultureIgnore case are not supported in portable library projects
- protected override void AssertForUndefinedValue(Action testCode, string parameterName, int invalidValue, Type enumType, bool allowDerivedExceptions = false)
- {
- Assert.ThrowsArgument(
- testCode,
- parameterName,
- allowDerivedExceptions);
- }
-
+#if NETFX_CORE // InvariantCulture and InvariantCultureIgnoreCase case are not supported in portable library projects
protected override bool ValueExistsForFramework(StringComparison value)
{
return !(value == StringComparison.InvariantCulture || value == StringComparison.InvariantCultureIgnoreCase);
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/XmlMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/XmlMediaTypeFormatterTests.cs
index 3d7fd1557..d10e927d3 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/XmlMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/XmlMediaTypeFormatterTests.cs
@@ -67,18 +67,14 @@ void CopyConstructor()
TestXmlMediaTypeFormatter formatter = new TestXmlMediaTypeFormatter()
{
Indent = true,
-#if !NETFX_CORE // We don't support MaxDepth in the portable library
MaxDepth = 42,
-#endif
UseXmlSerializer = true
};
TestXmlMediaTypeFormatter derivedFormatter = new TestXmlMediaTypeFormatter(formatter);
Assert.Equal(formatter.Indent, derivedFormatter.Indent);
-#if !NETFX_CORE // We don't support MaxDepth in the portable library
Assert.Equal(formatter.MaxDepth, derivedFormatter.MaxDepth);
-#endif
Assert.Equal(formatter.UseXmlSerializer, derivedFormatter.UseXmlSerializer);
}
@@ -90,7 +86,6 @@ public void DefaultMediaType_ReturnsApplicationXml()
Assert.Equal("application/xml", mediaType.MediaType);
}
-#if !NETFX_CORE // We don't support MaxDepth in the portable library
[Fact]
public void MaxDepthReturnsCorrectValue()
{
@@ -115,7 +110,6 @@ public async Task ReadDeeplyNestedObjectThrows()
stream.Position = 0;
await Assert.ThrowsAsync(() => formatter.ReadFromStreamAsync(typeof(SampleType), stream, null, null));
}
-#endif
[Fact]
public void Indent_RoundTrips()
@@ -520,23 +514,10 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContr
public override Task ReadFromStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- if (!isDefaultEncoding)
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();
string formattedContent = "" + content + "";
-#if NETFX_CORE
- // We need to supply the xml declaration when compiled in portable library for non utf-8 content
- if (String.Equals("utf-16", encoding, StringComparison.OrdinalIgnoreCase))
- {
- formattedContent = "" + formattedContent;
- }
-#endif
string mediaType = string.Format("application/xml; charset={0}", encoding);
// Act & assert
@@ -588,12 +569,6 @@ public Task ReadFromStreamAsync_ThrowsException_WhenGetDeserializerReturnsInvali
public override Task WriteToStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- if (!isDefaultEncoding)
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();
string formattedContent = "" + content +
diff --git a/test/System.Net.Http.Formatting.Test/Formatting/XmlSerializerMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/XmlSerializerMediaTypeFormatterTests.cs
index 0b48c73ff..865b2be43 100644
--- a/test/System.Net.Http.Formatting.Test/Formatting/XmlSerializerMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Formatting/XmlSerializerMediaTypeFormatterTests.cs
@@ -61,9 +61,7 @@ public async Task ReadDeeplyNestedObjectWorks()
{
XmlSerializerMediaTypeFormatter formatter = new XmlSerializerMediaTypeFormatter()
{
-#if !NETFX_CORE // We don't support MaxDepth in the portable library
MaxDepth = 5001
-#endif
};
StringContent content = new StringContent(GetDeeplyNestedObject(5000));
@@ -250,22 +248,9 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingXmlSerial
public override Task ReadFromStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- if (!isDefaultEncoding)
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
XmlSerializerMediaTypeFormatter formatter = new XmlSerializerMediaTypeFormatter();
string formattedContent = "" + content + "";
-#if NETFX_CORE
- if (String.Equals("utf-16", encoding, StringComparison.OrdinalIgnoreCase))
- {
- // We need to supply the xml declaration when compiled in portable library for non utf-8 content
- formattedContent = "" + formattedContent;
- }
-#endif
string mediaType = string.Format("application/xml; charset={0}", encoding);
// Act & assert
@@ -274,12 +259,6 @@ public override Task ReadFromStreamAsync_UsesCorrectCharacterEncoding(string con
public override Task WriteToStreamAsync_UsesCorrectCharacterEncoding(string content, string encoding, bool isDefaultEncoding)
{
- if (!isDefaultEncoding)
- {
- // XmlDictionaryReader/Writer only supports utf-8 and 16
- return TaskHelpers.Completed();
- }
-
// Arrange
XmlSerializerMediaTypeFormatter formatter = new XmlSerializerMediaTypeFormatter();
string formattedContent = "" + content + "";
diff --git a/test/System.Net.Http.Formatting.Test/Handlers/ProgressMessageHandlerTest.cs b/test/System.Net.Http.Formatting.Test/Handlers/ProgressMessageHandlerTest.cs
index b7d71b4e5..36f50cde6 100644
--- a/test/System.Net.Http.Formatting.Test/Handlers/ProgressMessageHandlerTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Handlers/ProgressMessageHandlerTest.cs
@@ -125,16 +125,13 @@ public ShortCircuitMessageHandler(bool includeResponseEntity)
protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
-#if NETFX_CORE // Extension method only available on non portable library
- HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
-#else
HttpResponseMessage response = request.CreateResponse();
-#endif
if (_includeResponseEntity)
{
response.Content = new StringContent("Response Entity");
response.Content.Headers.Add(TestHeader, TestValue);
}
+
return Task.FromResult(response);
}
}
diff --git a/test/System.Net.Http.Formatting.Test/Headers/CookieStateTest.cs b/test/System.Net.Http.Formatting.Test/Headers/CookieStateTest.cs
index a00af171c..68383c238 100644
--- a/test/System.Net.Http.Formatting.Test/Headers/CookieStateTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Headers/CookieStateTest.cs
@@ -29,19 +29,21 @@ public static TheoryDataSet EncodedCookieStateStrings
{
get
{
- return new TheoryDataSet
+ TheoryDataSet data = new TheoryDataSet
{
- { "?", "%3f" },
- { "=", "%3d" },
- { "", "%3cacb%3e" },
- { "{acb}", "%7bacb%7d" },
- { "[acb]", "%5bacb%5d" },
+ { "?", "%3F" },
+ { "=", "%3D" },
+ { "", "%3Cacb%3E" },
+ { "{acb}", "%7Bacb%7D" },
+ { "[acb]", "%5Bacb%5D" },
{ "\"acb\"", "%22acb%22" },
- { "a,b", "a%2cb" },
- { "a;b", "a%3bb" },
- { "a\\b", "a%5cb" },
- { "[]{}\\|!@#$%^&*()_-+=", "%5b%5d%7b%7d%5c%7c!%40%23%24%25%5e%26*()_-%2b%3d" },
+ { "a,b", "a%2Cb" },
+ { "a;b", "a%3Bb" },
+ { "a\\b", "a%5Cb" },
+ { "[]{}\\|!@#$%^&*()_-+=", "%5B%5D%7B%7D%5C%7C!%40%23%24%25%5E%26*()_-%2B%3D" },
};
+
+ return data;
}
}
diff --git a/test/System.Net.Http.Formatting.Test/HttpContentFormDataExtensionsTest.cs b/test/System.Net.Http.Formatting.Test/HttpContentFormDataExtensionsTest.cs
index d82bc3687..cf787bf37 100644
--- a/test/System.Net.Http.Formatting.Test/HttpContentFormDataExtensionsTest.cs
+++ b/test/System.Net.Http.Formatting.Test/HttpContentFormDataExtensionsTest.cs
@@ -8,9 +8,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
-#if NETFX_CORE
-using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
-#endif
namespace System.Net.Http
{
diff --git a/test/System.Net.Http.Formatting.Test/HttpRequestHeadersExtensionsTest.cs b/test/System.Net.Http.Formatting.Test/HttpRequestHeadersExtensionsTest.cs
index a02eaa6da..b385cb423 100644
--- a/test/System.Net.Http.Formatting.Test/HttpRequestHeadersExtensionsTest.cs
+++ b/test/System.Net.Http.Formatting.Test/HttpRequestHeadersExtensionsTest.cs
@@ -45,7 +45,7 @@ public static TheoryDataSet CookieMatches
new string[]
{
"adxcs=-",
- "ADXCS=si=0%3a1"
+ "ADXCS=si=0%3A1"
}
},
{
diff --git a/test/System.Net.Http.Formatting.Test/Internal/ByteRangeStreamTest.cs b/test/System.Net.Http.Formatting.Test/Internal/ByteRangeStreamTest.cs
index eb193b51a..391f31b09 100644
--- a/test/System.Net.Http.Formatting.Test/Internal/ByteRangeStreamTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Internal/ByteRangeStreamTest.cs
@@ -303,6 +303,7 @@ public async Task Position_PositionsNextRead()
}
}
+#if !NETFX_CORE // BeginX and EndX are not supported on streams in portable libraries
[Theory]
[PropertyData("ReadBoundsDataWithLimit")]
public void BeginRead_ReadsEffectiveLengthBytes(int from, int to, int innerLength, int effectiveLength)
@@ -330,6 +331,7 @@ public void BeginRead_ReadsEffectiveLengthBytes(int from, int to, int innerLengt
Assert.Equal(effectiveLength, rangeStream.Position);
}
}
+#endif
[Fact]
public async Task BeginRead_CanReadAfterLength()
diff --git a/test/System.Net.Http.Formatting.Test/Internal/ConcurrentDictionaryTests.cs b/test/System.Net.Http.Formatting.Test/Internal/ConcurrentDictionaryTests.cs
index 2294f8ff5..cc08f4d92 100644
--- a/test/System.Net.Http.Formatting.Test/Internal/ConcurrentDictionaryTests.cs
+++ b/test/System.Net.Http.Formatting.Test/Internal/ConcurrentDictionaryTests.cs
@@ -4,26 +4,10 @@
using System.Collections.Generic;
using Microsoft.TestCommon;
-#if NETFX_CORE
-namespace System.Net.Http.Internal
-#else
namespace System.Collections.Concurrent
-#endif
{
public class ConcurrentDictionaryTests
{
-#if NETFX_CORE // This doesn't exist on the ConcurrentDictionary in the full framework
- [Fact]
- public void IsReadOnly_ReturnsFalse()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act & Assert
- Assert.False(dictionary.IsReadOnly);
- }
-#endif
-
[Fact]
public void ContainsKey_ReturnsFalseWhenKeyIsNotPresent()
{
@@ -41,7 +25,7 @@ public void ContainsKey_ReturnsTrueWhenKeyIsPresent()
ConcurrentDictionary dictionary = new ConcurrentDictionary();
// Act
- dictionary.TryAdd(1, 2);
+ dictionary.TryAdd(1, 2);
// Assert
Assert.True(dictionary.ContainsKey(1));
@@ -129,211 +113,5 @@ public void AddOrUpdate_UpdatesValueWhenKeyIsPresent()
// Assert
Assert.Equal(3, result);
}
-
-#if NETFX_CORE
- [Fact]
- public void Add_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Add(0, 0));
- }
-
- [Fact]
- public void Add_KeyValuePairThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Add(new KeyValuePair(0, 0)));
- }
-
- [Fact]
- public void Clear_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Clear());
- }
-
- [Fact]
- public void Contains_KeyValuePairThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Contains(new KeyValuePair(0, 0)));
- }
-
- [Fact]
- public void CopyTo_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.CopyTo(new KeyValuePair[1], 1));
- }
-
- [Fact]
- public void GetEnumerator_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.GetEnumerator());
- }
-
- [Fact]
- public void Remove_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Remove(0));
- }
-
- [Fact]
- public void Remove_WithKeyValuePairThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Remove(new KeyValuePair(0, 0)));
- }
-
- [Fact]
- public void GetEnumerator_AsEnumerableThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => ((System.Collections.IEnumerable)dictionary).GetEnumerator());
- }
-
- [Fact]
- public void TryGetValue_ReturnsTrueAndValueWhenPresent()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
- dictionary.TryAdd(1, -1);
-
- // Act
- int returnedValue;
- bool tryResult = dictionary.TryGetValue(1, out returnedValue);
-
- // Assert
- Assert.Equal(-1, returnedValue);
- Assert.True(tryResult);
- }
-
- [Fact]
- public void TryGetValue_ReturnsFalseAndDefaultWhenMissing()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
- dictionary.TryAdd(1, -1);
-
- // Act
- int returnedValue;
- bool tryResult = dictionary.TryGetValue(2, out returnedValue);
-
- // Assert
- Assert.Equal(0, returnedValue);
- Assert.False(tryResult);
- }
-
- [Fact]
- public void TryRemove_ReturnsTrueAndRemovesWhenPresent()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
- dictionary.TryAdd(1, -1);
-
- // Act
- int returnedValue;
- bool tryResult = dictionary.TryGetValue(1, out returnedValue);
-
- // Assert
- Assert.Equal(-1, returnedValue);
- Assert.True(tryResult);
- }
-
- [Fact]
- public void TryRemove_ReturnsFalseAndDefaultWhenMissing()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
- dictionary.TryAdd(1, -1);
-
- // Act
- int returnedValue;
- bool tryResult = dictionary.TryGetValue(2, out returnedValue);
-
- // Assert
- Assert.Equal(0, returnedValue);
- Assert.False(tryResult);
- }
-
- [Fact]
- public void Count_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Count);
- }
-
- [Fact]
- public void GetItem_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary[0]);
- }
-
- [Fact]
- public void SetItem_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary[0] = 1);
- }
-
- [Fact]
- public void GetKeys_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Keys);
- }
-
- [Fact]
- public void GetValues_ThrowsNotImplementedException()
- {
- // Arrange
- ConcurrentDictionary dictionary = new ConcurrentDictionary();
-
- // Act/Assert
- Assert.Throws(() => dictionary.Values);
- }
-#endif
}
}
diff --git a/test/System.Net.Http.Formatting.Test/Internal/HttpValueCollectionTest.cs b/test/System.Net.Http.Formatting.Test/Internal/HttpValueCollectionTest.cs
index 03b5aadf0..fdf8a58cb 100644
--- a/test/System.Net.Http.Formatting.Test/Internal/HttpValueCollectionTest.cs
+++ b/test/System.Net.Http.Formatting.Test/Internal/HttpValueCollectionTest.cs
@@ -4,9 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
-#if !NETFX_CORE
using System.Net.Http.Formatting.Internal;
-#endif
using System.Web.WebPages.TestUtils;
using Microsoft.TestCommon;
@@ -20,11 +18,7 @@ public class HttpValueCollectionTest
private static HttpValueCollection CreateInstance()
{
-#if NETFX_CORE
- return new HttpValueCollection();
-#else
return HttpValueCollection.Create();
-#endif
}
#if !NETCOREAPP
@@ -79,11 +73,7 @@ public static TheoryDataSet ToStringTestData
hvc4.Add("na me", "");
dataSet.Add(hvc4, "na+me");
-#if NETFX_CORE
string encoded5 = "n%22%2C%3B%5Cn";
-#else
- string encoded5 = "n%22%2c%3b%5cn";
-#endif
var hvc5 = CreateInstance();
hvc5.Add("n\",;\\n", "");
@@ -103,22 +93,14 @@ public static TheoryDataSet ToStringTestData
hvc7.Add("n4", "v4");
dataSet.Add(hvc7, "n1=v1&n2=v2&n3=v3&n4=v4");
-#if NETFX_CORE
string encoded8 = "n%2C1=v%2C1&n%3B2=v%3B2";
-#else
- string encoded8 = "n%2c1=v%2c1&n%3b2=v%3b2";
-#endif
var hvc8 = CreateInstance();
hvc8.Add("n,1", "v,1");
hvc8.Add("n;2", "v;2");
dataSet.Add(hvc8, encoded8);
-#if NETFX_CORE
string encoded9 = "n1=%26&n2=%3B&n3=%26&n4=%2B&n5=%26&n6=%3D&n7=%26";
-#else
- string encoded9 = "n1=%26&n2=%3b&n3=%26&n4=%2b&n5=%26&n6=%3d&n7=%26";
-#endif
var hvc9 = CreateInstance();
hvc9.Add("n1", "&");
@@ -268,14 +250,8 @@ public void Create_InitializesCorrectly(IEnumerable
string expectedKey = kvp.Key ?? String.Empty;
string expectedValue = kvp.Value ?? String.Empty;
-#if NETFX_CORE
- KeyValuePair actualKvp = nvc.List[index];
- string actualKey = actualKvp.Key;
- string actualValue = actualKvp.Value;
-#else
string actualKey = nvc.AllKeys[index];
string actualValue = nvc[index];
-#endif
index++;
Assert.Equal(expectedKey, actualKey);
diff --git a/test/System.Net.Http.Formatting.Test/Internal/ReadOnlyStreamWithEncodingPreambleTest.cs b/test/System.Net.Http.Formatting.Test/Internal/ReadOnlyStreamWithEncodingPreambleTest.cs
deleted file mode 100644
index fc7fa40ac..000000000
--- a/test/System.Net.Http.Formatting.Test/Internal/ReadOnlyStreamWithEncodingPreambleTest.cs
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System.Collections.Generic;
-using System.IO;
-using System.Reflection;
-using System.Text;
-using Microsoft.TestCommon;
-
-namespace System.Net.Http.Internal
-{
- public class ReadOnlyStreamWithEncodingPreambleTest
- {
- [Theory]
- [EncodingData]
- public void StreamWithoutPreamble(Encoding encoding, bool includePreambleInInputStream)
- {
- using (MemoryStream inputStream = new MemoryStream())
- {
- // Arrange
- string message = "Hello, world" + Environment.NewLine // English
- + "こんにちは、世界" + Environment.NewLine // Japanese
- + "مرحبا، العالم"; // Arabic
-
- byte[] preamble = encoding.GetPreamble();
- byte[] encodedMessage = encoding.GetBytes(message);
-
- if (includePreambleInInputStream)
- {
- inputStream.Write(preamble, 0, preamble.Length);
- }
-
- inputStream.Write(encodedMessage, 0, encodedMessage.Length);
-
- byte[] expectedBytes = new byte[preamble.Length + encodedMessage.Length];
- preamble.CopyTo(expectedBytes, 0);
- encodedMessage.CopyTo(expectedBytes, preamble.Length);
-
- inputStream.Seek(0, SeekOrigin.Begin);
-
- using (ReadOnlyStreamWithEncodingPreamble wrapperStream = new ReadOnlyStreamWithEncodingPreamble(inputStream, encoding))
- {
- // Act
- int totalRead = 0;
- byte[] readBuffer = new byte[expectedBytes.Length];
-
- while (totalRead < readBuffer.Length)
- {
- int read = wrapperStream.Read(readBuffer, totalRead, readBuffer.Length - totalRead);
- totalRead += read;
-
- if (read == 0)
- break;
- }
-
- // Assert
- Assert.Equal(expectedBytes.Length, totalRead);
- Assert.Equal(expectedBytes, readBuffer);
- Assert.Equal(0, wrapperStream.Read(readBuffer, 0, 1)); // Make sure there are no stray bytes left in the stream
- }
- }
- }
-
- class EncodingDataAttribute : DataAttribute
- {
- public override IEnumerable