PoshCode Logo PowerShell Code Repository

HttpRest 1.0.2 (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1104"></script>download | new post

A BUG FIX for Invoke-Http

Documentation on this post on HuddledMasses

  1. ## Http Rest
  2. ####################################################################################################
  3. ## The first implementation of the HttpRest module, as a bunch of script functions
  4. ## Based on the REST api from MindTouch's Dream SDK
  5. ##
  6. ## INSTALL:
  7. ## You need log4net.dll mindtouch.core.dll mindtouch.dream.dll and SgmlReaderDll.dll from the SDK
  8. ## Download DREAM from http`://sourceforge.net/project/showfiles.php?group_id=173074
  9. ## Unpack it, and you can find these dlls in the "dist" folder.
  10. ## Make sure to put them in the folder with the module.
  11. ##
  12. ## For documentation of Dream:  http`://wiki.developer.mindtouch.com/Dream
  13. ####################################################################################################
  14. ## Version History
  15. ## 1.0   First Release
  16. ## 1.0.1 Added Get-WebPageContent
  17. ## 1.0.2 Fix for Invoke-Http not passing credentials correctly
  18. ####################################################################################################
  19. ## Usage:
  20. ##   function Get-Google {
  21. ##     Invoke-Http GET http`://www.google.com/search @{q=$args} |
  22. ##       Receive-Http Xml "//h3[@class='r']/a" | Select href, InnerText
  23. ##   }
  24. ##   #########################################################################
  25. ##   function Get-WebFile($url,$cred) {
  26. ##     Invoke-Http GET $url -auth $cred | Receive-Http File
  27. ##   }
  28. ##   #########################################################################
  29. ##   function Send-Paste {
  30. ##   PARAM($PastebinURI="http`://posh.jaykul.com/p/",[IO.FileInfo]$file)
  31. ##   PROCESS {
  32. ##     if($_){[IO.FileInfo]$file=$_}
  33. ##
  34. ##     if($file.Exists) {
  35. ##       $ofs="`n"
  36. ##       $result = Invoke-Http POST $PastebinURI @{
  37. ##         format="posh"           # PowerShell
  38. ##         expiry="d"              # (d)ay or (m)onth or (f)orever
  39. ##         poster=$([Security.Principal.WindowsIdentity]::GetCurrent().Name.Split("\")[-1])
  40. ##         code2="$((gc $file) -replace "http`://","http``://")" # To get past the spam filter.
  41. ##         paste="Send"
  42. ##       } -Type FORM_URLENCODED -Wait
  43. ##       $xml = $result.AsDocument().ToXml()
  44. ##       write-output $xml.SelectSingleNode("//*[@class='highlight']/*").href
  45. ##     } else { throw "File Not Found" }
  46. ##   }}
  47. ##
  48. ####################################################################################################
  49. if(!$PSScriptRoot){
  50.    Write-Debug $($MyInvocation.MyCommand | out-string)
  51.    $PSScriptRoot=(Split-Path $MyInvocation.MyCommand.Path -Parent)
  52. }
  53. #  Write-Debug "Invocation: $($MyInvocation.MyCommand.Path)"
  54. #  Write-Debug "Invocation: $($MyInvocation.MyCommand)"
  55. #  Write-Debug "Invocation: $($MyInvocation)"
  56.  
  57. Write-Debug "PSScriptRoot: '$PSScriptRoot'"
  58.  
  59.  
  60. # This Module depends on MindTouch.Dream
  61. $null = [Reflection.Assembly]::LoadFrom( "$PSScriptRoot\mindtouch.dream.dll" )
  62. # MindTouch.Dream requires: mindtouch.dream.dll, mindtouch.core.dll, SgmlReaderDll.dll, and log4net.dll)
  63. # This Module also depends on utility functions from System.Web
  64. $null = [Reflection.Assembly]::LoadWithPartialName("System.Web")
  65.  
  66. ## Some utility functions are defined at the bottom
  67. [uri]$global:url = ""
  68. [System.Management.Automation.PSCredential]$global:HttpRestCredential = $null
  69.  
  70. function Get-DreamMessage($Content,$Type) {
  71.    if(!$Content) {
  72.       return [MindTouch.Dream.DreamMessage]::Ok()
  73.    }
  74.    if( $Content -is [System.Xml.XmlDocument]) {
  75.       return [MindTouch.Dream.DreamMessage]::Ok( $Content )
  76.    }
  77.    
  78.    if(Test-Path $Content -EA "SilentlyContinue") {
  79.       return [MindTouch.Dream.DreamMessage]::FromFile((Convert-Path (Resolve-Path $Content)));
  80.    }
  81.    if($Type -is [String]) {
  82.       $Type = [MindTouch.Dream.MimeType]::$Type
  83.    }
  84.    if($Type -is [MindTouch.Dream.DreamMessage]) {
  85.       return [MindTouch.Dream.DreamMessage]::Ok( $Type, $Content )
  86.    } else {  
  87.       return [MindTouch.Dream.DreamMessage]::Ok( $([MindTouch.Dream.MimeType]::TEXT), $Content )
  88.    }
  89. }
  90.  
  91. function Get-DreamPlug {
  92.    PARAM ( $Url, [hashtable]$With )
  93.    if($Url -is [array]) {
  94.       if($Url[0] -is [hashtable]) {
  95.          $plug = [MindTouch.Dream.Plug]::New($global:url)
  96.          foreach($param in $url.GetEnumerator()) {
  97.             if($param.Value) {
  98.                $plug = $plug.At($param.Key,"=$(Encode-Twice $param.Value)")
  99.             } else {
  100.                $plug = $plug.At($param.Key)
  101.             }
  102.          }
  103.       }
  104.       else
  105.       {
  106.          [URI]$uri = Join-Url $global:url $url
  107.          $plug = [MindTouch.Dream.Plug]::New($uri)
  108.       }
  109.    }
  110.    elseif($url -is [string])
  111.    {
  112.       [URI]$uri = $url
  113.       if(!$uri.IsAbsoluteUri) {
  114.          $uri = Join-Url $global:url $url
  115.       }
  116.       $plug = [MindTouch.Dream.Plug]::New($uri)
  117.    }
  118.    else {
  119.       $plug = [MindTouch.Dream.Plug]::New($global:url)
  120.    }
  121.    if($with) {
  122.       foreach($param in $with.GetEnumerator()) {
  123.          if($param.Value) {
  124.             $plug = $plug.With($param.Key,$param.Value)
  125.          }
  126.       }
  127.    }
  128.    return $plug
  129. }
  130.  
  131. #CMDLET Receive-Http {
  132. Function Receive-Http {
  133. PARAM(
  134.    #  [Parameter(Position=1, Mandatory=$false)]
  135.    #  [ValidateSet("Xml", "File", "Text","Bytes")]
  136.    #  [Alias("As")]
  137.    $Output = "Xml"
  138. ,
  139.    #  [Parameter(Position=2, Mandatory=$false)]
  140.    [string]$Path
  141. ,
  142.    #  [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="Result")]
  143.    #  [Alias("IO")]
  144.    #  [MindTouch.Dream.Result``1[[MindTouch.Dream.DreamMessage]]]
  145.    $InputObject
  146. #,
  147.    #  [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="Response")]
  148.    #  [MindTouch.Dream.DreamMessage]
  149.    #  $response
  150. )
  151. BEGIN {
  152.    if($InputObject) {
  153.       Write-Output $inputObject | Receive-Http $Output $Path
  154.    } # else they'd better pass it in on the pipeline ...
  155. }
  156. PROCESS {
  157.    $response = $null
  158.    if($_ -is [MindTouch.Dream.Result``1[[MindTouch.Dream.DreamMessage]]]) {
  159.       $response = $_.Wait()
  160.    } elseif($_ -is [MindTouch.Dream.DreamMessage]) {
  161.       $response = $_
  162.    } elseif($_) {
  163.       throw "We can only pipeline [MindTouch.Dream.DreamMessage] objects, or [MindTouch.Dream.Result`1[DreamMessage]] objects"
  164.    }
  165.    
  166.    if($response) {
  167.       Write-Debug $($response | Out-String)
  168.       if(!$response.IsSuccessful) {
  169.          Write-Error $($response | Out-String)
  170.          Write-Verbose $response.AsText()
  171.          throw "ERROR: '$($response.Status)' Response Status."
  172.       } else {  
  173.          switch($Output) {
  174.             "File" {
  175.                ## Joel's magic filename guesser ...
  176.                if(!$Path) {
  177.                   [string]$fileName = ([regex]'(?i)filename=(.*)$').Match( $response.Headers["Content-Disposition"] ).Groups[1].Value
  178.                   $Path = $fileName.trim("\/""'")
  179.                   if(!$Path) {
  180.                      $fileName = $response.ResponseUri.Segments[-1]
  181.                      $Path = $fileName.trim("\/")
  182.                      if(!([IO.FileInfo]$Path).Extension) {
  183.                         $Path = $Path + "." + $response.ContentType.Split(";")[0].Split("/")[1]
  184.                      }
  185.                   }
  186.                }
  187.                
  188.                $File = Get-FileName $Path
  189.                [StreamUtil]::CopyToFile( $response.AsStream(), $response.ContentLength, $File )
  190.                Get-ChildItem $File
  191.             }
  192.             "XDoc" {
  193.                if($Path) {
  194.                   $response.AsDocument()[$Path]
  195.                } else {
  196.                   $response.AsDocument()#.ToXml()
  197.                }
  198.             }
  199.             "Xml" {
  200.                if($Path) {
  201.                   $response.AsDocument().ToXml().SelectNodes($Path)
  202.                } else {
  203.                   $response.AsDocument().ToXml()
  204.                }
  205.             }
  206.             "Text" {
  207.                if($Path) {
  208.                   $response.AsDocument()[$Path] | % { $_.AsText }
  209.                } else {
  210.                   $response.AsText()
  211.                }
  212.             }
  213.             "Bytes" {
  214.                $response.AsBytes()
  215.             }
  216.          }
  217.       }
  218.    }
  219. }
  220. }
  221. ## http`://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
  222. ## Nobody actually uses HEAD or OPTIONS, right?
  223. ## And nobody's even heard of TRACE or CONNECT ;)
  224.  
  225. # CMDLET Invoke-Http {
  226. Function Invoke-Http {
  227. PARAM(
  228.    # [Parameter(Position=0, Mandatory=$false)]
  229.    # [ValidateSet("Post", "Get", "Put", "Delete", "Head", "Options")] ## There are other verbs, but we need a list to make sure you don't screw up
  230.    [string]$Verb = "Get"
  231. ,
  232.    # [Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$true)]
  233.    # [string]
  234.    $Path
  235. ,
  236.    # [Parameter(Position=2, Mandatory=$false)]
  237.    [hashtable]$with
  238. ,
  239.    # [Parameter(Position=3, Mandatory=$false)]
  240.    $Content
  241. ,
  242.    $Type # Of Content
  243. ,
  244.    $authenticate
  245. ,
  246.    [switch]$waitForResponse
  247. )
  248. BEGIN {
  249.    $Verbs = "Post", "Get", "Put", "Delete", "Head", "Options"
  250.    if($Verbs -notcontains $Verb) {
  251.       Write-Warning "The specified verb '$Verb' is NOT one of the common verbs: $Verbs"
  252.    }
  253.  
  254.    if($Path) {
  255.       if($Content) {
  256.          Write-Output ($Path | Invoke-Http $Verb -With $With -Content $Content -Type $Type -Authenticate $authenticate -waitForResponse:$WaitForResponse)
  257.       } else {
  258.          Write-Output ($Path | Invoke-Http $Verb -With $With -Type $Type-Authenticate $authenticate -waitForResponse:$WaitForResponse)
  259.       }
  260.    } # else they'd better pass it in on the pipeline ...
  261. }
  262. PROCESS {
  263.    if($_) {
  264.       $Path = $_
  265.      
  266.       $plug = Get-DreamPlug $Path $With
  267.       Write-Verbose "Content Type: $Type"
  268.       Write-Verbose "Content: $Content"
  269.       ## Special Handling for FORM_URLENCODED
  270.       if($Type -like "Form*" -and !$Content) {
  271.          $dream = [MindTouch.Dream.DreamMessage]::Ok( $Plug.Uri )
  272.          $Plug = [MindTouch.Dream.Plug]::New( $Plug.Uri.SchemeHostPortPath )
  273.          Write-Verbose "RECREATED Plug: $($Plug.Uri.SchemeHostPortPath)"
  274.       } else {
  275.          $dream = Get-DreamMessage $Content $Type
  276.       }
  277.      
  278.       if(!$plug -or !$dream) {
  279.          throw "Can't come up with a request!"
  280.       }
  281.      
  282.       Write-Verbose $( $dream | Out-String )
  283.      
  284.       if($authenticate){
  285.             Write-Verbose "AUTHENTICATE AS $($authenticate.UserName)"
  286.          if($authenticate -is [System.Management.Automation.PSCredential]) {
  287.             Write-Verbose "AUTHENTICATING AS $($authenticate.UserName)"
  288.             $plug = $plug.WithCredentials($authenticate.GetNetworkCredential())
  289.          } elseif($authenticate -is [System.Net.NetworkCredential]) {
  290.             Write-Verbose "AUTHENTICATING AS $($authenticate.UserName)"
  291.             $plug = $plug.WithCredentials($authenticate)
  292.          } else {
  293.             $null = Get-HttpCredential
  294.             Write-Verbose "AUTHENTICATING AS $($global:HttpRestCredential.UserName)"
  295.             $plug = $plug.WithCredentials($global:HttpRestCredential.GetNetworkCredential())
  296.          }
  297.       }
  298.      
  299.       Write-Verbose $plug.Uri
  300.      
  301.       ## DEBUG:
  302.       Write-Debug "URI: $($Plug.Uri)"
  303.       Write-Debug "Verb: $($Verb.ToUpper())"
  304.       Write-Debug $($dream | Out-String)
  305.      
  306.       $result = $plug.InvokeAsync( $Verb.ToUpper(),  $dream )
  307.      
  308.       Write-Debug $($result | Out-String)
  309.       #  if($DebugPreference -eq "Continue") {
  310.       #     Write-Debug $($result.Wait() | Out-String)
  311.       #  }
  312.      
  313.       if($waitForResponse) {
  314.          $result = $result.Wait()
  315.       }
  316.      
  317.      
  318.       write-output $result
  319.    
  320.       trap [MindTouch.Dream.DreamResponseException] {
  321.          Write-Error @"
  322. TRAPPED DreamResponseException
  323.      
  324. $($_.Exception.Response | Out-String)
  325.  
  326. $($_.Exception.Response.Headers | Out-String)
  327. "@
  328.          break;
  329.       }
  330.    }
  331. }
  332. }
  333.  
  334.  
  335. function Get-WebPageContent {
  336. #[CmdletBinding()]
  337. param(
  338. #   [Parameter(Position=0,Mandatory=$true)]
  339.    [string]$url
  340. ,
  341. #   [Parameter(Position=1,Mandatory=$false)]
  342.    [string]$xpath=""
  343. ,
  344. #   [Parameter(Mandatory=$false)]
  345.    [hashtable]$with=@{}
  346. ,
  347. #   [Parameter(Position=2,Mandatory=$false)]
  348.    [switch]$AsXml
  349. )
  350. BEGIN { $out = "Text"; if($AsXml) { $out="Xml" } }
  351. PROCESS {
  352.    invoke-http get $url $with | receive-http $out $xpath
  353. }
  354. }
  355.  
  356. new-alias gwpc Get-WebPageContent -EA "SilentlyContinue"
  357. new-alias http Invoke-Http        -EA "SilentlyContinue"
  358. new-alias rcv  Receive-Http       -EA "SilentlyContinue"
  359.  
  360.  
  361. # function Get-Http { return Invoke-Http "GET" @args }
  362. # function New-Http { return Invoke-Http "PUT" @args }
  363. # function Update-Http { return Invoke-Http "POST" @args }
  364. # function Remove-Http { return Invoke-Http "DELETE" @args }
  365. # new-alias Set-Http Update-Http
  366. # new-alias Put-Http New-Http
  367. # new-alias Post-Http Update-Http
  368. # new-alias Delete-Http Remove-Http
  369.  
  370. function Set-HttpDefaultUrl {
  371. PARAM ([uri]$baseUri=$(Read-Host "Please enter the base Uri for your RESTful web-service"))
  372.    $global:url = $baseUri
  373. }
  374.  
  375. function Set-HttpCredential {
  376.    param($Credential=$(Get-CredentialBetter -Title   "Http Authentication Request - $($global:url.Host)" `
  377.                                       -Message "Your login for $($global:url.Host)" `
  378.                                       -Domain  $($global:url.Host)) )
  379.    if($Credential -is [System.Management.Automation.PSCredential]) {
  380.       $global:HttpRestCredential = $Credential
  381.    } elseif($Credential -is [System.Net.NetworkCredential]) {
  382.       $global:HttpRestCredential = new-object System.Management.Automation.PSCredential $Credential.UserName, $(ConvertTo-SecureString $credential.Password)
  383.    }
  384. }
  385.  
  386. function Get-HttpCredential([switch]$Secure) {
  387.    if(!$global:url) { Set-HttpDefaultUrl }
  388.    if(!$global:HttpRestCredential) { Set-HttpCredential }
  389.    if(!$Secure) {
  390.       return $global:HttpRestCredential.GetNetworkCredential();
  391.    } else {
  392.       return $global:HttpRestCredential
  393.    }
  394. }
  395.  
  396. # function Authenticate-Http {
  397. # PARAM($URL=@("users","authenticate"), $Credential = $(Get-HttpCredential))
  398. #   $plug = [MindTouch.Dream.Plug]::New( $global:url )
  399. #   $null = $plug.At("users", "authenticate").WithCredentials( $auth.UserName, $auth.Password ).Get()
  400. # }
  401.  
  402.  
  403. function Encode-Twice {
  404.    param([string]$text)
  405.    return [System.Web.HttpUtility]::UrlEncode( [System.Web.HttpUtility]::UrlEncode( $text ) )
  406. }
  407.  
  408. function Join-Url ( [uri]$baseUri=$global:url ) {
  409.    $ofs="/";$BaseUrl = ""
  410.    if($BaseUri -and $baseUri.AbsoluteUri) {
  411.       $BaseUrl = "$($baseUri.AbsoluteUri.Trim('/'))/"
  412.    }
  413.    return [URI]"$BaseUrl$([string]::join("/",@($args)).TrimStart('/'))"
  414. }
  415.  
  416. function ConvertTo-SecureString {
  417. Param([string]$input)
  418.    $result = new-object System.Security.SecureString
  419.  
  420.    foreach($c in $input.ToCharArray()) {
  421.       $result.AppendChar($c)
  422.    }
  423.    $result.MakeReadOnly()
  424.    return $result
  425. }
  426.  
  427. ## Unit-Test Get-FileName  ## Should return TRUE
  428. ##   (Get-FileName C:\Windows\System32\Notepad.exe)               -eq "C:\Windows\System32\Notepad.exe"   -and
  429. ##   (Get-FileName C:\Windows\Notepad.exe C:\Windows\System32\)   -eq "C:\Windows\System32\Notepad.exe"   -and
  430. ##   (Get-FileName WaitFor.exe C:\Windows\System32\WaitForIt.exe) -eq "C:\Windows\System32\WaitForIt.exe" -and
  431. ##   (Get-FileName -Path C:\Windows\System32\WaitForIt.exe)       -eq "C:\Windows\System32\WaitForIt.exe"      
  432. function Get-FileName {
  433.    param($fileName=$([IO.Path]::GetRandomFileName()), $path)
  434.    $fileName = $fileName.trim("\/""'")
  435.    ## if the $Path has a file name, and it's folder exists:
  436.    if($Path -and !(Test-Path $Path -Type Container) -and (Test-Path (Split-Path $path) -Type Container)) {
  437.       $path
  438.    ## if the $Path is just a folder (and it exists)
  439.    } elseif($Path -and (Test-Path $path -Type Container)) {
  440.       $fileName = Split-Path $fileName -leaf
  441.       Join-Path $path $fileName
  442.    ## If there's no valid $Path, and the $FileName has a folder...
  443.    } elseif((Split-Path $fileName) -and (Test-Path (Split-Path $fileName))) {
  444.       $fileName
  445.    } else {
  446.       Join-Path (Get-Location -PSProvider "FileSystem") (Split-Path $fileName -Leaf)
  447.    }
  448. }
  449.  
  450. function Get-UtcTime {
  451.    Param($Format="yyyyMMddhhmmss")
  452.    [DateTime]::Now.ToUniversalTime().ToString($Format)
  453. }
  454.  
  455. ## Get-CredentialBetter
  456. ## An improvement over the default cmdlet which has no options ...
  457. ###################################################################################################
  458. ## History
  459. ## v 1.2 Refactor ShellIds key out to a variable, and wrap lines a bit
  460. ## v 1.1 Add -Console switch and set registry values accordingly (ouch)
  461. ## v 1.0 Add Title, Message, Domain, and UserName options to the Get-Credential cmdlet
  462. ###################################################################################################
  463. function Get-CredentialBetter{
  464. PARAM([string]$UserName, [string]$Title, [string]$Message, [string]$Domain, [switch]$Console)
  465.    $ShellIdKey = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds"
  466.    ## Carefully EA=SilentlyContinue because by default it's MISSING, not $False
  467.    $cp = (Get-ItemProperty $ShellIdKey ConsolePrompting -ea "SilentlyContinue")
  468.    ## Compare to $True, because by default it's $null ...
  469.    $cp = $cp.ConsolePrompting -eq $True
  470.  
  471.    if($Console -and !$cp) {
  472.       Set-ItemProperty $ShellIdKey ConsolePrompting $True
  473.    } elseif(!$Console -and $Console.IsPresent -and $cp) {
  474.       Set-ItemProperty $ShellIdKey ConsolePrompting $False
  475.    }
  476.  
  477.    ## Now call the Host.UI method ... if they don't have one, we'll die, yay.
  478.    $Host.UI.PromptForCredential($Title,$Message,$UserName,$Domain,"Generic","Default")
  479.  
  480.    ## BoyScouts: Leave everything better than you found it (I'm tempted to leave it = True)
  481.    Set-ItemProperty $ShellIdKey ConsolePrompting $cp
  482. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me