Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,58 @@
--********************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;

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

private static Regex s_attribNameValueRegex;
private static Regex s_attribsRegex;
private static Regex s_imageRegex;
private static Regex s_inputFieldRegex;
private static Regex s_linkRegex;
private static Regex s_tagRegex;

#endregion Private Fields

#region Constructors

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

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

#endregion Constructors

#region Properties

/// <summary>
Expand Down Expand Up @@ -117,18 +153,39 @@ public WebCmdletElementCollection Images

#endregion Properties

#region Private Fields
#region Methods

private static Regex s_tagRegex;
private static Regex s_attribsRegex;
private static Regex s_attribNameValueRegex;
private static Regex s_inputFieldRegex;
private static Regex s_linkRegex;
private static Regex s_imageRegex;
/// <summary>
/// Reads the response content from the web response.
/// </summary>
protected void InitializeContent()
{
string contentType = ContentHelper.GetContentType(BaseResponse);
if (ContentHelper.IsText(contentType))
{
Encoding encoding = null;
// fill the Content buffer
string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);
this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding);
this.Encoding = encoding;
}
else
{
this.Content = string.Empty;
}
}

#endregion Private Fields
private PSObject CreateHtmlObject(string html, string tagName)
{
PSObject elementObject = new PSObject();

#region Methods
elementObject.Properties.Add(new PSNoteProperty("outerHTML", html));
elementObject.Properties.Add(new PSNoteProperty("tagName", tagName));

ParseAttributes(html, elementObject);

return elementObject;
}

private void EnsureHtmlParser()
{
Expand Down Expand Up @@ -169,16 +226,11 @@ private void EnsureHtmlParser()
}
}

private PSObject CreateHtmlObject(string html, string tagName)
private void InitializeRawContent(HttpResponseMessage baseResponse)
{
PSObject elementObject = new PSObject();

elementObject.Properties.Add(new PSNoteProperty("outerHTML", html));
elementObject.Properties.Add(new PSNoteProperty("tagName", tagName));

ParseAttributes(html, elementObject);

return elementObject;
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
raw.Append(Content);
this.RawContent = raw.ToString();
}

private void ParseAttributes(string outerHtml, PSObject elementObject)
Expand Down Expand Up @@ -223,70 +275,6 @@ private void ParseAttributes(string outerHtml, PSObject elementObject)
}
}

/// <summary>
/// Reads the response content from the web response.
/// </summary>
protected void InitializeContent()
{
string contentType = ContentHelper.GetContentType(BaseResponse);
if (ContentHelper.IsText(contentType))
{
Encoding encoding = null;
// fill the Content buffer
string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);
this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding);
this.Encoding = encoding;
}
else
{
this.Content = string.Empty;
}
}

#endregion Methods
}

// TODO: Merge Partials

// <summary>
/// Response object for html content without DOM parsing
/// </summary>
public partial class BasicHtmlWebResponseObject : WebResponseObject
{
#region Constructors

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

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

#endregion Constructors

#region Methods

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

#endregion Methods
}
}
Loading