Skip to content

Commit 816844d

Browse files
Merge pull request #79 from SergeyGulik/sample_continues_on_serverstop
Sample stream handler continues to run ignoring http server stop.
2 parents 3b3965f + c2aa1ef commit 816844d

24 files changed

Lines changed: 137 additions & 81 deletions

libraries/MTConnect.NET-HTTP/Ceen/Common/Interfaces.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,14 @@ internal interface INamedModule
418418
/// Interface for implementing a routing provider
419419
/// </summary>
420420
internal interface IRouter
421-
{
422-
/// <summary>
423-
/// Process the request for the specified context.
424-
/// </summary>
425-
/// <param name="context">The context to use.</param>
426-
/// <returns>A value indicating if the request is now processed</returns>
427-
Task<bool> Process(IHttpContext context);
421+
{
422+
/// <summary>
423+
/// Process the request for the specified context.
424+
/// </summary>
425+
/// <param name="context">The context to use.</param>
426+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
427+
/// <returns>A value indicating if the request is now processed</returns>
428+
Task<bool> Process(IHttpContext context, CancellationToken cancellationToken);
428429
}
429430

430431
/// <summary>
@@ -459,8 +460,9 @@ internal interface IHttpModule
459460
/// Process the request for the specified context.
460461
/// </summary>
461462
/// <param name="context">The context to use.</param>
463+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
462464
/// <returns>A value indicating if the request is now processed</returns>
463-
Task<bool> HandleAsync(IHttpContext context);
465+
Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken);
464466
}
465467

466468
/// <summary>

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/CORSHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
using Ceen;
@@ -61,8 +62,9 @@ public void AfterConfigure()
6162
/// Handles OPTIONS requests
6263
/// </summary>
6364
/// <param name="context">The request context</param>
65+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
6466
/// <returns>A value indicating if the request was handled</returns>
65-
public Task<bool> HandleAsync(IHttpContext context)
67+
public Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
6668
{
6769
var preflight = context.Request.Method == "OPTIONS";
6870
var origin = context.Request.Headers["Origin"];

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/ClearRequestStateHandler.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace Ceen.Httpd.Handler
@@ -8,12 +9,13 @@ namespace Ceen.Httpd.Handler
89
/// </summary>
910
internal class ClearRequestStateHandler : IHttpModule
1011
{
11-
/// <summary>
12-
/// Handles the request.
13-
/// </summary>
14-
/// <returns>The awaitable task.</returns>
15-
/// <param name="context">The http context.</param>
16-
public Task<bool> HandleAsync(IHttpContext context)
12+
/// <summary>
13+
/// Handles the request.
14+
/// </summary>
15+
/// <returns>The awaitable task.</returns>
16+
/// <param name="context">The http context.</param>
17+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
18+
public Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
1719
{
1820
context.Request.RequestState.Clear();
1921
return Task.FromResult(false);

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/FileHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ protected virtual async Task<bool> ServeRequest(string path, string mimetype, IH
692692
/// </summary>
693693
/// <returns>The awaitable task.</returns>
694694
/// <param name="context">The http context.</param>
695-
public virtual async Task<bool> HandleAsync(IHttpContext context)
695+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
696+
public virtual async Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
696697
{
697698
if (!string.Equals(context.Request.Method, "GET", StringComparison.Ordinal) && !string.Equals(context.Request.Method, "HEAD", StringComparison.Ordinal))
698699
{

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/FileMirrorHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ protected virtual string BuildUrl(IHttpContext context)
113113
/// </summary>
114114
/// <returns>An awaitable task.</returns>
115115
/// <param name="context">The request context.</param>
116-
public override async Task<bool> HandleAsync(IHttpContext context)
116+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
117+
public override async Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
117118
{
118119
if (!string.Equals(context.Request.Method, "GET", StringComparison.Ordinal) && !string.Equals(context.Request.Method, "HEAD", StringComparison.Ordinal))
119120
{
@@ -183,7 +184,7 @@ public override async Task<bool> HandleAsync(IHttpContext context)
183184
// We hit a race here where the file is downloaded and exists,
184185
// but the active tables have been cleared just after we checked the filecache
185186
if (m_filecache.TryGetValue(localpath, out var v))
186-
return await base.HandleAsync(context);
187+
return await base.HandleAsync(context, cancellationToken);
187188
}
188189

189190
// Download if we are the first process requesting it
@@ -195,7 +196,7 @@ public override async Task<bool> HandleAsync(IHttpContext context)
195196

196197
}
197198

198-
return await base.HandleAsync(context);
199+
return await base.HandleAsync(context, cancellationToken);
199200
}
200201
/// <summary>
201202
/// The predicate method for expiring a 404 entry

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/FunctionHandler.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace Ceen.Httpd.Handler
@@ -22,14 +23,15 @@ public FunctionHandler(HttpHandlerDelegate handler)
2223
m_handler = handler;
2324
}
2425

25-
#region IHttpModule implementation
26+
#region IHttpModule implementation
2627

27-
/// <summary>
28-
/// Handles the operation asynchronously.
29-
/// </summary>
30-
/// <returns>The awaitable task.</returns>
31-
/// <param name="context">The request context.</param>
32-
public Task<bool> HandleAsync(IHttpContext context)
28+
/// <summary>
29+
/// Handles the operation asynchronously.
30+
/// </summary>
31+
/// <returns>The awaitable task.</returns>
32+
/// <param name="context">The request context.</param>
33+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
34+
public Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
3335
{
3436
return m_handler(context);
3537
}

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/RedirectHandler.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace Ceen.Httpd.Handler
@@ -39,12 +40,13 @@ public RedirectHandler() { }
3940
/// <param name="target">The redirect target url.</param>
4041
public RedirectHandler(string target) { RedirectTarget = target; }
4142

42-
/// <summary>
43-
/// Handles the request by sending a redirect
44-
/// </summary>
45-
/// <returns>The awaitable task.</returns>
46-
/// <param name="context">The http context.</param>
47-
public Task<bool> HandleAsync(IHttpContext context)
43+
/// <summary>
44+
/// Handles the request by sending a redirect
45+
/// </summary>
46+
/// <returns>The awaitable task.</returns>
47+
/// <param name="context">The http context.</param>
48+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
49+
public Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
4850
{
4951
if (InternalRedirect)
5052
{

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/SessionHandler.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace Ceen.Httpd.Handler
@@ -32,12 +33,13 @@ internal class SessionHandler : IHttpModule
3233
/// </summary>
3334
public string SessionCookieSameSite { get; set; } = "Strict";
3435

35-
/// <summary>
36-
/// Handles the request
37-
/// </summary>
38-
/// <returns>The awaitable task.</returns>
39-
/// <param name="context">The requests context.</param>
40-
public async Task<bool> HandleAsync(IHttpContext context)
36+
/// <summary>
37+
/// Handles the request
38+
/// </summary>
39+
/// <returns>The awaitable task.</returns>
40+
/// <param name="context">The requests context.</param>
41+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
42+
public async Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
4143
{
4244
if (context.Session != null)
4345
return false;

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/SimpleProxyHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Net.Http;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Ceen;
78

@@ -47,8 +48,9 @@ protected string RewriteUrl(IHttpContext context)
4748
/// Handles a proxy request
4849
/// </summary>
4950
/// <param name="context">The request content</param>
51+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
5052
/// <returns><c>true</c> if the request was handled; <c>false</c> otherwise</returns>
51-
public async Task<bool> HandleAsync(IHttpContext context)
53+
public async Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
5254
{
5355
var targeturl = RewriteUrl(context);
5456
if (targeturl == null)

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/StaticHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace Ceen.Httpd.Handler
@@ -28,7 +29,8 @@ internal class StaticHandler : IHttpModule
2829
/// </summary>
2930
/// <returns>The awaitable task.</returns>
3031
/// <param name="context">The http context.</param>
31-
public Task<bool> HandleAsync(IHttpContext context)
32+
/// <param name="cancellationToken">The token indicating to stop handling.</param>
33+
public Task<bool> HandleAsync(IHttpContext context, CancellationToken cancellationToken)
3234
{
3335
context.Response.StatusCode = StatusCode;
3436
context.Response.StatusMessage = StatusMessage;

0 commit comments

Comments
 (0)