Skip to content

Instantly share code, notes, and snippets.

@loudej
Created November 14, 2011 18:37
Show Gist options
  • Save loudej/1364703 to your computer and use it in GitHub Desktop.
Save loudej/1364703 to your computer and use it in GitHub Desktop.
asp.net does not actually support multiple same-named incoming headers
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web.Mvc;
namespace HeaderChecking.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// send a request with http client api
HttpWebRequest r = (HttpWebRequest)WebRequest.Create("http://localhost:45651/Home/About");
r.Accept = "foo/bar, frap/quad,yadda/*";
r.Headers.Add("Accept-Ex", "foo/bar, frap/quad");
r.Headers.Add("Accept-Ex", "yadda/*");
r.GetResponse();
// send a request with socket api (only way to *actually* produce multiple headers)
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socket.Connect("localhost", 45651);
var data = Encoding.Default.GetBytes(
@"GET /Home/About HTTP/1.1
Host: localhost
Connection: close
Accept: text/plain, text/html
Accept: application/xml
Accept-Ex: foo/bar, frap/quad
Accept-Ex: yadda/*
");
socket.Send(data, 0, data.Length, SocketFlags.None);
// mark the end-of-request-body... we also don't bother to read response...
socket.Shutdown(SocketShutdown.Send);
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
string allRaw = Request.ServerVariables.Get("ALL_RAW");
string allHttp = Request.ServerVariables.Get("ALL_HTTP");
string acceptEx1 = Request.ServerVariables.Get("HTTP_ACCEPT_EX");
string[] acceptEx2 = Request.ServerVariables.GetValues("HTTP_ACCEPT_EX");
string acceptEx3 = Request.Headers.Get("Accept-Ex");
string[] acceptEx4 = Request.Headers.GetValues("Accept-Ex");
string accept1 = Request.ServerVariables.Get("HTTP_ACCEPT");
string[] accept2 = Request.ServerVariables.GetValues("HTTP_ACCEPT");
string accept3 = Request.Headers.Get("Accept");
string[] accept4 = Request.Headers.GetValues("Accept");
string[] acceptTypes = Request.AcceptTypes;
//Trace.Assert(acceptEx1 == "foo/bar, frap/quad,yadda/*");
//Trace.Assert(acceptEx2.Length == 1);
//Trace.Assert(acceptEx2[0] == "foo/bar, frap/quad,yadda/*");
//Trace.Assert(acceptEx3 == "foo/bar, frap/quad,yadda/*");
//Trace.Assert(acceptEx4.Length == 1);
//Trace.Assert(acceptEx4[0] == "foo/bar, frap/quad,yadda/*");
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment