Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell wget protocol violation

I am trying to browse a command on an embedded webserver using wget / invoke-webrequest. How can this error be avoided?

wget : The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

Already tried multiple things, for example below without success:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

When using BITS instead, this is the error I'm getting

start-bitstransfer : The server did not return the file size. The URL might point to dynamic content. The Content-Length header is not available in the server's HTTP reply.

Thanks so much for your help Clem

like image 257
clem Avatar asked Feb 07 '16 23:02

clem


Video Answer


1 Answers

Setting the ServerCertificateValidationCallback delegate won't help you - SSL/TLS is not the protocol being referred to - the protocol violation is with regards to the HTTP headers (eg. long after TLS has been established).

There's a .NET configuration flag called useUnsafeHeaderParsing that controls whether such violations are ignored or not.

Using reflection, it can also be set from the runtime. This Technet forum answer give's a great example of how to do so in PowerShell, we can wrap that in a nifty function like below:

function Set-UseUnsafeHeaderParsing
{
    param(
        [Parameter(Mandatory,ParameterSetName='Enable')]
        [switch]$Enable,

        [Parameter(Mandatory,ParameterSetName='Disable')]
        [switch]$Disable
    )

    $ShouldEnable = $PSCmdlet.ParameterSetName -eq 'Enable'

    $netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

    if($netAssembly)
    {
        $bindingFlags = [Reflection.BindingFlags] 'Static,GetProperty,NonPublic'
        $settingsType = $netAssembly.GetType('System.Net.Configuration.SettingsSectionInternal')

        $instance = $settingsType.InvokeMember('Section', $bindingFlags, $null, $null, @())

        if($instance)
        {
            $bindingFlags = 'NonPublic','Instance'
            $useUnsafeHeaderParsingField = $settingsType.GetField('useUnsafeHeaderParsing', $bindingFlags)

            if($useUnsafeHeaderParsingField)
            {
              $useUnsafeHeaderParsingField.SetValue($instance, $ShouldEnable)
            }
        }
    }
}

And then use like:

Set-UseUnsafeHeaderParsing -Enable

before calling Invoke-WebRequest

like image 174
Mathias R. Jessen Avatar answered Sep 24 '22 05:09

Mathias R. Jessen