public virtual long GetKeepAliveDuration(HttpResponse response, HttpContext context ) { Args.NotNull(response, "HTTP response"); HeaderElementIterator it = new BasicHeaderElementIterator(response.HeaderIterator (HTTP.ConnKeepAlive)); while (it.HasNext()) { HeaderElement he = it.NextElement(); string param = he.GetName(); string value = he.GetValue(); if (value != null && Sharpen.Runtime.EqualsIgnoreCase(param, "timeout")) { try { return(long.Parse(value) * 1000); } catch (FormatException) { } } } return(-1); }
/// <summary>Estimates the length of a formatted header element.</summary> /// <remarks>Estimates the length of a formatted header element.</remarks> /// <param name="elem">the header element to format, or <code>null</code></param> /// <returns>a length estimate, in number of characters</returns> protected internal virtual int EstimateHeaderElementLen(HeaderElement elem) { if (elem == null) { return(0); } int result = elem.GetName().Length; // name string value = elem.GetValue(); if (value != null) { // assume quotes, but no escaped characters result += 3 + value.Length; } // ="value" int parcnt = elem.GetParameterCount(); if (parcnt > 0) { for (int i = 0; i < parcnt; i++) { result += 2 + EstimateNameValuePairLen(elem.GetParameter(i)); } } // ; <param> return(result); }
// non-javadoc, see interface HeaderValueFormatter public virtual CharArrayBuffer FormatHeaderElement(CharArrayBuffer charBuffer, HeaderElement elem, bool quote) { Args.NotNull(elem, "Header element"); int len = EstimateHeaderElementLen(elem); CharArrayBuffer buffer = charBuffer; if (buffer == null) { buffer = new CharArrayBuffer(len); } else { buffer.EnsureCapacity(len); } buffer.Append(elem.GetName()); string value = elem.GetValue(); if (value != null) { buffer.Append('='); DoFormatValue(buffer, value, quote); } int parcnt = elem.GetParameterCount(); if (parcnt > 0) { for (int i = 0; i < parcnt; i++) { buffer.Append("; "); FormatNameValuePair(buffer, elem.GetParameter(i), quote); } } return(buffer); }
private void ParseNextElement() { // loop while there are headers left to parse while (this.headerIt.HasNext() || this.cursor != null) { if (this.cursor == null || this.cursor.AtEnd()) { // get next header value BufferHeaderValue(); } // Anything buffered? if (this.cursor != null) { // loop while there is data in the buffer while (!this.cursor.AtEnd()) { HeaderElement e = this.parser.ParseHeaderElement(this.buffer, this.cursor); if (!(e.GetName().Length == 0 && e.GetValue() == null)) { // Found something this.currentElement = e; return; } } // if at the end of the buffer if (this.cursor.AtEnd()) { // discard it this.cursor = null; this.buffer = null; } } } }
private static Org.Apache.Http.Entity.ContentType Create(HeaderElement helem) { string mimeType = helem.GetName(); NameValuePair[] @params = helem.GetParameters(); return(new Org.Apache.Http.Entity.ContentType(mimeType, @params != null && @params .Length > 0 ? @params : null)); }
// non-javadoc, see interface HeaderValueParser public virtual HeaderElement[] ParseElements(CharArrayBuffer buffer, ParserCursor cursor) { Args.NotNull(buffer, "Char array buffer"); Args.NotNull(cursor, "Parser cursor"); IList <HeaderElement> elements = new AList <HeaderElement>(); while (!cursor.AtEnd()) { HeaderElement element = ParseHeaderElement(buffer, cursor); if (!(element.GetName().Length == 0 && element.GetValue() == null)) { elements.AddItem(element); } } return(Sharpen.Collections.ToArray(elements, new HeaderElement[elements.Count])); }