Example #1
0
        /// <summary>Gets content type and encoding information from the headers if possible; defaults otherwise.</summary>
        /// <param name="accept">A comma-separated list of client-supported MIME Accept types.</param>
        /// <param name="acceptCharset">The specification for the character set encoding that the client requested.</param>
        /// <param name="contentType">After invocation, content type for the exception.</param>
        /// <param name="encoding">After invocation, encoding for the exception.</param>
        private static void TryGetResponseFormatForError(string accept, string acceptCharset, out string contentType, out Encoding encoding)
        {
            contentType = null;
            encoding    = null;
            if (accept != null)
            {
                try
                {
                    string[] availableTypes = new string[] { XmlConstants.MimeApplicationXml, XmlConstants.MimeApplicationJson };
                    contentType = HttpProcessUtility.SelectMimeType(accept, availableTypes);
                }
                catch (DataServiceException)
                {
                    // Ignore formatting erros in Accept and rely on text.
                }
            }

            if (acceptCharset != null)
            {
                try
                {
                    encoding = HttpProcessUtility.EncodingFromAcceptCharset(acceptCharset);
                }
                catch (DataServiceException)
                {
                    // Ignore formatting erros in Accept-Charset and rely on text.
                }
            }

            contentType = contentType ?? XmlConstants.MimeApplicationXml;
            encoding    = encoding ?? HttpProcessUtility.FallbackEncoding;
        }
Example #2
0
 private static void TryGetResponseFormatForError(string accept, string acceptCharset, Version responseVersion, out string contentType, out Encoding encoding, out string contentTypeWithCharsetAppended)
 {
     contentType = null;
     encoding    = null;
     contentTypeWithCharsetAppended = null;
     if (accept != null)
     {
         try
         {
             contentType = WebUtil.SelectResponseMediaType(accept, false, responseVersion < RequestDescription.Version3Dot0);
         }
         catch (DataServiceException)
         {
         }
     }
     if (acceptCharset != null)
     {
         try
         {
             encoding = HttpProcessUtility.EncodingFromAcceptCharset(acceptCharset);
         }
         catch (DataServiceException)
         {
         }
     }
     contentType = contentType ?? "application/xml";
     encoding    = encoding ?? HttpProcessUtility.FallbackEncoding;
     contentTypeWithCharsetAppended = contentType + ";" + "charset" + "=" + encoding.WebName;
 }
Example #3
0
 internal ResponseBodyWriter(bool hasMoved, IDataService service, IEnumerator queryResults, RequestDescription requestDescription, IODataResponseMessage responseMessage, ODataPayloadKind payloadKind)
 {
     this.hasMoved           = hasMoved;
     this.service            = service;
     this.queryResults       = queryResults;
     this.requestDescription = requestDescription;
     this.responseMessage    = responseMessage;
     this.payloadKind        = payloadKind;
     this.encoding           = HttpProcessUtility.EncodingFromAcceptCharset(this.service.OperationContext.Host.RequestAcceptCharSet);
     if ((((payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed)) || ((payloadKind == ODataPayloadKind.Property) || (payloadKind == ODataPayloadKind.Collection))) || (((payloadKind == ODataPayloadKind.EntityReferenceLink) || (payloadKind == ODataPayloadKind.EntityReferenceLinks)) || (((payloadKind == ODataPayloadKind.Error) || (payloadKind == ODataPayloadKind.ServiceDocument)) || (payloadKind == ODataPayloadKind.Parameter))))
     {
         DataServiceHostWrapper host = service.OperationContext.Host;
         if (WebUtil.GetEffectiveMaxResponseVersion(service.Configuration.DataServiceBehavior.MaxProtocolVersion, host.RequestMaxVersion) > RequestDescription.Version2Dot0)
         {
             bool isEntityOrFeed = (payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed);
             if (WebUtil.ResponseMediaTypeWouldBeJsonLight(host.RequestAccept, isEntityOrFeed))
             {
                 requestDescription.VerifyAndRaiseResponseVersion(RequestDescription.Version3Dot0, service);
                 host.ResponseVersion = RequestDescription.Version3Dot0.ToString() + ";";
             }
         }
     }
     if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
     {
         this.mediaResourceStream = service.StreamProvider.GetReadStream(this.queryResults.Current, RequestDescription.GetStreamProperty(this.requestDescription), this.service.OperationContext);
     }
     else if (payloadKind != ODataPayloadKind.BinaryValue)
     {
         string requestAcceptCharSet = this.service.OperationContext.Host.RequestAcceptCharSet;
         if (string.IsNullOrEmpty(requestAcceptCharSet) || (requestAcceptCharSet == "*"))
         {
             requestAcceptCharSet = "UTF-8";
         }
         if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
         {
             this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, ODataFormat.RawValue);
         }
         else
         {
             this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, this.service.OperationContext.Host.RequestAccept, requestAcceptCharSet);
         }
         try
         {
             this.contentFormat = ODataUtils.SetHeadersForPayload(this.messageWriter, payloadKind);
             if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
             {
                 responseMessage.SetHeader("Content-Type", this.requestDescription.MimeType);
             }
         }
         catch (ODataContentTypeException exception)
         {
             throw new DataServiceException(0x19f, null, System.Data.Services.Strings.DataServiceException_UnsupportedMediaType, null, exception);
         }
         string headerValue = this.requestDescription.ResponseVersion.ToString() + ";";
         responseMessage.SetHeader("DataServiceVersion", headerValue);
     }
 }