Inheritance: HttpParser.RawHttpParser
Beispiel #1
0
		public static String DumpParser (HttpParser p)
		{
			StringBuilder builder = new StringBuilder();
    
			// the stuff up to the break is ephermeral and only meaningful
			// while the parser is parsing. In general, this method is 
			// probably only useful during debugging.

			/*
			builder.Append("state :"); builder.Append(p.state); builder.Append("\n");
			builder.Append("header_state :"); builder.Append(p.header_state); builder.Append("\n");
			builder.Append("strict :"); builder.Append(p.strict); builder.Append("\n");
			builder.Append("index :"); builder.Append(p.index); builder.Append("\n");
			builder.Append("flags :"); builder.Append(p.flags); builder.Append("\n");
			builder.Append("nread :"); builder.Append(p.nread); builder.Append("\n");
			builder.Append("content_length :"); builder.Append(p.content_length); builder.Append("\n");


			builder.Append("type :"); builder.Append(p.type); builder.Append("\n");
			builder.Append("http_major :"); builder.Append(p.http_major); builder.Append("\n");
			builder.Append("http_minor :"); builder.Append(p.http_minor); builder.Append("\n");
			builder.Append("status_code :"); builder.Append(p.status_code); builder.Append("\n");
			builder.Append("method :"); builder.Append(p.method); builder.Append("\n");
			builder.Append("upgrade :"); builder.Append(p.upgrade); builder.Append("\n");
			*/

			return builder.ToString();

		}
		// 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 (HttpParser p, string message, MemoryStream 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.
			throw new HttpException (message);
		}
Beispiel #3
0
        // 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(HttpParser p, string message, MemoryStream 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.
            throw new HttpException(message);
        }
Beispiel #4
0
        public Parser(HttpParserType type)
        {
            unsafe
            {
                HttpParserSettings settings = new HttpParserSettings();
                settings.on_message_begin = OnMessageBegin;
                settings.on_message_complete = OnMessageComplete;
                settings.on_url = OnUrl;
                settings.on_header_field = OnHeaderField;
                settings.on_header_value = OnHeaderValue;
                settings.on_headers_complete = OnHeadersComplete;
                settings.on_body = OnBody;

                _parser = new HttpParser(settings, type);
            }
        }
Beispiel #5
0
        public Parser(HttpParserType type)
        {
            unsafe
            {
                HttpParserSettings settings = new HttpParserSettings();
                settings.on_message_begin    = OnMessageBegin;
                settings.on_message_complete = OnMessageComplete;
                settings.on_url              = OnUrl;
                settings.on_header_field     = OnHeaderField;
                settings.on_header_value     = OnHeaderValue;
                settings.on_headers_complete = OnHeadersComplete;
                settings.on_body             = OnBody;

                _parser = new HttpParser(settings, type);
            }
        }
Beispiel #6
0
        private static bool MessageNeedsEof(HttpParser parser)
        {
            if (parser.Type == HttpParserType.Request)
            {
                return(false);
            }

            if (parser.StatusCode / 100 == 1 ||                                        // 1xx e.g. Continue
                parser.StatusCode == 204 ||                                            // No Content
                parser.StatusCode == 304 ||                                            // Not Modified
                (parser.Flags & HttpParserFlags.SkipBody) == HttpParserFlags.SkipBody) // response to a HEAD request
            {
                return(false);
            }

            if ((parser.Flags & HttpParserFlags.Chunked) == HttpParserFlags.Chunked || parser.ContentLength != ulong.MaxValue)
            {
                return(false);
            }

            return(true);
        }
Beispiel #7
0
 unsafe int OnUrl(HttpParser parser, char* at, uint length)
 {
     if (Url != null)
     {
         string url = Marshal.PtrToStringUni((IntPtr)at, (int)length);
         Url(this, url);
     }
     return 0;
 }
Beispiel #8
0
 int OnMessageBegin(HttpParser parser)
 {
     if (MessageBegin != null)
     {
         MessageBegin(this);
     }
     return 0;
 }
Beispiel #9
0
 int OnHeadersComplete(HttpParser parser)
 {
     if (HeadersComplete != null)
     {
         HeadersComplete(this);
     }
     return 0;
 }
Beispiel #10
0
 public void RaiseOnHeadersComplete(HttpParser p)
 {
     Raise(OnHeadersComplete, p);
 }
Beispiel #11
0
 public void RaiseOnHeaderValue(HttpParser p, MemoryStream buf, int pos, int len)
 {
     Raise(OnHeaderValue, p, buf, pos, len);
 }
Beispiel #12
0
 public void RaiseOnQueryString(HttpParser p, MemoryStream buf, int pos, int len)
 {
     Raise(OnQueryString, p, buf, pos, len);
 }
Beispiel #13
0
 public void RaiseOnMessageComplete(HttpParser p)
 {
     Raise(OnMessageComplete, p);
 }
Beispiel #14
0
        public static void Main()
        {
            string content =
            @"POST    HTTP://LIULIJIN.INFO     HTTP/1.1
            HOST:LIULIJIN.INFO
            Cookie:s=1

            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            ";
            CodeTimer.Initialize();
            CodeTimer.Time("!", 50000, () =>
            {
                HttpParser parser = new HttpParser();
                parser.Go(content);
            });

            Console.ReadLine();
        }
		private void Raise (HttpDataCallback cb, HttpParser p, MemoryStream buf, int pos, int len)
		{
			if (null != cb && -1 != pos)
				cb (p,buf,pos,len);
		}
		private void Raise (HttpCallback cb, HttpParser p)
		{
			if (null != cb)
				cb (p);
		}
		public void RaiseOnHeadersComplete (HttpParser p)
		{
			Raise (OnHeadersComplete, p);
		}
		public void RaiseOnBody(HttpParser p, MemoryStream buf, int pos, int len)
		{
			Raise (OnBody, p, buf, pos, len);
		}
		public void RaiseOnHeaderValue (HttpParser p, MemoryStream buf, int pos, int len)
		{
			Raise (OnHeaderValue, p, buf, pos, len);
		}
		public void RaiseOnFragment (HttpParser p, MemoryStream buf, int pos, int len)
		{
			Raise (OnFragment, p, buf, pos, len);
		}
		public void RaiseOnQueryString (HttpParser p, MemoryStream buf, int pos, int len)
		{
			Raise (OnQueryString, p, buf, pos, len);
		}
Beispiel #22
0
 public void RaiseOnMessageBegin(HttpParser p)
 {
     Raise(OnMessageBegin, p);
 }
 internal int on_headers_complete_wrapper(HttpParser parser)
 {
     return Convert.ToInt32(HeadersCompleted(parser));
 }
Beispiel #24
0
        public static HttpParser Create(http_parser parser)
        {
            var httpParser = new HttpParser(parser);

            return(httpParser);
        }
 internal int on_message_begin_wrapper(HttpParser parser)
 {
     return Convert.ToInt32(MessageBegan(parser));
 }
Beispiel #26
0
 public void RaiseOnFragment(HttpParser p, MemoryStream buf, int pos, int len)
 {
     Raise(OnFragment, p, buf, pos, len);
 }
 internal int on_message_complete_wrapper(HttpParser parser)
 {
     return Convert.ToInt32(MessageCompleted(parser));
 }
Beispiel #28
0
 public void RaiseOnBody(HttpParser p, MemoryStream buf, int pos, int len)
 {
     Raise(OnBody, p, buf, pos, len);
 }
 internal unsafe int on_body_wrapper(HttpParser parser, byte* at, uint length)
 {
     var data = new byte[(int)length];
     Marshal.Copy((IntPtr)at, data, 0, (int)length);
     return Convert.ToInt32(Body(parser, data));
 }
Beispiel #30
0
 unsafe int OnBody(HttpParser parser, char* at, uint length)
 {
     if (Body != null)
     {
         string body = Marshal.PtrToStringUni((IntPtr)at, (int)length);
         Body(this, body);
     }
     return 0;
 }
 internal int on_message_complete_wrapper(HttpParser parser)
 {
     return(Convert.ToInt32(MessageCompleted(parser)));
 }
Beispiel #32
0
 unsafe int OnHeaderValue(HttpParser parser, char* at, uint length)
 {
     if (HeaderValue != null)
     {
         string headerValue = Marshal.PtrToStringUni((IntPtr)at, (int)length);
         HeaderValue(this, headerValue);
     }
     return 0;
 }
 internal int on_message_begin_wrapper(HttpParser parser)
 {
     return(Convert.ToInt32(MessageBegan(parser)));
 }
Beispiel #34
0
 int OnMessageComplete(HttpParser parser)
 {
     if (MessageComplete != null)
     {
         MessageComplete(this);
     }
     return 0;
 }
		public void RaiseOnMessageBegin (HttpParser p)
		{
			Raise (OnMessageBegin, p);
		}
 internal int on_headers_complete_wrapper(HttpParser parser)
 {
     return(Convert.ToInt32(HeadersCompleted(parser)));
 }
		public void RaiseOnMessageComplete (HttpParser p)
		{
			Raise (OnMessageComplete, p);
		}