예제 #1
0
        /// <summary>
        /// Read a new Response from the given Protocol
        /// </summary>
        /// <param name="p">the Protocol object</param>
        /// <exception cref="IOException">for I/O errors</exception>
        /// <exception cref="ProtocolException">for Protocol failures</exception>
        public Response(Protocol p)
        {
            // read one response into 'buffer'
            ByteArray ba       = p.GetResponseBuffer();
            ByteArray response = p.GetReader().ReadResponse(ba);

            buffer = response.Bytes;
            size   = buffer.Length - 2; // Skip the terminating CRLF
            utf8   = p.SupportsUtf8();
            parse();
        }
예제 #2
0
        /// <exception cref="IOException"></exception>
        /// <exception cref="ProtocolException"></exception>
        private void nstring(byte[] bytes, Protocol p, bool doQuote)
        {
            StreamWriter writer = getStreamWriter(p);
            int          len    = bytes.Length;

            // If length is greater than 1024 bytes, send as literal
            if (len > 1024)
            {
                literal(bytes, p);
                return;
            }

            // if 0 length, send as quoted-string
            bool quote  = len == 0 ? true : doQuote;
            bool escape = false;
            bool utf8   = p.SupportsUtf8();

            byte b;

            for (int i = 0; i < len; i++)
            {
                b = bytes[i];

                if (b == '\0' || b == '\r' || b == '\n' ||
                    (!utf8 && ((b & 0xff) > 0177)))
                {
                    // NUL, CR or LF means the bytes need to be sent as literals
                    literal(bytes, p);
                    return;
                }
                if (b == '*' || b == '%' || b == '(' || b == ')' || b == '{' ||
                    b == '"' || b == '\\' ||
                    ((b & 0xff) <= ' ') || ((b & 0xff) > 0177))
                {
                    quote = true;
                    if (b == '"' || b == '\\') // need to escape these characters
                    {
                        escape = true;
                    }
                }
            }

            /*
             * Make sure the (case-independent) string "NIL" is always quoted,
             * so as not to be confused with a real NIL (handled above in nstring).
             * This is more than is necessary, but it's rare to begin with and
             * this makes it safer than doing the test in nstring above in case
             * some code calls writeString when it should call writeNString.
             */
            if (!quote && bytes.Length == 3 &&
                (bytes[0] == 'N' || bytes[0] == 'n') &&
                (bytes[1] == 'I' || bytes[1] == 'i') &&
                (bytes[2] == 'L' || bytes[2] == 'l'))
            {
                quote = true;
            }

            if (quote) // start quote
            {
                writer.Write('"');
            }

            if (escape)
            {
                // already quoted
                for (int i = 0; i < len; i++)
                {
                    b = bytes[i];
                    if (b == '"' || b == '\\')
                    {
                        writer.Write('\\');
                    }
                    writer.Write(b);
                }
            }
            else
            {
                writer.Write(bytes);
            }

            if (quote) // end quote
            {
                writer.Write('"');
            }
        }