Skip to content

Messages from Server to Client can get lost. #25

@hempe

Description

@hempe

Not sure if this Project is dead or just Feature complete.

Anyway there seems to be a bug in this method.
If the server immediately sends a message to the client after the handshake this method reads to much and "throws away" what it read, which seema to cause lost packages on the receiving end.

public static async Task<string> ReadHttpHeaderAsync(Stream stream, CancellationToken token)
        {
            int length = 1024*16; // 16KB buffer more than enough for http header
            byte[] buffer = new byte[length];
            int offset = 0;
            int bytesRead = 0;

            do
            {
                if (offset >= length)
                {
                    throw new EntityTooLargeException("Http header message too large to fit in buffer (16KB)");
                }

                bytesRead = await stream.ReadAsync(buffer, offset, length - offset, token);
                offset += bytesRead;
                string header = Encoding.UTF8.GetString(buffer, 0, offset);

                // as per http specification, all headers should end this this
                if (header.Contains("\r\n\r\n"))
                {
                    return header;
                }

            } while (bytesRead > 0);

            return string.Empty;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions