public void AddHeader(string name, string value)
{
if (string.IsNullOrEmpty(name))
throw new BadRequestException("Invalid header name: " + name ?? "<null>");
if (string.IsNullOrEmpty(value))
throw new BadRequestException("Header '" + name + "' do not contain a value.");
switch (name.ToLower())
{
case "http_x_requested_with":
case "x-requested-with":
if (string.Compare(value, "XMLHttpRequest", true) == 0)
_isAjax = true;
break;
case "accept":
_acceptTypes = value.Split(',');
for (int i = 0; i < _acceptTypes.Length; ++i)
_acceptTypes[i] = _acceptTypes[i].Trim();
break;
case "content-length":
int t;
if (!int.TryParse(value, out t))
throw new BadRequestException("Invalid content length.");
ContentLength = t;
break; //todo: mayby throw an exception
case "host":
try
{
_uri = new Uri(Secure ? "https://" : "http://" + value + _uriPath);
_uriParts = _uri.AbsolutePath.Split(UriSplitters, StringSplitOptions.RemoveEmptyEntries);
}
catch (UriFormatException err)
{
throw new BadRequestException("Failed to parse uri: " + value + _uriPath, err);
}
break;
case "connection":
if (string.Compare(value, "close", true) == 0)
Connection = ConnectionType.Close;
else if (value.StartsWith("keep-alive", StringComparison.CurrentCultureIgnoreCase))
Connection = ConnectionType.KeepAlive;
else
throw new BadRequestException("Unknown 'Connection' header type.");
break;
default:
_headers.Add(name, value);
break;
}
}