{
Unknown,
WebRequest,
- WebResponse,
- HttpWebRequest,
- HttpWebResponse,
- HttpListenerRequest,
- HttpListenerResponse,
- FtpWebRequest,
- FtpWebResponse,
- FileWebRequest,
- FileWebResponse,
+ WebResponse
}
public class WebHeaderCollection : NameValueCollection, ISerializable
{
_type = WebHeaderCollectionType.WebRequest;
}
- return _type == WebHeaderCollectionType.WebRequest ||
- _type == WebHeaderCollectionType.HttpWebRequest ||
- _type == WebHeaderCollectionType.HttpListenerRequest;
+ return _type == WebHeaderCollectionType.WebRequest;
}
}
{
_type = WebHeaderCollectionType.WebResponse;
}
- return _type == WebHeaderCollectionType.WebResponse ||
- _type == WebHeaderCollectionType.HttpWebResponse ||
- _type == WebHeaderCollectionType.HttpListenerResponse;
+ return _type == WebHeaderCollectionType.WebResponse;
}
}
}
name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
- ThrowOnRestrictedHeader(name);
value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (value != null && value.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
InvalidateCachedArrays();
InnerCollection.Set(name, value);
}
{
throw new InvalidOperationException(SR.net_headers_rsp);
}
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (value != null && value.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
this.Set(header.GetName(), value);
}
{
throw new InvalidOperationException(SR.net_headers_rsp);
}
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (value != null && value.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
this.Add(header.GetName(), value);
}
string name = header.Substring(0, colpos);
string value = header.Substring(colpos + 1);
name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
- ThrowOnRestrictedHeader(name);
value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (value != null && value.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(header), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
InvalidateCachedArrays();
InnerCollection.Add(name, value);
}
}
name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
- ThrowOnRestrictedHeader(name);
value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (value != null && value.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
InvalidateCachedArrays();
InnerCollection.Add(name, value);
}
{
headerName = HttpValidationHelpers.CheckBadHeaderNameChars(headerName);
headerValue = HttpValidationHelpers.CheckBadHeaderValueChars(headerValue);
- if (_type == WebHeaderCollectionType.WebResponse)
- {
- if (headerValue != null && headerValue.Length > ushort.MaxValue)
- {
- throw new ArgumentOutOfRangeException(nameof(headerValue), headerValue, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
- }
- }
InvalidateCachedArrays();
InnerCollection.Add(headerName, headerValue);
}
- internal void ThrowOnRestrictedHeader(string headerName)
- {
- if (_type == WebHeaderCollectionType.HttpWebRequest)
- {
- if (HeaderInfo[headerName].IsRequestRestricted)
- {
- throw new ArgumentException(SR.Format(SR.net_headerrestrict, headerName), nameof(headerName));
- }
- }
- else if (_type == WebHeaderCollectionType.HttpListenerResponse)
- {
- if (HeaderInfo[headerName].IsResponseRestricted)
- {
- throw new ArgumentException(SR.Format(SR.net_headerrestrict, headerName), nameof(headerName));
- }
- }
- }
-
// Remove -
// Routine Description:
// Removes give header with validation to see if they are "proper" headers.
{
throw new ArgumentNullException(nameof(name));
}
- ThrowOnRestrictedHeader(name);
name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
if (_innerCollection != null)
{
Assert.Equal(new[] { "firstName" }, w.AllKeys);
Assert.Equal("first", w["firstName"]);
}
+
+ [Fact]
+ public void AddLongString_DoesNotThrow()
+ {
+ string longString = new string('a', 65536);
+ WebHeaderCollection headerCollection = new WebHeaderCollection();
+
+ headerCollection.Add("Long-Header", longString);
+ headerCollection["Long-Header-2"] = longString;
+
+ headerCollection.Add(HttpResponseHeader.SetCookie, "someValueToChangeType"); // this will implicitly change _type
+
+ headerCollection.Add("Long-Header-3", longString);
+ headerCollection["Long-Header-4"] = longString;
+
+ Assert.Equal(longString, headerCollection["Long-Header"]);
+ Assert.Equal(longString, headerCollection["Long-Header-2"]);
+ Assert.Equal(longString, headerCollection["Long-Header-3"]);
+ Assert.Equal(longString, headerCollection["Long-Header-4"]);
+ }
}
}