Skip to content

Commit

Permalink
Merge pull request PowerShell#1468 from PowerShell/frangom/webcmdlets
Browse files Browse the repository at this point in the history
Port Invoke-WebRequest/RestMethod to OPS
  • Loading branch information
vors authored Jul 25, 2016
2 parents 26a6644 + bcfe498 commit 181d5de
Show file tree
Hide file tree
Showing 41 changed files with 3,551 additions and 1,492 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************++
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/

Expand All @@ -9,15 +9,14 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Microsoft.Win32;
using System.Diagnostics;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Response object for html content without DOM parsing
/// </summary>
public class BasicHtmlWebResponseObject : WebResponseObject
public partial class BasicHtmlWebResponseObject : WebResponseObject
{
#region Properties

Expand Down Expand Up @@ -109,30 +108,6 @@ public WebCmdletElementCollection Images

#endregion Properties

#region Constructors

/// <summary>
/// Constructor for HtmlWebResponseObject
/// </summary>
/// <param name="response"></param>
public BasicHtmlWebResponseObject(WebResponse response)
: this(response, null) { }

/// <summary>
/// Constructor for HtmlWebResponseObject with memory stream
/// </summary>
/// <param name="response"></param>
/// <param name="contentStream"></param>
public BasicHtmlWebResponseObject(WebResponse response, Stream contentStream)
: base(response, contentStream)
{
EnsureHtmlParser();
InitializeContent();
InitializeRawContent(response);
}

#endregion Constructors

#region Private Fields

private static Regex _tagRegex;
Expand All @@ -146,13 +121,6 @@ public BasicHtmlWebResponseObject(WebResponse response, Stream contentStream)

#region Methods

private void InitializeRawContent(WebResponse baseResponse)
{
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
raw.Append(Content);
this.RawContent = raw.ToString();
}

private void EnsureHtmlParser()
{

Expand Down Expand Up @@ -204,8 +172,7 @@ private PSObject CreateHtmlObject(string html, string tagName)

return elementObject;
}



private void ParseAttributes(string outerHtml, PSObject elementObject)
{
// We might get an empty input for a directive from the HTML file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/********************************************************************++
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Net;
using System.Reflection;
using System.Globalization;
using Dbg = System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
internal static class ContentHelper
internal static partial class ContentHelper
{
#region Constants

Expand Down Expand Up @@ -46,32 +40,6 @@ internal static bool IsJson(string contentType)
return CheckIsJson(contentType);
}

internal static Encoding GetEncoding(WebResponse response)
{
string characterSet = null;
HttpWebResponse httpResponse = response as HttpWebResponse;
if (null != httpResponse)
{
characterSet = httpResponse.CharacterSet;
}

return GetEncodingOrDefault(characterSet);
}

// Gets the content type with safe fallback - in the situation
// of FTPWebResponse that retuns NotImplemented.
internal static string GetContentType(WebResponse response)
{
string contentType = null;
try
{
contentType = response.ContentType;
}
catch (NotImplementedException) { }

return contentType;
}

internal static Encoding GetEncodingOrDefault(string characterSet)
{
// get the name of the codepage to use for response content
Expand All @@ -84,7 +52,8 @@ internal static Encoding GetEncodingOrDefault(string characterSet)
}
catch (ArgumentException)
{
encoding = Encoding.GetEncoding(null);
// 0, default code page
encoding = Encoding.GetEncoding(0);
}

return encoding;
Expand Down Expand Up @@ -176,33 +145,7 @@ private static bool CheckIsJson(string contentType)

return (isJson);
}

internal static StringBuilder GetRawContentHeader(WebResponse baseResponse)
{
StringBuilder raw = new StringBuilder();

// add protocol and status line
string protocol = WebResponseHelper.GetProtocol(baseResponse);
if (!String.IsNullOrEmpty(protocol))
{
int statusCode = WebResponseHelper.GetStatusCode(baseResponse);
string statusDescription = WebResponseHelper.GetStatusDescription(baseResponse);
raw.AppendFormat("{0} {1} {2}", protocol, statusCode, statusDescription);
raw.AppendLine();
}

// add headers
foreach (string key in baseResponse.Headers.AllKeys)
{
string value = baseResponse.Headers[key];
raw.AppendFormat("{0}: {1}", key, value);
raw.AppendLine();
}

raw.AppendLine();
return raw;
}


#endregion Internal Helper Methods
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/********************************************************************++
#if !CORECLR

/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/

using System;
using System.Management.Automation;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using mshtml;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;
using ExecutionContext = System.Management.Automation.ExecutionContext;
Expand All @@ -20,7 +18,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Response object for html content
/// </summary>
public class HtmlWebResponseObject : WebResponseObject, IDisposable
public partial class HtmlWebResponseObject : WebResponseObject, IDisposable
{
#region Properties

Expand Down Expand Up @@ -223,37 +221,7 @@ public WebCmdletElementCollection AllElements
}

#endregion Properties

#region Constructors

/// <summary>
/// Constructor for HtmlWebResponseObject
/// </summary>
/// <param name="response"></param>
/// <param name="executionContext"></param>
internal HtmlWebResponseObject(WebResponse response, ExecutionContext executionContext)
: this(response, null, executionContext) { }

/// <summary>
/// Constructor for HtmlWebResponseObject with memory stream
/// </summary>
/// <param name="response"></param>
/// <param name="contentStream"></param> /// <param name="executionContext"></param>
internal HtmlWebResponseObject(WebResponse response, Stream contentStream, ExecutionContext executionContext)
: base(response, contentStream)
{
if (executionContext == null)
{
throw PSTraceSource.NewArgumentNullException("executionContext");
}

_executionContext = executionContext;
InitializeContent();
InitializeRawContent(response);
}

#endregion Constructors


#region Private Fields

private static Regex _tagRegex;
Expand All @@ -263,16 +231,6 @@ internal HtmlWebResponseObject(WebResponse response, Stream contentStream, Execu
#endregion Private Fields

#region Methods

private void InitializeRawContent(WebResponse baseResponse)
{
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
if (null != Content)
{
raw.Append(Content);
}
this.RawContent = raw.ToString();
}

// The "onreadystatechange" event handler
private void ReadyStateChanged(IHTMLEventObj obj)
Expand Down Expand Up @@ -550,3 +508,4 @@ private void CleanupNativeResources()
}
}
}
#endif
Loading

0 comments on commit 181d5de

Please sign in to comment.