Skip to content

Commit

Permalink
Update Resolve-URI.ps1
Browse files Browse the repository at this point in the history
Add encoding for reserved characters and switch to only affect restricted characters depnding on the usage
  • Loading branch information
Akaizoku committed Jan 20, 2020
1 parent 38e8296 commit 36170e8
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions Public/Resolve-URI.ps1
Original file line number Diff line number Diff line change
@@ -1,28 +1,90 @@
function Resolve-URI {
<#
.SYNOPSIS
Resolve URI
.DESCRIPTION
Parse a unform resource identifier (URI) and resolve restricted and reserved characters
.PARAMETER URI
The URI parameter corresponds to the uniform resource identifier to resolve.
.INPUTS
System.String. You can pipe the uniform resource identifier to Resolve-URI.DESCRIPTION
.OUTPUTS
System.String. Resolve-URI returns the encoded uniform resource identifier.
File name: Resolve-URI.ps1
Author: Florian Carrier
Creation date: 12/12/2018
Last modified: 16/01/2020
.LINK
https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
.LINK
https://en.wikipedia.org/wiki/Percent-encoding
#>
[CmdletBinding ()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "URI to resolve"
HelpMessage = "URI to resolve",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[String]
$URI
$URI,
[Parameter (
HelpMessage = "Switch to limit parsing to restricted characters"
)]
[Switch]
$RestrictedOnly
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
# List of restricted characters
$RestrictedCharacters = [Ordered]@{
"\" = '/'
"%" = '%25'
}
# List of reserved characters
$ReservedCharacters = [Ordered]@{
" " = '%20'
"," = '%2C'
";" = '%3B'
":" = '%3A'
"!" = '%21'
"?" = '%3F'
"'" = '%27'
"(" = '%28'
")" = '%29'
"[" = '%5B'
"]" = '%5D'
"@" = '%40'
"*" = '%2A'
"/" = '%2F'
"&" = '%26'
"#" = '%23'
"+" = '%2B'
"=" = '%3D'
"$" = '%24'
}
}
Process {
# Encode URI
# Encode restricted characters
foreach ($RestrictedCharacter in $RestrictedCharacters.GetEnumerator()) {
$URI = $URI.Replace($RestrictedCharacter.Key, $RestrictedCharacter.Value)
}
# Encode reserved characters
if (-Not $RestrictedOnly) {
foreach ($ReservedCharacter in $ReservedCharacters.GetEnumerator()) {
$URI = $URI.Replace($ReservedCharacter.Key, $ReservedCharacter.Value)
}
}
# Return encoded URI
return $URI
}
Expand Down

0 comments on commit 36170e8

Please sign in to comment.