|
| 1 | +// The Sisk Framework source code |
| 2 | +// Copyright (c) 2024- PROJECT PRINCIPIUM and all Sisk contributors |
| 3 | +// |
| 4 | +// The code below is licensed under the MIT license as |
| 5 | +// of the date of its publication, available at |
| 6 | +// |
| 7 | +// File name: HttpHostClientContext.cs |
| 8 | +// Repository: https://github.com/sisk-http/core |
| 9 | + |
| 10 | +using System.Net; |
| 11 | +using System.Net.Sockets; |
| 12 | +using System.Runtime.CompilerServices; |
| 13 | +using System.Security.Cryptography.X509Certificates; |
| 14 | + |
| 15 | +namespace Sisk.Cadente; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Represents an HTTP connection state that manages the entire client connection. |
| 19 | +/// </summary> |
| 20 | +public sealed class HttpHostClientContext : IDisposable { |
| 21 | + |
| 22 | + private TcpClient client; |
| 23 | + private AutoResetEvent disposedEvent = new AutoResetEvent ( false ); |
| 24 | + private HttpHost baseHost; |
| 25 | + bool ended = false; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets the remote address of the connected client. |
| 29 | + /// </summary> |
| 30 | + public IPEndPoint RemoteAddress { get; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets the remote certificate of the connected client if authenticated with an SSL connection. |
| 34 | + /// </summary> |
| 35 | + public X509Certificate? RemoteCertificate { get; internal set; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets or sets the action handler for incoming HTTP requests within this client context. |
| 39 | + /// </summary> |
| 40 | + public event HttpContextHandler? ContextCreated; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Closes the client connection. |
| 44 | + /// </summary> |
| 45 | + |
| 46 | + // Note: This method shouldn't be called on the dispose. The HttpHost.HandleTcpClient method should |
| 47 | + // dispose the TcpClient instead. |
| 48 | + public void Close () { |
| 49 | + this.client.Close (); |
| 50 | + this.ended = true; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Blocks the current thread until this <see cref="HttpHostClientContext"/> connection |
| 55 | + /// is terminated. |
| 56 | + /// </summary> |
| 57 | + public bool WaitUntilClose () { |
| 58 | + return this.disposedEvent.WaitOne (); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Blocks the current thread until this <see cref="HttpHostClientContext"/> connection |
| 63 | + /// is terminated, using a maximum timeout interval. |
| 64 | + /// </summary> |
| 65 | + /// <param name="timeout">Defines the maximum time waiting for the connection termination.</param> |
| 66 | + /// <param name="closeOnTimeout">Defines if this client context should terminate the client connection if the timeout is reached.</param> |
| 67 | + public bool WaitUntilClose ( TimeSpan timeout, bool closeOnTimeout = false ) { |
| 68 | + var b = this.disposedEvent.WaitOne ( timeout ); |
| 69 | + if (!b && closeOnTimeout) |
| 70 | + this.Close (); |
| 71 | + |
| 72 | + return b; |
| 73 | + } |
| 74 | + |
| 75 | + internal HttpHostClientContext ( TcpClient client, HttpHost baseHost, IPEndPoint remoteAddress ) { |
| 76 | + this.baseHost = baseHost; |
| 77 | + this.RemoteAddress = remoteAddress; |
| 78 | + this.client = client; |
| 79 | + } |
| 80 | + |
| 81 | + [MethodImpl ( MethodImplOptions.AggressiveInlining )] |
| 82 | + internal async ValueTask InvokeContextCreated ( HttpHostContext context ) { |
| 83 | + if (this.ended) |
| 84 | + return; |
| 85 | + if (ContextCreated != null) |
| 86 | + await ContextCreated.Invoke ( this.baseHost, context ); |
| 87 | + } |
| 88 | + |
| 89 | + /// <inheritdoc/> |
| 90 | + public void Dispose () { |
| 91 | + this.ended = true; |
| 92 | + this.disposedEvent.Set (); |
| 93 | + this.disposedEvent.Dispose (); |
| 94 | + } |
| 95 | +} |
0 commit comments