| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Net; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using WireMock.Validation; |
| | | 8 | | |
| | | 9 | | namespace WireMock.Http |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The tiny http server. |
| | | 13 | | /// </summary> |
| | | 14 | | public class TinyHttpServer |
| | | 15 | | { |
| | | 16 | | private readonly Action<HttpListenerContext, CancellationToken> _httpHandler; |
| | | 17 | | |
| | | 18 | | private readonly HttpListener _listener; |
| | | 19 | | |
| | | 20 | | private readonly CancellationTokenSource _cts; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets a value indicating whether this server is started. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <value> |
| | | 26 | | /// <c>true</c> if this server is started; otherwise, <c>false</c>. |
| | | 27 | | /// </value> |
| | 28 | 28 | | public bool IsStarted { get; private set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the url. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <value> |
| | | 34 | | /// The urls. |
| | | 35 | | /// </value> |
| | | 36 | | [PublicAPI] |
| | 56 | 37 | | public List<Uri> Urls { get; } = new List<Uri>(); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets the ports. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <value> |
| | | 43 | | /// The ports. |
| | | 44 | | /// </value> |
| | | 45 | | [PublicAPI] |
| | 74 | 46 | | public List<int> Ports { get; } = new List<int>(); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="TinyHttpServer"/> class. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="uriPrefixes">The uriPrefixes.</param> |
| | | 52 | | /// <param name="httpHandler">The http handler.</param> |
| | 28 | 53 | | public TinyHttpServer([NotNull] Action<HttpListenerContext, CancellationToken> httpHandler, [NotNull] params str |
| | 28 | 54 | | { |
| | 28 | 55 | | Check.NotNull(httpHandler, nameof(httpHandler)); |
| | 28 | 56 | | Check.NotEmpty(uriPrefixes, nameof(uriPrefixes)); |
| | | 57 | | |
| | 28 | 58 | | _cts = new CancellationTokenSource(); |
| | | 59 | | |
| | 28 | 60 | | _httpHandler = httpHandler; |
| | | 61 | | |
| | | 62 | | // Create a listener. |
| | 28 | 63 | | _listener = new HttpListener(); |
| | 140 | 64 | | foreach (string uriPrefix in uriPrefixes) |
| | 28 | 65 | | { |
| | 28 | 66 | | var uri = new Uri(uriPrefix); |
| | 28 | 67 | | Urls.Add(uri); |
| | 28 | 68 | | Ports.Add(uri.Port); |
| | | 69 | | |
| | 28 | 70 | | _listener.Prefixes.Add(uriPrefix); |
| | 28 | 71 | | } |
| | 28 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Start the server. |
| | | 76 | | /// </summary> |
| | | 77 | | [PublicAPI] |
| | | 78 | | public void Start() |
| | 28 | 79 | | { |
| | 28 | 80 | | _listener.Start(); |
| | | 81 | | |
| | 28 | 82 | | IsStarted = true; |
| | | 83 | | |
| | 28 | 84 | | Task.Run( |
| | 28 | 85 | | async () => |
| | 53 | 86 | | { |
| | 28 | 87 | | //using (_listener) |
| | 53 | 88 | | { |
| | 76 | 89 | | while (!_cts.Token.IsCancellationRequested) |
| | 76 | 90 | | { |
| | 76 | 91 | | HttpListenerContext context = await _listener.GetContextAsync(); |
| | 51 | 92 | | _httpHandler(context, _cts.Token); |
| | 51 | 93 | | } |
| | 28 | 94 | | |
| | 28 | 95 | | _listener.Stop(); |
| | 28 | 96 | | IsStarted = false; |
| | 28 | 97 | | } |
| | 28 | 98 | | }, |
| | 28 | 99 | | _cts.Token); |
| | 28 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Stop the server. |
| | | 104 | | /// </summary> |
| | | 105 | | [PublicAPI] |
| | | 106 | | public void Stop() |
| | 27 | 107 | | { |
| | 27 | 108 | | _listener?.Stop(); |
| | | 109 | | |
| | 27 | 110 | | _cts.Cancel(); |
| | 27 | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |