sslstream and decoding BR

23 views Asked by At

An error occurs in the process of receiving data through sslstream and decoding BR.

Here's the full source. If you do it In the sentence below, the first data is converted normally, but an error occurs in the second data. Why is that so?

>> byte[] bbb = Brotli.DecompressBytes(body); // an error occurs in the second data

// --- full source
 TcpClient client = new TcpClient("jsonplaceholder.typicode.com", 443);
            string http =
$@"GET /comments HTTP/1.1
Host: jsonplaceholder.typicode.com
Sec-Ch-Ua: ""Chromium"";v=""117"", ""Not;A=Brand"";v=""8""
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: ""Windows""
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Connection: close

";
            byte[] tttt = Encoding.UTF8.GetBytes("[\r\n{");
            byte[] recvBuff = new byte[8192];
            byte[] databuf = new byte[8192000];
            string headersss = "";
            List<byte> result = new List<byte>();
            StringBuilder sb = new StringBuilder();


            using (SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null)) {
                sslStream.AuthenticateAsClient("jsonplaceholder.typicode.com", null, System.Security.Authentication.SslProtocols.Tls12, false);
                sslStream.Write(Encoding.UTF8.GetBytes(http));

                bool isHead = true;
                bool isBody = false;
                byte[] body = null;
                byte[] temp = new byte[1024];
                List<byte> bodyLen = new List<byte>();
                int bodyIndex = 0;
                string repProtocol = "";
                int repStatus = 0;
                bool isEnd = false;
                int line = 0;
                bool lr = false;
                int bufferSize = 4096;
                bool isKey = true;
                string keyStr = "";
                string valueStr = "";

                while (!isEnd) {
                    if (isBody) {
                        if (body.Length - bodyIndex < bufferSize) {
                            bufferSize = body.Length - bodyIndex;
                        }
                        int size = sslStream.Read(body, bodyIndex, bufferSize);

                        if (size > 0) {
                            bodyIndex += size;
                            if (bodyIndex >= body.Length) {
                                isBody = false;
                                bodyIndex = 0;
                                bufferSize = 4096;

                                byte[] bbb = Brotli.DecompressBytes(body); // an error occurs in the second data
                                string reb = Encoding.UTF8.GetString(bbb);

                                body = null;
                                bodyLen.Clear();

                                continue;
                                //isEnd = true;
                            }
                        }
                        else {
                            isEnd = true;
                        }
                    }
                    else if (isHead) {
                        int bs = sslStream.ReadByte();
                        if (bs > 0) {
                            switch (bs) {
                                case 13:
                                    lr = true;
                                    break;
                                case 10: 
                                    if (lr) {
                                        lr = false; 
                                        line++;
                                        if (line == 2) {
                                            isHead = false;
                                        }
                                        else {
                                            if (string.IsNullOrEmpty(repProtocol)) {
                                                string[] headStatuses = keyStr.Split(' ');
                                                if (headStatuses.Length < 2) {
                                                    throw new Exception("头部信息无效");
                                                }
                                                repProtocol = headStatuses[0];
                                                repStatus = int.Parse(headStatuses[1]);
                                            }
                                            else {
                                                headersss += $"{keyStr.Trim()} / {valueStr.Trim()}\n";
                                                keyStr = "";
                                                valueStr = "";
                                            }
                                        }
                                    }
                                    break;

                                case 91:  
                                    if (isKey) {
                                        isKey = false;
                                    }
                                    else {
                                        valueStr += ':';
                                    }
                                    break;

                                default:
                                    char chr = (char)bs;
                                    if (isKey) {
                                        keyStr += chr;
                                    }
                                    else {
                                        valueStr += chr;
                                    }
                                    line = 0;
                                    lr = false;
                                    break;
                            }
                        }
                        else {
                            isEnd = true;
                        }
                    }
                    else {
                        int bs = sslStream.ReadByte();
                        if (bs > 0) {
                            switch (bs) {
                                case 13:
                                    lr = true;
                                    break;

                                case 10: 
                                    if (lr && bodyLen.Count > 0) {
                                        string lenStr = System.Text.Encoding.UTF8.GetString(bodyLen.ToArray());
                                        int len = Convert.ToInt32(lenStr, 16);
                                        headersss += $" [+]Find '\\r',rnCount:{len}";
                                        body = new byte[len];
                                        bodyIndex = 0;
                                        isBody = true;
                                    }
                                    break;

                                default:
                                    lr = false;
                                    bodyLen.Add((byte)bs);
                                    break;
                            }
                        }
                        else {
                            isEnd = true;
                        }
                    }
                }
            }
0

There are 0 answers