Skip to content

Instantly share code, notes, and snippets.

@tfwio
Last active December 26, 2015 15:49
Show Gist options
  • Save tfwio/7175420 to your computer and use it in GitHub Desktop.
Save tfwio/7175420 to your computer and use it in GitHub Desktop.
Two helper snipits (not to forget) for reading from shoutcast streams in Microsoft.NET applications (app.config), of which one or the other may be used. See the readme for info on how to use and the source (forum post).

SetAllowUnsafeHeaderParsing Snippits

Brief

Either of the two provided snipts address the same issue.

  • the csharp file addresses the issue with a block of code you should call in your 'main' program-process.
  • the xml app.config may alternatively be used for brevity.

Why

Without implementing the provided setting, you will find it difficult to write an application having the ability to stream ShoutCast audio data from a shoutcast-server due limitations within .NET's web/client/socket configuration settings. To address this issue, the microsoft-forum-post(s) point out a per-application configuration setting that resolves such an issue.

Now, this leaves for some other potential erronious handling of URI data. For this, you may like to google .NET URI HACKS on stack-overflow or google.

Source: Social.MSDN.Microsoft.com/forums/...

see: how-do-i-enable-useunsafeheaderparsing-from-code-net-20

<!-- added to application configuration file: app.config -->
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
<!--/configuration-->
public static bool SetAllowUnsafeHeaderParsing20()
{
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment