Skip to content

Invoke-RestMethod: Link relation type is not captured by Regex when value is not quoted #15972

Closed

Description

@sliekens

Prerequisites

Steps to reproduce

Using Invoke-RestMethod with -FollowRelLink works when the server returns a Link header with relation type rel="next", but not when the relation type is rel=next without quotes, although the Web Linking RFC allows it. Quotes are only required under specific circumstances, never for IANA relation types.

  relation-types = relation-type
                 | <"> relation-type *( 1*SP relation-type ) <">
  relation-type  = reg-rel-type | ext-rel-type
  reg-rel-type   = LOALPHA *( LOALPHA | DIGIT | "." | "-" )
  ext-rel-type   = URI

Note that extension relation types are REQUIRED to be absolute URIs in Link headers, and MUST be quoted if they contain a semicolon (";") or comma (",") (as these characters are used as delimiters in the header itself).

The Regex pattern can be found here.

const string pattern = "<(?<url>.*?)>;\\s*rel=(\"?)(?<rel>.*?)\\1[^\\w -.]?";

Expected behavior

PS> Invoke-RestMethod "https://api.guildwars2.com/v2/quaggans?page=0&page_size=10" -FollowRelLink

id           url
--           ---
404          https://static.staticwars.com/quaggans/404.jpg
aloha        https://static.staticwars.com/quaggans/aloha.jpg
attack       https://static.staticwars.com/quaggans/attack.jpg
bear         https://static.staticwars.com/quaggans/bear.jpg
bowl         https://static.staticwars.com/quaggans/bowl.jpg
box          https://static.staticwars.com/quaggans/box.jpg
breakfast    https://static.staticwars.com/quaggans/breakfast.jpg
bubble       https://static.staticwars.com/quaggans/bubble.jpg
cake         https://static.staticwars.com/quaggans/cake.jpg
cheer        https://static.staticwars.com/quaggans/cheer.jpg
coffee       https://static.staticwars.com/quaggans/coffee.jpg
construction https://static.staticwars.com/quaggans/construction.jpg
cow          https://static.staticwars.com/quaggans/cow.jpg
cry          https://static.staticwars.com/quaggans/cry.jpg
elf          https://static.staticwars.com/quaggans/elf.jpg
ghost        https://static.staticwars.com/quaggans/ghost.jpg
girl         https://static.staticwars.com/quaggans/girl.jpg
hat          https://static.staticwars.com/quaggans/hat.jpg
helmut       https://static.staticwars.com/quaggans/helmut.jpg
hoodie-down  https://static.staticwars.com/quaggans/hoodie-down.jpg
hoodie-up    https://static.staticwars.com/quaggans/hoodie-up.jpg
killerwhale  https://static.staticwars.com/quaggans/killerwhale.jpg
knight       https://static.staticwars.com/quaggans/knight.jpg
lollipop     https://static.staticwars.com/quaggans/lollipop.jpg
lost         https://static.staticwars.com/quaggans/lost.jpg
moving       https://static.staticwars.com/quaggans/moving.jpg
party        https://static.staticwars.com/quaggans/party.jpg
present      https://static.staticwars.com/quaggans/present.jpg
quaggan      https://static.staticwars.com/quaggans/quaggan.jpg
rain         https://static.staticwars.com/quaggans/rain.jpg
scifi        https://static.staticwars.com/quaggans/scifi.jpg
seahawks     https://static.staticwars.com/quaggans/seahawks.jpg
sleep        https://static.staticwars.com/quaggans/sleep.jpg
summer       https://static.staticwars.com/quaggans/summer.jpg
vacation     https://static.staticwars.com/quaggans/vacation.jpg

Actual behavior

PS> Invoke-RestMethod "https://api.guildwars2.com/v2/quaggans?page=0&page_size=10" -FollowRelLink

id        url
--        ---
404       https://static.staticwars.com/quaggans/404.jpg
aloha     https://static.staticwars.com/quaggans/aloha.jpg
attack    https://static.staticwars.com/quaggans/attack.jpg
bear      https://static.staticwars.com/quaggans/bear.jpg
bowl      https://static.staticwars.com/quaggans/bowl.jpg
box       https://static.staticwars.com/quaggans/box.jpg
breakfast https://static.staticwars.com/quaggans/breakfast.jpg
bubble    https://static.staticwars.com/quaggans/bubble.jpg
cake      https://static.staticwars.com/quaggans/cake.jpg
cheer     https://static.staticwars.com/quaggans/cheer.jpg

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.4
PSEdition                      Core
GitCommitId                    7.1.4
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Activity

  1. sliekens commented on Aug 22, 2021

    @sliekens
    ContributorAuthor

    This is caused by the lazy .*? expression in the rel capturing group. If the numbered capturing group for the quote is empty, then the rel named group will also be empty (because the shortest possible match for .* is the empty string itself).

    // Always results in an empty match when the quote is missing
    (\"?)(?<rel>.+*)\1
    

    I came up with this pattern that uses conditional expressions instead of trying to match an empty capturing group.

    <(?<url>.*?)>;\s*rel=(?<quoted>")?(?<rel>(?(quoted).*?|[^,;]*))(?(quoted)")
    

    I tested the pattern with quotes and without quotes, both work now.

    With quotes

    image

    Without quotes

    image

  2. added a commit that references this issue on Aug 22, 2021
    d97abd3
  3. ghost removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy