Esempio n. 1
0
        /// <summary>
        /// Returns a list of
        /// <see cref="Apache.Http.NameValuePair">NameValuePairs</see>
        /// as parsed from the given string using the given character
        /// encoding.
        /// </summary>
        /// <param name="s">text to parse.</param>
        /// <param name="charset">Encoding to use when decoding the parameters.</param>
        /// <param name="parameterSeparator">
        /// The characters used to separate parameters, by convention,
        /// <code>'&'</code>
        /// and
        /// <code>';'</code>
        /// .
        /// </param>
        /// <returns>
        /// a list of
        /// <see cref="Apache.Http.NameValuePair">Apache.Http.NameValuePair</see>
        /// as built from the URI's query portion.
        /// </returns>
        /// <since>4.3</since>
        public static IList <NameValuePair> Parse(string s, Encoding charset, params char[]
                                                  parameterSeparator)
        {
            if (s == null)
            {
                return(Sharpen.Collections.EmptyList());
            }
            BasicHeaderValueParser parser = BasicHeaderValueParser.Instance;
            CharArrayBuffer        buffer = new CharArrayBuffer(s.Length);

            buffer.Append(s);
            ParserCursor          cursor = new ParserCursor(0, buffer.Length());
            IList <NameValuePair> list   = new AList <NameValuePair>();

            while (!cursor.AtEnd())
            {
                NameValuePair nvp = parser.ParseNameValuePair(buffer, cursor, parameterSeparator);
                if (nvp.GetName().Length > 0)
                {
                    list.AddItem(new BasicNameValuePair(DecodeFormFields(nvp.GetName(), charset), DecodeFormFields
                                                            (nvp.GetValue(), charset)));
                }
            }
            return(list);
        }
Esempio n. 2
0
        /// <summary>Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.</summary>
        /// <remarks>
        /// Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
        /// <p>Syntax of the Set-Cookie HTTP Response Header:</p>
        /// <p>This is the format a CGI script would use to add to
        /// the HTTP headers a new piece of data which is to be stored by
        /// the client for later retrieval.</p>
        /// <PRE>
        /// Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
        /// </PRE>
        /// <p>Please note that the Netscape draft specification does not fully conform to the HTTP
        /// header format. Comma character if present in <code>Set-Cookie</code> will not be treated
        /// as a header element separator</p>
        /// </remarks>
        /// <seealso><a href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
        /// *  The Cookie Spec.</a></seealso>
        /// <param name="header">the <tt>Set-Cookie</tt> received from the server</param>
        /// <returns>an array of <tt>Cookie</tt>s parsed from the Set-Cookie value</returns>
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException">if an exception occurs during parsing
        ///     </exception>
        public override IList <Apache.Http.Cookie.Cookie> Parse(Header header, CookieOrigin
                                                                origin)
        {
            Args.NotNull(header, "Header");
            Args.NotNull(origin, "Cookie origin");
            if (!Sharpen.Runtime.EqualsIgnoreCase(header.GetName(), SM.SetCookie))
            {
                throw new MalformedCookieException("Unrecognized cookie header '" + header.ToString
                                                       () + "'");
            }
            NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.Default;
            CharArrayBuffer           buffer;
            ParserCursor cursor;

            if (header is FormattedHeader)
            {
                buffer = ((FormattedHeader)header).GetBuffer();
                cursor = new ParserCursor(((FormattedHeader)header).GetValuePos(), buffer.Length(
                                              ));
            }
            else
            {
                string s = header.GetValue();
                if (s == null)
                {
                    throw new MalformedCookieException("Header value is null");
                }
                buffer = new CharArrayBuffer(s.Length);
                buffer.Append(s);
                cursor = new ParserCursor(0, buffer.Length());
            }
            return(Parse(new HeaderElement[] { parser.ParseHeader(buffer, cursor) }, origin));
        }
Esempio n. 3
0
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
        public virtual IList <Apache.Http.Cookie.Cookie> Parse(Header header, CookieOrigin
                                                               origin)
        {
            Args.NotNull(header, "Header");
            Args.NotNull(origin, "Cookie origin");
            HeaderElement[] helems    = header.GetElements();
            bool            versioned = false;
            bool            netscape  = false;

            foreach (HeaderElement helem in helems)
            {
                if (helem.GetParameterByName("version") != null)
                {
                    versioned = true;
                }
                if (helem.GetParameterByName("expires") != null)
                {
                    netscape = true;
                }
            }
            if (netscape || !versioned)
            {
                // Need to parse the header again, because Netscape style cookies do not correctly
                // support multiple header elements (comma cannot be treated as an element separator)
                NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.Default;
                CharArrayBuffer           buffer;
                ParserCursor cursor;
                if (header is FormattedHeader)
                {
                    buffer = ((FormattedHeader)header).GetBuffer();
                    cursor = new ParserCursor(((FormattedHeader)header).GetValuePos(), buffer.Length(
                                                  ));
                }
                else
                {
                    string s = header.GetValue();
                    if (s == null)
                    {
                        throw new MalformedCookieException("Header value is null");
                    }
                    buffer = new CharArrayBuffer(s.Length);
                    buffer.Append(s);
                    cursor = new ParserCursor(0, buffer.Length());
                }
                helems = new HeaderElement[] { parser.ParseHeader(buffer, cursor) };
                return(GetCompat().Parse(helems, origin));
            }
            else
            {
                if (SM.SetCookie2.Equals(header.GetName()))
                {
                    return(GetStrict().Parse(helems, origin));
                }
                else
                {
                    return(GetObsoleteStrict().Parse(helems, origin));
                }
            }
        }
Esempio n. 4
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        protected internal override IHttpRequest ParseHead(SessionInputBuffer sessionBuffer
                                                           )
        {
            this.lineBuf.Clear();
            int i = sessionBuffer.ReadLine(this.lineBuf);

            if (i == -1)
            {
                throw new ConnectionClosedException("Client closed connection");
            }
            ParserCursor cursor      = new ParserCursor(0, this.lineBuf.Length());
            RequestLine  requestline = this.lineParser.ParseRequestLine(this.lineBuf, cursor);

            return(this.requestFactory.NewHttpRequest(requestline));
        }
Esempio n. 5
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        protected internal override HttpResponse ParseHead(SessionInputBuffer sessionBuffer
                                                           )
        {
            this.lineBuf.Clear();
            int i = sessionBuffer.ReadLine(this.lineBuf);

            if (i == -1)
            {
                throw new NoHttpResponseException("The target server failed to respond");
            }
            //create the status line from the status string
            ParserCursor cursor     = new ParserCursor(0, this.lineBuf.Length());
            StatusLine   statusline = lineParser.ParseStatusLine(this.lineBuf, cursor);

            return(this.responseFactory.NewHttpResponse(statusline, null));
        }
Esempio n. 6
0
        /// <exception cref="Apache.Http.ParseException"></exception>
        public virtual HeaderElement ParseHeader(CharArrayBuffer buffer, ParserCursor cursor
                                                 )
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            NameValuePair         nvp     = ParseNameValuePair(buffer, cursor);
            IList <NameValuePair> @params = new AList <NameValuePair>();

            while (!cursor.AtEnd())
            {
                NameValuePair param = ParseNameValuePair(buffer, cursor);
                @params.AddItem(param);
            }
            return(new BasicHeaderElement(nvp.GetName(), nvp.GetValue(), Sharpen.Collections.ToArray
                                              (@params, new NameValuePair[@params.Count])));
        }
Esempio n. 7
0
        /// <exception cref="Apache.Http.Auth.MalformedChallengeException"></exception>
        protected internal override void ParseChallenge(CharArrayBuffer buffer, int pos,
                                                        int len)
        {
            HeaderValueParser parser = BasicHeaderValueParser.Instance;
            ParserCursor      cursor = new ParserCursor(pos, buffer.Length());

            HeaderElement[] elements = parser.ParseElements(buffer, cursor);
            if (elements.Length == 0)
            {
                throw new MalformedChallengeException("Authentication challenge is empty");
            }
            [email protected]();
            foreach (HeaderElement element in elements)
            {
                [email protected](element.GetName(), element.GetValue());
            }
        }
Esempio n. 8
0
        /// <summary>Parses textual representation of <code>Content-Type</code> value.</summary>
        /// <remarks>Parses textual representation of <code>Content-Type</code> value.</remarks>
        /// <param name="s">text</param>
        /// <returns>content type</returns>
        /// <exception cref="Org.Apache.Http.ParseException">
        /// if the given text does not represent a valid
        /// <code>Content-Type</code> value.
        /// </exception>
        /// <exception cref="Sharpen.UnsupportedCharsetException">
        /// Thrown when the named charset is not available in
        /// this instance of the Java virtual machine
        /// </exception>
        public static Org.Apache.Http.Entity.ContentType Parse(string s)
        {
            Args.NotNull(s, "Content type");
            CharArrayBuffer buf = new CharArrayBuffer(s.Length);

            buf.Append(s);
            ParserCursor cursor = new ParserCursor(0, s.Length);

            HeaderElement[] elements = BasicHeaderValueParser.Instance.ParseElements(buf, cursor
                                                                                     );
            if (elements.Length > 0)
            {
                return(Create(elements[0]));
            }
            else
            {
                throw new ParseException("Invalid content type: " + s);
            }
        }
Esempio n. 9
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        protected override HttpResponse ParseHead(SessionInputBuffer sessionBuffer)
        {
            //read out the HTTP status string
            int          count  = 0;
            ParserCursor cursor = null;

            do
            {
                // clear the buffer
                this.lineBuf.Clear();
                int i = sessionBuffer.ReadLine(this.lineBuf);
                if (i == -1 && count == 0)
                {
                    // The server just dropped connection on us
                    throw new NoHttpResponseException("The target server failed to respond");
                }
                cursor = new ParserCursor(0, this.lineBuf.Length());
                if (lineParser.HasProtocolVersion(this.lineBuf, cursor))
                {
                    // Got one
                    break;
                }
                else
                {
                    if (i == -1 || Reject(this.lineBuf, count))
                    {
                        // Giving up
                        throw new ProtocolException("The server failed to respond with a " + "valid HTTP response"
                                                    );
                    }
                }
                if (this.log.IsDebugEnabled())
                {
                    this.log.Debug("Garbage in response: " + this.lineBuf.ToString());
                }
                count++;
            }while (true);
            //create the status line from the status string
            StatusLine statusline = lineParser.ParseStatusLine(this.lineBuf, cursor);

            return(this.responseFactory.NewHttpResponse(statusline, null));
        }
Esempio n. 10
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));
        }
Esempio n. 11
0
File: Parser.cs Progetto: Stu042/Cy
 public Parser(ParserCursor parserCursor, IErrorDisplay display)
 {
     _parserCursor = parserCursor;
     _display      = display;
 }