|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.WebUtilities; |
| 10 | + |
| 11 | +namespace k8s |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// This HttpDelegatingHandler is to rewrite the response and return first line to autorest client |
| 15 | + /// then use WatchExt to create a watch object which interact with the replaced http response to get watch works. |
| 16 | + /// </summary> |
| 17 | + internal class WatcherDelegatingHandler : DelegatingHandler |
| 18 | + { |
| 19 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, |
| 20 | + CancellationToken cancellationToken) |
| 21 | + { |
| 22 | + var originResponse = await base.SendAsync(request, cancellationToken); |
| 23 | + |
| 24 | + if (originResponse.IsSuccessStatusCode) |
| 25 | + { |
| 26 | + var query = QueryHelpers.ParseQuery(request.RequestUri.Query); |
| 27 | + |
| 28 | + if (query.TryGetValue("watch", out var values) && values.Any(v => v == "true")) |
| 29 | + { |
| 30 | + originResponse.Content = new LineSeparatedHttpContent(originResponse.Content); |
| 31 | + } |
| 32 | + } |
| 33 | + return originResponse; |
| 34 | + } |
| 35 | + |
| 36 | + internal class LineSeparatedHttpContent : HttpContent |
| 37 | + { |
| 38 | + private readonly HttpContent _originContent; |
| 39 | + private Stream _originStream; |
| 40 | + |
| 41 | + public LineSeparatedHttpContent(HttpContent originContent) |
| 42 | + { |
| 43 | + _originContent = originContent; |
| 44 | + } |
| 45 | + |
| 46 | + internal PeekableStreamReader StreamReader { get; private set; } |
| 47 | + |
| 48 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) |
| 49 | + { |
| 50 | + _originStream = await _originContent.ReadAsStreamAsync(); |
| 51 | + |
| 52 | + StreamReader = new PeekableStreamReader(_originStream); |
| 53 | + |
| 54 | + var firstLine = await StreamReader.PeekLineAsync(); |
| 55 | + |
| 56 | + var writer = new StreamWriter(stream); |
| 57 | + |
| 58 | +// using (writer) // leave open |
| 59 | + { |
| 60 | + await writer.WriteAsync(firstLine); |
| 61 | + await writer.FlushAsync(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + protected override bool TryComputeLength(out long length) |
| 66 | + { |
| 67 | + length = 0; |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + internal class PeekableStreamReader : StreamReader |
| 72 | + { |
| 73 | + private Queue<string> _buffer; |
| 74 | + public PeekableStreamReader(Stream stream) : base(stream) |
| 75 | + { |
| 76 | + _buffer = new Queue<string>(); |
| 77 | + } |
| 78 | + |
| 79 | + public override string ReadLine() |
| 80 | + { |
| 81 | + if (_buffer.Count > 0) |
| 82 | + { |
| 83 | + return _buffer.Dequeue(); |
| 84 | + } |
| 85 | + return base.ReadLine(); |
| 86 | + } |
| 87 | + public override Task<string> ReadLineAsync() |
| 88 | + { |
| 89 | + if (_buffer.Count > 0) |
| 90 | + { |
| 91 | + return Task.FromResult(_buffer.Dequeue()); |
| 92 | + } |
| 93 | + return base.ReadLineAsync(); |
| 94 | + } |
| 95 | + public async Task<string> PeekLineAsync() |
| 96 | + { |
| 97 | + var line = await ReadLineAsync(); |
| 98 | + _buffer.Enqueue(line); |
| 99 | + return line; |
| 100 | + } |
| 101 | + |
| 102 | + public override int Read() |
| 103 | + { |
| 104 | + throw new NotImplementedException(); |
| 105 | + } |
| 106 | + |
| 107 | + public override int Read(char[] buffer, int index, int count) |
| 108 | + { |
| 109 | + throw new NotImplementedException(); |
| 110 | + } |
| 111 | + public override Task<int> ReadAsync(char[] buffer, int index, int count) |
| 112 | + { |
| 113 | + throw new NotImplementedException(); |
| 114 | + } |
| 115 | + public override int ReadBlock(char[] buffer, int index, int count) |
| 116 | + { |
| 117 | + throw new NotImplementedException(); |
| 118 | + } |
| 119 | + public override Task<int> ReadBlockAsync(char[] buffer, int index, int count) |
| 120 | + { |
| 121 | + throw new NotImplementedException(); |
| 122 | + } |
| 123 | + public override string ReadToEnd() |
| 124 | + { |
| 125 | + throw new NotImplementedException(); |
| 126 | + } |
| 127 | + public override Task<string> ReadToEndAsync() |
| 128 | + { |
| 129 | + throw new NotImplementedException(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments