| | | 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 CoreWCF.Channels; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal class HttpStreamFormatter : IDispatchMessageFormatter |
| | | 12 | | { |
| | | 13 | | private readonly string _contractName; |
| | | 14 | | private readonly string _contractNs; |
| | | 15 | | private readonly string _operationName; |
| | | 16 | | |
| | 6 | 17 | | public HttpStreamFormatter(OperationDescription operation) |
| | | 18 | | { |
| | 6 | 19 | | if (operation == null) |
| | | 20 | | { |
| | 0 | 21 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 22 | | } |
| | | 23 | | |
| | 6 | 24 | | _operationName = operation.Name; |
| | 6 | 25 | | _contractName = operation.DeclaringContract.Name; |
| | 6 | 26 | | _contractNs = operation.DeclaringContract.Namespace; |
| | 6 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 30 | | { |
| | 1 | 31 | | parameters[0] = GetStreamFromMessage(message, true); |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) |
| | | 35 | | { |
| | 1 | 36 | | Message message = CreateMessageFromStream(result); |
| | 1 | 37 | | if (result == null) |
| | | 38 | | { |
| | 0 | 39 | | SingleBodyParameterMessageFormatter.SuppressReplyEntityBody(message); |
| | | 40 | | } |
| | | 41 | | |
| | 1 | 42 | | return message; |
| | | 43 | | } |
| | | 44 | | |
| | 5 | 45 | | internal static bool IsEmptyMessage(Message message) => message.IsEmpty; |
| | | 46 | | |
| | | 47 | | private Message CreateMessageFromStream(object data) |
| | | 48 | | { |
| | | 49 | | Message result; |
| | 1 | 50 | | if (data == null) |
| | | 51 | | { |
| | 0 | 52 | | result = Message.CreateMessage(MessageVersion.None, (string)null); |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 1 | 56 | | if (!(data is Stream streamData)) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Paramet |
| | | 59 | | } |
| | | 60 | | |
| | 1 | 61 | | result = ByteStreamMessage.CreateMessage(streamData); |
| | 1 | 62 | | result.Properties[WebBodyFormatMessageProperty.Name] = WebBodyFormatMessageProperty.RawProperty; |
| | | 63 | | } |
| | | 64 | | |
| | 1 | 65 | | return result; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private Stream GetStreamFromMessage(Message message, bool isRequest) |
| | | 69 | | { |
| | 1 | 70 | | message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out object prop); |
| | 1 | 71 | | WebBodyFormatMessageProperty formatProperty = (prop as WebBodyFormatMessageProperty); |
| | 1 | 72 | | if (formatProperty == null) |
| | | 73 | | { |
| | | 74 | | // GET and DELETE do not go through the encoder |
| | 0 | 75 | | if (IsEmptyMessage(message)) |
| | | 76 | | { |
| | 0 | 77 | | return new MemoryStream(); |
| | | 78 | | } |
| | | 79 | | else |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 1 | 85 | | if (formatProperty.Format != WebContentFormat.Raw) |
| | | 86 | | { |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.I |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | return new MessageBodyStream(message, null, null, HttpStreamMessage.StreamElementName, string.Empty, isReque |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |