public static async Task <HttpResponseMessage> CreateNewAsync(Stream responseStream, HttpMethod requestMethod)
        {
            // https://tools.ietf.org/html/rfc7230#section-3
            // The normal procedure for parsing an HTTP message is to read the
            // start - line into a structure, read each header field into a hash table
            // by field name until the empty line, and then use the parsed data to
            // determine if a message body is expected.If a message body has been
            // indicated, then it is read as a stream until an amount of octets
            // equal to the message body length is read or the connection is closed.

            // https://tools.ietf.org/html/rfc7230#section-3
            // All HTTP/ 1.1 messages consist of a start - line followed by a sequence
            // of octets in a format similar to the Internet Message Format
            // [RFC5322]: zero or more header fields(collectively referred to as
            // the "headers" or the "header section"), an empty line indicating the
            // end of the header section, and an optional message body.
            // HTTP - message = start - line
            //					* (header - field CRLF )
            //					CRLF
            //					[message - body]

            string startLine = await HttpMessageHelper.ReadStartLineAsync(responseStream).ConfigureAwait(false);

            var statusLine = StatusLine.Parse(startLine);
            var response   = new HttpResponseMessage(statusLine.StatusCode);

            string headers = await HttpMessageHelper.ReadHeadersAsync(responseStream).ConfigureAwait(false);

            var headerSection = await HeaderSection.CreateNewAsync(headers);

            var headerStruct = headerSection.ToHttpResponseHeaders();

            HttpMessageHelper.AssertValidHeaders(headerStruct.ResponseHeaders, headerStruct.ContentHeaders);
            byte[] contentBytes = await HttpMessageHelper.GetContentBytesAsync(responseStream, headerStruct, requestMethod, statusLine).ConfigureAwait(false);

            contentBytes     = HttpMessageHelper.HandleGzipCompression(headerStruct.ContentHeaders, contentBytes);
            response.Content = contentBytes is null ? null : new ByteArrayContent(contentBytes);

            HttpMessageHelper.CopyHeaders(headerStruct.ResponseHeaders, response.Headers);
            if (response.Content != null)
            {
                HttpMessageHelper.CopyHeaders(headerStruct.ContentHeaders, response.Content.Headers);
            }
            return(response);
        }
Ejemplo n.º 2
0
        public void Parse(ITextSegment textSegment)
        {
            Verify.Argument.IsNotNull(textSegment, nameof(textSegment));

            while (textSegment.Length > 0)
            {
                if (_line.Parse(textSegment))
                {
                    bool staged             = false;
                    bool unstaged           = false;
                    var  conflictType       = ConflictType.None;
                    var  stagedFileStatus   = FileStatus.Unknown;
                    var  unstagedFileStatus = FileStatus.Unknown;

                    var x  = _line.X;
                    var y  = _line.Y;
                    var to = _line.To;

                    if (x == '?')
                    {
                        staged             = false;
                        unstaged           = true;
                        unstagedFileStatus = FileStatus.Added;
                        ++_unstagedUntrackedCount;
                    }
                    else
                    {
                        if (x == 'C' || x == 'R')
                        {
                            var from = _line.From;
                            if (x == 'C')
                            {
                                x = 'A';
                                stagedFileStatus = FileStatus.Added;
                            }
                            else
                            {
                                if (!_stagedFiles.ContainsKey(from))
                                {
                                    var file = new TreeFileData(from, FileStatus.Removed, ConflictType.None, StagedStatus.Staged);
                                    _stagedFiles.Add(from, file);
                                    ++_stagedRemovedCount;
                                }
                                x = 'A';
                                stagedFileStatus = FileStatus.Added;
                            }
                        }
                        conflictType = GetConflictType(x, y);
                        if (conflictType != ConflictType.None)
                        {
                            staged             = false;
                            unstaged           = true;
                            unstagedFileStatus = FileStatus.Unmerged;
                            ++_unmergedCount;
                        }
                        else
                        {
                            if (x != ' ')
                            {
                                staged           = true;
                                stagedFileStatus = CharToFileStatus(x);
                                AddStagedStats(stagedFileStatus, 1);
                            }
                            if (y != ' ')
                            {
                                unstaged           = true;
                                unstagedFileStatus = CharToFileStatus(y);
                                AddUnstagedStats(unstagedFileStatus, 1);
                            }
                        }
                    }

                    if (staged)
                    {
                        var file = new TreeFileData(to, stagedFileStatus, ConflictType.None, StagedStatus.Staged);
                        if (_stagedFiles.TryGetValue(to, out var existing))
                        {
                            AddStagedStats(existing.FileStatus, -1);
                            _stagedFiles[to] = file;
                        }
                        else
                        {
                            _stagedFiles.Add(to, file);
                        }
                    }
                    if (unstaged)
                    {
                        var file = new TreeFileData(to, unstagedFileStatus, conflictType, StagedStatus.Unstaged);
                        if (_unstagedFiles.TryGetValue(to, out var existing))
                        {
                            if (existing.FileStatus == FileStatus.Removed)
                            {
                                --_unstagedRemovedCount;
                                _unstagedFiles[to] = file;
                            }
                        }
                        else
                        {
                            _unstagedFiles.Add(to, file);
                        }
                    }

                    _line.Reset();
                }
            }
        }