| | | 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.Xml; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal class HttpStreamMessage : Message |
| | | 11 | | { |
| | | 12 | | internal const string StreamElementName = "Binary"; |
| | | 13 | | private BodyWriter _bodyWriter; |
| | | 14 | | private readonly MessageHeaders _headers; |
| | | 15 | | private readonly MessageProperties _properties; |
| | | 16 | | |
| | 4 | 17 | | public HttpStreamMessage(BodyWriter writer) |
| | | 18 | | { |
| | 4 | 19 | | _bodyWriter = writer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | 4 | 20 | | _headers = new MessageHeaders(MessageVersion.None, 1); |
| | 4 | 21 | | _properties = new MessageProperties(); |
| | 4 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | public HttpStreamMessage(MessageHeaders headers, MessageProperties properties, BodyWriter bodyWriter) |
| | | 25 | | { |
| | 0 | 26 | | _headers = new MessageHeaders(headers); |
| | 0 | 27 | | _properties = new MessageProperties(properties); |
| | 0 | 28 | | _bodyWriter = bodyWriter ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bodyWrit |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public override MessageHeaders Headers |
| | | 32 | | { |
| | | 33 | | get |
| | | 34 | | { |
| | 20 | 35 | | if (IsDisposed) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException()); |
| | | 38 | | } |
| | | 39 | | |
| | 20 | 40 | | return _headers; |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public override bool IsEmpty => false; |
| | | 45 | | |
| | 4 | 46 | | public override bool IsFault => false; |
| | | 47 | | |
| | | 48 | | public override MessageProperties Properties |
| | | 49 | | { |
| | | 50 | | get |
| | | 51 | | { |
| | 28 | 52 | | if (IsDisposed) |
| | | 53 | | { |
| | 0 | 54 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException()); |
| | | 55 | | } |
| | | 56 | | |
| | 28 | 57 | | return _properties; |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public override MessageVersion Version |
| | | 62 | | { |
| | | 63 | | get |
| | | 64 | | { |
| | 24 | 65 | | if (IsDisposed) |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException()); |
| | | 68 | | } |
| | | 69 | | |
| | 24 | 70 | | return MessageVersion.None; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | protected override void OnBodyToString(XmlDictionaryWriter writer) |
| | | 75 | | { |
| | 0 | 76 | | if (_bodyWriter.IsBuffered) |
| | | 77 | | { |
| | 0 | 78 | | _bodyWriter.WriteBodyContents(writer); |
| | | 79 | | } |
| | | 80 | | else |
| | | 81 | | { |
| | 0 | 82 | | writer.WriteString(SR.Format(SR.MessageBodyIsStream)); |
| | | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | protected override void OnClose() |
| | | 87 | | { |
| | 4 | 88 | | Exception ex = null; |
| | | 89 | | try |
| | | 90 | | { |
| | 4 | 91 | | base.OnClose(); |
| | 4 | 92 | | } |
| | | 93 | | catch (Exception e) |
| | | 94 | | { |
| | 0 | 95 | | if (Fx.IsFatal(e)) |
| | | 96 | | { |
| | 0 | 97 | | throw; |
| | | 98 | | } |
| | 0 | 99 | | ex = e; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | try |
| | | 103 | | { |
| | 4 | 104 | | if (_properties != null) |
| | | 105 | | { |
| | 4 | 106 | | _properties.Dispose(); |
| | | 107 | | } |
| | 4 | 108 | | } |
| | 0 | 109 | | catch (Exception e) |
| | | 110 | | { |
| | 0 | 111 | | if (Fx.IsFatal(e)) |
| | | 112 | | { |
| | 0 | 113 | | throw; |
| | | 114 | | } |
| | 0 | 115 | | if (ex == null) |
| | | 116 | | { |
| | 0 | 117 | | ex = e; |
| | | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | |
| | 4 | 121 | | if (ex != null) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex); |
| | | 124 | | } |
| | | 125 | | |
| | 4 | 126 | | _bodyWriter = null; |
| | 4 | 127 | | } |
| | | 128 | | |
| | | 129 | | protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) |
| | | 130 | | { |
| | | 131 | | BodyWriter bufferedBodyWriter; |
| | 0 | 132 | | if (_bodyWriter.IsBuffered) |
| | | 133 | | { |
| | 0 | 134 | | bufferedBodyWriter = _bodyWriter; |
| | | 135 | | } |
| | | 136 | | else |
| | | 137 | | { |
| | 0 | 138 | | bufferedBodyWriter = _bodyWriter.CreateBufferedCopy(maxBufferSize); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | return new HttpStreamMessageBuffer(Headers, new MessageProperties(Properties), bufferedBodyWriter); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 145 | | { |
| | 4 | 146 | | _bodyWriter.WriteBodyContents(writer); |
| | 4 | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | private Exception CreateDisposedException() => new ObjectDisposedException("", SR.Format(SR.MessageClosed)); |
| | | 150 | | |
| | | 151 | | internal class HttpStreamMessageBuffer : MessageBuffer |
| | | 152 | | { |
| | | 153 | | private BodyWriter _bodyWriter; |
| | | 154 | | private bool _closed; |
| | | 155 | | private MessageHeaders _headers; |
| | | 156 | | private MessageProperties _properties; |
| | | 157 | | |
| | | 158 | | public HttpStreamMessageBuffer(MessageHeaders headers, |
| | | 159 | | MessageProperties properties, BodyWriter bodyWriter) |
| | 0 | 160 | | : base() |
| | | 161 | | { |
| | 0 | 162 | | _bodyWriter = bodyWriter; |
| | 0 | 163 | | _headers = headers; |
| | 0 | 164 | | _properties = properties; |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | public override int BufferSize => 0; |
| | | 168 | | |
| | 0 | 169 | | private object ThisLock { get; } = new object(); |
| | | 170 | | |
| | | 171 | | public override void Close() |
| | | 172 | | { |
| | 0 | 173 | | lock (ThisLock) |
| | | 174 | | { |
| | 0 | 175 | | if (!_closed) |
| | | 176 | | { |
| | 0 | 177 | | _closed = true; |
| | 0 | 178 | | _bodyWriter = null; |
| | 0 | 179 | | _headers = null; |
| | 0 | 180 | | _properties = null; |
| | | 181 | | } |
| | 0 | 182 | | } |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | public override Message CreateMessage() |
| | | 186 | | { |
| | 0 | 187 | | lock (ThisLock) |
| | | 188 | | { |
| | 0 | 189 | | if (_closed) |
| | | 190 | | { |
| | 0 | 191 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException()); |
| | | 192 | | } |
| | 0 | 193 | | return new HttpStreamMessage(_headers, _properties, _bodyWriter); |
| | | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | private Exception CreateDisposedException() |
| | | 198 | | { |
| | 0 | 199 | | return new ObjectDisposedException("", SR.Format(SR.MessageBufferIsClosed)); |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | } |