// this one is a little bit different: // the current `position` of the buffer is the location of the // error, `ini_pos` indicates where the position of // the buffer when it was passed to the `execute` method of the parser, i.e. // using this information and `limit` we'll know all the valid data // in the buffer around the error we can use to print pretty error // messages. public void RaiseOnError(Parser p, string message, ByteBuffer buf, int ini_pos) { if (null != OnError) OnError (p, message, buf, ini_pos); // if on_error gets called it MUST throw an exception, else the parser // will attempt to continue parsing, which it can't because it's // in an invalid state. Console.WriteLine ("ERROR: '{0}'", message); throw new System.Exception (message); }
protected override int OnHeadersComplete(Parser parser) { base.OnHeadersComplete (parser); StatusCode = parser.StatusCode; if (Request.Method == Method.Head) return 1; return 0; }
public int RaiseOnHeadersComplete(Parser p) { return Raise (OnHeadersComplete, p); }
public void RaiseOnHeaderField(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnHeaderField, p, buf, pos, len); }
public void RaiseOnFragment(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnFragment, p, buf, pos, len); }
private int Raise(DataCallback cb, Parser p, ByteBuffer buf, int pos, int len) { if (cb == null || pos == -1) return 0; try { return cb (p,buf,pos,len); } catch (System.Exception e) { Console.WriteLine (e); RaiseOnError (p, e.Message, buf, pos); return -1; } }
public void RaiseOnBody(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnBody, p, buf, pos, len); }
public void RaiseOnQueryString(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnQueryString, p, buf, pos, len); }
private int Raise(Callback cb, Parser p) { if (cb == null) return 0; try { return cb (p); } catch (System.Exception e) { Console.WriteLine (e); RaiseOnError (p, e.Message, null, -1); return -1; } }
public void RaiseOnMessageComplete(Parser p) { Raise (OnMessageComplete, p); }
public void RaiseOnPath(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnPath, p, buf, pos, len); }
public void RaiseOnMessageBegin(Parser p) { Raise (OnMessageBegin, p); }
public void RaiseOnHeaderValue(Parser p, ByteBuffer buf, int pos, int len) { Raise (OnHeaderValue, p, buf, pos, len); }
private int OnQueryString(Parser parser, ByteBuffer data, int pos, int len) { string str = Encoding.ASCII.GetString (data.Bytes, pos, len); query_data_builder.Append (str); return 0; }
private int OnPath(Parser parser, ByteBuffer data, int pos, int len) { string str = Encoding.ASCII.GetString (data.Bytes, pos, len); str = Utility.UrlDecode (str, Encoding.ASCII); Path = Path == null ? str : String.Concat (Path, str); return 0; }
protected override void OnFinishedReading(Parser parser) { base.OnFinishedReading (parser); MajorVersion = parser.Major; MinorVersion = parser.Minor; Method = parser.HttpMethod; if (query_data_builder.Length != 0) { QueryData = Utility.ParseUrlEncodedData (query_data_builder.ToString ()); query_data_builder.Length = 0; } Transaction.OnRequestReady (); }