private bool SetAllowUnsafeHeaderParsing(bool setState)
{
try {
lock (lockingObj) {
// update our counter of the number of requests needing
// unsafe header processing
if (setState == true) unsafeHeaderUserCount++;
else unsafeHeaderUserCount--;
// if there was already a request using unsafe heaser processing, we
// dont need to take any action.
if (unsafeHeaderUserCount > 1)
return true;
// if the request tried to turn off unsafe header processing but it is
// still needed by another request, we should wait.
if (unsafeHeaderUserCount >= 1 && setState == false)
return true;
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly == null)
return false;
//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)
return false;
//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)
return false;
//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)
return false;
// and finally set our setting
aUseUnsafeHeaderParsing.SetValue(anInstance, setState);
return true;
}
}
catch (Exception e) {
if (e.GetType() == typeof(ThreadAbortException))
throw e;
logger.Error("Unsafe header parsing setting change failed.");
return false;
}
}