Beispiel #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);
        }
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
        protected internal virtual IList <Apache.Http.Cookie.Cookie> Parse(HeaderElement[]
                                                                           elems, CookieOrigin origin)
        {
            IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(elems
                                                                                              .Length);

            foreach (HeaderElement headerelement in elems)
            {
                string name  = headerelement.GetName();
                string value = headerelement.GetValue();
                if (name == null || name.Length == 0)
                {
                    throw new MalformedCookieException("Cookie name may not be empty");
                }
                BasicClientCookie cookie = new BasicClientCookie(name, value);
                cookie.SetPath(GetDefaultPath(origin));
                cookie.SetDomain(GetDefaultDomain(origin));
                // cycle through the parameters
                NameValuePair[] attribs = headerelement.GetParameters();
                for (int j = attribs.Length - 1; j >= 0; j--)
                {
                    NameValuePair attrib = attribs[j];
                    string        s      = attrib.GetName().ToLower(Sharpen.Extensions.GetEnglishCulture());
                    cookie.SetAttribute(s, attrib.GetValue());
                    CookieAttributeHandler handler = FindAttribHandler(s);
                    if (handler != null)
                    {
                        handler.Parse(cookie, attrib.GetValue());
                    }
                }
                cookies.AddItem(cookie);
            }
            return(cookies);
        }
 /// <summary>Sets parameter of URI query overriding existing value if set.</summary>
 /// <remarks>
 /// Sets parameter of URI query overriding existing value if set. The parameter name and value
 /// are expected to be unescaped and may contain non ASCII characters.
 /// <p/>
 /// Please note query parameters and custom query component are mutually exclusive. This method
 /// will remove custom query if present.
 /// </remarks>
 public virtual Apache.Http.Client.Utils.URIBuilder SetParameter(string param, string
                                                                 value)
 {
     if (this.queryParams == null)
     {
         this.queryParams = new AList <NameValuePair>();
     }
     if (!this.queryParams.IsEmpty())
     {
         for (IEnumerator <NameValuePair> it = this.queryParams.GetEnumerator(); it.HasNext
                  ();)
         {
             NameValuePair nvp = it.Next();
             if (nvp.GetName().Equals(param))
             {
                 it.Remove();
             }
         }
     }
     this.queryParams.AddItem(new BasicNameValuePair(param, value));
     this.encodedQuery = null;
     this.encodedSchemeSpecificPart = null;
     this.query = null;
     return(this);
 }
Beispiel #4
0
        // non-javadoc, see interface HeaderValueFormatter
        public virtual CharArrayBuffer FormatNameValuePair(CharArrayBuffer charBuffer, NameValuePair
                                                           nvp, bool quote)
        {
            Args.NotNull(nvp, "Name / value pair");
            int             len    = EstimateNameValuePairLen(nvp);
            CharArrayBuffer buffer = charBuffer;

            if (buffer == null)
            {
                buffer = new CharArrayBuffer(len);
            }
            else
            {
                buffer.EnsureCapacity(len);
            }
            buffer.Append(nvp.GetName());
            string value = nvp.GetValue();

            if (value != null)
            {
                buffer.Append('=');
                DoFormatValue(buffer, value, quote);
            }
            return(buffer);
        }
Beispiel #5
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])));
        }
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
        private IList <Apache.Http.Cookie.Cookie> CreateCookies(HeaderElement[] elems, CookieOrigin
                                                                origin)
        {
            IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(elems
                                                                                              .Length);

            foreach (HeaderElement headerelement in elems)
            {
                string name  = headerelement.GetName();
                string value = headerelement.GetValue();
                if (name == null || name.Length == 0)
                {
                    throw new MalformedCookieException("Cookie name may not be empty");
                }
                BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
                cookie.SetPath(GetDefaultPath(origin));
                cookie.SetDomain(GetDefaultDomain(origin));
                cookie.SetPorts(new int[] { origin.GetPort() });
                // cycle through the parameters
                NameValuePair[] attribs = headerelement.GetParameters();
                // Eliminate duplicate attributes. The first occurrence takes precedence
                // See RFC2965: 3.2  Origin Server Role
                IDictionary <string, NameValuePair> attribmap = new Dictionary <string, NameValuePair
                                                                                >(attribs.Length);
                for (int j = attribs.Length - 1; j >= 0; j--)
                {
                    NameValuePair param = attribs[j];
                    attribmap.Put(param.GetName().ToLower(Sharpen.Extensions.GetEnglishCulture()), param
                                  );
                }
                foreach (KeyValuePair <string, NameValuePair> entry in attribmap.EntrySet())
                {
                    NameValuePair attrib = entry.Value;
                    string        s      = attrib.GetName().ToLower(Sharpen.Extensions.GetEnglishCulture());
                    cookie.SetAttribute(s, attrib.GetValue());
                    CookieAttributeHandler handler = FindAttribHandler(s);
                    if (handler != null)
                    {
                        handler.Parse(cookie, attrib.GetValue());
                    }
                }
                cookies.AddItem(cookie);
            }
            return(cookies);
        }
Beispiel #7
0
        /// <summary>Estimates the length of a formatted name-value pair.</summary>
        /// <remarks>Estimates the length of a formatted name-value pair.</remarks>
        /// <param name="nvp">the name-value pair to format, or <code>null</code></param>
        /// <returns>a length estimate, in number of characters</returns>
        protected internal virtual int EstimateNameValuePairLen(NameValuePair nvp)
        {
            if (nvp == null)
            {
                return(0);
            }
            int result = nvp.GetName().Length;
            // name
            string value = nvp.GetValue();

            if (value != null)
            {
                // assume quotes, but no escaped characters
                result += 3 + value.Length;
            }
            // ="value"
            return(result);
        }
Beispiel #8
0
        // non-javadoc, see interface HeaderValueParser
        public virtual HeaderElement ParseHeaderElement(CharArrayBuffer buffer, ParserCursor
                                                        cursor)
        {
            Args.NotNull(buffer, "Char array buffer");
            Args.NotNull(cursor, "Parser cursor");
            NameValuePair nvp = ParseNameValuePair(buffer, cursor);

            NameValuePair[] @params = null;
            if (!cursor.AtEnd())
            {
                char ch = buffer.CharAt(cursor.GetPos() - 1);
                if (ch != ElemDelimiter)
                {
                    @params = ParseParameters(buffer, cursor);
                }
            }
            return(CreateHeaderElement(nvp.GetName(), nvp.GetValue(), @params));
        }