Ejemplo n.º 1
0
        // non-javadoc, see interface LineParser
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        public virtual StatusLine ParseStatusLine(CharArrayBuffer buffer, ParserCursor cursor
                                                  )
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            int indexFrom = cursor.GetPos();
            int indexTo   = cursor.GetUpperBound();

            try
            {
                // handle the HTTP-Version
                ProtocolVersion ver = ParseProtocolVersion(buffer, cursor);
                // handle the Status-Code
                SkipWhitespace(buffer, cursor);
                int i     = cursor.GetPos();
                int blank = buffer.IndexOf(' ', i, indexTo);
                if (blank < 0)
                {
                    blank = indexTo;
                }
                int    statusCode;
                string s = buffer.SubstringTrimmed(i, blank);
                for (int j = 0; j < s.Length; j++)
                {
                    if (!char.IsDigit(s[j]))
                    {
                        throw new ParseException("Status line contains invalid status code: " + buffer.Substring
                                                     (indexFrom, indexTo));
                    }
                }
                try
                {
                    statusCode = System.Convert.ToInt32(s);
                }
                catch (FormatException)
                {
                    throw new ParseException("Status line contains invalid status code: " + buffer.Substring
                                                 (indexFrom, indexTo));
                }
                //handle the Reason-Phrase
                i = blank;
                string reasonPhrase;
                if (i < indexTo)
                {
                    reasonPhrase = buffer.SubstringTrimmed(i, indexTo);
                }
                else
                {
                    reasonPhrase = string.Empty;
                }
                return(CreateStatusLine(ver, statusCode, reasonPhrase));
            }
            catch (IndexOutOfRangeException)
            {
                throw new ParseException("Invalid status line: " + buffer.Substring(indexFrom, indexTo
                                                                                    ));
            }
        }
Ejemplo n.º 2
0
 /// <exception cref="Apache.Http.Auth.MalformedChallengeException"></exception>
 protected internal override void ParseChallenge(CharArrayBuffer buffer, int beginIndex
                                                 , int endIndex)
 {
     this.challenge = buffer.SubstringTrimmed(beginIndex, endIndex);
     if (this.challenge.Length == 0)
     {
         if (this.state == NTLMScheme.State.Uninitiated)
         {
             this.state = NTLMScheme.State.ChallengeReceived;
         }
         else
         {
             this.state = NTLMScheme.State.Failed;
         }
     }
     else
     {
         if (this.state.CompareTo(NTLMScheme.State.MsgType1Generated) < 0)
         {
             this.state = NTLMScheme.State.Failed;
             throw new MalformedChallengeException("Out of sequence NTLM response message");
         }
         else
         {
             if (this.state == NTLMScheme.State.MsgType1Generated)
             {
                 this.state = NTLMScheme.State.MsgType2Recevied;
             }
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>Parses a request line.</summary>
        /// <remarks>Parses a request line.</remarks>
        /// <param name="buffer">a buffer holding the line to parse</param>
        /// <returns>the parsed request line</returns>
        /// <exception cref="Org.Apache.Http.ParseException">in case of a parse error</exception>
        public virtual RequestLine ParseRequestLine(CharArrayBuffer buffer, ParserCursor
                                                    cursor)
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            int indexFrom = cursor.GetPos();
            int indexTo   = cursor.GetUpperBound();

            try
            {
                SkipWhitespace(buffer, cursor);
                int i     = cursor.GetPos();
                int blank = buffer.IndexOf(' ', i, indexTo);
                if (blank < 0)
                {
                    throw new ParseException("Invalid request line: " + buffer.Substring(indexFrom, indexTo
                                                                                         ));
                }
                string method = buffer.SubstringTrimmed(i, blank);
                cursor.UpdatePos(blank);
                SkipWhitespace(buffer, cursor);
                i     = cursor.GetPos();
                blank = buffer.IndexOf(' ', i, indexTo);
                if (blank < 0)
                {
                    throw new ParseException("Invalid request line: " + buffer.Substring(indexFrom, indexTo
                                                                                         ));
                }
                string uri = buffer.SubstringTrimmed(i, blank);
                cursor.UpdatePos(blank);
                ProtocolVersion ver = ParseProtocolVersion(buffer, cursor);
                SkipWhitespace(buffer, cursor);
                if (!cursor.AtEnd())
                {
                    throw new ParseException("Invalid request line: " + buffer.Substring(indexFrom, indexTo
                                                                                         ));
                }
                return(CreateRequestLine(method, uri, ver));
            }
            catch (IndexOutOfRangeException)
            {
                throw new ParseException("Invalid request line: " + buffer.Substring(indexFrom, indexTo
                                                                                     ));
            }
        }
Ejemplo n.º 4
0
        /// <summary>Creates a new header from a buffer.</summary>
        /// <remarks>
        /// Creates a new header from a buffer.
        /// The name of the header will be parsed immediately,
        /// the value only if it is accessed.
        /// </remarks>
        /// <param name="buffer">the buffer containing the header to represent</param>
        /// <exception cref="Org.Apache.Http.ParseException">in case of a parse error</exception>
        public BufferedHeader(CharArrayBuffer buffer) : base()
        {
            Args.NotNull(buffer, "Char array buffer");
            int colon = buffer.IndexOf(':');

            if (colon == -1)
            {
                throw new ParseException("Invalid header: " + buffer.ToString());
            }
            string s = buffer.SubstringTrimmed(0, colon);

            if (s.Length == 0)
            {
                throw new ParseException("Invalid header: " + buffer.ToString());
            }
            this.buffer   = buffer;
            this.name     = s;
            this.valuePos = colon + 1;
        }
Ejemplo n.º 5
0
        /// <exception cref="Apache.Http.Auth.MalformedChallengeException"></exception>
        protected internal override void ParseChallenge(CharArrayBuffer buffer, int beginIndex
                                                        , int endIndex)
        {
            string challenge = buffer.SubstringTrimmed(beginIndex, endIndex);

            if (log.IsDebugEnabled())
            {
                log.Debug("Received challenge '" + challenge + "' from the auth server");
            }
            if (state == GGSSchemeBase.State.Uninitiated)
            {
                token = Base64.DecodeBase64(Sharpen.Runtime.GetBytesForString(challenge));
                state = GGSSchemeBase.State.ChallengeReceived;
            }
            else
            {
                log.Debug("Authentication already attempted");
                state = GGSSchemeBase.State.Failed;
            }
        }
Ejemplo n.º 6
0
        public virtual NameValuePair ParseNameValuePair(CharArrayBuffer buffer, ParserCursor
                                                        cursor, char[] delimiters)
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            bool terminated = false;
            int  pos        = cursor.GetPos();
            int  indexFrom  = cursor.GetPos();
            int  indexTo    = cursor.GetUpperBound();
            // Find name
            string name;

            while (pos < indexTo)
            {
                char ch = buffer.CharAt(pos);
                if (ch == '=')
                {
                    break;
                }
                if (IsOneOf(ch, delimiters))
                {
                    terminated = true;
                    break;
                }
                pos++;
            }
            if (pos == indexTo)
            {
                terminated = true;
                name       = buffer.SubstringTrimmed(indexFrom, indexTo);
            }
            else
            {
                name = buffer.SubstringTrimmed(indexFrom, pos);
                pos++;
            }
            if (terminated)
            {
                cursor.UpdatePos(pos);
                return(CreateNameValuePair(name, null));
            }
            // Find value
            string value;
            int    i1      = pos;
            bool   qouted  = false;
            bool   escaped = false;

            while (pos < indexTo)
            {
                char ch = buffer.CharAt(pos);
                if (ch == '"' && !escaped)
                {
                    qouted = !qouted;
                }
                if (!qouted && !escaped && IsOneOf(ch, delimiters))
                {
                    terminated = true;
                    break;
                }
                if (escaped)
                {
                    escaped = false;
                }
                else
                {
                    escaped = qouted && ch == '\\';
                }
                pos++;
            }
            int i2 = pos;

            // Trim leading white spaces
            while (i1 < i2 && (HTTP.IsWhitespace(buffer.CharAt(i1))))
            {
                i1++;
            }
            // Trim trailing white spaces
            while ((i2 > i1) && (HTTP.IsWhitespace(buffer.CharAt(i2 - 1))))
            {
                i2--;
            }
            // Strip away quotes if necessary
            if (((i2 - i1) >= 2) && (buffer.CharAt(i1) == '"') && (buffer.CharAt(i2 - 1) == '"'
                                                                   ))
            {
                i1++;
                i2--;
            }
            value = buffer.Substring(i1, i2);
            if (terminated)
            {
                pos++;
            }
            cursor.UpdatePos(pos);
            return(CreateNameValuePair(name, value));
        }
Ejemplo n.º 7
0
        // non-javadoc, see interface LineParser
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        public virtual ProtocolVersion ParseProtocolVersion(CharArrayBuffer buffer, ParserCursor
                                                            cursor)
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            string protoname   = this.protocol.GetProtocol();
            int    protolength = protoname.Length;
            int    indexFrom   = cursor.GetPos();
            int    indexTo     = cursor.GetUpperBound();

            SkipWhitespace(buffer, cursor);
            int i = cursor.GetPos();

            // long enough for "HTTP/1.1"?
            if (i + protolength + 4 > indexTo)
            {
                throw new ParseException("Not a valid protocol version: " + buffer.Substring(indexFrom
                                                                                             , indexTo));
            }
            // check the protocol name and slash
            bool ok = true;

            for (int j = 0; ok && (j < protolength); j++)
            {
                ok = (buffer.CharAt(i + j) == protoname[j]);
            }
            if (ok)
            {
                ok = (buffer.CharAt(i + protolength) == '/');
            }
            if (!ok)
            {
                throw new ParseException("Not a valid protocol version: " + buffer.Substring(indexFrom
                                                                                             , indexTo));
            }
            i += protolength + 1;
            int period = buffer.IndexOf('.', i, indexTo);

            if (period == -1)
            {
                throw new ParseException("Invalid protocol version number: " + buffer.Substring(indexFrom
                                                                                                , indexTo));
            }
            int major;

            try
            {
                major = System.Convert.ToInt32(buffer.SubstringTrimmed(i, period));
            }
            catch (FormatException)
            {
                throw new ParseException("Invalid protocol major version number: " + buffer.Substring
                                             (indexFrom, indexTo));
            }
            i = period + 1;
            int blank = buffer.IndexOf(' ', i, indexTo);

            if (blank == -1)
            {
                blank = indexTo;
            }
            int minor;

            try
            {
                minor = System.Convert.ToInt32(buffer.SubstringTrimmed(i, blank));
            }
            catch (FormatException)
            {
                throw new ParseException("Invalid protocol minor version number: " + buffer.Substring
                                             (indexFrom, indexTo));
            }
            cursor.UpdatePos(blank);
            return(CreateProtocolVersion(major, minor));
        }
Ejemplo n.º 8
0
        private NameValuePair ParseNameValuePair(CharArrayBuffer buffer, ParserCursor cursor
                                                 )
        {
            bool terminated = false;
            int  pos        = cursor.GetPos();
            int  indexFrom  = cursor.GetPos();
            int  indexTo    = cursor.GetUpperBound();
            // Find name
            string name = null;

            while (pos < indexTo)
            {
                char ch = buffer.CharAt(pos);
                if (ch == '=')
                {
                    break;
                }
                if (ch == ';')
                {
                    terminated = true;
                    break;
                }
                pos++;
            }
            if (pos == indexTo)
            {
                terminated = true;
                name       = buffer.SubstringTrimmed(indexFrom, indexTo);
            }
            else
            {
                name = buffer.SubstringTrimmed(indexFrom, pos);
                pos++;
            }
            if (terminated)
            {
                cursor.UpdatePos(pos);
                return(new BasicNameValuePair(name, null));
            }
            // Find value
            string value = null;
            int    i1    = pos;

            while (pos < indexTo)
            {
                char ch = buffer.CharAt(pos);
                if (ch == ';')
                {
                    terminated = true;
                    break;
                }
                pos++;
            }
            int i2 = pos;

            // Trim leading white spaces
            while (i1 < i2 && (HTTP.IsWhitespace(buffer.CharAt(i1))))
            {
                i1++;
            }
            // Trim trailing white spaces
            while ((i2 > i1) && (HTTP.IsWhitespace(buffer.CharAt(i2 - 1))))
            {
                i2--;
            }
            value = buffer.Substring(i1, i2);
            if (terminated)
            {
                pos++;
            }
            cursor.UpdatePos(pos);
            return(new BasicNameValuePair(name, value));
        }