8000 Support Link Header pagination in WebCmdlets by SteveL-MSFT · Pull Request #3828 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Support Link Header pagination in WebCmdlets #3828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
addressed code review feedback
  • Loading branch information
Steve Lee (POWERSHELL) committed May 23, 2017
commit cbf2afbf3e95c103f697aa53c5e49f08cfa496a4
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SwitchParameter FollowRelLink
/// </summary>
[Parameter]
[Alias("ML")]
[ValidateRange(0, Int32.MaxValue)]
[ValidateRange(1, Int32.MaxValue)]
public int MaximumFollowRelLink
{
get { return base._maximumFollowRelLink; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public class InvokeWebRequestCommand : WebRequestPSCmdlet
{
#region Virtual Method Overrides

/// <summary>
/// Default constructor for InvokeWebRequestCommand
/// </summary>
public InvokeWebRequestCommand() : base()
{
this._parseRelLink = true;
}

/// <summary>
/// Process the web response and output corresponding objects.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet
/// </summary>
private CancellationTokenSource _cancelToken = null;

/// <summary>
/// Parse Rel Links
/// </summary>
internal bool _parseRelLink = false;

/// <summary>
/// Automatically follow Rel Links
/// </summary>
Expand All @@ -77,7 +82,7 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet
/// <summary>
/// Maximum number of Rel Links to follow
/// </summary>
internal int _maximumFollowRelLink = -1;
internal int _maximumFollowRelLink = Int32.MaxValue;

private HttpMethod GetHttpMethod(WebRequestMethod method)
{
Expand Down Expand Up @@ -252,7 +257,7 @@ internal virtual HttpRequestMessage GetRequest(Uri uri)
}

// Some web sites (e.g. Twitter) will return exception on POST when Expect100 is sent
// Default behaviour is continue to send body content anyway after a short period
// Default behavior is continue to send body content anyway after a short period
// Here it send the two part as a whole.
request.Headers.ExpectContinue = false;

Expand Down Expand Up @@ -394,17 +399,18 @@ protected override void ProcessRecord()
using (HttpClient client = GetHttpClient())
{
int followedRelLink = 0;
Uri uri = Uri;
do
{
if (followedRelLink > 0)
{
string linkVerboseMsg = string.Format(CultureInfo.CurrentCulture,
"Following rel link {0}",
Uri.AbsoluteUri);
WebCmdletStrings.FollowingRelLinkVerboseMsg,
uri.AbsoluteUri);
WriteVerbose(linkVerboseMsg);
}

using (HttpRequestMessage request = GetRequest(Uri))
using (HttpRequestMessage request = GetRequest(uri))
{
FillRequestStream(request);
try
Expand All @@ -414,7 +420,7 @@ protected override void ProcessRecord()
requestContentLength = request.Content.Headers.ContentLength.Value;

string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture,
"{0} {1} with {2}-byte payload",
WebCmdletStrings.WebMethodInvocationVerboseMsg,
request.Method,
request.RequestUri,
requestContentLength);
Expand All @@ -424,7 +430,7 @@ protected override void ProcessRecord()

string contentType = ContentHelper.GetContentType(response);
string respVerboseMsg = string.Format(CultureInfo.CurrentCulture,
"received {0}-byte response of content type {1}",
WebCmdletStrings.WebResponseVerboseMsg,
response.Content.Headers.ContentLength,
contentType);
WriteVerbose(respVerboseMsg);
Expand Down Expand Up @@ -461,7 +467,10 @@ protected override void ProcessRecord()
ThrowTerminatingError(er);
}

ParseLinkHeader(response, Uri);
if (_parseRelLink || _followRelLink)
{
ParseLinkHeader(response, uri);
}
ProcessResponse(response);
UpdateSession(response);

Expand Down Expand Up @@ -497,7 +506,7 @@ protected override void ProcessRecord()
{
return;
}
Uri = new Uri(_relationLink["next"]);
uri = new Uri(_relationLink["next"]);
followedRelLink++;
}
}
Expand Down Expand Up @@ -676,18 +685,18 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr

// we only support the URL in angle brackets and `rel`, other attributes are ignored
// user can still parse it themselves via the Headers property
Regex regex = new Regex("<(?<url>.*?)>;\\srel=\"(?<rel>.*?)\"");
string pattern = "<(?<url>.*?)>;\\srel=\"(?<rel>.*?)\"";
IEnumerable<string> links;
if (response.Headers.TryGetValues("Link", out links))
{
foreach(string link in links.FirstOrDefault().Split(","))
{
Match match = regex.Match(link);
Match match = Regex.Match(link, pattern);
if (match.Success)
{
string url = match.Groups["url"].Value;
string rel = match.Groups["rel"].Value;
if (!_relationLink.ContainsKey(rel))
if (url != String.Empty && rel != String.Empty && !_relationLink.ContainsKey(rel))
{
Uri absoluteUri = new Uri(requestUri, url);
_relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Dictionary<string, IEnumerable<string>> Headers
/// <summary>
/// gets the RelationLink property
/// </summary>
public Dictionary<string, string> RelationLink { get; set; }
public Dictionary<string, string> RelationLink { get; internal set; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,13 @@
<data name="ResponseStatusCodeFailure" xml:space="preserve">
<value>Response status code does not indicate success: {0} ({1}).</value>
</data>
</root>
<data name="FollowingRelLinkVerboseMsg" xml:space="preserve">
<value>Following rel link {0}</value>
</data>
<data name="WebMethodInvocationVerboseMsg" xml:space="preserve">
<value>{0} {1} with {2}-byte payload</value>
</data>
<data name="WebResponseVerboseMsg" xml:space="preserve">
<value>received {0}-byte response of content type {1}</value>
</data>
</root>
0