Example #1
0
        public async Task EndSendResponse(uint requestID, ConnectionType ctype)
        {
            isSendingResponse = false;
            m_currentResponse?.Clear();
            m_currentResponse = null;

            bool doclose = ctype == ConnectionType.Close;

            if (doclose)
            {
                m_isClosing = true;
                m_requests.Clear();
                TriggerKeepalive = true;
                return;
            }
            else
            {
                LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
                if (Stream != null && Stream.CanWrite)
                {
                    ContextTimeoutManager.ContextEnterActiveSend();
                    try
                    {
                        await Stream.FlushAsync().ConfigureAwait(false);
                    }
                    catch
                    {
                    };
                    ContextTimeoutManager.ContextLeaveActiveSend();
                }

                if (Stream == null || !Stream.CanWrite)
                {
                    return;
                }

                TriggerKeepalive = true;
                lock (m_requestsLock)
                {
                    m_waitingResponse = false;
                    if (m_requests != null && m_requests.Count > 0)
                    {
                        HttpRequest nextRequest = m_requests.Dequeue();
                        if (nextRequest != null)
                        {
                            m_waitingResponse = true;
                            RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest));
                        }
                    }
                }
            }
        }
Example #2
0
        public void EndSendResponse(uint requestID, ConnectionType ctype)
        {
            isSendingResponse = false;
            m_currentResponse?.Clear();
            m_currentResponse = null;
            lock (m_requestsLock)
                m_waitingResponse = false;

            if (contextID < 0)
            {
                return;
            }

            if (ctype == ConnectionType.Close)
            {
                m_isClosing = true;
                m_requests.Clear();
                TriggerKeepalive = true;
                return;
            }
            else
            {
                if (Stream == null || !Stream.CanWrite)
                {
                    return;
                }

                LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
                HttpRequest nextRequest = null;
                lock (m_requestsLock)
                {
                    if (m_requests != null && m_requests.Count > 0)
                    {
                        nextRequest = m_requests.Dequeue();
                    }
                    if (nextRequest != null && RequestReceived != null)
                    {
                        m_waitingResponse = true;
                        TriggerKeepalive  = false;
                    }
                    else
                    {
                        TriggerKeepalive = true;
                    }
                }
                if (nextRequest != null)
                {
                    RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest));
                }
            }
            ContextTimeoutManager.PulseWaitSend();
        }
Example #3
0
        private void OnRequestLine(object sender, RequestLineEventArgs e)
        {
            m_currentRequest.Method      = e.HttpMethod;
            m_currentRequest.HttpVersion = e.HttpVersion;
            m_currentRequest.UriPath     = e.UriPath;
            m_currentRequest.AddHeader("remote_addr", LocalIPEndPoint.Address.ToString());
            m_currentRequest.AddHeader("remote_port", LocalIPEndPoint.Port.ToString());
            m_currentRequest.ArrivalTS = ContextTimeoutManager.GetTimeStamp();

            FirstRequestLineReceived = true;
            TriggerKeepalive         = false;
            MonitorKeepaliveStartMS  = 0;
            LastActivityTimeMS       = ContextTimeoutManager.EnvironmentTickCount();
        }
Example #4
0
        public bool TrySendResponse(int bytesLimit)
        {
            if (m_currentResponse == null)
            {
                return(false);
            }
            if (m_currentResponse.Sent)
            {
                return(false);
            }

            if (!CanSend())
            {
                return(false);
            }

            LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
            return(m_currentResponse.SendNextAsync(bytesLimit));
        }
Example #5
0
        private void SendAsyncEnd(IAsyncResult res)
        {
            bool didleave = false;

            try
            {
                m_stream.EndWrite(res);
                ContextTimeoutManager.ContextLeaveActiveSend();
                didleave = true;
                m_currentResponse.CheckSendNextAsyncContinue();
            }
            catch (Exception e)
            {
                e.GetHashCode();
                if (m_stream != null)
                {
                    Disconnect(SocketError.NoRecovery);
                }
            }
            if (!didleave)
            {
                ContextTimeoutManager.ContextLeaveActiveSend();
            }
        }
Example #6
0
        public bool Send(byte[] buffer, int offset, int size)
        {
            if (m_stream == null || m_sock == null || !m_sock.Connected)
            {
                return(false);
            }

            if (offset + size > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer.");
            }

            LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();

            bool ok = true;

            ContextTimeoutManager.ContextEnterActiveSend();
            lock (sendLock) // can't have overlaps here
            {
                try
                {
                    m_stream.Write(buffer, offset, size);
                }
                catch
                {
                    ok = false;
                }
            }

            ContextTimeoutManager.ContextLeaveActiveSend();
            if (!ok && m_stream != null)
            {
                Disconnect(SocketError.NoRecovery);
            }
            return(ok);
        }
Example #7
0
        private void OnRequestCompleted(object source, EventArgs args)
        {
            TriggerKeepalive        = false;
            MonitorKeepaliveStartMS = 0;
            FullRequestReceived     = true;
            LastActivityTimeMS      = ContextTimeoutManager.EnvironmentTickCount();

            if (m_maxRequests == 0)
            {
                return;
            }

            if (--m_maxRequests == 0)
            {
                m_currentRequest.Connection = ConnectionType.Close;
            }

            if (m_currentRequest.Uri == null)
            {
                // should not happen
                try
                {
                    Uri uri = new Uri(m_currentRequest.Secure ? "https://" : "http://" + m_currentRequest.UriPath);
                    m_currentRequest.Uri     = uri;
                    m_currentRequest.UriPath = uri.AbsolutePath;
                }
                catch
                {
                    return;
                }
            }

            // load cookies if they exist
            if (m_currentRequest.Headers["cookie"] != null)
            {
                m_currentRequest.SetCookies(new RequestCookies(m_currentRequest.Headers["cookie"]));
            }

            m_currentRequest.Body.Seek(0, SeekOrigin.Begin);

            bool donow = true;

            lock (m_requestsLock)
            {
                if (m_waitingResponse)
                {
                    m_requests.Enqueue(m_currentRequest);
                    donow = false;
                }
                else
                {
                    m_waitingResponse = true;
                }
            }

            if (donow)
            {
                RequestReceived?.Invoke(this, new RequestEventArgs(m_currentRequest));
            }

            m_currentRequest = new HttpRequest(this);
        }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpContextFactory"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="bufferSize">Amount of bytes to read from the incoming socket stream.</param>
 /// <param name="factory">Used to create a request parser.</param>
 public HttpContextFactory(ILogWriter writer)
 {
     m_logWriter = writer;
     ContextTimeoutManager.Start();
 }
Example #9
0
 /// <summary>
 /// Server is shutting down so shut down the factory
 /// </summary>
 public void Shutdown()
 {
     ContextTimeoutManager.Stop();
 }