Ejemplo n.º 1
0
        public void HttpReadLine_Breaked()
        {
            var l7Conversation  = this.ReassembleSingleL7Conversation <TcpConversationTracker>(TestPcapFile.HttpTest0);
            var stream          = new PduDataStream(l7Conversation, PduDataProviderType.Breaked);
            var pduStreamReader = new PduStreamReader(stream, Encoding.ASCII);

            Assert.False(pduStreamReader.EndOfStream);
            Assert.Equal("GET / HTTP/1.1", pduStreamReader.ReadLine());
            Assert.Equal("Host: icanhazip.com", pduStreamReader.ReadLine());
            Assert.Equal("User-Agent: curl/7.54.0", pduStreamReader.ReadLine());
            Assert.Equal("Accept: */*", pduStreamReader.ReadLine());
            Assert.Equal("", pduStreamReader.ReadLine());
            Assert.True(pduStreamReader.EndOfStream);

            Assert.True(pduStreamReader.NewMessage());
            Assert.False(pduStreamReader.EndOfStream);

            var s = pduStreamReader.ReadToEnd();

            Assert.Equal(597, s.Length);
            Assert.StartsWith("HTTP/1.1 200 OK", s);
            Assert.EndsWith("147.229.14.29\n", s);

            Assert.True(pduStreamReader.EndOfStream);
        }
Ejemplo n.º 2
0
        private Byte[] ReadChunk(PduStreamReader reader)
        {
            var chunkSizeLine = reader.ReadLine()?.Trim();

            if (chunkSizeLine == null)
            {
                return(null);
            }
            var chunkSize = Convert.ToInt32(chunkSizeLine, 16);

            if (chunkSize == 0)
            {
                return(null);
            }

            var chunkBuffer = new Byte[chunkSize];
            var readTotal   = 0;

            do
            {
                var read = reader.Read(chunkBuffer, readTotal, chunkSize - readTotal);
                if (read == 0)
                {
                    return(null);
                }
                readTotal += read;
            } while(readTotal < chunkSize);

            if (reader.ReadLine() == null)
            {
                return(null);
            }

            return(chunkBuffer);
        }
Ejemplo n.º 3
0
        private Boolean ParseHeader(PduStreamReader reader)
        {
            var headerLine = reader.ReadLine();

            if (headerLine == null)
            {
                this.ParsingError = new HttpParsingException("Nothing to read");
                return(false);
            }

            if (Enum.TryParse(headerLine.Split(' ').FirstOrDefault(), true, out HttpRequestMethod httpRequestMethod))
            {
                // HTTP Request
                this.Header = new HttpHeaderRequest(reader, headerLine, httpRequestMethod);
            }
            else if (headerLine.StartsWith("HTTP"))
            {
                // HTTP Response
                this.Header = new HttpHeaderResponse(reader, headerLine);
            }
            else
            {
                this.ParsingError = new HttpParsingException("Not a HTTP message");
                return(false);
            }

            switch (this.Header)
            {
            case HttpHeaderRequest _:
                this.Type = HttpMessageType.Request;
                break;

            case HttpHeaderResponse _:
                this.Type = HttpMessageType.Response;
                break;

            default:
                this.Type = HttpMessageType.None;
                break;
            }

            return(true);
        }
Ejemplo n.º 4
0
        protected void ParseHeaderValues(PduStreamReader reader)
        {
            String line;

            while ((line = reader.ReadLine()) != null && line != "")
            {
                var fieldNameIndex = line.IndexOf(':');
                var fieldName      = line.Substring(0, fieldNameIndex).ToLower();
                var fieldValue     = line.Substring(fieldNameIndex + 1, line.Length - fieldName.Length - 1).Trim().ToLower();
                if (this.Fields.ContainsKey(fieldName))
                {
                    this.Fields[fieldName].Add(fieldValue);
                }
                else
                {
                    this.Fields[fieldName] = new List <String> {
                        fieldValue
                    };
                }
            }
        }