/// <summary>
 /// Processes parameters, and uses the BaseParamAttribute-derived members of the
 /// ParmWrapper objects for the EndPointDescriptor to polymorphically extract the 
 /// appropriate values for each argument.
 /// </summary>
 /// <param name="request">IRequest object.</param>
 /// <param name="endPoint">Endpoint desciptor; has the ParmWrapper objects.</param>
 /// <param name="moduleInstance">Module for this endpoint.</param>
 /// <returns>An array of objects, suitable for using with MethodInfo.Invoke.</returns>
 public static object[] GetArguments(IRequest request, IEndPointDescriptor endPoint, 
                              SidModule moduleInstance)
 {
     var args = new object[endPoint.Parameters.Count];
     for (var i = 0; i < args.Length; i++) {
         var parm = endPoint.Parameters[i];
         args[i] = parm.GetParamValue(request, moduleInstance);
     }
     return args;
 }
 /// <summary>
 /// If the path has replaceable tokens, we'll need a Regex Match object, for the 
 /// PathParams to use.   If there are none, this returns null.   This function does
 /// some validation, making sure that if we have PathAttributes, we must have a 
 /// non-null Regex, and that it matches the Url in the request.  
 /// The contract for this function is:  if it returns without error, the caller can be
 /// assured that either there are no PathParameters, OR it has a non-null valid Match
 /// object that has Groups in it that can be used to extract the appropriate values 
 /// from the request.
 /// </summary>
 /// <param name="request">Request object.</param>
 /// <param name="endPoint">Endpoint object; has list of parms.</param>
 /// <param name="pathRegex">The path regex generated earlier for this request.</param>
 /// <returns>The Match object for ParmWrappers to use.</returns>
 static Match GetPathRegexMatch(IRequest request, IEndPointDescriptor endPoint, 
                                Regex pathRegex)
 {
     Match match = null;
     if (endPoint.Parameters.Any(p => p is PathAttribute)) {
         if (pathRegex == null) {
             throw new ApiException(HttpStatusCode.InternalServerError,
                 String.Format("FATAL ERROR handling request for Url {0}" +
                 "no regex was found for extracting the path params",
                     request.Url.AbsolutePath));
         }
         match = pathRegex.Match(request.Url.AbsolutePath);
         if (!match.Success) {
             throw new ApiException(HttpStatusCode.InternalServerError,
                 String.Format("FATAL ERROR - don't know how to resolve uri {0} " +
                 "with path params", request.Url.AbsolutePath));
         }
     }
     return match;
 }
 /// <summary>
 /// If the endpoint (the specified endpoint if it's not OPTIONS, or the endpoint 
 /// referred to by the OPTIONS pre-flight request if it is) has the [Public] attribute,
 /// then just set the Access-Allow-Control-Allow-Origin to '*'.  Otherwise, invoke the
 /// SID-user-supplied handler for evaluating the Origin.   
 /// </summary>
 /// <param name="request">Request object.</param>
 /// <param name="responseHeaders">Headers to return to caller.</param>
 /// <param name="endPoint">End point descriptor.</param>
 /// <param name="optionsRequest">Is it an OPTIONS request?</param>
 void DoCORSHandling(IRequest request, IDictionary<string, string> responseHeaders, 
                     IEndPointDescriptor endPoint, bool optionsRequest)
 {
     var publicEndpoint = optionsRequest ? GetReferredEndpointPublicStatus(request) :
                                         endPoint.Public;
     // Call the IsValidOrigin handler, so clients can determine if they want to
     // allow the request.
     if (publicEndpoint) {
         responseHeaders["Access-Control-Allow-Origin"] = "*";
     }
     else {
         InvokeIsValidOriginHandler(request, responseHeaders);
     }
 }
Example #4
0
        /// <summary>
        /// Loads up a trie with a complete path.
        /// </summary>
        /// <param name="path">Path to break into nodes and store.</param>
        /// <param name="endPoint">EndPointDescriptor to store at the terminus.</param>
        public void Insert(string path, IEndPointDescriptor endPoint)
        {
            var createRegex = false;
            var t = this;
            var inputSegments = path.Split('/');
            var regexPathSegments = new List<string>();

            var i = 0;
            foreach (var segment in inputSegments) {
                var greedyStar = false;
                var regexString = CheckIfReplaceableToken(segment, out greedyStar);
                regexPathSegments.Add(regexString ?? segment);
                var segmentIsRegex = !string.IsNullOrEmpty(regexString);
                createRegex = createRegex | segmentIsRegex;

                var next = t.Next(inputSegments, i++);
                if (next == null) {
                    next = new Trie(segment, segmentIsRegex, greedyStar);
                    if (t.Children == null) {
                        t.Children = new List<Trie>();
                    }
                    t.Children.Add(next);
                }
                t = next;
                if (greedyStar) {
                    break;
                }
            }
            t.Terminal = true;
            t.Path = path;
            t.EndPoint = endPoint;
            if (!createRegex) {
                return;
            }
            t.PathRegex = new Regex("^/?" + string.Join("/", regexPathSegments) + "$",
                RegexOptions.Compiled);
        }