WebResponseObject.Common merge partials atomic commits (#18703)

This commit is contained in:
CarloToso authored and GitHub committed 2022-12-13 18:18:16 +05:00
1 parent b6f83e7520
commit c3a080caf9
1 file changed
+75 -111
@@ -13,101 +13,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// WebResponseObject.
/// </summary>
public partial class WebResponseObject
{
#region Properties
/// <summary>
/// Gets or protected sets the response body content.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public byte[] Content { get; protected set; }
/// <summary>
/// Gets the response status code.
/// </summary>
public int StatusCode
{
get { return (WebResponseHelper.GetStatusCode(BaseResponse)); }
}
/// <summary>
/// Gets the response status description.
/// </summary>
public string StatusDescription
{
get { return (WebResponseHelper.GetStatusDescription(BaseResponse)); }
}
private MemoryStream _rawContentStream;
/// <summary>
/// Gets the response body content as a <see cref="MemoryStream"/>.
/// </summary>
public MemoryStream RawContentStream
{
get { return (_rawContentStream); }
}
/// <summary>
/// Gets the length (in bytes) of <see cref="RawContentStream"/>.
/// </summary>
public long RawContentLength
{
get { return (RawContentStream == null ? -1 : RawContentStream.Length); }
}
/// <summary>
/// Gets or protected sets the full response content.
/// </summary>
/// <value>
/// Full response content, including the HTTP status line, headers, and body.
/// </value>
public string RawContent { get; protected set; }
#endregion Properties
#region Methods
/// <summary>
/// Reads the response content from the web response.
/// </summary>
private void InitializeContent()
{
this.Content = this.RawContentStream.ToArray();
}
private static bool IsPrintable(char c)
{
return (char.IsLetterOrDigit(c) || char.IsPunctuation(c) || char.IsSeparator(c) || char.IsSymbol(c) || char.IsWhiteSpace(c));
}
/// <summary>
/// Returns the string representation of this web response.
/// </summary>
/// <returns>The string representation of this web response.</returns>
public sealed override string ToString()
{
char[] stringContent = System.Text.Encoding.ASCII.GetChars(Content);
for (int counter = 0; counter < stringContent.Length; counter++)
{
if (!IsPrintable(stringContent[counter]))
{
stringContent[counter] = '.';
}
}
return new string(stringContent);
}
#endregion Methods
}
// TODO: Merge Partials
/// <summary>
/// WebResponseObject.
/// </summary>
public partial class WebResponseObject
public class WebResponseObject
{
#region Properties
@@ -116,27 +22,52 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public HttpResponseMessage BaseResponse { get; set; }
/// <summary>
/// Gets or protected sets the response body content.
/// </summary>
public byte[] Content { get; protected set; }
/// <summary>
/// Gets the Headers property.
/// </summary>
public Dictionary<string, IEnumerable<string>> Headers
{
get
{
_headers ??= WebResponseHelper.GetHeadersDictionary(BaseResponse);
return _headers;
}
}
public Dictionary<string, IEnumerable<string>> Headers => _headers ??= WebResponseHelper.GetHeadersDictionary(BaseResponse);
private Dictionary<string, IEnumerable<string>> _headers = null;
/// <summary>
/// Gets or protected sets the full response content.
/// </summary>
/// <value>
/// Full response content, including the HTTP status line, headers, and body.
/// </value>
public string RawContent { get; protected set; }
/// <summary>
/// Gets the length (in bytes) of <see cref="RawContentStream"/>.
/// </summary>
public long RawContentLength => RawContentStream is null ? -1 : RawContentStream.Length;
/// <summary>
/// Gets or protected sets the response body content as a <see cref="MemoryStream"/>.
/// </summary>
public MemoryStream RawContentStream { get; protected set; }
/// <summary>
/// Gets the RelationLink property.
/// </summary>
public Dictionary<string, string> RelationLink { get; internal set; }
#endregion
/// <summary>
/// Gets the response status code.
/// </summary>
public int StatusCode => WebResponseHelper.GetStatusCode(BaseResponse);
/// <summary>
/// Gets the response status description.
/// </summary>
public string StatusDescription => WebResponseHelper.GetStatusDescription(BaseResponse);
#endregion Properties
#region Constructors
@@ -165,6 +96,14 @@ namespace Microsoft.PowerShell.Commands
#region Methods
/// <summary>
/// Reads the response content from the web response.
/// </summary>
private void InitializeContent()
{
this.Content = this.RawContentStream.ToArray();
}
private void InitializeRawContent(HttpResponseMessage baseResponse)
{
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
@@ -178,21 +117,27 @@ namespace Microsoft.PowerShell.Commands
this.RawContent = raw.ToString();
}
private static bool IsPrintable(char c) => char.IsLetterOrDigit(c)
|| char.IsPunctuation(c)
|| char.IsSeparator(c)
|| char.IsSymbol(c)
|| char.IsWhiteSpace(c);
private void SetResponse(HttpResponseMessage response, Stream contentStream)
{
if (response == null) { throw new ArgumentNullException(nameof(response)); }
if (response is null) { throw new ArgumentNullException(nameof(response)); }
BaseResponse = response;
MemoryStream ms = contentStream as MemoryStream;
if (ms != null)
{
_rawContentStream = ms;
RawContentStream = ms;
}
else
{
Stream st = contentStream;
if (contentStream == null)
if (contentStream is null)
{
st = StreamHelper.GetResponseStream(response);
}
@@ -204,11 +149,30 @@ namespace Microsoft.PowerShell.Commands
}
int initialCapacity = (int)Math.Min(contentLength, StreamHelper.DefaultReadBuffer);
_rawContentStream = new WebResponseContentMemoryStream(st, initialCapacity, cmdlet: null, response.Content.Headers.ContentLength.GetValueOrDefault());
RawContentStream = new WebResponseContentMemoryStream(st, initialCapacity, cmdlet: null, response.Content.Headers.ContentLength.GetValueOrDefault());
}
// set the position of the content stream to the beginning
_rawContentStream.Position = 0;
RawContentStream.Position = 0;
}
#endregion
/// <summary>
/// Returns the string representation of this web response.
/// </summary>
/// <returns>The string representation of this web response.</returns>
public sealed override string ToString()
{
char[] stringContent = System.Text.Encoding.ASCII.GetChars(Content);
for (int counter = 0; counter < stringContent.Length; counter++)
{
if (!IsPrintable(stringContent[counter]))
{
stringContent[counter] = '.';
}
}
return new string(stringContent);
}
#endregion Methods
}
}