/// <summary>Builds a Content-Type header which includes media type and encoding information.</summary> /// <param name="mediaType">Media type to be used.</param> /// <param name="encoding">Encoding to be used in response, possibly null.</param> /// <returns>The value for the Content-Type header.</returns> internal static string BuildContentType(MediaType mediaType, Encoding encoding) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(mediaType != null, "mediaType != null"); return mediaType.ToText(encoding); }
private static IEnumerable<ODataPayloadKind> DetectPayloadKindImplementation(MediaType contentType) { if (((HttpUtils.CompareMediaTypeNames("multipart", contentType.TypeName) && HttpUtils.CompareMediaTypeNames("mixed", contentType.SubTypeName)) && (contentType.Parameters != null)) && contentType.Parameters.Any<KeyValuePair<string, string>>(kvp => HttpUtils.CompareMediaTypeParameterNames("boundary", kvp.Key))) { return new ODataPayloadKind[] { ODataPayloadKind.Batch }; } return Enumerable.Empty<ODataPayloadKind>(); }
private static IEnumerable<ODataPayloadKind> DetectPayloadKindImplementation(MediaType contentType) { if (HttpUtils.CompareMediaTypeNames("text", contentType.TypeName) && HttpUtils.CompareMediaTypeNames("text/plain", contentType.SubTypeName)) { return new ODataPayloadKind[] { ODataPayloadKind.Value }; } return new ODataPayloadKind[] { ODataPayloadKind.BinaryValue }; }
internal ODataPayloadKindDetectionInfo(MediaType contentType, ODataMessageReaderSettings messageReaderSettings, IEdmModel model, IEnumerable<ODataPayloadKind> possiblePayloadKinds) { ExceptionUtils.CheckArgumentNotNull<MediaType>(contentType, "contentType"); ExceptionUtils.CheckArgumentNotNull<ODataMessageReaderSettings>(messageReaderSettings, "readerSettings"); ExceptionUtils.CheckArgumentNotNull<IEnumerable<ODataPayloadKind>>(possiblePayloadKinds, "possiblePayloadKinds"); this.contentType = contentType; this.messageReaderSettings = messageReaderSettings; this.model = model; this.possiblePayloadKinds = possiblePayloadKinds; }
/// <summary> /// Constructor. /// </summary> /// <param name="contentType">The parsed content type as <see cref="MediaType"/>.</param> /// <param name="encoding">The encoding from the content type or the default encoding from <see cref="MediaType" />.</param> /// <param name="messageReaderSettings">The <see cref="ODataMessageReaderSettings"/> being used for reading the message.</param> /// <param name="model">The <see cref="IEdmModel"/> for the payload.</param> /// <param name="possiblePayloadKinds">The possible payload kinds based on content type negotiation.</param> internal ODataPayloadKindDetectionInfo( MediaType contentType, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, IEdmModel model, IEnumerable<ODataPayloadKind> possiblePayloadKinds) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(contentType, "contentType"); ExceptionUtils.CheckArgumentNotNull(messageReaderSettings, "readerSettings"); ExceptionUtils.CheckArgumentNotNull(possiblePayloadKinds, "possiblePayloadKinds"); this.contentType = contentType; this.encoding = encoding; this.messageReaderSettings = messageReaderSettings; this.model = model; this.possiblePayloadKinds = possiblePayloadKinds; }
/// <summary>Gets the best encoding available for the specified charset request.</summary> /// <param name="acceptableCharsets"> /// The Accept-Charset header value (eg: "iso-8859-5, unicode-1-1;q=0.8"). /// </param> /// <param name="mediaType">The media type used to compute the default encoding for the payload.</param> /// <param name="utf8Encoding">The encoding to use for UTF-8 charsets; we use the one without the BOM.</param> /// <param name="defaultEncoding">The encoding to use if no encoding could be computed from the <paramref name="acceptableCharsets"/> or <paramref name="mediaType"/>.</param> /// <returns>An Encoding object appropriate to the specifed charset request.</returns> internal static Encoding EncodingFromAcceptableCharsets(string acceptableCharsets, MediaType mediaType, Encoding utf8Encoding, Encoding defaultEncoding) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(mediaType != null, "mediaType != null"); // Determines the appropriate encoding mapping according to // RFC 2616.14.2 (http://tools.ietf.org/html/rfc2616#section-14.2). Encoding result = null; if (!string.IsNullOrEmpty(acceptableCharsets)) { // PERF: in the future if we find that computing the encoding from the accept charsets is // too expensive we could introduce a cache of original strings to resolved encoding. CharsetPart[] parts = new List<CharsetPart>(AcceptCharsetParts(acceptableCharsets)).ToArray(); // NOTE: List<T>.Sort uses an unstable sort algorithm; if charsets have the same quality value // we want to pick the first one specified so we need a stable sort. KeyValuePair<int, CharsetPart>[] sortedParts = parts.StableSort(delegate(CharsetPart x, CharsetPart y) { return y.Quality - x.Quality; }); foreach (KeyValuePair<int, CharsetPart> sortedPart in sortedParts) { CharsetPart part = sortedPart.Value; if (part.Quality > 0) { // When UTF-8 is specified, select the version that doesn't use the BOM. if (String.Compare("utf-8", part.Charset, StringComparison.OrdinalIgnoreCase) == 0) { result = utf8Encoding; break; } else { result = GetEncodingFromCharsetName(part.Charset); if (result != null) { break; } // If the charset is not supported it is ignored so other possible charsets are evaluated. } } } } // No Charset was specifed, or if charsets were specified, no valid charset was found. // Returning a different charset is also valid. Get the default encoding for the media type. if (result == null) { result = mediaType.SelectEncoding(); if (result == null) { return defaultEncoding; } } return result; }
internal static string BuildContentType(MediaType mediaType, Encoding encoding) { return mediaType.ToText(encoding); }
internal static Encoding EncodingFromAcceptableCharsets(string acceptableCharsets, MediaType mediaType, Encoding utf8Encoding, Encoding defaultEncoding) { Encoding encodingFromCharsetName = null; if (!string.IsNullOrEmpty(acceptableCharsets)) { foreach (KeyValuePair<int, CharsetPart> pair in new List<CharsetPart>(AcceptCharsetParts(acceptableCharsets)).ToArray().StableSort<CharsetPart>((x, y) => y.Quality - x.Quality)) { CharsetPart part = pair.Value; if (part.Quality > 0) { if (string.Compare("utf-8", part.Charset, StringComparison.OrdinalIgnoreCase) == 0) { encodingFromCharsetName = utf8Encoding; break; } encodingFromCharsetName = GetEncodingFromCharsetName(part.Charset); if (encodingFromCharsetName != null) { break; } } } } if (encodingFromCharsetName == null) { encodingFromCharsetName = mediaType.SelectEncoding(); if (encodingFromCharsetName == null) { return defaultEncoding; } } return encodingFromCharsetName; }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="contentType">The content type of the message.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private static IEnumerable<ODataPayloadKind> DetectPayloadKindImplementation(MediaType contentType) { Debug.Assert(contentType != null, "contentType != null"); if (HttpUtils.CompareMediaTypeNames(MimeConstants.MimeTextType, contentType.TypeName) && HttpUtils.CompareMediaTypeNames(MimeConstants.MimeTextPlain, contentType.SubTypeName)) { return new ODataPayloadKind[] { ODataPayloadKind.Value }; } return new ODataPayloadKind[] { ODataPayloadKind.BinaryValue }; }