Example #1
0
 public static void SafeResponseHeadersParsed(this IHttpMessageInspector messageInspector, HttpHeadersSummary headers)
 {
     if (messageInspector != null)
     {
         messageInspector.ResponseHeadersParsed(headers);
     }
 }
Example #2
0
        private void ProcessReadyHeaders(string headers)
        {
            int contentOffsetSize = this.currentHeaderBufferSize - headers.Length - HeaderDelimiterSize;
            byte[] contentOffsetData = null;
            if (contentOffsetSize > 0)
            {
                contentOffsetData = new byte[contentOffsetSize];
                Buffer.BlockCopy(this.headerBuffer, headers.Length + HeaderDelimiterSize, contentOffsetData, 0, contentOffsetSize);
            }

            HttpHeadersSummary summary = new HttpHeadersSummary();

            string[] headerLines = headers.SplitNoEmpty(HeaderLinesSeparator);

            if (headerLines.Length < 1)
            {
                throw new InvalidOperationException("HTTP message should contain at least one status line");
            }

            summary.StatusLine = this.ProcessHeaderStatusLine(headerLines[0]);

            summary.Headers = headerLines
                .Skip(1)
                .Select(line => ProcessHeaderLine(line))
                .ToLookup(lineParts => lineParts[0], lineParts => lineParts[1]);

            string contentLengthValue = summary.Headers[ContentLengthHeader].FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(contentLengthValue))
            {
                summary.ContentLength = contentLengthValue.ToInt(errorMessage: "Content length header value invalid");
            }

            headers = string.Join(HeaderStatusLineValuesSeparator, summary.StatusLine)
                + HeaderLinesSeparator
                + string.Join(HeaderLinesSeparator, headerLines.Skip(1));

            this.ProcessedData = System.Text.Encoding.ASCII.GetBytes(headers)
                .Concat(headerDelimiter)
                .Concat(contentOffsetData ?? new byte[0])
                .ToArray();

            summary.HeaderLength = headers.Length + headerDelimiter.Length;
            summary.HeadersContent = headers;

            this.HeaderContent = headers;
            this.ContentOffset = contentOffsetSize;
            this.HeadersSummary = summary;
        }
Example #3
0
        public void InitFromSummary(HttpHeadersSummary summary)
        {
            if (summary.StatusLine.Count != 3)
            {
                throw new InvalidOperationException("First line of HTTP request should consist of 3 parts");
            }

            this.ContentLength = summary.ContentLength.GetValueOrDefault(0);
            this.Host = summary.Headers.SafeGetValue(HostHeader, errorMessage: "Host header not found in request");
            this.Method = summary.StatusLine[0];
            this.Location = summary.StatusLine[1];
            this.HttpVersion = summary.StatusLine[2];
            this.Headers = summary.Headers;
            this.HeaderLength = summary.HeaderLength;

            this.IsInitialized = true;

            // Recalculate LoadedContentLength and ExtraContentOffset
            this.IncrementContentSize(0);
        }
Example #4
0
        public void InitFromSummary(HttpHeadersSummary summary, HttpMessage httpMessage)
        {
            if (summary.StatusLine.Count < 2 || summary.StatusLine.Count > 3)
            {
                throw new InvalidOperationException("First line of HTTP request should consist of 3 parts");
            }

            this.ContentLength = summary.ContentLength.GetValueOrDefault(0);
            this.HttpVersion = summary.StatusLine[0];
            this.Status = summary.StatusLine[1];
            this.Reason = summary.StatusLine.Count == 3 ? summary.StatusLine[2] : null;
            this.Headers = summary.Headers;
            this.HeaderLength = summary.HeaderLength;

            var transferEncodingValue = this.Headers[TransferEncodingHeaderName].FirstOrDefault();
            if (transferEncodingValue != null && transferEncodingValue.ToUpperInvariant() == ChunkedTransferValue)
            {
                this.IsChunkedTransfer = true;
            }

            this.IsInitialized = true;

            // Recalculate LoadedContentLength and ExtraContentOffset
            this.AddContent(new byte[0]);
        }