private int ParseHeaders(System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders, string[] lines)
        {
            //Blank line separates headers from content, so read headers until we find blank line.
            int    lineIndex = 1;
            string line = null, nextLine = null;

            while (lineIndex + 1 < lines.Length && !string.IsNullOrEmpty((line = lines[lineIndex++])))
            {
                //If the following line starts with space or tab (or any whitespace), it is really part of this header but split for human readability.
                //Combine these lines into a single comma separated style header for easier parsing.
                while (lineIndex < lines.Length && !string.IsNullOrEmpty((nextLine = lines[lineIndex])))
                {
                    if (nextLine.Length > 0 && char.IsWhiteSpace(nextLine[0]))
                    {
                        line += "," + nextLine.TrimStart();
                        lineIndex++;
                    }
                    else
                    {
                        break;
                    }
                }

                ParseHeader(line, headers, contentHeaders);
            }
            return(lineIndex);
        }
Esempio n. 2
0
 public static void CopyHeadersTo(this HttpHeaders httpHeaders, IHeaderDictionary headerDictionary)
 {
     foreach (var httpHeader in httpHeaders)
     {
         headerDictionary.Add(httpHeader.Key, new StringValues(httpHeader.Value.ToArray()));
     }
 }
        /// <summary>
        /// Parses a line from an HTTP request or response message containing a header name and value pair.
        /// </summary>
        /// <param name="line">A string containing the data to be parsed.</param>
        /// <param name="headers">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection to which the parsed header will be added.</param>
        /// <param name="contentHeaders">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection for the message content, to which the parsed header will be added.</param>
        private void ParseHeader(string line, System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders)
        {
            //Header format is
            //name: value
            int    headerKeySeparatorIndex = line.IndexOf(":", StringComparison.OrdinalIgnoreCase);
            string headerName  = line.Substring(0, headerKeySeparatorIndex).Trim();
            string headerValue = line.Substring(headerKeySeparatorIndex + 1).Trim();

            //Not sure how to determine where request headers and and content headers begin,
            //at least not without a known set of headers (general headers first the content headers)
            //which seems like a bad way of doing it. So we'll assume if it's a known content header put it there
            //else use request headers.

            IList <string> values = ParseValues(headerValue);

            System.Net.Http.Headers.HttpHeaders headersToAddTo = IsContentHeader(headerName) ? contentHeaders : headers;

            if (values.Count > 1)
            {
                headersToAddTo.TryAddWithoutValidation(headerName, values);
            }
            else
            {
                headersToAddTo.TryAddWithoutValidation(headerName, values.First());
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new <see cref="HttpHeaders"/> with values from a <see cref="System.Net.Http.Headers.HttpHeaders"/> and <see cref="HttpContentHeaders"/>.
        /// </summary>
        /// <param name="httpHeaders">The <see cref="System.Net.Http.Headers.HttpHeaders"/>.</param>
        /// <param name="httpContentHeaders">The <see cref="HttpContentHeaders"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="httpHeaders"/></exception>
        public HttpHeaders(System.Net.Http.Headers.HttpHeaders httpHeaders, HttpContentHeaders httpContentHeaders)
        {
            if (httpHeaders == null)
            {
                throw new ArgumentNullException(nameof(httpHeaders));
            }

            foreach (var header in httpHeaders)
            {
                foreach (var headerValue in header.Value)
                {
                    Add(header.Key, headerValue);
                }
            }

            if (httpContentHeaders != null)
            {
                foreach (var header in httpContentHeaders)
                {
                    foreach (var headerValue in header.Value)
                    {
                        Add(header.Key, headerValue);
                    }
                }
            }
        }
Esempio n. 5
0
 public static void CopyHeadersTo(this IHeaderDictionary headerDictionary, HttpHeaders httpHeaders,
                                  HttpContentHeaders contentHeaders)
 {
     foreach (var requestHeader in headerDictionary)
     {
         if (HeadersHelper.IsContentHeader(requestHeader.Key))
         {
             contentHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
         else
         {
             httpHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
     }
 }
Esempio n. 6
0
 /// <inheritdoc cref="TixFactory.Http.HttpHeaders(System.Net.Http.Headers.HttpHeaders, HttpContentHeaders)"/>
 public HttpRequestHeaders(System.Net.Http.Headers.HttpHeaders httpHeaders, HttpContentHeaders httpContentHeaders)
     : base(httpHeaders, httpContentHeaders)
 {
 }
        protected virtual HttpContent Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (data.Length == 0)
            {
                throw new ArgumentException("data cannot be an empty string.", "data");
            }

            if (!LineTerminators.Any(data.Contains))
            {
                throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", "data");
            }

            HttpContent retVal = null;

            try {
                System.IO.MemoryStream contentStream = new System.IO.MemoryStream();
                try {
                    retVal = new StreamContent(contentStream);

                    string[] lines = data.Split(LineTerminators, StringSplitOptions.None);

                    //First line is the 'request' line containing http protocol details like method, uri, http version etc.
                    ParseStatusLine(lines[0], message);

                    int lineIndex = ParseHeaders(headers, retVal.Headers, lines);

                    if (lineIndex < lines.Length - 1)
                    {
                        //Read rest of any remaining data as content.
                        if (lineIndex < lines.Length - 1)
                        {
                            //This is inefficient in multiple ways, but not sure of a good way of correcting. Revisit.
                            byte[] body = System.Text.UTF8Encoding.UTF8.GetBytes(string.Join(null, lines, lineIndex, lines.Length - lineIndex));
                            contentStream.Write(body, 0, body.Length);
                            contentStream.Seek(0, System.IO.SeekOrigin.Begin);
                        }
                    }
                } catch {
                    if (contentStream != null)
                    {
                        contentStream.Dispose();
                    }

                    throw;
                }
            } catch {
                if (retVal != null)
                {
                    retVal.Dispose();
                }

                throw;
            }

            return(retVal);
        }
Esempio n. 8
0
        internal static DateTimeOffset?GetDateTimeOffsetValue(HeaderDescriptor descriptor, HttpHeaders store, DateTimeOffset?defaultValue = null)
        {
            Debug.Assert(store != null);

            object storedValue = store.GetParsedValues(descriptor);

            if (storedValue != null)
            {
                return((DateTimeOffset)storedValue);
            }
            else if (defaultValue != null && store.Contains(descriptor))
            {
                return(defaultValue);
            }

            return(null);
        }
Esempio n. 9
0
        internal HttpGeneralHeaders(HttpHeaders parent)
        {
            Debug.Assert(parent != null);

            _parent = parent;
        }
Esempio n. 10
0
        internal static DateTimeOffset?GetDateTimeOffsetValue(HeaderDescriptor descriptor, HttpHeaders store)
        {
            Debug.Assert(store != null);

            object storedValue = store.GetParsedValues(descriptor);

            if (storedValue != null)
            {
                return((DateTimeOffset)storedValue);
            }
            return(null);
        }