Пример #1
0
        /// <summary>
        /// Gets the current request type.
        /// </summary>
        /// <returns>Request command type</returns>
        private RequestCommand GetRequestCommandType()
        {
            // Check if this is a request for $syncscopes
            if (_serviceHost.RelativeUriSegments.Length == 1 &&
                0 == String.Compare(_serviceHost.RelativeUriSegments[0], SyncServiceConstants.SYNC_SCOPES_URL_SEGMENT, StringComparison.InvariantCultureIgnoreCase))
            {
                // Only GET is allowed for $syncscopes, throw an error if the request http method is not GET.
                if (0 != String.Compare(_serviceHost.RequestHttpMethod, SyncServiceConstants.HTTP_VERB_GET, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw SyncServiceException.CreateMethodNotAllowed(Strings.InvalidHttpMethodForSyncScopesRequest, SyncServiceConstants.HTTP_VERB_GET);
                }

                return(RequestCommand.SyncScopes);
            }

            // The item at index 1 of the RelativeUriSegments array contains the operation name.
            if (_serviceHost.RelativeUriSegments.Length < 2 || String.IsNullOrEmpty(_serviceHost.RelativeUriSegments[1].Trim()))
            {
                throw SyncServiceException.CreateBadRequestError(Strings.MissingOperationNameinRequest);
            }

            RequestCommand requestCommand;
            string         operation = _serviceHost.RelativeUriSegments[1];

            switch (operation.ToLowerInvariant())
            {
            case SyncServiceConstants.SYNC_OPERATION_UPLOAD_CHANGES:
                // Only POST is allowed for upload changes, throw an error if the request http method is not POST.
                if (0 != String.Compare(_serviceHost.RequestHttpMethod, SyncServiceConstants.HTTP_VERB_POST, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw SyncServiceException.CreateMethodNotAllowed(Strings.InvalidHttpMethodForUploadChangesRequest, SyncServiceConstants.HTTP_VERB_GET);
                }

                requestCommand = RequestCommand.UploadChanges;

                break;

            case SyncServiceConstants.SYNC_OPERATION_DOWNLOAD_CHANGES:
                requestCommand = RequestCommand.DownloadChanges;

                break;

            case SyncServiceConstants.SYNC_OPERATION_SCOPE_METADATA:
                // Only GET is allowed for $metadata, throw an error if the request http method is not GET.
                if (0 != String.Compare(_serviceHost.RequestHttpMethod, SyncServiceConstants.HTTP_VERB_GET, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw SyncServiceException.CreateMethodNotAllowed(Strings.InvalidHttpMethodForScopeMetadataRequest, SyncServiceConstants.HTTP_VERB_GET);
                }

                requestCommand = RequestCommand.ScopeMetadata;

                break;

            default:
                throw SyncServiceException.CreateBadRequestError(Strings.InvalidOperationName);
            }

            return(requestCommand);
        }
Пример #2
0
        /// <summary>
        /// Validate the HTTP Verb for GET/POST and check if the URL matches the allowed format.
        /// </summary>
        internal void ValidateRequestHttpVerbAndSegments()
        {
            if (_operationContext == null)
            {
                throw SyncServiceException.CreateBadRequestError(Strings.NullWebOperationContext);
            }

            // Only allow GET and POST verbs
            if (0 != String.Compare(RequestHttpMethod, "GET", StringComparison.InvariantCultureIgnoreCase) &&
                0 != String.Compare(RequestHttpMethod, "POST", StringComparison.InvariantCultureIgnoreCase))
            {
                SyncTracer.Warning("Request HTTP method is not GET or POST. HTTP Method: {0}", RequestHttpMethod ?? String.Empty);
                throw SyncServiceException.CreateMethodNotAllowed(Strings.UnsupportedHttpMethod, "GET, POST");
            }

            // Check if we have less than 2 relative segments. The maximum segments we can have is 2
            // which is for /scope/operation.
            if (RelativeUriSegments.Length > 2)
            {
                throw SyncServiceException.CreateBadRequestError(Strings.InvalidUrlFormat);
            }
        }