private void BufferHeaderValue() { this.cursor = null; this.buffer = null; while (this.headerIt.HasNext()) { Header h = this.headerIt.NextHeader(); if (h is FormattedHeader) { this.buffer = ((FormattedHeader)h).GetBuffer(); this.cursor = new ParserCursor(0, this.buffer.Length()); this.cursor.UpdatePos(((FormattedHeader)h).GetValuePos()); break; } else { string value = h.GetValue(); if (value != null) { this.buffer = new CharArrayBuffer(value.Length); this.buffer.Append(value); this.cursor = new ParserCursor(0, this.buffer.Length()); break; } } } }
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; } } } }
/// <exception cref="Org.Apache.Http.ParseException"></exception> public virtual HeaderElement[] GetElements() { ParserCursor cursor = new ParserCursor(0, this.buffer.Length()); cursor.UpdatePos(this.valuePos); return(BasicHeaderValueParser.Instance.ParseElements(this.buffer, cursor)); }
// 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 )); } }
/// <exception cref="Org.Apache.Http.ParseException"></exception> public static StatusLine ParseStatusLine(string value, LineParser parser) { Args.NotNull(value, "Value"); CharArrayBuffer buffer = new CharArrayBuffer(value.Length); buffer.Append(value); ParserCursor cursor = new ParserCursor(0, value.Length); return((parser != null ? parser : Org.Apache.Http.Message.BasicLineParser.Instance ).ParseStatusLine(buffer, cursor)); }
/// <summary>Parses elements with the given parser.</summary> /// <remarks>Parses elements with the given parser.</remarks> /// <param name="value">the header value to parse</param> /// <param name="parser">the parser to use, or <code>null</code> for default</param> /// <returns>array holding the header elements, never <code>null</code></returns> /// <exception cref="Org.Apache.Http.ParseException"></exception> public static HeaderElement[] ParseElements(string value, HeaderValueParser parser ) { Args.NotNull(value, "Value"); CharArrayBuffer buffer = new CharArrayBuffer(value.Length); buffer.Append(value); ParserCursor cursor = new ParserCursor(0, value.Length); return((parser != null ? parser : Org.Apache.Http.Message.BasicHeaderValueParser. Instance).ParseElements(buffer, cursor)); }
/// <summary>Helper to skip whitespace.</summary> /// <remarks>Helper to skip whitespace.</remarks> protected internal virtual void SkipWhitespace(CharArrayBuffer buffer, ParserCursor cursor) { int pos = cursor.GetPos(); int indexTo = cursor.GetUpperBound(); while ((pos < indexTo) && HTTP.IsWhitespace(buffer.CharAt(pos))) { pos++; } cursor.UpdatePos(pos); }
// non-javadoc, see interface LineParser public virtual bool HasProtocolVersion(CharArrayBuffer buffer, ParserCursor cursor ) { Args.NotNull(buffer, "Char array buffer"); Args.NotNull(cursor, "Parser cursor"); int index = cursor.GetPos(); string protoname = this.protocol.GetProtocol(); int protolength = protoname.Length; if (buffer.Length() < protolength + 4) { return(false); } // not long enough for "HTTP/1.1" if (index < 0) { // end of line, no tolerance for trailing whitespace // this works only for single-digit major and minor version index = buffer.Length() - 4 - protolength; } else { if (index == 0) { // beginning of line, tolerate leading whitespace while ((index < buffer.Length()) && HTTP.IsWhitespace(buffer.CharAt(index))) { index++; } } } // else within line, don't tolerate whitespace if (index + protolength + 4 > buffer.Length()) { return(false); } // just check protocol name and slash, no need to analyse the version bool ok = true; for (int j = 0; ok && (j < protolength); j++) { ok = (buffer.CharAt(index + j) == protoname[j]); } if (ok) { ok = (buffer.CharAt(index + protolength) == '/'); } return(ok); }
// 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])); }
/// <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 )); } }
// 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)); }
// non-javadoc, see interface HeaderValueParser public virtual NameValuePair[] ParseParameters(CharArrayBuffer buffer, ParserCursor cursor) { Args.NotNull(buffer, "Char array buffer"); Args.NotNull(cursor, "Parser cursor"); int pos = cursor.GetPos(); int indexTo = cursor.GetUpperBound(); while (pos < indexTo) { char ch = buffer.CharAt(pos); if (HTTP.IsWhitespace(ch)) { pos++; } else { break; } } cursor.UpdatePos(pos); if (cursor.AtEnd()) { return(new NameValuePair[] { }); } IList <NameValuePair> @params = new AList <NameValuePair>(); while (!cursor.AtEnd()) { NameValuePair param = ParseNameValuePair(buffer, cursor); @params.AddItem(param); char ch = buffer.CharAt(cursor.GetPos() - 1); if (ch == ElemDelimiter) { break; } } return(Sharpen.Collections.ToArray(@params, new NameValuePair[@params.Count])); }
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)); }
// non-javadoc, see interface HeaderValueParser public virtual NameValuePair ParseNameValuePair(CharArrayBuffer buffer, ParserCursor cursor) { return(ParseNameValuePair(buffer, cursor, AllDelimiters)); }
// 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)); }