-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTrace-WebRequest.ps1
More file actions
152 lines (141 loc) · 4.93 KB
/
Copy pathTrace-WebRequest.ps1
File metadata and controls
152 lines (141 loc) · 4.93 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
<#
.SYNOPSIS
Provides details about a retrieving a URI.
.NOTES
TODO: Add support for other Invoke-WebRequest parameters.
.INPUTS
System.Uri to retrieve.
.FUNCTIONALITY
HTTP
.LINK
Import-CharConstants.ps1
.LINK
Write-Info.ps1
.LINK
Import-Variables.ps1
.EXAMPLE
Trace-WebRequest.ps1 g.co/p3phelp -SkipHeaders -SkipContent
g.co is CN=*.google.com from CN=WR2, O=Google Trust Services, C=US
Valid 05/12/2025 01:42:58 to 08/04/2025 01:42:57
GET https://g.co/p3phelp
HTTP/1.1 302 Found
Following redirect to https://support.google.com/accounts/answer/151657?hl=en
support.google.com is CN=*.google.com from CN=WR2, O=Google Trust Services, C=US
Valid 05/12/2025 01:42:58 to 08/04/2025 01:42:57
GET https://support.google.com/accounts/answer/151657?hl=en
HTTP/1.1 301 MovedPermanently
Following redirect to https://support.google.com/accounts/topic/3382252?hl=en&visit_id=638845176026805186-2907418293&rd=1
GET https://support.google.com/accounts/topic/3382252?hl=en&visit_id=638845176026805186-2907418293&rd=1
HTTP/1.1 301 MovedPermanently
Following redirect to https://support.google.com/accounts/?hl=en&visit_id=638845176026805186-2907418293&rd=2&topic=3382252
GET https://support.google.com/accounts/?hl=en&visit_id=638845176026805186-2907418293&rd=2&topic=3382252
HTTP/1.1 200 OK
#>
using namespace System.Net.Http
#Requires -Version 7
[CmdletBinding()] Param(
# The URL to retrieve.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('Url','Href','Src')][uri] $Uri,
# The HTTP method verb to use.
[HttpMethod] $Method = 'GET',
# A file to log the request to.
[string] $LogFile,
# Indicates headers shouldn't be output.
[switch] $SkipHeaders,
# Indicates content shouldn't be output.
[switch] $SkipContent
)
Begin
{
$certhost = @{}
Import-CharConstants.ps1 :lock: :outbox_tray: :inbox_tray: :information_source: 'timer clock' -AsEmoji
filter Get-HttpStatusColor
{
[CmdletBinding()][OutputType([ConsoleColor])] Param(
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][int] $Code
)
switch([int]::DivRem($Code, 100).Item1)
{
1 {return [ConsoleColor]::White}
2 {return [ConsoleColor]::Green}
3 {return [ConsoleColor]::Blue}
4 {return [ConsoleColor]::Red}
5 {return [ConsoleColor]::DarkRed}
}
}
function Trace-Uri
{
[CmdletBinding()] Param(
# The URL to retrieve.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('Url','Href','Src')][uri] $Uri,
# The HTTP method verb to use.
[HttpMethod] $Method = 'GET'
)
if(!$certhost.Contains($Uri.Host))
{
$certinfo = Get-ServerCertificate.ps1 $Uri.Host
$certhost[$Uri.Host] = $certinfo
Write-Info.ps1 "$lock $($Uri.Host) is $($certinfo.Subject) from $($certinfo.Issuer)" -fg Magenta
Write-Info.ps1 "${timer clock} Valid $($certinfo.Issued) to $($certinfo.Expires)" -fg DarkMagenta
}
$request = New-Object Net.Http.HttpRequestMessage -ArgumentList $Method, $Uri
$requestLine, $requestRawHeaders = "$Method $Uri", ($request.Headers.ToString())
Write-Verbose $requestLine
Write-Verbose $requestRawHeaders
Write-Info.ps1 "$outbox_tray $requestLine" -fg DarkGreen
#Write-Info.ps1 $requestRawHeaders -fg DarkGray
if($LogFile)
{@"
###
$Method $Uri
$requestRawHeaders
"@ |Add-Content $LogFile
}
Write-Debug $requestLine
$StatusCode = 0
Invoke-WebRequest -Uri $Uri -SkipHttpErrorCheck -MaximumRedirection 0 -AllowInsecureRedirect -EA Ignore |
Import-Variables.ps1
if(!$StatusCode)
{
if($LogFile)
{@"
###
# Response: $($_.Message)
"@ |Add-Content $LogFile
}
return
}
$statusLine, $rawHeaders = ($RawContent -replace '(?s)\r?\n\r?\n.*\z') -split '\r?\n',2
if($null -eq $rawHeaders) {$rawHeaders = ''}
Write-Verbose $statusLine
Write-Verbose $rawHeaders
Write-Info.ps1 "$inbox_tray $statusLine" -fg (Get-HttpStatusColor $StatusCode)
if(!$SkipHeaders) {Write-Info.ps1 $rawHeaders -fg Gray}
if(!$SkipContent -and $Content) {Write-Info.ps1 $Content -fg White}
if($LogFile)
{@"
###
# Response:
$RawContent
"@ |Add-Content $LogFile
}
if([int]::DivRem($StatusCode, 100).Item1 -eq 3)
{
foreach($location in $Headers.Location |ForEach-Object {New-Object Uri $Uri,$_})
{
Write-Info.ps1 "$information_source Following redirect to $location" -fg DarkBlue
Trace-Uri $location
}
}
}
}
Process
{
if(!$Uri.IsAbsoluteUri -and [uri]::IsWellFormedUriString("https://$Uri", 'Absolute'))
{
[uri] $Uri = "https://$Uri"
}
Trace-Uri -Uri $Uri -Method $Method
}