/// <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> /// 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); }
// 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); }
/// <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); }
/// <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]))); }
/// <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); }
// 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)); }
/// <summary>Obtains character set of the entity, if known.</summary> /// <remarks>Obtains character set of the entity, if known.</remarks> /// <param name="entity">must not be null</param> /// <returns>the character set, or null if not found</returns> /// <exception cref="Org.Apache.Http.ParseException">if header elements cannot be parsed /// </exception> /// <exception cref="System.ArgumentException">if entity is null</exception> [System.ObsoleteAttribute(@"(4.1.3) use Org.Apache.Http.Entity.ContentType.GetOrDefault(Org.Apache.Http.HttpEntity)")] public static string GetContentCharSet(HttpEntity entity) { Args.NotNull(entity, "Entity"); string charset = null; if (entity.GetContentType() != null) { HeaderElement[] values = entity.GetContentType().GetElements(); if (values.Length > 0) { NameValuePair param = values[0].GetParameterByName("charset"); if (param != null) { charset = param.GetValue(); } } } return(charset); }