| | | 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.Xml; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | internal abstract class XmlByteStreamReader : XmlDictionaryReader |
| | | 12 | | { |
| | | 13 | | private string _base64StringValue; |
| | | 14 | | private NameTable _nameTable; |
| | | 15 | | private bool _readBase64AsString; |
| | | 16 | | |
| | | 17 | | protected ReaderPosition position; |
| | | 18 | | protected XmlDictionaryReaderQuotas quotas; |
| | | 19 | | |
| | 22 | 20 | | protected XmlByteStreamReader(XmlDictionaryReaderQuotas quotas) |
| | | 21 | | { |
| | 22 | 22 | | this.quotas = quotas; |
| | 22 | 23 | | position = ReaderPosition.None; |
| | 22 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | public override int AttributeCount => 0; |
| | | 27 | | |
| | 0 | 28 | | public override string BaseURI => string.Empty; |
| | | 29 | | |
| | 0 | 30 | | public override bool CanCanonicalize => false; |
| | | 31 | | |
| | 0 | 32 | | public override bool CanReadBinaryContent => true; |
| | | 33 | | |
| | 0 | 34 | | public override bool CanReadValueChunk => false; |
| | | 35 | | |
| | 0 | 36 | | public override bool CanResolveEntity => false; |
| | | 37 | | |
| | 0 | 38 | | public override int Depth => position == ReaderPosition.Content ? 1 : 0; |
| | | 39 | | |
| | 0 | 40 | | public override bool EOF => position == ReaderPosition.EOF; |
| | | 41 | | |
| | 0 | 42 | | public override bool HasAttributes => false; |
| | | 43 | | |
| | 0 | 44 | | public override bool HasValue => position == ReaderPosition.Content; |
| | | 45 | | |
| | 0 | 46 | | public override bool IsDefault => false; |
| | | 47 | | |
| | 0 | 48 | | public override bool IsEmptyElement => false; |
| | | 49 | | |
| | 1 | 50 | | public override string LocalName => position == ReaderPosition.StartElement ? ByteStreamMessageUtility.StreamEle |
| | | 51 | | |
| | | 52 | | public override void MoveToStartElement() |
| | | 53 | | { |
| | 0 | 54 | | base.MoveToStartElement(); |
| | 0 | 55 | | position = ReaderPosition.StartElement; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public override XmlNameTable NameTable |
| | | 59 | | { |
| | | 60 | | get |
| | | 61 | | { |
| | 0 | 62 | | if (_nameTable == null) |
| | | 63 | | { |
| | 0 | 64 | | _nameTable = new NameTable(); |
| | 0 | 65 | | _nameTable.Add(ByteStreamMessageUtility.StreamElementName); |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return _nameTable; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | 1 | 72 | | public override string NamespaceURI => string.Empty; |
| | | 73 | | |
| | | 74 | | public override XmlNodeType NodeType |
| | | 75 | | { |
| | | 76 | | get |
| | | 77 | | { |
| | 77 | 78 | | switch (position) |
| | | 79 | | { |
| | | 80 | | case ReaderPosition.StartElement: |
| | 42 | 81 | | return XmlNodeType.Element; |
| | | 82 | | case ReaderPosition.Content: |
| | 11 | 83 | | return XmlNodeType.Text; |
| | | 84 | | case ReaderPosition.EndElement: |
| | 7 | 85 | | return XmlNodeType.EndElement; |
| | | 86 | | default: |
| | | 87 | | // and StreamPosition.EOF |
| | 17 | 88 | | return XmlNodeType.None; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | public override string Prefix => string.Empty; |
| | | 94 | | |
| | 9 | 95 | | public override XmlDictionaryReaderQuotas Quotas => quotas; |
| | | 96 | | |
| | | 97 | | public override ReadState ReadState |
| | | 98 | | { |
| | | 99 | | get |
| | | 100 | | { |
| | 1 | 101 | | switch (position) |
| | | 102 | | { |
| | | 103 | | case ReaderPosition.None: |
| | 0 | 104 | | return ReadState.Initial; |
| | | 105 | | case ReaderPosition.StartElement: |
| | | 106 | | case ReaderPosition.Content: |
| | | 107 | | case ReaderPosition.EndElement: |
| | 0 | 108 | | return ReadState.Interactive; |
| | | 109 | | case ReaderPosition.EOF: |
| | 1 | 110 | | return ReadState.Closed; |
| | | 111 | | default: |
| | | 112 | | Fx.Assert("Unknown ReadState hit in XmlByteStreamReader"); |
| | 0 | 113 | | return ReadState.Error; |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public override string Value |
| | | 119 | | { |
| | | 120 | | get |
| | | 121 | | { |
| | 7 | 122 | | switch (position) |
| | | 123 | | { |
| | | 124 | | case ReaderPosition.Content: |
| | 7 | 125 | | if (!_readBase64AsString) |
| | | 126 | | { |
| | 7 | 127 | | _base64StringValue = Convert.ToBase64String(ReadContentAsBase64()); |
| | 7 | 128 | | _readBase64AsString = true; |
| | | 129 | | } |
| | 7 | 130 | | return _base64StringValue; |
| | | 131 | | |
| | | 132 | | default: |
| | 0 | 133 | | return string.Empty; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public override void Close() |
| | | 139 | | { |
| | 12 | 140 | | if (!IsClosed) |
| | | 141 | | { |
| | | 142 | | try |
| | | 143 | | { |
| | 12 | 144 | | OnClose(); |
| | 12 | 145 | | } |
| | | 146 | | finally |
| | | 147 | | { |
| | 12 | 148 | | position = ReaderPosition.EOF; |
| | 12 | 149 | | IsClosed = true; |
| | 12 | 150 | | } |
| | | 151 | | } |
| | 12 | 152 | | } |
| | | 153 | | |
| | 39 | 154 | | protected bool IsClosed { get; private set; } |
| | | 155 | | |
| | | 156 | | protected virtual void OnClose() |
| | | 157 | | { |
| | 12 | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | public override string GetAttribute(int i) => throw Fx.Exception.ArgumentOutOfRange(nameof(i), i, SR.ArgumentNot |
| | | 161 | | |
| | 0 | 162 | | public override string GetAttribute(string name, string namespaceURI) => null; |
| | | 163 | | |
| | 0 | 164 | | public override string GetAttribute(string name) => null; |
| | | 165 | | |
| | | 166 | | public override string LookupNamespace(string prefix) |
| | | 167 | | { |
| | 0 | 168 | | if (prefix == string.Empty) |
| | | 169 | | { |
| | 0 | 170 | | return string.Empty; |
| | | 171 | | } |
| | 0 | 172 | | else if (prefix == "xml") |
| | | 173 | | { |
| | 0 | 174 | | return ByteStreamMessageUtility.XmlNamespace; |
| | | 175 | | } |
| | 0 | 176 | | else if (prefix == "xmlns") |
| | | 177 | | { |
| | 0 | 178 | | return ByteStreamMessageUtility.XmlNamespaceNamespace; |
| | | 179 | | } |
| | | 180 | | else |
| | | 181 | | { |
| | 0 | 182 | | return null; |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | public override bool MoveToAttribute(string name, string ns) => false; |
| | | 187 | | |
| | 0 | 188 | | public override bool MoveToAttribute(string name) => false; |
| | | 189 | | |
| | | 190 | | public override bool MoveToElement() |
| | | 191 | | { |
| | 0 | 192 | | if (position == ReaderPosition.None) |
| | | 193 | | { |
| | 0 | 194 | | position = ReaderPosition.StartElement; |
| | 0 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | return false; |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | public override bool MoveToFirstAttribute() => false; |
| | | 202 | | |
| | 0 | 203 | | public override bool MoveToNextAttribute() => false; |
| | | 204 | | |
| | | 205 | | public override bool Read() |
| | | 206 | | { |
| | 47 | 207 | | switch (position) |
| | | 208 | | { |
| | | 209 | | case ReaderPosition.None: |
| | 17 | 210 | | position = ReaderPosition.StartElement; |
| | 17 | 211 | | return true; |
| | | 212 | | case ReaderPosition.StartElement: |
| | 8 | 213 | | position = ReaderPosition.Content; |
| | 8 | 214 | | return true; |
| | | 215 | | case ReaderPosition.Content: |
| | 7 | 216 | | position = ReaderPosition.EndElement; |
| | 7 | 217 | | return true; |
| | | 218 | | case ReaderPosition.EndElement: |
| | 8 | 219 | | position = ReaderPosition.EOF; |
| | 8 | 220 | | return false; |
| | | 221 | | case ReaderPosition.EOF: |
| | 7 | 222 | | return false; |
| | | 223 | | default: |
| | | 224 | | // we should never get here |
| | | 225 | | // it means we managed to get into some unknown position in the stream |
| | | 226 | | Fx.Assert(false, "Unknown read position in XmlByteStreamReader"); |
| | 0 | 227 | | return false; |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | public override bool ReadAttributeValue() => false; |
| | | 232 | | |
| | | 233 | | public override abstract int ReadContentAsBase64(byte[] buffer, int index, int count); |
| | | 234 | | |
| | 0 | 235 | | public override int ReadContentAsBinHex(byte[] buffer, int index, int count) => throw Fx.Exception.AsError(new N |
| | | 236 | | |
| | 0 | 237 | | public override void ResolveEntity() => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 238 | | |
| | | 239 | | public byte[] ToByteArray() |
| | | 240 | | { |
| | 5 | 241 | | if (IsClosed) |
| | | 242 | | { |
| | 0 | 243 | | throw Fx.Exception.AsError( |
| | 0 | 244 | | new ObjectDisposedException(SR.XmlReaderClosed)); |
| | | 245 | | } |
| | | 246 | | |
| | 5 | 247 | | return OnToByteArray(); |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | protected abstract byte[] OnToByteArray(); |
| | | 251 | | |
| | | 252 | | public Stream ToStream() |
| | | 253 | | { |
| | 3 | 254 | | if (IsClosed) |
| | | 255 | | { |
| | 0 | 256 | | throw Fx.Exception.AsError( |
| | 0 | 257 | | new ObjectDisposedException(SR.XmlReaderClosed)); |
| | | 258 | | } |
| | | 259 | | |
| | 3 | 260 | | return OnToStream(); |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | protected abstract Stream OnToStream(); |
| | | 264 | | |
| | | 265 | | protected void EnsureInContent() |
| | | 266 | | { |
| | | 267 | | // This method is only being called from XmlByteStreamReader.ReadContentAsBase64. |
| | | 268 | | // We don't block if the position is None or StartElement since we want our XmlByteStreamReader |
| | | 269 | | // to be a little bit smarter when people just to access the content of the ByteStreamMessage. |
| | 9 | 270 | | if (position == ReaderPosition.EndElement |
| | 9 | 271 | | || position == ReaderPosition.EOF) |
| | | 272 | | { |
| | 0 | 273 | | throw Fx.Exception.AsError( |
| | 0 | 274 | | new InvalidOperationException(SR.Format(SR.ByteStreamReaderNotInByteStream, ByteStreamMessageUtility |
| | | 275 | | } |
| | | 276 | | else |
| | | 277 | | { |
| | 9 | 278 | | position = ReaderPosition.Content; |
| | | 279 | | } |
| | 9 | 280 | | } |
| | | 281 | | |
| | | 282 | | protected enum ReaderPosition |
| | | 283 | | { |
| | | 284 | | None, |
| | | 285 | | StartElement, |
| | | 286 | | Content, |
| | | 287 | | EndElement, |
| | | 288 | | EOF |
| | | 289 | | } |
| | | 290 | | } |
| | | 291 | | } |