Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 9f22734

Browse files
committed
Ignore null valued headers #429
1 parent 90ece57 commit 9f22734

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/FrameResponseHeaders.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ public void CopyTo(ref MemoryPoolIterator2 output)
3939
{
4040
foreach (var value in kv.Value)
4141
{
42-
output.CopyFrom(_CrLf, 0, 2);
43-
output.CopyFromAscii(kv.Key);
44-
output.CopyFrom(_colonSpace, 0, 2);
45-
output.CopyFromAscii(value);
42+
if (value != null)
43+
{
44+
output.CopyFrom(_CrLf, 0, 2);
45+
output.CopyFromAscii(kv.Key);
46+
output.CopyFrom(_colonSpace, 0, 2);
47+
output.CopyFromAscii(value);
48+
}
4649
}
4750
}
4851
}

test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ResponseTests.cs

+51-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Net.Http;
67
using System.Threading.Tasks;
78
using Microsoft.AspNet.Builder;
89
using Microsoft.AspNet.Hosting;
10+
using Microsoft.AspNet.Http;
911
using Microsoft.AspNet.Testing.xunit;
1012
using Microsoft.Extensions.Configuration;
1113
using Xunit;
@@ -44,7 +46,7 @@ public async Task LargeDownload()
4446
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
4547
}
4648
});
47-
});
49+
});
4850

4951
using (var app = hostBuilder.Build().Start())
5052
{
@@ -70,5 +72,53 @@ public async Task LargeDownload()
7072
}
7173
}
7274
}
75+
76+
[ConditionalFact]
77+
[FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on mono.")]
78+
public async Task IgnoreNullHeaderValues()
79+
{
80+
var config = new ConfigurationBuilder()
81+
.AddInMemoryCollection(new Dictionary<string, string>
82+
{
83+
{ "server.urls", "http://localhost:8793/" }
84+
})
85+
.Build();
86+
87+
var hostBuilder = new WebHostBuilder(config)
88+
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
89+
.UseStartup(app =>
90+
{
91+
app.Run(async context =>
92+
{
93+
context.Response.Headers.Add("NullString", (string)null);
94+
context.Response.Headers.Add("EmptyString", "");
95+
context.Response.Headers.Add("NullStringArray", new string[] { null });
96+
context.Response.Headers.Add("EmptyStringArray", new string[] { "" });
97+
context.Response.Headers.Add("MixedStringArray", new string[] { null, "" });
98+
99+
context.Response.ContentLength = 0;
100+
await context.Response.WriteAsync("");
101+
});
102+
});
103+
104+
using (var app = hostBuilder.Build().Start())
105+
{
106+
using (var client = new HttpClient())
107+
{
108+
var response = await client.GetAsync("http://localhost:8793/");
109+
response.EnsureSuccessStatusCode();
110+
111+
var headers = response.Headers;
112+
Assert.False(headers.Contains("NullString"));
113+
Assert.True(headers.Contains("EmptyString"));
114+
Assert.Equal(headers.GetValues("EmptyString").Single(), "" );
115+
Assert.False(headers.Contains("NullStringArray"));
116+
Assert.True(headers.Contains("EmptyStringArray"));
117+
Assert.Equal(headers.GetValues("EmptyStringArray").Single(), "");
118+
Assert.True(headers.Contains("MixedStringArray"));
119+
Assert.Equal(headers.GetValues("MixedStringArray").Single(), "");
120+
}
121+
}
122+
}
73123
}
74124
}

0 commit comments

Comments
 (0)