• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ThreeMammals / Ocelot / 16444211173

22 Jul 2025 12:25PM UTC coverage: 87.672% (-0.3%) from 87.958%
16444211173

Pull #2300

github

web-flow
Merge 8b98a65a3 into c721273e2
Pull Request #2300: #941 Added SseDelegatingHandler

5718 of 6522 relevant lines covered (87.67%)

7509.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.57
src/Ocelot/Requester/SseDelegatingHandler.cs
1
using Microsoft.AspNetCore.Http;
2

3
namespace Ocelot.Requester
4
{
5
    public class SseDelegatingHandler : DelegatingHandler
6
    {
7
        private readonly IHttpContextAccessor _httpContextAccessor;
8

9
        public SseDelegatingHandler(IHttpContextAccessor httpContextAccessor)
1✔
10
        {
11
            _httpContextAccessor = httpContextAccessor;
1✔
12
        }
1✔
13

14
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
15
        {
16
            var httpContext = _httpContextAccessor.HttpContext;
1✔
17

18
            var isSse = request.Headers.Accept.Any(h => h.MediaType == "text/event-stream");
2✔
19
            if (!isSse)
1✔
20
            {
21
                return await base.SendAsync(request, cancellationToken);
1✔
22
            }
23

24
            // Correct overload: only 2 parameters
25
            var response = await base.SendAsync(request, cancellationToken);
×
26

27
            httpContext.Response.StatusCode = (int)response.StatusCode;
×
28
            httpContext.Response.ContentType = "text/event-stream";
×
29

30
            // Forward response headers
31
            foreach (var header in response.Headers)
×
32
            {
33
                httpContext.Response.Headers[header.Key] = header.Value.ToArray();
×
34
            }
35
            foreach (var header in response.Content.Headers)
×
36
            {
37
                httpContext.Response.Headers[header.Key] = header.Value.ToArray();
×
38
            }
39

40
            httpContext.Response.Headers.Remove("transfer-encoding");
×
41

42
            // Stream content
43
            await using var downstreamStream = await response.Content.ReadAsStreamAsync(cancellationToken);
×
44
            using var reader = new StreamReader(downstreamStream, Encoding.UTF8);
×
45

46
            while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
×
47
            {
48
                var line = await reader.ReadLineAsync();
×
49
                if (line != null)
×
50
                {
51
                    var buffer = Encoding.UTF8.GetBytes(line + "\n");
×
52
                    await httpContext.Response.Body.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
×
53
                    await httpContext.Response.Body.FlushAsync(cancellationToken);
×
54
                }
55
            }
56

57
            // Dummy response to complete pipeline
58
            return new HttpResponseMessage(response.StatusCode)
×
59
            {
×
60
                ReasonPhrase = "SSE stream has been forwarded"
×
61
            };
×
62
        }
1✔
63
    }
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc