Ejemplo n.º 1
0
        private static IEnumerable <KeyValuePair <string, object> > ParseETagValue(IList <ResourceProperty> etagProperties, string ifMatchHeaderValue)
        {
            bool flag;

            if (ifMatchHeaderValue == "*")
            {
                return(WebUtil.EmptyKeyValuePairStringObject);
            }
            string      stringToUnescape = ifMatchHeaderValue.Substring("W/\"".Length, (ifMatchHeaderValue.Length - "W/\"".Length) - 1);
            KeyInstance instance         = null;
            Exception   innerException   = null;

            try
            {
                flag = KeyInstance.TryParseNullableTokens(Uri.UnescapeDataString(stringToUnescape), out instance);
            }
            catch (DataServiceException exception2)
            {
                flag           = false;
                innerException = exception2;
            }
            if (!flag)
            {
                throw DataServiceException.CreatePreConditionFailedError(System.Data.Services.Strings.Serializer_ETagValueDoesNotMatch, innerException);
            }
            if (instance.PositionalValues.Count != etagProperties.Count)
            {
                throw DataServiceException.CreatePreConditionFailedError(System.Data.Services.Strings.Serializer_ETagValueDoesNotMatch);
            }
            KeyValuePair <string, object>[] pairArray = new KeyValuePair <string, object> [etagProperties.Count];
            for (int i = 0; i < pairArray.Length; i++)
            {
                ResourceProperty property    = etagProperties[i];
                object           targetValue = null;
                string           text        = (string)instance.PositionalValues[i];
                if (text != "null")
                {
                    try
                    {
                        flag = WebConvert.TryKeyStringToPrimitive(text, property.Type, out targetValue);
                    }
                    catch (OverflowException exception3)
                    {
                        flag           = false;
                        innerException = exception3;
                    }
                    if (!flag)
                    {
                        throw DataServiceException.CreatePreConditionFailedError(System.Data.Services.Strings.Serializer_ETagValueDoesNotMatch, innerException);
                    }
                }
                pairArray[i] = new KeyValuePair <string, object>(etagProperties[i].Name, targetValue);
            }
            return(pairArray);
        }
Ejemplo n.º 2
0
 private void ApplyStandardPaging(string skipToken)
 {
     if (!string.IsNullOrEmpty(skipToken))
     {
         KeyInstance instance;
         if (!this.IsPageSizeDefined)
         {
             throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.RequestQueryProcessor_SkipTokenSupportedOnPagedSets);
         }
         WebUtil.CheckSyntaxValid(KeyInstance.TryParseNullableTokens(skipToken, out instance));
         if (this.topLevelOrderingInfo.OrderingExpressions.Count != instance.PositionalValues.Count)
         {
             throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.DataService_SDP_SkipTokenNotMatchingOrdering(instance.PositionalValues.Count, skipToken, this.topLevelOrderingInfo.OrderingExpressions.Count));
         }
         this.queryExpression = this.queryExpression.QueryableWhere(this.orderingParser.BuildSkipTokenFilter(this.topLevelOrderingInfo, instance));
         this.description.VerifyProtocolVersion(RequestDescription.Version2Dot0, this.service);
         this.description.VerifyRequestVersion(RequestDescription.Version2Dot0, this.service);
         this.description.VerifyAndRaiseResponseVersion(RequestDescription.Version2Dot0, this.service);
     }
 }
Ejemplo n.º 3
0
 private void ApplyCustomPaging(string skipToken)
 {
     if (!string.IsNullOrEmpty(skipToken))
     {
         KeyInstance instance;
         WebUtil.CheckSyntaxValid(KeyInstance.TryParseNullableTokens(skipToken, out instance));
         ParameterExpression parameterForIt         = Expression.Parameter(this.description.LastSegmentInfo.TargetResourceType.InstanceType, "it");
         RequestQueryParser.ExpressionParser parser = new RequestQueryParser.ExpressionParser(this.service, this.description, parameterForIt, string.Empty);
         object[] skipTokenValues = new object[instance.PositionalValues.Count];
         int      num             = 0;
         foreach (object obj2 in instance.PositionalValues)
         {
             skipTokenValues[num++] = parser.ParseSkipTokenLiteral((string)obj2);
         }
         this.CheckAndApplyCustomPaging(skipTokenValues);
         this.description.VerifyProtocolVersion(RequestDescription.Version2Dot0, this.service);
         this.description.VerifyRequestVersion(RequestDescription.Version2Dot0, this.service);
     }
     else
     {
         this.CheckAndApplyCustomPaging(null);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Parse the given etag value in the If-Match request header.
        /// </summary>
        /// <param name="etagProperties">List of etag properties for the type whose etag values we are parsing.</param>
        /// <param name="ifMatchHeaderValue">value of the If-Match header as specified in the request.</param>
        /// <returns>returns the etag value as a list containing the property name and its corresponding value. If the If-Match header value is '*', then returns an empty collection.</returns>
        private static IEnumerable <KeyValuePair <string, object> > ParseETagValue(IList <ResourceProperty> etagProperties, string ifMatchHeaderValue)
        {
            Debug.Assert(etagProperties != null && etagProperties.Count != 0, "There must be atleast one etag property specified");
            Debug.Assert(!String.IsNullOrEmpty(ifMatchHeaderValue), "IfMatch header cannot be null");

            if (ifMatchHeaderValue == XmlConstants.HttpAnyETag)
            {
                // if the value is '*', then we return an empty IEnumerable.
                return(new KeyValuePair <string, object> [0]);
            }

            Debug.Assert(ifMatchHeaderValue.StartsWith(XmlConstants.HttpWeakETagPrefix, StringComparison.Ordinal), "If-Match header must be properly formatted - this check is done in DataService.CheckETagValues method");
            Debug.Assert(ifMatchHeaderValue.Length >= XmlConstants.HttpWeakETagPrefix.Length + 1, "If-Match header must be properly formatted - this check is done in DataService.CheckETagValues method");

            // Just get the etag value - we need to ignore the 'W/"' and the last '"' character from the etag
            string strippedETag = ifMatchHeaderValue.Substring(XmlConstants.HttpWeakETagPrefix.Length, ifMatchHeaderValue.Length - XmlConstants.HttpWeakETagPrefix.Length - 1);

            KeyInstance keyInstance = null;
            bool        success;
            Exception   innerException = null;

            // In V1, when we didn't have IConcurrencyProvider interface, we always used to compute the
            // latest etag from the entity instance we got from IUpdatable.GetResource and then comparing
            // it with the If-Match request header. Hence all invalid cases always used to throw
            // DataServiceException with 412, since the etags didn't match.
            // In V1.5, we have added the support for IConcurrencyProvider, which means we need to parse
            // the etag values and parse it to the provider, if it has implement this interface. To avoid
            // breaking changes, we need to catch all parsing errors and report them as 412 instead of 400
            // to avoid it from becoming a breaking change.
            try
            {
                success = KeyInstance.TryParseNullableTokens(Uri.UnescapeDataString(strippedETag), out keyInstance);
            }
            catch (DataServiceException e)
            {
                success        = false;
                innerException = e;
            }

            if (!success)
            {
                // We could have throwed BadRequest here since the etag value is not properly formattted. But since
                // we used to do throw 412 in V1, keeping it that way to avoid breaking change.
                throw DataServiceException.CreatePreConditionFailedError(Strings.Serializer_ETagValueDoesNotMatch, innerException);
            }

            if (keyInstance.PositionalValues.Count != etagProperties.Count)
            {
                // We could have throwed BadRequest here since the etag value is not properly formattted. But since
                // we used to do throw 412 in V1, keeping it that way to avoid breaking change.
                throw DataServiceException.CreatePreConditionFailedError(Strings.Serializer_ETagValueDoesNotMatch);
            }

            KeyValuePair <string, object>[] etagPropertyInfo = new KeyValuePair <string, object> [etagProperties.Count];
            for (int i = 0; i < etagPropertyInfo.Length; i++)
            {
                ResourceProperty etagProperty  = etagProperties[i];
                object           propertyValue = null;
                string           value         = (string)keyInstance.PositionalValues[i];

                if (value != XmlConstants.NullLiteralInETag)
                {
                    // The reason we need to catch the Overflow Exception here is because of the Bug #679728.
                    // In V1, when we didn't have IConcurrencyProvider interface, we always used to compute the
                    // latest etag from the entity instance we got from IUpdatable.GetResource and then comparing
                    // it with the If-Match request header. Hence all invalid cases always used to throw
                    // DataServiceException with 412, since the etags didn't match.
                    // In V1.5, we have added the support for IConcurrencyProvider, which means we need to parse
                    // the etag values and parse it to the provider, if it has implement this interface. To avoid
                    // breaking changes, we need to catch all parsing errors and report them as 412 instead of 400
                    // to avoid it from becoming a breaking change.
                    try
                    {
                        success = System.Data.Services.Parsing.WebConvert.TryKeyStringToPrimitive(value, etagProperty.Type, out propertyValue);
                    }
                    catch (OverflowException e)
                    {
                        success        = false;
                        innerException = e;
                    }

                    if (!success)
                    {
                        // We could have throwed BadRequest here since the etag value is not properly formattted. But since
                        // we used to do throw 412 in V1, keeping it that way to avoid breaking change.
                        throw DataServiceException.CreatePreConditionFailedError(Strings.Serializer_ETagValueDoesNotMatch, innerException);
                    }
                }

                etagPropertyInfo[i] = new KeyValuePair <string, object>(etagProperties[i].Name, propertyValue);
            }

            return(etagPropertyInfo);
        }