Skip to content

Commit

Permalink
- Added HTTP POST verb support to DumbDownloadModuleFromWeb, Download…
Browse files Browse the repository at this point in the history
…ModuleFromWeb

- Added parsing of XML psget:properties / psget:verb if it exists, to allow the directory Xml to say that some downloads are via POST
- This is mostly a work around for CodePlex license agreement checks which break standard HTTP semantics (they require a post to the Url, set some cookies and redirect to a GET to ensure the license has been agreed to - yuck)
  • Loading branch information
Iristyle committed Mar 27, 2012
1 parent 59e3e07 commit 351c6ea
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions PsGet/PsGet.psm1
Expand Up @@ -60,17 +60,18 @@ process {
}

$Type = $moduleData.Type
$ModuleName = $moduleData.Id
$ModuleName = $moduleData.Id
$Verb = $moduleData.Verb

$downloadResult = DumbDownloadModuleFromWeb -DownloadURL:$moduleData.DownloadUrl -ModuleName:$moduleData.Id -Type:$Type
$downloadResult = DumbDownloadModuleFromWeb -DownloadURL:$moduleData.DownloadUrl -ModuleName:$moduleData.Id -Type:$Type -Verb:$Verb

$TempModuleFolderPath = $downloadResult.ModuleFolderPath
break
}
"Web" {
Write-Verbose "Module will be installed from $ModuleUrl"

$result = DownloadModuleFromWeb -DownloadURL:$ModuleUrl -ModuleName:$ModuleName -Type:$Type
$result = DownloadModuleFromWeb -DownloadURL:$ModuleUrl -ModuleName:$ModuleName -Type:$Type -Verb:GET

$Type = $result.Type
$ModuleName = $result.ModuleName
Expand Down Expand Up @@ -313,12 +314,16 @@ function Get-PsGetModuleInfo {
"application/zip" { $Type = $PSGET_ZIP }
default { $Type = $PSGET_PSM1 }
}

$Verb = if ($_.properties.Verb -imatch 'POST') { "POST" }
else { "GET" }

New-Object PSObject -Property @{
"Title" = $_.title.innertext
"Id" = $_.id
"Type" = $Type
"DownloadUrl" = $_.content.src
"Verb" = $Verb
} |
Add-Member -MemberType AliasProperty -Name ModuleName -Value Title -PassThru |
Add-Member -MemberType AliasProperty -Name ModuleUrl -Value DownloadUrl -PassThru
Expand Down Expand Up @@ -417,7 +422,7 @@ function TryGuessTypeByExtension($fileName){
return $PSGET_PSM1
}

function DumbDownloadModuleFromWeb($DownloadURL, $ModuleName, $Type) {
function DumbDownloadModuleFromWeb($DownloadURL, $ModuleName, $Type, $Verb) {

#Create folder to download module content into
$TempModuleFolderPath = join-path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString() + "\$ModuleName")
Expand All @@ -427,7 +432,13 @@ function DumbDownloadModuleFromWeb($DownloadURL, $ModuleName, $Type) {
$client = (new-object Net.WebClient)
$client.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$DownloadFilePath = [System.IO.Path]::GetTempFileName()
$client.DownloadFile($DownloadURL, $DownloadFilePath)
if ($Verb -eq 'POST') {
$client.Headers['Content-type'] = 'application/x-www-form-urlencoded'
[IO.File]::WriteAllBytes($DownloadFilePath, $client.UploadData($DownloadURL, ''))
}
else {
$client.DownloadFile($DownloadURL, $DownloadFilePath)
}

switch ($Type) {
$PSGET_ZIP {
Expand All @@ -454,7 +465,8 @@ Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$DownloadURL,
[String]$ModuleName,
[String]$Type
[String]$Type,
[String]$Verb
)
function TryGuessFileName($client, $downloadUrl){
## Try get module name from content disposition header (e.g. attachment; filename="Pscx-2.0.0.1.zip" )
Expand Down Expand Up @@ -487,7 +499,13 @@ Param(
$client = (new-object Net.WebClient)
$client.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$DownloadFilePath = [System.IO.Path]::GetTempFileName()
$client.DownloadFile($DownloadURL, $DownloadFilePath)
if ($Verb -eq 'POST') {
$client.Headers['Content-type'] = 'application/x-www-form-urlencoded'
[IO.File]::WriteAllBytes($DownloadFilePath, $client.UploadData($DownloadURL, ''))
}
else {
$client.DownloadFile($DownloadURL, $DownloadFilePath)
}

$CandidateFileName = TryGuessFileName $client $downloadUrl

Expand Down

0 comments on commit 351c6ea

Please sign in to comment.