Exemple #1
0
        public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary <string, string> requestParams, object requestDto)
        {
            var pathInfo = !restPath.IsWildCardPath
                ? GetSanitizedPathInfo(httpReq.PathInfo, out _)
                : httpReq.PathInfo;

            return(restPath.CreateRequest(pathInfo, requestParams, requestDto));
        }
Exemple #2
0
        public void RegisterRestPaths(HttpListenerHost appHost, Type requestType)
        {
            var attrs = appHost.GetRouteAttributes(requestType);

            foreach (RouteAttribute attr in attrs)
            {
                var restPath = new RestPath(appHost.CreateInstance, appHost.GetParseFn, requestType, attr.Path, attr.Verbs, attr.IsHidden, attr.Summary, attr.Description);

                RegisterRestPath(restPath);
            }
        }
Exemple #3
0
        public static object CreateRequest(HttpListenerHost host, IRequest httpReq, RestPath restPath, ILogger logger)
        {
            var requestType = restPath.RequestType;

            if (RequireqRequestStream(requestType))
            {
                // Used by IRequiresRequestStream
                return(CreateRequiresRequestStreamRequest(host, httpReq, requestType));
            }

            var requestParams = GetFlattenedRequestParams(httpReq);

            return(CreateRequest(host, httpReq, restPath, requestParams));
        }
        public StringMapTypeDeserializer(Func <Type, object> createInstanceFn, Func <Type, Func <string, object> > getParseFn, Type type)
        {
            _CreateInstanceFn = createInstanceFn;
            _GetParseFn       = getParseFn;
            this.type         = type;

            foreach (var propertyInfo in RestPath.GetSerializableProperties(type))
            {
                var propertySetFn         = TypeAccessor.GetSetPropertyMethod(type, propertyInfo);
                var propertyType          = propertyInfo.PropertyType;
                var propertyParseStringFn = GetParseFn(propertyType);
                var propertySerializer    = new PropertySerializerEntry(propertySetFn, propertyParseStringFn, propertyType);

                propertySetterMap[propertyInfo.Name] = propertySerializer;
            }
        }
Exemple #5
0
        private Dictionary <string, SwaggerMethod> GetPathInfo(RestPath info)
        {
            var result = new Dictionary <string, SwaggerMethod>();

            foreach (var verb in info.Verbs)
            {
                var responses = new Dictionary <string, SwaggerResponse>
                {
                };

                responses["200"] = new SwaggerResponse
                {
                    description = "OK"
                };

                var security = new List <Dictionary <string, string[]> >();

                var apiKeySecurity = new Dictionary <string, string[]>();
                apiKeySecurity["api_key"] = new string[] { };

                security.Add(apiKeySecurity);

                result[verb.ToLower()] = new SwaggerMethod
                {
                    summary     = info.Summary,
                    description = info.Description,
                    produces    = new[]
                    {
                        "application/json"
                    },
                    consumes = new[]
                    {
                        "application/json"
                    },
                    operationId = info.RequestType.Name,
                    tags        = new string[] { },

                    parameters = new SwaggerParam[] { },

                    responses = responses,

                    security = security.ToArray()
                };
            }

            return(result);
        }
Exemple #6
0
        public void RegisterRestPaths(HttpListenerHost appHost, Type requestType)
        {
            var attrs = appHost.GetRouteAttributes(requestType);

            foreach (RouteAttribute attr in attrs)
            {
                var restPath = new RestPath(appHost.CreateInstance, appHost.GetParseFn, requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes);

                if (!restPath.IsValid)
                {
                    throw new NotSupportedException(string.Format(
                                                        "RestPath '{0}' on Type '{1}' is not Valid", attr.Path, requestType.GetMethodName()));
                }

                RegisterRestPath(restPath);
            }
        }
        public void RegisterRestPath(RestPath restPath)
        {
            if (!restPath.Path.StartsWith("/"))
            {
                throw new ArgumentException(string.Format("Route '{0}' on '{1}' must start with a '/'", restPath.Path, restPath.RequestType.GetMethodName()));
            }
            if (restPath.Path.IndexOfAny(InvalidRouteChars) != -1)
            {
                throw new ArgumentException(string.Format("Route '{0}' on '{1}' contains invalid chars. ", restPath.Path, restPath.RequestType.GetMethodName()));
            }

            if (!RestPathMap.TryGetValue(restPath.FirstMatchHashKey, out List <RestPath> pathsAtFirstMatch))
            {
                pathsAtFirstMatch = new List <RestPath>();
                RestPathMap[restPath.FirstMatchHashKey] = pathsAtFirstMatch;
            }
            pathsAtFirstMatch.Add(restPath);
        }
Exemple #8
0
        public static object CreateRequest(HttpListenerHost host, IRequest httpReq, RestPath restPath, ILogger logger)
        {
            var requestType = restPath.RequestType;

            if (RequireqRequestStream(requestType))
            {
                // Used by IRequiresRequestStream
                var request = ServiceHandler.CreateRequest(httpReq, restPath, GetRequestParams(httpReq), host.CreateInstance(requestType));

                var rawReq = (IRequiresRequestStream)request;
                rawReq.RequestStream = httpReq.InputStream;
                return(rawReq);
            }

            var requestParams = GetFlattenedRequestParams(httpReq);

            return(CreateRequest(host, httpReq, restPath, requestParams));
        }
Exemple #9
0
        private Dictionary <string, SwaggerMethod> GetPathInfo(RestPath info)
        {
            var result = new Dictionary <string, SwaggerMethod>();

            foreach (var verb in info.Verbs)
            {
                var responses = new Dictionary <string, SwaggerResponse>
                {
                    { "200", new SwaggerResponse {
                          description = "OK"
                      } }
                };

                var apiKeySecurity = new Dictionary <string, string[]>
                {
                    { "api_key", Array.Empty <string>() }
                };

                result[verb.ToLowerInvariant()] = new SwaggerMethod
                {
                    summary     = info.Summary,
                    description = info.Description,
                    produces    = new[] { "application/json" },
                    consumes    = new[] { "application/json" },
                    operationId = info.RequestType.Name,
                    tags        = Array.Empty <string>(),

                    parameters = Array.Empty <SwaggerParam>(),

                    responses = responses,

                    security = new [] { apiKeySecurity }
                };
            }

            return(result);
        }
Exemple #10
0
 private static void SetRoute(IRequest req, RestPath route)
 {
     req.Items["__route"] = route;
 }
Exemple #11
0
        public static async Task <object> CreateRequest(HttpListenerHost host, IRequest httpReq, RestPath restPath, ILogger logger)
        {
            var requestType = restPath.RequestType;

            if (RequireqRequestStream(requestType))
            {
                // Used by IRequiresRequestStream
                var requestParams = await GetRequestParams(httpReq).ConfigureAwait(false);

                var request = ServiceHandler.CreateRequest(httpReq, restPath, requestParams, host.CreateInstance(requestType));

                var rawReq = (IRequiresRequestStream)request;
                rawReq.RequestStream = httpReq.InputStream;
                return(rawReq);
            }
            else
            {
                var requestParams = await GetFlattenedRequestParams(httpReq).ConfigureAwait(false);

                var requestDto = await CreateContentTypeRequest(host, httpReq, restPath.RequestType, httpReq.ContentType).ConfigureAwait(false);

                return(CreateRequest(httpReq, restPath, requestParams, requestDto));
            }
        }
Exemple #12
0
        public RestPath GetRestPathForRequest(string httpMethod, string pathInfo, ILogger logger)
        {
            var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo);

            List <RestPath> firstMatches;

            var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts);

            foreach (var potentialHashMatch in yieldedHashMatches)
            {
                if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches))
                {
                    continue;
                }

                var bestScore = -1;
                foreach (var restPath in firstMatches)
                {
                    var score = restPath.MatchScore(httpMethod, matchUsingPathParts, logger);
                    if (score > bestScore)
                    {
                        bestScore = score;
                    }
                }

                if (bestScore > 0)
                {
                    foreach (var restPath in firstMatches)
                    {
                        if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts, logger))
                        {
                            return(restPath);
                        }
                    }
                }
            }

            var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts);

            foreach (var potentialHashMatch in yieldedWildcardMatches)
            {
                if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches))
                {
                    continue;
                }

                var bestScore = -1;
                foreach (var restPath in firstMatches)
                {
                    var score = restPath.MatchScore(httpMethod, matchUsingPathParts, logger);
                    if (score > bestScore)
                    {
                        bestScore = score;
                    }
                }
                if (bestScore > 0)
                {
                    foreach (var restPath in firstMatches)
                    {
                        if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts, logger))
                        {
                            return(restPath);
                        }
                    }
                }
            }

            return(null);
        }
Exemple #13
0
        public static object CreateRequest(HttpListenerHost host, IRequest httpReq, RestPath restPath, Dictionary <string, string> requestParams)
        {
            var requestDto = CreateContentTypeRequest(host, httpReq, restPath.RequestType, httpReq.ContentType);

            return(CreateRequest(httpReq, restPath, requestParams, requestDto));
        }
Exemple #14
0
 internal ServiceHandler(RestPath restPath, string responseContentType)
 {
     _restPath            = restPath;
     _responseContentType = responseContentType;
 }