| | | 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.Net.Mime; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Web; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Dispatcher |
| | | 11 | | { |
| | | 12 | | internal abstract class MultiplexingFormatMapping |
| | | 13 | | { |
| | | 14 | | protected Encoding writeEncoding; |
| | | 15 | | protected string writeCharset; |
| | | 16 | | protected WebContentTypeMapper contentTypeMapper; |
| | | 17 | | |
| | | 18 | | public abstract WebMessageFormat MessageFormat { get; } |
| | | 19 | | public abstract WebContentFormat ContentFormat { get; } |
| | | 20 | | public abstract string DefaultMediaType { get; } |
| | | 21 | | protected abstract MessageEncoder Encoder { get; } |
| | | 22 | | |
| | | 23 | | private ContentType _defaultContentType; |
| | | 24 | | |
| | | 25 | | public ContentType DefaultContentType |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | 74 | 29 | | if (_defaultContentType == null) |
| | | 30 | | { |
| | 74 | 31 | | _defaultContentType = new ContentType(DefaultMediaType) { CharSet = writeCharset }; |
| | | 32 | | } |
| | | 33 | | |
| | 74 | 34 | | return _defaultContentType; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 74 | 38 | | public MultiplexingFormatMapping(Encoding writeEncoding, WebContentTypeMapper contentTypeMapper) |
| | | 39 | | { |
| | 74 | 40 | | if (writeEncoding == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding)); |
| | | 43 | | } |
| | | 44 | | |
| | 74 | 45 | | this.writeEncoding = writeEncoding; |
| | 74 | 46 | | writeCharset = TextEncoderDefaults.EncodingToCharSet(writeEncoding); |
| | 74 | 47 | | this.contentTypeMapper = contentTypeMapper; |
| | 74 | 48 | | } |
| | | 49 | | |
| | | 50 | | public bool CanFormatResponse(ContentType acceptHeaderElement, bool matchCharset, out ContentType contentType) |
| | | 51 | | { |
| | 0 | 52 | | if (acceptHeaderElement == null) |
| | | 53 | | { |
| | 0 | 54 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(acceptHeaderElement)); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Scrub the content type so that it is only mediaType and the charset |
| | 0 | 58 | | string charset = acceptHeaderElement.CharSet; |
| | 0 | 59 | | contentType = new ContentType(acceptHeaderElement.MediaType); |
| | 0 | 60 | | contentType.CharSet = DefaultContentType.CharSet; |
| | 0 | 61 | | string contentTypeStr = contentType.ToString(); |
| | | 62 | | |
| | 0 | 63 | | if (matchCharset && |
| | 0 | 64 | | !string.IsNullOrEmpty(charset) && |
| | 0 | 65 | | !string.Equals(charset, DefaultContentType.CharSet, StringComparison.OrdinalIgnoreCase)) |
| | | 66 | | { |
| | 0 | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | if (contentTypeMapper != null && |
| | 0 | 71 | | contentTypeMapper.GetMessageFormatForContentType(contentType.MediaType) == ContentFormat) |
| | | 72 | | { |
| | 0 | 73 | | return true; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (Encoder.IsContentTypeSupported(contentTypeStr) && |
| | 0 | 77 | | (charset == null || contentType.CharSet == DefaultContentType.CharSet)) |
| | | 78 | | { |
| | 0 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | contentType = null; |
| | 0 | 83 | | return false; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |