8

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
clem
  • 81
  • 1
  • 3
  • So you've tried two clients: [wget](https://www.gnu.org/software/wget/) (from Powershell) and [Microsoft BITS](https://msdn.microsoft.com/en-us/library/aa362708%28v=vs.85%29.aspx). In both cases, you get "protocol errors" from the server. Hmmm.... Q: What happens if you run wget from the command line, or .bat file? Q: Can you successfully talk to the server from *ANY* client? Q: What exactly is the server supposed to be doing? what exactly *IS* the failing response, and how is it implemented? – paulsm4 Feb 07 '16 at 23:32
  • @paulsm4 wget is aliased to `Invoke-WebRequest` – Mathias R. Jessen Feb 07 '16 at 23:43
  • That is correct, having similar issues with both. Trying to download photos from a WiFi enabled camera. The connection to the embedded server is fine. When I do this same exact thing in linux with wget it works just fine. Would prefer to use BITS though. – clem Feb 07 '16 at 23:46
  • Q: What happens if you run wget from the command line, or .bat file? >> Same exact error Q: Can you successfully talk to the server from ANY client? >> Communication is fine, can browse the url with a browser Q: What exactly is the server supposed to be doing? what exactly IS the failing response, and how is it implemented? >> Download the file/picture. Embedded in WiFi Camera. – clem Feb 07 '16 at 23:52

1 Answers1

17

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

Community
  • 1
  • 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks for the quick reply When using reflection I get this error which I`m not able to resolve Exception calling "SetValue" with "2" argument(s): "Object of type 'System.Management.Automation.SwitchParameter' cannot be converted to type 'System.Boolean'." – clem Feb 08 '16 at 00:06