handling a malformed request

Feb 18, 2012 at 6:45 PM

I'm using the custom protocol quick start.  In the CommandReader derived class, it parses to find the data length.  If no length is available, its malformed. In the quickstart, it throws an exception and essentially crashes the server.  I've taken care of the exception here, but i will continue to cycle through the command reader multiple times until the malformed requested is flushed. I'd like to just, recognize it as a malformed as just stop any further processing.  I added a command JERR which just logs the error and this takes care of it although, like I said, there will be several passes through here as it processes the malformed message.  I tried setting length and lengthleft = 0 in addition to changing the command name, but that had the side effect of adding errors to subsequent messages (even if they were good).

        public override BinaryCommandInfo FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left)
        {
            left = 0;

            int leftLength = 10 - this.BufferSegments.Count;

            if (length < leftLength)
            {
                AddArraySegment(readBuffer, offset, length, isReusableBuffer);
                NextCommandReader = this;
                return null;
            }

            AddArraySegment(readBuffer, offset, leftLength, isReusableBuffer);

            string commandName = BufferSegments.Decode(Encoding.ASCII, 0, 4);

			int commandDataLength = 0;
			string lengthAsString = BufferSegments.Decode(Encoding.ASCII, 5, 4).TrimStart('0');
			if (!int.TryParse(lengthAsString, out commandDataLength))
			{
				commandName = "JERR";
			}

Thanks for all the help.  

Coordinator
Feb 19, 2012 at 5:31 AM

JERR may be not required.

You can disconnect the connection and log the exception by the appSession.

Feb 19, 2012 at 4:51 PM

I don't want to disconnect the session. If I think of it as a malformed request (JSON-RPC), all I want to do is disregard the request and keep the connection open.

Thoughts?

Coordinator
Feb 20, 2012 at 12:45 AM

Just clear the buffer, and then return null.

Feb 20, 2012 at 1:33 AM

When you say "clear the buffer" do you mean write nulls to the incoming readbuffer?

    public override BinaryCommandInfo FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left)

Thanks. Almost there.

Coordinator
Feb 20, 2012 at 1:35 AM

Invoke "ClearBufferSegments();" in this method.