2018-09-27 10:50:39 -07:00
|
|
|
using System;
|
2018-07-09 23:51:49 +10:00
|
|
|
using System.Collections.Generic;
|
2018-09-27 10:50:39 -07:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace k8s
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This HttpDelegatingHandler is to rewrite the response and return first line to autorest client
|
|
|
|
|
/// then use WatchExt to create a watch object which interact with the replaced http response to get watch works.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class WatcherDelegatingHandler : DelegatingHandler
|
|
|
|
|
{
|
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-03-19 04:54:44 +00:00
|
|
|
var originResponse = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2020-04-20 09:33:40 -07:00
|
|
|
if (originResponse.IsSuccessStatusCode && request.Method == HttpMethod.Get) // all watches are GETs, so we can ignore others
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2020-04-20 09:33:40 -07:00
|
|
|
string query = request.RequestUri.Query;
|
|
|
|
|
int index = query.IndexOf("watch=true");
|
2020-04-22 12:15:45 -07:00
|
|
|
if (index > 0 && (query[index - 1] == '&' || query[index - 1] == '?'))
|
2018-07-09 23:51:49 +10:00
|
|
|
{
|
2020-03-22 21:18:45 -07:00
|
|
|
originResponse.Content = new LineSeparatedHttpContent(originResponse.Content, cancellationToken);
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
return originResponse;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
internal class CancelableStream : Stream
|
|
|
|
|
{
|
|
|
|
|
private readonly Stream _innerStream;
|
|
|
|
|
private readonly CancellationToken _cancellationToken;
|
|
|
|
|
|
|
|
|
|
public CancelableStream(Stream innerStream, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_innerStream = innerStream;
|
|
|
|
|
_cancellationToken = cancellationToken;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
public override void Flush() =>
|
|
|
|
|
_innerStream.FlushAsync(_cancellationToken).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
public override async Task FlushAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
|
|
|
|
|
{
|
|
|
|
|
await _innerStream.FlushAsync(cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) =>
|
|
|
|
|
_innerStream.ReadAsync(buffer, offset, count, _cancellationToken).GetAwaiter().GetResult();
|
|
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
|
|
|
|
|
{
|
|
|
|
|
return await _innerStream.ReadAsync(buffer, offset, count, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value) => _innerStream.SetLength(value);
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) =>
|
|
|
|
|
_innerStream.WriteAsync(buffer, offset, count, _cancellationToken).GetAwaiter().GetResult();
|
|
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
|
|
|
|
|
{
|
|
|
|
|
await _innerStream.WriteAsync(buffer, offset, count, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
public override bool CanRead => _innerStream.CanRead;
|
|
|
|
|
|
|
|
|
|
public override bool CanSeek => _innerStream.CanSeek;
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite => _innerStream.CanWrite;
|
|
|
|
|
|
|
|
|
|
public override long Length => _innerStream.Length;
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => _innerStream.Position;
|
|
|
|
|
set => _innerStream.Position = value;
|
|
|
|
|
}
|
2020-04-01 03:45:27 +01:00
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
_innerStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LinkedCancellationTokenSource CreateCancellationTokenSource(CancellationToken userCancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return new LinkedCancellationTokenSource(_cancellationToken, userCancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly struct LinkedCancellationTokenSource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource;
|
|
|
|
|
|
|
|
|
|
public LinkedCancellationTokenSource(CancellationToken token1, CancellationToken token2)
|
|
|
|
|
{
|
|
|
|
|
if (token1.CanBeCanceled && token2.CanBeCanceled)
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token1, token2);
|
|
|
|
|
Token = _cancellationTokenSource.Token;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource = null;
|
|
|
|
|
Token = token1.CanBeCanceled ? token1 : token2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CancellationToken Token { get; }
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
internal class LineSeparatedHttpContent : HttpContent
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpContent _originContent;
|
2020-03-22 21:18:45 -07:00
|
|
|
private readonly CancellationToken _cancellationToken;
|
2018-09-27 10:50:39 -07:00
|
|
|
private Stream _originStream;
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
public LineSeparatedHttpContent(HttpContent originContent, CancellationToken cancellationToken)
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
|
|
|
|
_originContent = originContent;
|
2020-03-22 21:18:45 -07:00
|
|
|
_cancellationToken = cancellationToken;
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal PeekableStreamReader StreamReader { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
|
|
|
|
|
{
|
2020-03-19 04:54:44 +00:00
|
|
|
_originStream = await _originContent.ReadAsStreamAsync().ConfigureAwait(false);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
StreamReader = new PeekableStreamReader(new CancelableStream(_originStream, _cancellationToken));
|
2018-07-09 23:51:49 +10:00
|
|
|
|
2020-03-19 04:54:44 +00:00
|
|
|
var firstLine = await StreamReader.PeekLineAsync().ConfigureAwait(false);
|
2018-07-09 23:51:49 +10:00
|
|
|
|
|
|
|
|
var writer = new StreamWriter(stream);
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
await writer.WriteAsync(firstLine).ConfigureAwait(false);
|
|
|
|
|
await writer.FlushAsync().ConfigureAwait(false);
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool TryComputeLength(out long length)
|
|
|
|
|
{
|
|
|
|
|
length = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-09 23:51:49 +10:00
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
internal class PeekableStreamReader : TextReader
|
2018-07-09 23:51:49 +10:00
|
|
|
{
|
2020-04-01 03:45:27 +01:00
|
|
|
private readonly Queue<string> _buffer;
|
|
|
|
|
private readonly StreamReader _inner;
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
public PeekableStreamReader(Stream stream)
|
2018-07-09 23:51:49 +10:00
|
|
|
{
|
|
|
|
|
_buffer = new Queue<string>();
|
2020-04-01 03:45:27 +01:00
|
|
|
_inner = new StreamReader(stream);
|
2018-07-09 23:51:49 +10:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
public override string ReadLine() => throw new NotImplementedException();
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2018-07-09 23:51:49 +10:00
|
|
|
public override Task<string> ReadLineAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_buffer.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(_buffer.Dequeue());
|
|
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2020-04-01 03:45:27 +01:00
|
|
|
return _inner.ReadLineAsync();
|
2018-07-09 23:51:49 +10:00
|
|
|
}
|
2020-03-22 21:18:45 -07:00
|
|
|
|
2018-07-09 23:51:49 +10:00
|
|
|
public async Task<string> PeekLineAsync()
|
|
|
|
|
{
|
2020-03-19 04:54:44 +00:00
|
|
|
var line = await ReadLineAsync().ConfigureAwait(false);
|
2018-07-09 23:51:49 +10:00
|
|
|
_buffer.Enqueue(line);
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
public override int Read() => throw new NotImplementedException();
|
2018-07-09 23:51:49 +10:00
|
|
|
|
2020-03-22 21:18:45 -07:00
|
|
|
public override int Read(char[] buffer, int index, int count) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public override Task<int> ReadAsync(char[] buffer, int index, int count) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public override int ReadBlock(char[] buffer, int index, int count) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public override Task<int> ReadBlockAsync(char[] buffer, int index, int count) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public override string ReadToEnd() => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public override Task<string> ReadToEndAsync() => throw new NotImplementedException();
|
2020-04-01 03:45:27 +01:00
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
_inner.Dispose();
|
|
|
|
|
}
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
2018-07-09 23:51:49 +10:00
|
|
|
}
|
|
|
|
|
}
|