| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net.Http; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | internal abstract class XmlStreamedByteStreamReader : XmlByteStreamReader |
| | | 13 | | { |
| | | 14 | | protected XmlStreamedByteStreamReader(XmlDictionaryReaderQuotas quotas) |
| | 7 | 15 | | : base(quotas) |
| | | 16 | | { |
| | 7 | 17 | | } |
| | | 18 | | |
| | 7 | 19 | | public static XmlStreamedByteStreamReader Create(Stream stream, XmlDictionaryReaderQuotas quotas) => new StreamX |
| | | 20 | | |
| | 0 | 21 | | public static XmlStreamedByteStreamReader Create(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuota |
| | | 22 | | |
| | 0 | 23 | | public static XmlStreamedByteStreamReader Create(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuo |
| | | 24 | | |
| | | 25 | | protected override void OnClose() |
| | | 26 | | { |
| | 3 | 27 | | ReleaseStream(); |
| | 3 | 28 | | base.OnClose(); |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | public override int ReadContentAsBase64(byte[] buffer, int index, int count) |
| | | 32 | | { |
| | 3 | 33 | | EnsureInContent(); |
| | 3 | 34 | | ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, true); |
| | | 35 | | |
| | 3 | 36 | | if (count == 0) |
| | | 37 | | { |
| | 0 | 38 | | return 0; |
| | | 39 | | } |
| | | 40 | | |
| | 3 | 41 | | Stream stream = GetStream(); |
| | 3 | 42 | | int numBytesRead = stream.Read(buffer, index, count); |
| | 3 | 43 | | if (numBytesRead == 0) |
| | | 44 | | { |
| | 0 | 45 | | position = ReaderPosition.EndElement; |
| | | 46 | | } |
| | | 47 | | |
| | 3 | 48 | | return numBytesRead; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | protected override byte[] OnToByteArray() |
| | | 52 | | { |
| | 1 | 53 | | throw Fx.Exception.AsError( |
| | 1 | 54 | | new InvalidOperationException(SR.GetByteArrayFromStreamContentNotAllowed)); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | protected override Stream OnToStream() |
| | | 58 | | { |
| | 1 | 59 | | Stream result = GetStream(); |
| | | 60 | | |
| | | 61 | | Fx.Assert(result != null, "The inner stream is null. Please check if the reader is closed or the ToStream me |
| | | 62 | | |
| | 1 | 63 | | ReleaseStream(); |
| | 1 | 64 | | return result; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | protected abstract Stream GetStream(); |
| | | 68 | | |
| | | 69 | | protected abstract void ReleaseStream(); |
| | | 70 | | |
| | | 71 | | public override bool TryGetBase64ContentLength(out int length) |
| | | 72 | | { |
| | | 73 | | // in ByteStream encoder, we're not concerned about individual xml nodes |
| | | 74 | | // therefore we can just return the entire length of the stream |
| | 3 | 75 | | Stream stream = GetStream(); |
| | 3 | 76 | | if (!IsClosed && stream.CanSeek) |
| | | 77 | | { |
| | 3 | 78 | | long streamLength = stream.Length; |
| | 3 | 79 | | if (streamLength <= int.MaxValue) |
| | | 80 | | { |
| | 3 | 81 | | length = (int)streamLength; |
| | 3 | 82 | | return true; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | length = -1; |
| | 0 | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal class StreamXmlStreamedByteStreamReader : XmlStreamedByteStreamReader |
| | | 91 | | { |
| | | 92 | | private Stream _stream; |
| | | 93 | | |
| | | 94 | | public StreamXmlStreamedByteStreamReader(Stream stream, XmlDictionaryReaderQuotas quotas) |
| | 7 | 95 | | : base(quotas) |
| | | 96 | | { |
| | | 97 | | Fx.Assert(stream != null, "The 'stream' parameter should not be null."); |
| | | 98 | | |
| | 7 | 99 | | _stream = stream; |
| | 7 | 100 | | } |
| | | 101 | | |
| | 7 | 102 | | protected override Stream GetStream() => _stream; |
| | | 103 | | |
| | | 104 | | protected override void ReleaseStream() |
| | | 105 | | { |
| | 4 | 106 | | _stream = null; |
| | 4 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | internal class HttpRequestMessageStreamedBodyReader : XmlStreamedByteStreamReader |
| | | 111 | | { |
| | | 112 | | private HttpRequestMessage _httpRequestMessage; |
| | | 113 | | |
| | | 114 | | public HttpRequestMessageStreamedBodyReader(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas |
| | 0 | 115 | | : base(quotas) |
| | | 116 | | { |
| | | 117 | | Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should not be null."); |
| | | 118 | | |
| | 0 | 119 | | _httpRequestMessage = httpRequestMessage; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | protected override Stream GetStream() |
| | | 123 | | { |
| | 0 | 124 | | if (_httpRequestMessage == null) |
| | | 125 | | { |
| | 0 | 126 | | return null; |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | HttpContent content = _httpRequestMessage.Content; |
| | 0 | 130 | | if (content != null) |
| | | 131 | | { |
| | 0 | 132 | | return content.ReadAsStreamAsync().Result; |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | return new MemoryStream(Array.Empty<byte>()); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | protected override void ReleaseStream() |
| | | 139 | | { |
| | 0 | 140 | | _httpRequestMessage = null; |
| | 0 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | internal class HttpResponseMessageStreamedBodyReader : XmlStreamedByteStreamReader |
| | | 145 | | { |
| | | 146 | | private HttpResponseMessage _httpResponseMessage; |
| | | 147 | | |
| | | 148 | | public HttpResponseMessageStreamedBodyReader(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuo |
| | 0 | 149 | | : base(quotas) |
| | | 150 | | { |
| | | 151 | | Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should not be null."); |
| | | 152 | | |
| | 0 | 153 | | _httpResponseMessage = httpResponseMessage; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | protected override Stream GetStream() |
| | | 157 | | { |
| | 0 | 158 | | if (_httpResponseMessage == null) |
| | | 159 | | { |
| | 0 | 160 | | return null; |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | HttpContent content = _httpResponseMessage.Content; |
| | 0 | 164 | | if (content != null) |
| | | 165 | | { |
| | 0 | 166 | | return content.ReadAsStreamAsync().Result; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | return new MemoryStream(Array.Empty<byte>()); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | protected override void ReleaseStream() |
| | | 173 | | { |
| | 0 | 174 | | _httpResponseMessage = null; |
| | 0 | 175 | | } |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | } |