/// <summary> /// Try to find a header name. /// </summary> /// <returns></returns> private bool GetHeaderName() { // empty line. body is begining. if (_reader.Current == '\r' && _reader.Peek == '\n') { // Eat the line break _reader.Consume('\r', '\n'); // Don't have a body? if (_bodyBytesLeft == 0) { OnComplete(); _parserMethod = ParseFirstLine; } else { _parserMethod = GetBody; } return(true); } _headerName = _reader.ReadUntil(':'); if (_headerName == null) { return(false); } _reader.Consume(); // eat colon _parserMethod = GetHeaderValue; return(true); }
/// <summary> /// Try to find a header name. /// </summary> /// <returns></returns> private bool GetHeaderName() { // empty line. body is begining. if (_reader.Current == '\r' && _reader.Peek == '\n') { // Eat the line break _reader.Consume('\r', '\n'); // Don't have a body? if (_bodyBytesLeft == 0) { OnComplete(); _parserMethod = ParseFirstLine; } else _parserMethod = GetBody; return true; } _headerName = _reader.ReadUntil(':'); if (_headerName == null) return false; _reader.Consume(); // eat colon _parserMethod = GetHeaderValue; return true; }
private void Value_CompletedOrMultiLine(char ch) { if (IsHorizontalWhitespace(ch)) { _headerValue.Append(' '); _parserMethod = Value_StripWhitespacesBefore; return; } if (ch == '\r') { return; //empty line } HeaderParsed(_headerName.ToString(), _headerValue.ToString()); ResetLineParsing(); _parserMethod = Name_StripWhiteSpacesBefore; if (ch == '\n') { //Header completed TriggerHeaderCompleted(); return; } _lookAhead = ch; }
private bool GetHeaderName() { // empty line. body is begining. if (_reader.Current == '\r' && _reader.Peek == '\n') { // Eat the line break _reader.Consume('\r', '\n'); // Don't have a body? if (_bodyBytesLeft == 0) { _onBody(null); _parserMethod = ParseFirstLine; return(false); } _parserMethod = GetBody; return(true); } _headerName = _reader.ReadUntil(':'); if (_headerName == null) { throw new ParseException(ExceptionMessage.InvalidHeaderName); } _reader.Consume(); // eat colon _parserMethod = GetHeaderValue; return(true); }
public bool ParseFirstLine() { _reader.Consume('\r', '\n'); if (!_reader.Contains('\n')) { throw new ParseException("Invalid firstline."); } var firstLine = _reader.ReadFoldedLine(); if (firstLine.EndsWith(SipConstants.SipTwoZeroString)) { SipRequestLine requestLine = new SipRequestLineParser().Parse(firstLine); _listener.OnRequest(requestLine); } else if (firstLine.StartsWith(SipConstants.SipTwoZeroString)) { var message = new SipResponse(); var statusLine = new SipStatusLineParser().Parse(firstLine); _listener.OnResponse(statusLine); } else { throw new ParseException(ExceptionMessage.InvalidFirstLineFormat); } _parserMethod = GetHeaderName; return(true); }
/// <summary> /// Read cookie value qouted /// </summary> private bool Value_Qouted() { MoveNext(); // skip '"' var last = char.MinValue; while (Current != '"' && !IsEOF) { if (Current == '"' && last == '\\') { _cookieValue += '#'; MoveNext(); } else { _cookieValue += Current; } last = Current; MoveNext(); } _parserMethod = Value_After; return(true); }
private bool GetHeaderValue() { // remove white spaces. _reader.ConsumeWhiteSpaces(); string value = _reader.ReadFoldedLine(); if (value == null) { throw new ParseException(ExceptionMessage.InvalidFormat); } _headerValue += value; _headerValue = ProcessSingleLine(_headerValue); if (System.String.Compare(_headerName, SipHeaderNames.ContentLength, System.StringComparison.OrdinalIgnoreCase) == 0) { if (!int.TryParse(value, out _bodyBytesLeft)) { throw new ParseException("Content length is not a number."); } } _onHeader(_headerName, _headerValue); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return(true); }
/// <summary> /// Parses the first line in a request/response. /// </summary> /// <returns><c>true</c> if first line is well formatted; otherwise <c>false</c>.</returns> /// <exception cref="BadRequestException">Invalid request/response line.</exception> public bool ParseFirstLine() { _reader.Consume('\r', '\n'); // Do not contain a complete first line. if (!_reader.Contains('\n')) { return(false); } var words = new string[3]; words[0] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[1] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[2] = _reader.ReadLine(); if (string.IsNullOrEmpty(words[0]) || string.IsNullOrEmpty(words[1]) || string.IsNullOrEmpty(words[2])) { throw new BadRequestException("Invalid request/response line."); } OnFirstLine(words); _parserMethod = GetHeaderName; return(true); }
/// <summary> /// Reset parser to initial state. /// </summary> public void Reset() { _logger.Info("Resetting.."); _headerValue = null; _headerName = string.Empty; _bodyBytesLeft = 0; _parserMethod = ParseFirstLine; }
/// <summary> /// Get header values. /// </summary> /// <returns></returns> /// <remarks>Will also look for multi header values and automatically merge them to one line.</remarks> /// <exception cref="ParserException">Content length is not a number.</exception> private bool GetHeaderValue() { // remove white spaces. _reader.Consume(' ', '\t'); // multi line or empty value? if (_reader.Current == '\r' && _reader.Peek == '\n') { _reader.Consume('\r', '\n'); // empty value. if (_reader.Current != '\t' && _reader.Current != ' ') { OnHeader(_headerName, string.Empty); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return(true); } if (_reader.RemainingLength < 1) { return(false); } // consume one whitespace _reader.Consume(); // and fetch the rest. return(GetHeaderValue()); } string value = _reader.ReadLine(); if (value == null) { return(false); } _headerValue += value; if (string.Compare(_headerName, "Content-Length", true) == 0) { if (!int.TryParse(value, out _bodyBytesLeft)) { throw new ParserException("Content length is not a number."); } } OnHeader(_headerName, value); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return(true); }
private void Name_ParseUntilComma(char ch) { if (ch == ':') { _parserMethod = Value_StripWhitespacesBefore; return; } _headerName.Append(ch); }
private void Value_StripWhitespacesBefore(char ch) { if (IsHorizontalWhitespace(ch)) { return; } _parserMethod = Value_ParseUntilQouteOrNewLine; _lookAhead = ch; }
private void Name_StripWhiteSpacesBefore(char ch) { if (IsHorizontalWhitespace(ch)) { return; } _parserMethod = Name_ParseUntilComma; _lookAhead = ch; }
/// <summary> /// Remove all white spaces until colon is found /// </summary> protected virtual bool Name_After() { while (CharExtensions.IsWhiteSpace(Current) || Current == ':') { MoveNext(); } _parserMethod = Value_Before; return(true); }
/// <summary> /// Parse state method, remove all white spaces before the cookie name /// </summary> protected bool Name_Before() { while (CharExtensions.IsWhiteSpace(Current)) { MoveNext(); } _parserMethod = Name; return(true); }
/// <summary> /// Read cookie name until white space or equals are found /// </summary> protected virtual bool Name() { while (!CharExtensions.IsWhiteSpace(Current) && Current != '=') { _cookieName += Current; MoveNext(); } _parserMethod = Name_After; return(true); }
/// <summary> /// Read cookie value /// </summary> private bool Value() { while (Current != ';' && !IsEOF) { _cookieValue += Current; MoveNext(); } _parserMethod = Value_After; return(true); }
private void Value_ParseQuoted(char ch) { if (ch == '"') { // exited the quouted string _parserMethod = Value_ParseUntilQouteOrNewLine; return; } _headerValue.Append(ch); }
/// <summary> /// Determine if the cookie value is quoted or regular. /// </summary> protected virtual bool Value_Before() { if (Current == '"') { _parserMethod = Value_Qouted; } else { _parserMethod = Value; } MoveNext(); return(true); }
private bool Value_After() { OnCookie(_cookieName, _cookieValue); _cookieName = ""; _cookieValue = ""; while (CharExtensions.IsWhiteSpace(Current) || Current == ';') { MoveNext(); } _parserMethod = Name_Before; return(true); }
public static T[] ParseArray <T>(string arrayString, ParserMethod <T> parser) { string[] ss = arrayString.Split(';'); T[] ts = new T[ss.Length]; for (int i = 0; i < ss.Length; i++) { ts[i] = parser(ss[i].Trim()); } return(ts); }
/// <summary> /// 解析请求或响应消息的第一行 /// </summary> /// <returns><c>true</c> if first line is well formatted; otherwise <c>false</c>.</returns> /// <exception cref="BadRequestException">Invalid request/response line.</exception> private bool ParseFirstLine() { /* HTTP Message Example * GET /path/file.html HTTP/1.0 * From: [email protected] * User-Agent: HTTPTool/1.0 * [blank line here] * * HTTP/1.0 200 OK * Date: Fri, 31 Dec 1999 23:59:59 GMT * Content-Type: text/html * Content-Length: 1354 * * <html> * <body> * <h1>Happy New Millennium!</h1> * (more file contents) * </body> * </html> */ _reader.Consume('\r', '\n'); // Do not contain a complete first line. if (!_reader.Contains('\n')) { return(false); } var words = new string[3]; words[0] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[1] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[2] = _reader.ReadLine(); if (string.IsNullOrEmpty(words[0]) || string.IsNullOrEmpty(words[1]) || string.IsNullOrEmpty(words[2])) { throw new BadRequestException("Invalid request/response line."); } // 成功找到消息的第一行 OnFirstLine(words); // 指定下一步解析方式 _parserMethod = GetHeaderName; return(true); }
/// <summary> /// Parse cookie string /// </summary> /// <returns>A generated cookie collection.</returns> public IHttpCookieCollection Parse(string value) { if (value == null) throw new ArgumentNullException("value"); _headerValue = value; _cookies = new HttpCookieCollection(); _parserMethod = Name_Before; while (!IsEOF) { _parserMethod(); } OnCookie(_cookieName, _cookieValue); return (IHttpCookieCollection)_cookies; }
public static T[] ParseArray <T>(string arrayString, ParserMethod <T> parser) { string[] array = arrayString.Split(new char[] { ';' }); T[] array2 = new T[array.Length]; int num = array.Length; for (int i = 0; i < num; i++) { array2[i] = parser(array[i].Trim()); } return(array2); }
internal void Lex(byte[] bytes, Action <string> onFirstLine, Action <string, string> onHeader, Action <byte[]> onBody) { _onBody = onBody; _onHeader = onHeader; _onFirstLine = onFirstLine; _parserMethod = ParseFirstLine; _buffer = bytes; _logger.Debug("Lexing " + _buffer.Length + " bytes."); _reader.Assign(_buffer, 0, _buffer.Length); while (_parserMethod()) { _logger.Trace("Switched lexer method to " + _parserMethod.Method.Name + " at index " + _reader.Index); } }
internal void Lex(byte[] bytes, Action<string> onFirstLine, Action<string, string> onHeader, Action<byte[]> onBody) { _onBody = onBody; _onHeader = onHeader; _onFirstLine = onFirstLine; _parserMethod = ParseFirstLine; _buffer = bytes; _logger.Debug("Lexing " + _buffer.Length + " bytes."); _reader.Assign(_buffer, 0, _buffer.Length); while (_parserMethod()) { _logger.Trace("Switched lexer method to " + _parserMethod.Method.Name + " at index " + _reader.Index); } }
private bool ParseFirstLine() { _reader.Consume('\r', '\n'); if (!_reader.Contains('\n')) { throw new ParseException("Invalid firstline."); } var firstLine = _reader.ReadFoldedLine(); _onFirstLine(firstLine); _parserMethod = GetHeaderName; return(true); }
private T Parse <T>(string input, ParserMethod <T> parser) where T : NativeXNode { List <CodeToken> tokens = NativeXCodeParser.Tokenize(input.ToCharArray()); int currentToken = 0; bool parseSuccess = false; T result = parser(tokens, ref currentToken, ref parseSuccess); Assert.AreEqual(tokens.Count, currentToken); Assert.AreEqual(tokens[0].Start, result.Start); Assert.AreEqual(tokens[tokens.Count - 1].End, result.End); if (result != null) { result.BuildScope(null); } return(result); }
protected void Declare(XmlNodeType nodeType, ParserMethod parserMethod) { if (_nodeTypeMap.ContainsKey(nodeType)) { _nodeTypeMap[nodeType].Add(parserMethod); } else { _nodeTypeMap[nodeType] = new List <ParserMethod>() { parserMethod }; } if (nodeType == XmlNodeType.Attribute) { _doAttributes = true; } }
/// <summary> /// Parse cookie string /// </summary> /// <returns>A generated cookie collection.</returns> public IHttpCookieCollection Parse(string value) { if (value == null) { throw new ArgumentNullException("value"); } _headerValue = value; _cookies = new HttpCookieCollection(); _parserMethod = Name_Before; while (!IsEOF) { _parserMethod(); } OnCookie(_cookieName, _cookieValue); return((IHttpCookieCollection)_cookies); }
private void Value_ParseUntilQouteOrNewLine(char ch) { if (ch == '"') { _parserMethod = Value_ParseQuoted; return; } if (ch == '\r') { return; } if (ch == '\n') { _parserMethod = Value_CompletedOrMultiLine; return; } _headerValue.Append(ch); }
private bool ParseRequestLine() { _reader.Consume('\r', '\n'); // Do not contain a complete first line. if (!_reader.Contains('\n')) return false; var words = new string[3]; words[0] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[1] = _reader.ReadUntil(' '); _reader.Consume(); // eat delimiter words[2] = _reader.ReadLine(); if (string.IsNullOrEmpty(words[0]) || string.IsNullOrEmpty(words[1]) || string.IsNullOrEmpty(words[2])) throw new BadRequestException("Invalid request/response line."); _reqLineRecieved(words); _parserMethod = GetHeaderName; return true; }
private void FirstLine(char ch) { //ignore empty lines before the request line if (ch == '\r' || ch == '\n' && _headerName.Length == 0) { return; } if (ch == '\r') { return; } if (ch == '\n') { var line = _headerName.ToString().Split(new char[] { ' ' }, 3); RequestLineParsed(line[0], line[1], line[2]); _headerName.Clear(); _parserMethod = Name_StripWhiteSpacesBefore; return; } _headerName.Append(ch); }
public SipParser(ISipParserListener listener) { _listener = listener; _parserMethod = ParseFirstLine; }
/// <summary> /// Parse state method, remove all white spaces before the cookie name /// </summary> protected bool Name_Before() { while (CharExtensions.IsWhiteSpace(Current)) { MoveNext(); } _parserMethod = Name; return true; }
/// <summary> /// Initializes a new instance of the <see cref="HttpParser"/> class. /// </summary> public HttpParser() { _parserMethod = ParseFirstLine; }
private bool Value_After() { OnCookie(_cookieName, _cookieValue); _cookieName = ""; _cookieValue = ""; while (CharExtensions.IsWhiteSpace(Current) || Current == ';') { MoveNext(); } _parserMethod = Name_Before; return true; }
/// <summary> /// Read cookie name until white space or equals are found /// </summary> protected virtual bool Name() { while (!CharExtensions.IsWhiteSpace(Current) && Current != '=') { _cookieName += Current; MoveNext(); } _parserMethod = Name_After; return true; }
private bool GetHeaderValue() { // remove white spaces. _reader.ConsumeWhiteSpaces(); string value = _reader.ReadFoldedLine(); if (value == null) throw new ParseException(ExceptionMessage.InvalidFormat); _headerValue += value; _headerValue = ProcessSingleLine(_headerValue); if (System.String.Compare(_headerName, SipHeaderNames.ContentLength, System.StringComparison.OrdinalIgnoreCase) == 0) { if (!int.TryParse(value, out _bodyBytesLeft)) throw new ParseException("Content length is not a number."); } _onHeader(_headerName, _headerValue); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return true; }
/// <summary> /// Reset parser state /// </summary> public void Reset() { ResetLineParsing(); _parserMethod = FirstLine; }
/// <summary> /// Initializes a new instance of the <see cref="HeaderParser" /> class. /// </summary> public HeaderParser() { _parserMethod = FirstLine; }
private void Value_StripWhitespacesBefore(char ch) { if (IsHorizontalWhitespace(ch)) return; _parserMethod = Value_ParseUntilQouteOrNewLine; _lookAhead = ch; }
private void Value_ParseUntilQouteOrNewLine(char ch) { if (ch == '"') { _parserMethod = Value_ParseQuoted; return; } if (ch == '\r') return; if (ch == '\n') { _parserMethod = Value_CompletedOrMultiLine; return; } _headerValue.Append(ch); }
private void Value_CompletedOrMultiLine(char ch) { if (IsHorizontalWhitespace(ch)) { _headerValue.Append(' '); _parserMethod = Value_StripWhitespacesBefore; return; } if (ch == '\r') return; //empty line HeaderParsed(_headerName.ToString(), _headerValue.ToString()); ResetLineParsing(); _parserMethod = Name_StripWhiteSpacesBefore; if (ch == '\n') { //Header completed TriggerHeaderCompleted(); return; } _lookAhead = ch; }
private void Name_StripWhiteSpacesBefore(char ch) { if (IsHorizontalWhitespace(ch)) return; _parserMethod = Name_ParseUntilComma; _lookAhead = ch; }
/// <summary> /// Remove all white spaces until colon is found /// </summary> protected virtual bool Name_After() { while (CharExtensions.IsWhiteSpace(Current) || Current == ':') { MoveNext(); } _parserMethod = Value_Before; return true; }
/// <summary> /// Get header values. /// </summary> /// <returns></returns> /// <remarks>Will also look for multi header values and automatically merge them to one line.</remarks> /// <exception cref="ParserException">Content length is not a number.</exception> private bool GetHeaderValue() { // remove white spaces. _reader.Consume(' ', '\t'); // multi line or empty value? if (_reader.Current == '\r' && _reader.Peek == '\n') { _reader.Consume('\r', '\n'); // empty value. if (_reader.Current != '\t' && _reader.Current != ' ') { OnHeader(_headerName, string.Empty); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return true; } if (_reader.RemainingLength < 1) return false; // consume one whitespace _reader.Consume(); // and fetch the rest. return GetHeaderValue(); } string value = _reader.ReadLine(); if (value == null) return false; _headerValue += value; if (string.Compare(_headerName, "Content-Length", true) == 0) { if (!int.TryParse(value, out _bodyBytesLeft)) throw new ParserException("Content length is not a number."); } OnHeader(_headerName, value); _headerName = null; _headerValue = string.Empty; _parserMethod = GetHeaderName; return true; }
private bool ParseFirstLine() { _reader.Consume('\r', '\n'); if (!_reader.Contains('\n')) throw new ParseException("Invalid firstline."); var firstLine = _reader.ReadFoldedLine(); _onFirstLine(firstLine); _parserMethod = GetHeaderName; return true; }
/// <summary> /// HTTP消息解析器,用于解析请求或响应消息 /// </summary> public MessageParser() { _parserMethod = ParseFirstLine; }
/// <summary> /// Read cookie value qouted /// </summary> private bool Value_Qouted() { MoveNext(); // skip '"' var last = char.MinValue; while (Current != '"' && !IsEOF) { if (Current == '"' && last == '\\') { _cookieValue += '#'; MoveNext(); } else { _cookieValue += Current; } last = Current; MoveNext(); } _parserMethod = Value_After; return true; }
/// <summary> /// Read cookie value /// </summary> private bool Value() { while (Current != ';' && !IsEOF) { _cookieValue += Current; MoveNext(); } _parserMethod = Value_After; return true; }
public void Reset() { _parserMethod = ParseFirstLine; }
/// <summary> /// Determine if the cookie value is quoted or regular. /// </summary> protected virtual bool Value_Before() { if (Current == '"') { _parserMethod = Value_Qouted; } else { _parserMethod = Value; } MoveNext(); return true; }
public bool ParseFirstLine() { _reader.Consume('\r', '\n'); if (!_reader.Contains('\n')) throw new ParseException("Invalid firstline."); var firstLine = _reader.ReadFoldedLine(); if (firstLine.EndsWith(SipConstants.SipTwoZeroString)) { SipRequestLine requestLine = new SipRequestLineParser().Parse(firstLine); _listener.OnRequest(requestLine); } else if (firstLine.StartsWith(SipConstants.SipTwoZeroString)) { var message = new SipResponse(); var statusLine = new SipStatusLineParser().Parse(firstLine); _listener.OnResponse(statusLine); } else { throw new ParseException(ExceptionMessage.InvalidFirstLineFormat); } _parserMethod = GetHeaderName; return true; }
private bool GetHeaderName() { // empty line. body is begining. if (_reader.Current == '\r' && _reader.Peek == '\n') { // Eat the line break _reader.Consume('\r', '\n'); // Don't have a body? if (_bodyBytesLeft == 0) { _onBody(null); _parserMethod = ParseFirstLine; return false; } _parserMethod = GetBody; return true; } _headerName = _reader.ReadUntil(':'); if (_headerName == null) throw new ParseException(ExceptionMessage.InvalidHeaderName); _reader.Consume(); // eat colon _parserMethod = GetHeaderValue; return true; }
private void FirstLine(char ch) { //ignore empty lines before the request line if (ch == '\r' || ch == '\n' && _headerName.Length == 0) return; if (ch == '\r') return; if (ch == '\n') { var line = _headerName.ToString().Split(new char[] { ' ' }, 3); RequestLineParsed(line[0], line[1], line[2]); _headerName.Clear(); _parserMethod = Name_StripWhiteSpacesBefore; return; } _headerName.Append(ch); }