Example #1
0
 public HttpParseResult WriteEof()
 {
     if (state == HttpParseState.RecvContent)
     {
         OnContent?.Invoke(contentBuffer);
         return(HttpParseResult.End);
     }
     else
     {
         return(HttpParseResult.Processing);
     }
 }
Example #2
0
        /// <summary>Events</summary>
        public void Event()
        {
            RaiseEvent raiseEvent;

            while (Events.TryDequeue(out raiseEvent))
            {
                // Raise start event
                if (raiseEvent.EventType == EventType.Started)
                {
                    OnStarted?.Invoke();
                }

                // Raise stop event
                if (raiseEvent.EventType == EventType.Stopped)
                {
                    OnStopped?.Invoke();
                }

                // Raise link event
                if (raiseEvent.EventType == EventType.Linked)
                {
                    OnLinked?.Invoke(raiseEvent.Link);
                }

                // Raise redirection event
                if (raiseEvent.EventType == EventType.Redirect)
                {
                    OnRedirect?.Invoke(raiseEvent.Link);
                }

                // Raise post redirection event
                if (raiseEvent.EventType == EventType.Redirected)
                {
                    OnRedirected?.Invoke(raiseEvent.Link);
                }

                // Raise approval event
                if (raiseEvent.EventType == EventType.Approval)
                {
                    OnApproval?.Invoke(raiseEvent.Link, raiseEvent.Content);
                }

                // Raise content event
                if (raiseEvent.EventType == EventType.Content)
                {
                    OnContent?.Invoke(raiseEvent.Link, raiseEvent.Content);
                }

                // Raise unlink event
                if (raiseEvent.EventType == EventType.Unlinked)
                {
                    OnUnlinked?.Invoke(raiseEvent.Link, raiseEvent.Reason);
                }

                // Raise failure event
                if (raiseEvent.EventType == EventType.Failed)
                {
                    OnFailed?.Invoke(raiseEvent.Link, raiseEvent.Failure);
                }
            }
        }
Example #3
0
        public HttpParseResult Parse()
        {
            if (state == HttpParseState.Begin)
            {
                OnReset?.Invoke();
            }

            while (true)
            {
                bool passed = false;

                switch (state)
                {
                case HttpParseState.Begin:
                    passed = ParseHttpMethod();
                    break;

                case HttpParseState.EndMethod:
                    passed = ParseRequestUri();
                    break;

                case HttpParseState.EndRequestUri:
                    passed = ParseHttpVer();
                    break;

                case HttpParseState.BeginHeaderKey:
                    passed = ParseHeaderKey();
                    break;

                case HttpParseState.EndHeaderKey:
                    passed = ParseHeaderValue();
                    break;

                case HttpParseState.RecvContent:
                    if (contentLength.HasValue &&
                        contentLength.Value < buffer.Length)
                    {
                        var slice = buffer.Take(contentLength.Value).ToArray();
                        buffer = buffer.Skip(contentLength.Value).ToArray();
                        OnContentChunk?.Invoke(slice);
                        contentBuffer = contentBuffer.Concat(slice).ToArray();
                    }
                    else
                    {
                        contentBuffer = contentBuffer.Concat(buffer).ToArray();
                        OnContentChunk?.Invoke(buffer);
                        buffer = new byte[] { };
                    }

                    if (contentLength.HasValue &&
                        contentLength == contentBuffer.Length)
                    {
                        OnContent?.Invoke(contentBuffer);
                        state = HttpParseState.End;
                    }

                    break;
                }

                if (passed == false || buffer.Length == 0)
                {
                    break;
                }
            }

            var parseEnd = state == HttpParseState.End;

            if (parseEnd)
            {
                state = HttpParseState.Begin;
            }

            if (parseEnd && buffer.Length == 0)
            {
                return(HttpParseResult.End);
            }
            else if (parseEnd)
            {
                return(HttpParseResult.EndWithTrailing);
            }
            else
            {
                return(HttpParseResult.Processing);
            }
        }