HttpServer.HttpRequest.AddHeader C# (CSharp) Method

AddHeader() public method

Called during parsing of a IHttpRequest.
If a header is incorrect.
public AddHeader ( string name, string value ) : void
name string Name of the header, should not be url encoded
value string Value of the header, should not be url encoded
return void
        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;
            }
        }

Usage Example

Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected virtual void OnHeaderReceived(object sender, HeaderEventArgs e)
 {
     if (string.Compare(e.Name, "expect", true) == 0 && e.Value.Contains("100-continue"))
     {
         lock (requestsInServiceIDs)
         {
             if (requestsInServiceIDs.Count == 0)
             {
                 Respond("HTTP/1.1", HttpStatusCode.Continue, "Please continue.");
             }
         }
     }
     m_currentRequest.AddHeader(e.Name, e.Value);
 }
All Usage Examples Of HttpServer.HttpRequest::AddHeader