| | | 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.Headers; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Diagnostics; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | public abstract class MessageEncoder |
| | | 13 | | { |
| | | 14 | | public abstract string ContentType { get; } |
| | | 15 | | |
| | | 16 | | public abstract string MediaType { get; } |
| | | 17 | | |
| | | 18 | | public abstract MessageVersion MessageVersion { get; } |
| | | 19 | | |
| | | 20 | | public virtual T GetProperty<T>() where T : class |
| | | 21 | | { |
| | 0 | 22 | | if (typeof(T) == typeof(FaultConverter)) |
| | | 23 | | { |
| | 0 | 24 | | return (T)(object)FaultConverter.GetDefaultFaultConverter(MessageVersion); |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders) |
| | | 31 | | { |
| | 2536 | 32 | | return ReadMessageAsync(stream, maxSizeOfHeaders, null); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public abstract Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders, string contentType); |
| | | 36 | | |
| | | 37 | | public Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager) |
| | | 38 | | { |
| | 195 | 39 | | return ReadMessage(buffer, bufferManager, null); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public abstract Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType); |
| | | 43 | | |
| | | 44 | | // used for buffered streaming |
| | | 45 | | internal async Task<ArraySegment<byte>> BufferMessageStreamAsync(Stream stream, BufferManager bufferManager, int |
| | | 46 | | { |
| | 0 | 47 | | byte[] buffer = bufferManager.TakeBuffer(ConnectionOrientedTransportDefaults.ConnectionBufferSize); |
| | 0 | 48 | | int offset = 0; |
| | 0 | 49 | | int currentBufferSize = Math.Min(buffer.Length, maxBufferSize); |
| | | 50 | | |
| | 0 | 51 | | while (offset < currentBufferSize) |
| | | 52 | | { |
| | 0 | 53 | | int count = await stream.ReadAsync(buffer, offset, currentBufferSize - offset); |
| | 0 | 54 | | if (count == 0) |
| | | 55 | | { |
| | 0 | 56 | | stream.Dispose(); |
| | 0 | 57 | | break; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | offset += count; |
| | 0 | 61 | | if (offset == currentBufferSize) |
| | | 62 | | { |
| | 0 | 63 | | if (currentBufferSize >= maxBufferSize) |
| | | 64 | | { |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 66 | | MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxBufferSize)); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | currentBufferSize = Math.Min(currentBufferSize * 2, maxBufferSize); |
| | 0 | 70 | | byte[] temp = bufferManager.TakeBuffer(currentBufferSize); |
| | 0 | 71 | | Buffer.BlockCopy(buffer, 0, temp, 0, offset); |
| | 0 | 72 | | bufferManager.ReturnBuffer(buffer); |
| | 0 | 73 | | buffer = temp; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return new ArraySegment<byte>(buffer, 0, offset); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | // used for buffered streaming |
| | | 81 | | internal virtual async Task<Message> ReadMessageAsync(Stream stream, BufferManager bufferManager, int maxBufferS |
| | | 82 | | string contentType) |
| | | 83 | | { |
| | 0 | 84 | | return ReadMessage(await BufferMessageStreamAsync(stream, bufferManager, maxBufferSize), bufferManager, cont |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public override string ToString() |
| | | 88 | | { |
| | 0 | 89 | | return ContentType; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public abstract Task WriteMessageAsync(Message message, Stream stream); |
| | | 93 | | |
| | | 94 | | public ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager) |
| | | 95 | | { |
| | 533 | 96 | | ArraySegment<byte> arraySegment = WriteMessage(message, maxMessageSize, bufferManager, 0); |
| | 533 | 97 | | return arraySegment; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | public abstract ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, |
| | | 101 | | BufferManager bufferManager, int messageOffset); |
| | | 102 | | |
| | | 103 | | public virtual bool IsContentTypeSupported(string contentType) |
| | | 104 | | { |
| | 672 | 105 | | if (contentType == null) |
| | | 106 | | { |
| | 0 | 107 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(contentType)) |
| | | 108 | | } |
| | | 109 | | |
| | 672 | 110 | | return IsContentTypeSupported(contentType, ContentType, MediaType); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | protected bool IsContentTypeSupported(string contentType, string supportedContentType, string supportedMediaType |
| | | 114 | | { |
| | 713 | 115 | | if (supportedContentType == contentType) |
| | | 116 | | { |
| | 670 | 117 | | return true; |
| | | 118 | | } |
| | | 119 | | |
| | 43 | 120 | | if (contentType.Length > supportedContentType.Length && |
| | 43 | 121 | | contentType.StartsWith(supportedContentType, StringComparison.Ordinal) && |
| | 43 | 122 | | contentType[supportedContentType.Length] == ';') |
| | | 123 | | { |
| | 8 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | // now check case-insensitively |
| | 35 | 128 | | if (contentType.StartsWith(supportedContentType, StringComparison.OrdinalIgnoreCase)) |
| | | 129 | | { |
| | 0 | 130 | | if (contentType.Length == supportedContentType.Length) |
| | | 131 | | { |
| | 0 | 132 | | return true; |
| | | 133 | | } |
| | 0 | 134 | | else if (contentType.Length > supportedContentType.Length) |
| | | 135 | | { |
| | 0 | 136 | | char ch = contentType[supportedContentType.Length]; |
| | | 137 | | |
| | | 138 | | // Linear Whitespace is allowed to appear between the end of one property and the semicolon. |
| | | 139 | | // LWS = [CRLF]? (SP | HT)+ |
| | 0 | 140 | | if (ch == ';') |
| | | 141 | | { |
| | 0 | 142 | | return true; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | // Consume the [CRLF]? |
| | 0 | 146 | | int i = supportedContentType.Length; |
| | 0 | 147 | | if (ch == '\r' && contentType.Length > supportedContentType.Length + 1 && contentType[i + 1] == '\n' |
| | | 148 | | { |
| | 0 | 149 | | i += 2; |
| | 0 | 150 | | ch = contentType[i]; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // Look for a ';' or nothing after (SP | HT)+ |
| | 0 | 154 | | if (ch == ' ' || ch == '\t') |
| | | 155 | | { |
| | 0 | 156 | | i++; |
| | 0 | 157 | | while (i < contentType.Length) |
| | | 158 | | { |
| | 0 | 159 | | ch = contentType[i]; |
| | 0 | 160 | | if (ch != ' ' && ch != '\t') |
| | | 161 | | { |
| | | 162 | | break; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | ++i; |
| | | 166 | | } |
| | | 167 | | } |
| | 0 | 168 | | if (ch == ';' || i == contentType.Length) |
| | | 169 | | { |
| | 0 | 170 | | return true; |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // sometimes we get a contentType that has parameters, but our encoders |
| | | 176 | | // merely expose the base content-type, so we will check a stripped version |
| | | 177 | | try |
| | | 178 | | { |
| | 35 | 179 | | MediaTypeHeaderValue parsedContentType = MediaTypeHeaderValue.Parse(contentType); |
| | | 180 | | |
| | 35 | 181 | | if (supportedMediaType.Length > 0 && !supportedMediaType.Equals(parsedContentType.MediaType, StringCompa |
| | | 182 | | { |
| | 15 | 183 | | return false; |
| | | 184 | | } |
| | | 185 | | |
| | 20 | 186 | | if (!IsCharSetSupported(parsedContentType.CharSet)) |
| | | 187 | | { |
| | 0 | 188 | | return false; |
| | | 189 | | } |
| | 20 | 190 | | } |
| | 0 | 191 | | catch (FormatException) |
| | | 192 | | { |
| | | 193 | | // bad content type, so we definitely don't support it! |
| | 0 | 194 | | return false; |
| | | 195 | | } |
| | | 196 | | |
| | 20 | 197 | | return true; |
| | 15 | 198 | | } |
| | | 199 | | |
| | | 200 | | protected virtual bool IsCharSetSupported(string charset) |
| | | 201 | | { |
| | 0 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | protected void ThrowIfMismatchedMessageVersion(Message message) |
| | | 206 | | { |
| | 1708 | 207 | | if (message.Version != MessageVersion) |
| | | 208 | | { |
| | 0 | 209 | | throw TraceUtility.ThrowHelperError( |
| | 0 | 210 | | new ProtocolException(SR.Format(SR.EncoderMessageVersionMismatch, message.Version, MessageVersion)), |
| | 0 | 211 | | message); |
| | | 212 | | } |
| | 1708 | 213 | | } |
| | | 214 | | |
| | | 215 | | internal string GetTraceSourceString() |
| | | 216 | | { |
| | | 217 | | // if (_traceSourceString == null) |
| | | 218 | | // { |
| | | 219 | | // _traceSourceString = Runtime.Diagnostics.DiagnosticTraceBase.CreateDefaultSourceString(this); |
| | | 220 | | // } |
| | | 221 | | |
| | 0 | 222 | | return null; |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | } |