/// <summary> /// A request was received from the parser. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnRequest(object sender, FactoryRequestEventArgs e) { _context = this; Response = HttpFactory.Current.Get <IResponse>(this, e.Request); _logger.Debug("Received '" + e.Request.Method + " " + e.Request.Uri.PathAndQuery + "' from " + Socket.RemoteEndPoint); LastAction = DateTime.Now; // keep alive. if (e.Request.Connection != null && e.Request.Connection.Type == ConnectionType.KeepAlive) { Response.Add(new StringHeader("Keep-Alive", "timeout=5, max=" + HTTPContextManager.MAX_KEEPALIVE)); } Request = e.Request; CurrentRequestReceived(this, new RequestEventArgs(this, e.Request, Response)); RequestReceived(this, new RequestEventArgs(this, e.Request, Response)); if (Response.Connection.Type == ConnectionType.KeepAlive) { HTTPContextManager.Register(this); } RequestCompleted(this, new RequestEventArgs(this, e.Request, Response)); CurrentRequestCompleted(this, new RequestEventArgs(this, e.Request, Response)); }
/// <summary> /// Close and release socket. /// </summary> private void Close() { lock (this) { if (Socket == null) { return; } try { HTTPContextManager.Unregister(this); Socket.Disconnect(true); Socket.Close(); Socket = null; Stream.Close(); Stream.Dispose(); Stream = null; MessageFactoryContext.RequestCompleted -= OnRequest; MessageFactoryContext.ContinueResponseRequested -= On100Continue; MessageFactoryContext.Reset(); } catch (Exception err) { _logger.Warning("Failed to close context properly.", err); } } Disconnected(this, EventArgs.Empty); }
private void OnConnectionTimeout(object state) { HTTPContextManager.Unregister(this); _logger.Info("Keep-Alive timeout"); Disconnect(); }
/// <summary> /// Disconnect context. /// </summary> public void Disconnect() { HTTPContextManager.Unregister(this); Close(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpFactory"/> class. /// </summary> public HttpFactory() { HTTPContextManager.Start(); _current = this; AddDefaultCreators(); }