Esempio n. 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;
        }
Esempio n. 2
0
 internal Encoding SelectEncoding()
 {
     if (this.parameters != null)
     {
         foreach (KeyValuePair <string, string> pair in this.parameters)
         {
             if (string.Equals(pair.Key, "charset", StringComparison.OrdinalIgnoreCase) && (pair.Value.Trim().Length > 0))
             {
                 return(HttpProcessUtility.EncodingFromName(pair.Value));
             }
         }
     }
     if (string.Equals(this.type, "text", StringComparison.OrdinalIgnoreCase))
     {
         if (string.Equals(this.subType, "xml", StringComparison.OrdinalIgnoreCase))
         {
             return(null);
         }
         return(HttpProcessUtility.MissingEncoding);
     }
     if (string.Equals(this.type, "application", StringComparison.OrdinalIgnoreCase) && string.Equals(this.subType, "json", StringComparison.OrdinalIgnoreCase))
     {
         return(HttpProcessUtility.FallbackEncoding);
     }
     return(null);
 }
Esempio n. 3
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;
 }
Esempio n. 4
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);
     }
 }
Esempio n. 5
0
        internal static bool ResponseMediaTypeWouldBeJsonLight(string acceptTypesText, bool isEntityOrFeed)
        {
            string[] availableTypes = GetAvailableMediaTypesForV3WithJsonLight(isEntityOrFeed);
            string   b = HttpProcessUtility.SelectMimeType(acceptTypesText, availableTypes);

            if (!string.Equals("application/json;odata=light", b, StringComparison.OrdinalIgnoreCase))
            {
                return(string.Equals("application/json", b, StringComparison.OrdinalIgnoreCase));
            }
            return(true);
        }
Esempio n. 6
0
            internal int SelectQualityValue()
            {
                int    num;
                string parameterValue = this.GetParameterValue("q");

                if (parameterValue == null)
                {
                    return(0x3e8);
                }
                int textIndex = 0;

                HttpProcessUtility.ReadQualityValue(parameterValue, ref textIndex, out num);
                return(num);
            }
        private static Version ValidateVersionHeader(string headerName, string headerValue)
        {
            KeyValuePair <Version, string> pair;

            if (string.IsNullOrEmpty(headerValue))
            {
                return(null);
            }
            if (!HttpProcessUtility.TryReadVersion(headerValue, out pair))
            {
                throw DataServiceException.CreateBadRequestError(Strings.DataService_VersionCannotBeParsed(headerValue, headerName));
            }
            return(pair.Key);
        }
Esempio n. 8
0
        internal static string SelectResponseMediaType(string acceptTypesText, bool entityTarget, bool effectiveMaxResponseVersionIsLessThanThree)
        {
            string[] strArray;
            if (effectiveMaxResponseVersionIsLessThanThree)
            {
                strArray = GetAvailableMediaTypesForV2(entityTarget);
            }
            else
            {
                strArray = GetAvailableMediaTypesForV3WithoutJsonLight(entityTarget);
            }
            string str = HttpProcessUtility.SelectMimeType(acceptTypesText, strArray);

            if (CompareMimeType(str, "application/json"))
            {
                str = "application/json;odata=verbose";
            }
            return(str);
        }
Esempio n. 9
0
 internal int GetMatchingParts(string candidate)
 {
     return(this.GetMatchingParts(HttpProcessUtility.MimeTypesFromAcceptHeader(candidate).Single <HttpProcessUtility.MediaType>()));
 }
Esempio n. 10
0
 internal int GetMatchingParts(HttpProcessUtility.MediaType candidate)
 {
     int num = -1;
     if (candidate != null)
     {
         if (this.type == "*")
         {
             return 0;
         }
         if (candidate.subType == null)
         {
             return num;
         }
         string type = candidate.type;
         if (!WebUtil.CompareMimeType(this.type, type))
         {
             return num;
         }
         if (this.subType == "*")
         {
             return 1;
         }
         string subType = candidate.subType;
         if (WebUtil.CompareMimeType(this.subType, subType) && string.Equals(this.GetParameterValue("odata"), candidate.GetParameterValue("odata"), StringComparison.OrdinalIgnoreCase))
         {
             num = 2;
         }
     }
     return num;
 }