Beispiel #1
0
        private void EnsureODataFormatAndContentType()
        {
            string header = null;

            if (!this.settings.UseFormat.HasValue)
            {
                header = this.message.GetHeader("Content-Type");
                header = (header == null) ? null : header.Trim();
            }
            if (!string.IsNullOrEmpty(header))
            {
                ODataPayloadKind kind;
                MediaType        type;
                this.format = MediaTypeUtils.GetFormatFromContentType(header, new ODataPayloadKind[] { this.writerPayloadKind }, MediaTypeResolver.DefaultMediaTypeResolver, out type, out this.encoding, out kind, out this.batchBoundary);
            }
            else
            {
                MediaType type2;
                this.format = MediaTypeUtils.GetContentTypeFromSettings(this.settings, this.writerPayloadKind, MediaTypeResolver.DefaultMediaTypeResolver, out type2, out this.encoding);
                if (this.writerPayloadKind == ODataPayloadKind.Batch)
                {
                    this.batchBoundary = ODataBatchWriterUtils.CreateBatchBoundary(this.writingResponse);
                    header             = ODataBatchWriterUtils.CreateMultipartMixedContentType(this.batchBoundary);
                }
                else
                {
                    this.batchBoundary = null;
                    header             = HttpUtils.BuildContentType(type2, this.encoding);
                }
                this.message.SetHeader("Content-Type", header);
            }
        }
        private void DetermineChangesetBoundaryAndEncoding(string contentType)
        {
            MediaType        type;
            ODataPayloadKind kind;

            MediaTypeUtils.GetFormatFromContentType(contentType, new ODataPayloadKind[] { ODataPayloadKind.Batch }, MediaTypeResolver.DefaultMediaTypeResolver, out type, out this.changesetEncoding, out kind, out this.changesetBoundary);
        }
        private ODataBatchOperationHeaders ValidatePartHeaders(ODataBatchOperationHeaders headers, out bool isChangeSetPart)
        {
            string str;

            if (!headers.TryGetValue("Content-Type", out str))
            {
                throw new ODataException(Strings.ODataBatchReaderStream_MissingContentTypeHeader);
            }
            if (MediaTypeUtils.MediaTypeAndSubtypeAreEqual(str, "application/http"))
            {
                string str2;
                isChangeSetPart = false;
                if (!headers.TryGetValue("Content-Transfer-Encoding", out str2) || (string.Compare(str2, "binary", StringComparison.OrdinalIgnoreCase) != 0))
                {
                    throw new ODataException(Strings.ODataBatchReaderStream_MissingOrInvalidContentEncodingHeader("Content-Transfer-Encoding", "binary"));
                }
                return(headers);
            }
            if (!MediaTypeUtils.MediaTypeStartsWithTypeAndSubtype(str, "multipart/mixed"))
            {
                throw new ODataException(Strings.ODataBatchReaderStream_InvalidContentTypeSpecified("Content-Type", str, "multipart/mixed", "application/http"));
            }
            isChangeSetPart = true;
            if (this.changesetBoundary != null)
            {
                throw new ODataException(Strings.ODataBatchReaderStream_NestedChangesetsAreNotSupported);
            }
            return(headers);
        }
        private void ProcessContentType(params ODataPayloadKind[] payloadKinds)
        {
            MediaType type;
            string    contentTypeHeader = this.GetContentTypeHeader();

            this.format = MediaTypeUtils.GetFormatFromContentType(contentTypeHeader, payloadKinds, this.MediaTypeResolver, out type, out this.encoding, out this.readerPayloadKind, out this.batchBoundary);
        }
        /// <summary>
        /// Validates the headers that have been read for a part.
        /// </summary>
        /// <param name="headers">The set of headers to validate.</param>
        /// <param name="isChangeSetPart">true if the headers indicate a changset part; otherwise false.</param>
        /// <returns>The set of validated headers.</returns>
        /// <remarks>
        /// An operation part is required to have content type 'application/http' and content transfer
        /// encoding 'binary'. A changeset is required to have content type 'multipart/mixed'.
        /// Note that we allow additional headers for batch parts; clients of the library can choose
        /// to be more strict.
        /// </remarks>
        private ODataBatchOperationHeaders ValidatePartHeaders(ODataBatchOperationHeaders headers, out bool isChangeSetPart)
        {
            string contentType;

            if (!headers.TryGetValue(ODataConstants.ContentTypeHeader, out contentType))
            {
                throw new ODataException(Strings.ODataBatchReaderStream_MissingContentTypeHeader);
            }

            if (MediaTypeUtils.MediaTypeAndSubtypeAreEqual(contentType, MimeConstants.MimeApplicationHttp))
            {
                isChangeSetPart = false;

                // An operation part is required to have application/http content type and
                // binary content transfer encoding.
                string transferEncoding;
                if (!headers.TryGetValue(ODataConstants.ContentTransferEncoding, out transferEncoding) ||
                    string.Compare(transferEncoding, ODataConstants.BatchContentTransferEncoding, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new ODataException(Strings.ODataBatchReaderStream_MissingOrInvalidContentEncodingHeader(
                                                 ODataConstants.ContentTransferEncoding,
                                                 ODataConstants.BatchContentTransferEncoding));
                }
            }
            else if (MediaTypeUtils.MediaTypeStartsWithTypeAndSubtype(contentType, MimeConstants.MimeMultipartMixed))
            {
                isChangeSetPart = true;

                if (this.changesetBoundary != null)
                {
                    // Nested changesets are not supported
                    throw new ODataException(Strings.ODataBatchReaderStream_NestedChangesetsAreNotSupported);
                }
            }
            else
            {
                throw new ODataException(Strings.ODataBatchReaderStream_InvalidContentTypeSpecified(
                                             ODataConstants.ContentTypeHeader,
                                             contentType,
                                             MimeConstants.MimeMultipartMixed,
                                             MimeConstants.MimeApplicationHttp));
            }

            return(headers);
        }
        /// <summary>
        /// Parse the content type header value to retrieve the boundary and encoding of a changeset.
        /// </summary>
        /// <param name="contentType">The content type to parse.</param>
        private void DetermineChangesetBoundaryAndEncoding(string contentType)
        {
            Debug.Assert(!string.IsNullOrEmpty(contentType), "Should have validated that non-null, non-empty content type header exists.");

            MediaType        mediaType;
            ODataPayloadKind readerPayloadKind;

            MediaTypeUtils.GetFormatFromContentType(
                contentType,
                new ODataPayloadKind[] { ODataPayloadKind.Batch },
                MediaTypeResolver.DefaultMediaTypeResolver,
                out mediaType,
                out this.changesetEncoding,
                out readerPayloadKind,
                out this.changesetBoundary);
            Debug.Assert(readerPayloadKind == ODataPayloadKind.Batch, "Must find batch payload kind.");
            Debug.Assert(this.changesetBoundary != null && this.changesetBoundary.Length > 0, "Boundary string should have been validated by now.");
            Debug.Assert(HttpUtils.CompareMediaTypeNames(MimeConstants.MimeMultipartMixed, mediaType.FullTypeName), "Must be multipart/mixed media type.");
        }
        private bool TryGetSinglePayloadKindResultFromContentType(out IEnumerable <ODataPayloadKindDetectionResult> payloadKindResults, out MediaType contentType)
        {
            if (this.message.UseBufferingReadStream == true)
            {
                throw new ODataException(Microsoft.Data.OData.Strings.ODataMessageReader_DetectPayloadKindMultipleTimes);
            }
            IList <ODataPayloadKindDetectionResult> list = MediaTypeUtils.GetPayloadKindsForContentType(this.GetContentTypeHeader(), this.MediaTypeResolver, out contentType);

            payloadKindResults = from r in list
                                 where ODataUtilsInternal.IsPayloadKindSupported(r.PayloadKind, !this.readingResponse)
                                 select r;

            if (payloadKindResults.Count <ODataPayloadKindDetectionResult>() > 1)
            {
                this.message.UseBufferingReadStream = true;
                return(false);
            }
            return(true);
        }
Beispiel #8
0
        internal static IList <KeyValuePair <string, string> > ReadMimeType(string contentType, out string mediaTypeName, out string mediaTypeCharset)
        {
            if (string.IsNullOrEmpty(contentType))
            {
                throw new ODataException(Strings.HttpUtils_ContentTypeMissing);
            }
            IList <KeyValuePair <MediaType, string> > list = ReadMediaTypes(contentType);

            if (list.Count != 1)
            {
                throw new ODataException(Strings.HttpUtils_NoOrMoreThanOneContentTypeSpecified(contentType));
            }
            KeyValuePair <MediaType, string> pair = list[0];
            MediaType key = pair.Key;

            MediaTypeUtils.CheckMediaTypeForWildCards(key);
            mediaTypeName = key.FullTypeName;
            KeyValuePair <MediaType, string> pair2 = list[0];

            mediaTypeCharset = pair2.Value;
            return(key.Parameters);
        }
Beispiel #9
0
        /// <summary>Reads a Content-Type header and extracts the media type's name (type/subtype) and parameters.</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="mediaTypeCharset">The (optional) charset parameter of the media type.</param>
        /// <returns>The parameters of the media type not including the 'charset' parameter.</returns>
        internal static IList <KeyValuePair <string, string> > ReadMimeType(string contentType, out string mediaTypeName, out string mediaTypeCharset)
        {
            DebugUtils.CheckNoExternalCallers();
            if (String.IsNullOrEmpty(contentType))
            {
                throw new ODataException(Strings.HttpUtils_ContentTypeMissing);
            }

            IList <KeyValuePair <MediaType, string> > mediaTypes = ReadMediaTypes(contentType);

            if (mediaTypes.Count != 1)
            {
                throw new ODataException(Strings.HttpUtils_NoOrMoreThanOneContentTypeSpecified(contentType));
            }

            MediaType mediaType = mediaTypes[0].Key;

            MediaTypeUtils.CheckMediaTypeForWildCards(mediaType);

            mediaTypeName    = mediaType.FullTypeName;
            mediaTypeCharset = mediaTypes[0].Value;

            return(mediaType.Parameters);
        }