forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicHtmlWebResponseObject.Common.cs
More file actions
291 lines (242 loc) · 9.95 KB
/
Copy pathBasicHtmlWebResponseObject.Common.cs
File metadata and controls
291 lines (242 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Response object for html content without DOM parsing.
/// </summary>
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>
/// Gets the text body content of this response.
/// </summary>
/// <value>
/// Content of the response body, decoded using <see cref="Encoding"/>,
/// if the <c>Content-Type</c> response header is a recognized text
/// type. Otherwise <c>null</c>.
/// </value>
public new string Content { get; private set; }
/// <summary>
/// Gets the encoding of the text body content of this response.
/// </summary>
/// <value>
/// Encoding of the response body from the <c>Content-Type</c> header,
/// or <c>null</c> if the encoding could not be determined.
/// </value>
public Encoding Encoding { get; private set; }
private WebCmdletElementCollection _inputFields;
/// <summary>
/// Gets the HTML input field elements parsed from <see cref="Content"/>.
/// </summary>
public WebCmdletElementCollection InputFields
{
get
{
if (_inputFields == null)
{
EnsureHtmlParser();
List<PSObject> parsedFields = new List<PSObject>();
MatchCollection fieldMatch = s_inputFieldRegex.Matches(Content);
foreach (Match field in fieldMatch)
{
parsedFields.Add(CreateHtmlObject(field.Value, "INPUT"));
}
_inputFields = new WebCmdletElementCollection(parsedFields);
}
return _inputFields;
}
}
private WebCmdletElementCollection _links;
/// <summary>
/// Gets the HTML a link elements parsed from <see cref="Content"/>.
/// </summary>
public WebCmdletElementCollection Links
{
get
{
if (_links == null)
{
EnsureHtmlParser();
List<PSObject> parsedLinks = new List<PSObject>();
MatchCollection linkMatch = s_linkRegex.Matches(Content);
foreach (Match link in linkMatch)
{
parsedLinks.Add(CreateHtmlObject(link.Value, "A"));
}
_links = new WebCmdletElementCollection(parsedLinks);
}
return _links;
}
}
private WebCmdletElementCollection _images;
/// <summary>
/// Gets the HTML img elements parsed from <see cref="Content"/>.
/// </summary>
public WebCmdletElementCollection Images
{
get
{
if (_images == null)
{
EnsureHtmlParser();
List<PSObject> parsedImages = new List<PSObject>();
MatchCollection imageMatch = s_imageRegex.Matches(Content);
foreach (Match image in imageMatch)
{
parsedImages.Add(CreateHtmlObject(image.Value, "IMG"));
}
_images = new WebCmdletElementCollection(parsedImages);
}
return _images;
}
}
#endregion Properties
#region Methods
/// <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);
if (string.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType))
{
characterSet = Encoding.UTF8.HeaderName;
}
this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding);
this.Encoding = encoding;
}
else
{
this.Content = string.Empty;
}
}
private PSObject CreateHtmlObject(string html, string tagName)
{
PSObject elementObject = new PSObject();
elementObject.Properties.Add(new PSNoteProperty("outerHTML", html));
elementObject.Properties.Add(new PSNoteProperty("tagName", tagName));
ParseAttributes(html, elementObject);
return elementObject;
}
private void EnsureHtmlParser()
{
if (s_tagRegex == null)
{
s_tagRegex = new Regex(@"<\w+((\s+[^""'>/=\s\p{Cc}]+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (s_attribsRegex == null)
{
s_attribsRegex = new Regex(@"(?<=\s+)([^""'>/=\s\p{Cc}]+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (s_attribNameValueRegex == null)
{
s_attribNameValueRegex = new Regex(@"([^""'>/=\s\p{Cc}]+)(?:\s*=\s*(?:""(.*?)""|'(.*?)'|([^'"">\s]+)))?",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (s_inputFieldRegex == null)
{
s_inputFieldRegex = new Regex(@"<input\s+[^>]*(/>|>.*?</input>)",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (s_linkRegex == null)
{
s_linkRegex = new Regex(@"<a\s+[^>]*(/>|>.*?</a>)",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (s_imageRegex == null)
{
s_imageRegex = new Regex(@"<img\s[^>]*?>",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}
private void InitializeRawContent(HttpResponseMessage baseResponse)
{
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
raw.Append(Content);
this.RawContent = raw.ToString();
}
private void ParseAttributes(string outerHtml, PSObject elementObject)
{
// We might get an empty input for a directive from the HTML file
if (!string.IsNullOrEmpty(outerHtml))
{
// Extract just the opening tag of the HTML element (omitting the closing tag and any contents,
// including contained HTML elements)
var match = s_tagRegex.Match(outerHtml);
// Extract all the attribute specifications within the HTML element opening tag
var attribMatches = s_attribsRegex.Matches(match.Value);
foreach (Match attribMatch in attribMatches)
{
// Extract the name and value for this attribute (allowing for variations like single/double/no
// quotes, and no value at all)
var nvMatches = s_attribNameValueRegex.Match(attribMatch.Value);
Debug.Assert(nvMatches.Groups.Count == 5);
// Name is always captured by group #1
string name = nvMatches.Groups[1].Value;
// The value (if any) is captured by group #2, #3, or #4, depending on quoting or lack thereof
string value = null;
if (nvMatches.Groups[2].Success)
{
value = nvMatches.Groups[2].Value;
}
else if (nvMatches.Groups[3].Success)
{
value = nvMatches.Groups[3].Value;
}
else if (nvMatches.Groups[4].Success)
{
value = nvMatches.Groups[4].Value;
}
elementObject.Properties.Add(new PSNoteProperty(name, value));
}
}
}
#endregion Methods
}
}