/// <summary> /// Given the Accept and the Accept-Charset headers of the request message computes the media type, encoding and <see cref="ODataFormat"/> /// to be used for the response message. /// </summary> /// <param name="settings">The writer settings to use for serializing the response payload.</param> /// <param name="payloadKind">The kind of payload to be serialized as part of the response message.</param> /// <param name="mediaType">The media type to be used in the response message.</param> /// <param name="encoding">The encoding to be used in the response message.</param> /// <returns>The <see cref="ODataFormat"/> used when serializing the response.</returns> internal static ODataFormat GetContentTypeFromSettings( ODataWriterSettings settings, ODataPayloadKind payloadKind, out MediaType mediaType, out Encoding encoding) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(settings != null, "settings != null"); // compute format, media type and encoding ODataFormat format; if (settings.AcceptableMediaTypes != null) { mediaType = GetMediaType(settings.AcceptableMediaTypes, payloadKind, out format); encoding = GetEncoding(settings.AcceptableCharsets, payloadKind, mediaType); } else { mediaType = GetDefaultMediaType(payloadKind, settings.Format, out format); encoding = mediaType.SelectEncoding(); } return(format); }
/// <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. List <CharsetPart> parts = new List <CharsetPart>(AcceptCharsetParts(acceptableCharsets)); parts.Sort(delegate(CharsetPart x, CharsetPart y) { return(y.Quality - x.Quality); }); foreach (CharsetPart part in parts) { 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); }
/// <summary>Reads a Content-Type header and extracts the media type's name (type/subtype) and encoding.</summary> /// <param name="contentType">The Content-Type header.</param> /// <param name="mediaTypeName">The media type in standard type/subtype form, without parameters.</param> /// <param name="encoding">Encoding (possibly null).</param> /// <returns>parameters of content type</returns> internal static IList <KeyValuePair <string, string> > ReadContentType(string contentType, out string mediaTypeName, out Encoding encoding) { DebugUtils.CheckNoExternalCallers(); if (String.IsNullOrEmpty(contentType)) { throw new ODataException(Strings.HttpUtils_ContentTypeMissing); } MediaType mediaType = ReadMediaTypes(contentType)[0]; mediaTypeName = mediaType.TypeName; encoding = mediaType.SelectEncoding(); return(mediaType.Parameters); }
/// <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. List<CharsetPart> parts = new List<CharsetPart>(AcceptCharsetParts(acceptableCharsets)); parts.Sort(delegate(CharsetPart x, CharsetPart y) { return y.Quality - x.Quality; }); foreach (CharsetPart part in parts) { 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; }
/// <summary> /// Given the Accept and the Accept-Charset headers of the request message computes the media type, encoding and <see cref="ODataFormat"/> /// to be used for the response message. /// </summary> /// <param name="settings">The writer settings to use for serializing the response payload.</param> /// <param name="payloadKind">The kind of payload to be serialized as part of the response message.</param> /// <param name="mediaType">The media type to be used in the response message.</param> /// <param name="encoding">The encoding to be used in the response message.</param> /// <returns>The <see cref="ODataFormat"/> used when serializing the response.</returns> internal static ODataFormat GetContentTypeFromSettings( ODataWriterSettings settings, ODataPayloadKind payloadKind, out MediaType mediaType, out Encoding encoding) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(settings != null, "settings != null"); // compute format, media type and encoding ODataFormat format; if (settings.AcceptableMediaTypes != null) { mediaType = GetMediaType(settings.AcceptableMediaTypes, payloadKind, out format); encoding = GetEncoding(settings.AcceptableCharsets, payloadKind, mediaType); } else { mediaType = GetDefaultMediaType(payloadKind, settings.Format, out format); encoding = mediaType.SelectEncoding(); } return format; }