Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 7fdc019

Browse files
committed
Ensure that BOM is not sent in UTF8-encoded response
1 parent 90c2589 commit 7fdc019

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

examples/rabbitmq_auth_backend_webapi_dotnet/Controllers/AuthController.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
using System.Net;
33
using System.Net.Http;
44
using System.Net.Http.Formatting;
5+
using System.Text;
56
using System.Web.Http;
67

78
namespace WebApiHttpAuthService.Controllers
89
{
910
[RoutePrefix("auth")]
1011
public class AuthController : ApiController
1112
{
13+
// Note: the following is necessary to ensure that no
14+
// BOM is part of the response
15+
private static readonly encoding = new UTF8Encoding(false);
1216

13-
[Route("user")]
17+
[Route("user")]
1418
[HttpPost]
1519
public HttpResponseMessage user(FormDataCollection form)
1620
{
@@ -34,7 +38,7 @@ public HttpResponseMessage user(FormDataCollection form)
3438
}
3539

3640
var resp = new HttpResponseMessage(HttpStatusCode.OK);
37-
resp.Content = new StringContent(content, System.Text.Encoding.UTF8, "text/plain");
41+
resp.Content = new StringContent(content, encoding, "text/plain");
3842
return resp;
3943
}
4044

@@ -62,7 +66,7 @@ public HttpResponseMessage vhost(FormDataCollection form)
6266
}
6367

6468
var resp = new HttpResponseMessage(HttpStatusCode.OK);
65-
resp.Content = new StringContent(content, System.Text.Encoding.UTF8, "text/plain");
69+
resp.Content = new StringContent(content, encoding, "text/plain");
6670
return resp;
6771
}
6872

@@ -96,7 +100,7 @@ public HttpResponseMessage resource(FormDataCollection form)
96100

97101

98102
var resp = new HttpResponseMessage(HttpStatusCode.OK);
99-
resp.Content = new StringContent(content, System.Text.Encoding.UTF8, "text/plain");
103+
resp.Content = new StringContent(content, encoding, "text/plain");
100104
return resp;
101105
}
102106

@@ -129,7 +133,7 @@ public HttpResponseMessage topic(FormDataCollection form)
129133
}
130134

131135
var resp = new HttpResponseMessage(HttpStatusCode.OK);
132-
resp.Content = new StringContent(content, System.Text.Encoding.UTF8, "text/plain");
136+
resp.Content = new StringContent(content, encoding, "text/plain");
133137
return resp;
134138
}
135139

0 commit comments

Comments
 (0)