private void AddMethodRoutesToCache(NancyModule module, string moduleKey, string method) { foreach (var description in module.GetRouteDescription(method)) { _Cache.Add(new RouteCacheEntry(moduleKey, method, description.Path, description.Condition)); } }
public DefaultNancyModuleBuilderFixture() { this.module = new FakeNancyModule(); this.rootPathProvider = A.Fake<IRootPathProvider>(); this.responseFormatter = new DefaultResponseFormatter(this.rootPathProvider); this.viewFactory = A.Fake<IViewFactory>(); this.builder = new DefaultNancyModuleBuilder(this.viewFactory, this.responseFormatter); }
/// <summary> /// Builds a fully configured <see cref="NancyModule"/> instance, based upon the provided <paramref name="module"/>. /// </summary> /// <param name="module">The <see cref="NancyModule"/> that shoule be configured.</param> /// <param name="context">The current request context.</param> /// <returns>A fully configured <see cref="NancyModule"/> instance.</returns> public NancyModule BuildModule(NancyModule module, NancyContext context) { module.Context = context; module.Response = this.responseFormatter; module.ViewFactory = this.viewFactory; return module; }
/// <summary> /// Builds a fully configured <see cref="NancyModule"/> instance, based upon the provided <paramref name="module"/>. /// </summary> /// <param name="module">The <see cref="NancyModule"/> that shoule be configured.</param> /// <param name="context">The current request context.</param> /// <returns>A fully configured <see cref="NancyModule"/> instance.</returns> public NancyModule BuildModule(NancyModule module, NancyContext context) { // Currently we don't connect view location, binders etc. module.Context = context; module.Response = new DefaultResponseFormatter(rootPathProvider, context, serializers); module.ModelBinderLocator = this.modelBinderLocator; return module; }
private static void CreateNegotiationContext(NancyModule module, NancyContext context) { // TODO - not sure if this should be here or not, but it'll do for now :) context.NegotiationContext = new NegotiationContext { ModuleName = module.GetModuleName(), ModulePath = module.ModulePath, }; }
/// <summary> /// Builds a fully configured <see cref="NancyModule"/> instance, based upon the provided <paramref name="module"/>. /// </summary> /// <param name="module">The <see cref="NancyModule"/> that shoule be configured.</param> /// <param name="context">The current request context.</param> /// <returns>A fully configured <see cref="NancyModule"/> instance.</returns> public NancyModule BuildModule(NancyModule module, NancyContext context) { module.Context = context; module.Response = this.responseFormatterFactory.Create(context); module.ViewFactory = this.viewFactory; module.ModelBinderLocator = this.modelBinderLocator; return module; }
public DefaultNancyModuleBuilderFixture() { this.module = new FakeNancyModule(); this.responseFormatterFactory = A.Fake<IResponseFormatterFactory>(); this.viewFactory = A.Fake<IViewFactory>(); this.modelBinderLocator = A.Fake<IModelBinderLocator>(); this.builder = new DefaultNancyModuleBuilder(this.viewFactory, this.responseFormatterFactory, this.modelBinderLocator); }
/// <summary> /// Enables basic authentication for a module /// </summary> /// <param name="module">Module to add handlers to (usually "this")</param> /// <param name="configuration">Forms authentication configuration</param> public static void Enable(NancyModule module, BasicAuthenticationConfiguration configuration) { if (module == null) { throw new ArgumentNullException("module"); } if (configuration == null) { throw new ArgumentNullException("configuration"); } module.RequiresAuthentication(); module.Before.AddItemToStartOfPipeline(GetCredentialRetrievalHook(configuration)); module.After.AddItemToEndOfPipeline(GetAuthenticationPromptHook(configuration)); }
public Route(string path, RouteParameters parameters, NancyModule module, Func<object, Response> action) { if (path == null) { throw new ArgumentNullException("path", "The path parameter cannot be null."); } if (action == null) { throw new ArgumentNullException("action", "The action parameter cannot be null."); } this.Path = path; this.Module = module; this.Parameters = parameters; this.Action = action; }
/// <summary> /// Get the description for a route. /// </summary> /// <param name="module">The module that the route is defined in.</param> /// <param name="path">The path of the route that the description should be retrieved for.</param> /// <returns>A <see cref="string"/> containing the description of the route if it could be found, otherwise <see cref="string.Empty"/>.</returns> public string GetDescription(NancyModule module, string path) { var assembly = module.GetType().Assembly; var moduleName = string.Concat(module.GetType().FullName, ".resources"); var resourceName = assembly .GetManifestResourceNames() .FirstOrDefault(x => x.Equals(moduleName, StringComparison.OrdinalIgnoreCase)); if (resourceName != null) { var manager = new ResourceManager(resourceName.Replace(".resources", string.Empty), assembly); return manager.GetString(path); } return string.Empty; }
/// <summary> /// Renders the view with the name and model defined by the <paramref name="viewName"/> and <paramref name="model"/> parameters. /// </summary> /// <param name="module">The <see cref="NancyModule"/> from there the view rendering is being invoked.</param> /// <param name="viewName">The name of the view to render.</param> /// <param name="model">The model that should be passed into the view.</param> /// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns> public Action<Stream> RenderView(NancyModule module, string viewName, dynamic model) { if (module == null) { throw new ArgumentNullException("module", "The value of the module parameter cannot be null."); } if (viewName == null && model == null) { throw new ArgumentException("viewName and model parameters cannot both be null."); } if (model == null && viewName.Length == 0) { throw new ArgumentException("The viewName parameter cannot be empty when the model parameters is null."); } var actualViewName = viewName ?? GetViewNameFromModel(model); return this.GetRenderedView(actualViewName, model); }
/// <summary> /// Ensures 404 status codes for null model. /// </summary> public static void ReturnNotFoundWhenModelIsNull(this NancyModule module) { module.After += Ensure404When(model => model == null); }
/// <summary> /// Bind the incoming request to an existing instance /// </summary> /// <typeparam name="TModel">Model type</typeparam> /// <param name="module">Current module</param> /// <param name="instance">The class instance to bind properties to</param> /// <param name="blacklistedProperties">Property names to blacklist from binding</param> public static TModel BindTo <TModel>(this NancyModule module, TModel instance, params string[] blacklistedProperties) { dynamic adapter = new DynamicModelBinderAdapter(module.ModelBinderLocator, module.Context, instance, blacklistedProperties); return(adapter); }
/// <summary> /// Bind the incoming request to a model /// </summary> /// <param name="module">Current module</param> /// <param name="blacklistedProperties">Property names to blacklist from binding</param> /// <returns>Model adapter - cast to a model type to bind it</returns> public static dynamic Bind(this NancyModule module, params string[] blacklistedProperties) { return(new DynamicModelBinderAdapter(module.ModelBinderLocator, module.Context, null, blacklistedProperties)); }
private Response getFile(int level, dynamic parameters, NancyModule page) { var ri = this.Request.Url; //string root = hostServer.pathModule + "\\" + ri.HostName; string root = hostServer.pathModule + "\\" + this.Context.domain; string url = ri.ToString().ToLower().Split('?')[0]; string[] a = url.Split('.'); string ext = a[a.Length - 1]; string content_type = ""; a = url.Split('/'); string file_name = a[a.Length - 1]; content_type = hostType.GetContentType(ext); var o = (Response)""; if (ri.Path.StartsWith("/_") && ri.Path.Contains(".")) { //string key_ri = url.Replace("http://", string.Empty); string key_ri = page.Context.domain + ri.Path; string data = ""; hostModule.dicModule.TryGetValue(key_ri, out data); o = (Response)data; o.StatusCode = HttpStatusCode.OK; o.ContentType = content_type; } else { string path1 = parameters.path1; //if (path1 != null) //{ // switch (path1.ToArray()[0]) // { // case '~': // return getFileResource(url, parameters, page); // case '.': // return getFileModule(parameters, page); // } //} //if (content_type == "") content_type = "text/plain"; string refUri = page.Request.Headers.Referrer; string host = page.Request.Url.HostName.ToLower(); string mod_folder = ""; //if (refUri == "") refUri = this.Request.Url.SiteBase; #region >> ref uri .... if (!string.IsNullOrEmpty(refUri)) { if (refUri.EndsWith(".html")) { return(getFileStatic(ext, content_type, url, level, parameters, page)); } refUri = System.Web.HttpUtility.UrlDecode(refUri.ToLower().Split('?')[0]).Replace("\\", "/"); var uri = new Uri(refUri); string refHost = uri.getDomain(), refPath = uri.LocalPath; if (host == refHost) { if (refUri.EndsWith(".css")) { if (refUri.Contains("/~")) { return(getFileModuleCSS(refUri, this.Request.Url.Path, this)); } string page_url = this.Request.Cookies["page_url"]; if (!string.IsNullOrEmpty(page_url)) { if (page_url.EndsWith(".html")) { return(getFileStatic(ext, content_type, url, level, parameters, page)); } page_url = System.Web.HttpUtility.UrlDecode(page_url); var uri0 = new Uri(page_url); string page_path = uri0.LocalPath; if (page_path == "" || page_path == "/") { page_path = "index"; } else { if (page_path.StartsWith("/")) { page_path = page_path.Substring(1); } page_path = page_path.Split('/')[0]; } if (page_path.EndsWith(hostServer.pathExtSite)) { page_path = page_path.Substring(0, page_path.Length - hostServer.pathExtSite.Length); } mod_folder = page_path; } } else { if (refPath == "" || refPath == "/") { mod_folder = "index"; } else { if (refPath.StartsWith("/")) { refPath = refPath.Substring(1); } refPath = refPath.Split('/')[0]; if (refPath.EndsWith(hostServer.pathExtSite)) { refPath = refPath.Substring(0, refPath.Length - hostServer.pathExtSite.Length); } mod_folder = refPath; } } } } #endregion o = Response.AsText(url); if (mod_folder != "" || ext == "html") { string p = "", p1 = path1, p2 = "", p3 = "", p4 = "", p5 = ""; switch (level) { case 0: p = "\\"; break; case 1: p = "\\" + p1 + "\\"; break; case 2: p2 = parameters.path2; p = "\\" + p1 + "\\" + p2 + "\\"; break; case 3: p2 = parameters.path2; p3 = parameters.path3; p = "\\" + p1 + "\\" + p2 + "\\" + p3 + "\\"; break; case 4: p2 = parameters.path2; p3 = parameters.path3; p4 = parameters.path4; p = "\\" + p1 + "\\" + p2 + "\\" + p3 + "\\" + p4 + "\\"; break; case 5: p2 = parameters.path2; p3 = parameters.path3; p4 = parameters.path4; p5 = parameters.path5; p = "\\" + p1 + "\\" + p2 + "\\" + p3 + "\\" + p4 + "\\" + p5 + "\\"; break; default: break; } string path_file = (root + "\\" + mod_folder + p + file_name).ToLower(); if (p.StartsWith("\\.")) { path_file = (root + p + file_name).ToLower(); } if (ext == "css" || ext == "js" || ext == "html") { ObjectCache cache = MemoryCache.Default; if (ext == "html") { string data = cache[path_file] as string; data = RenderTemplate(data, null); o = (Response)data; o.WithCookie(new Nancy.Cookies.NancyCookie("page_url", url)); } else { string data = cache[path_file] as string; o = (Response)data; } o.StatusCode = HttpStatusCode.OK; o.ContentType = content_type; } else { checkFile(path_file); o = Response.AsFile(path_file).WithContentType(content_type); } } } return(o); }
/// <summary> /// This module requires authentication /// </summary> /// <param name="module">Module to enable</param> public static void RequiresAuthentication(this NancyModule module) { module.Before.AddItemToEndOfPipeline(RequiresAuthentication); }
public static void AddValidationError(this NancyModule module, string propertyName, string errorMessage) { module.ModelValidationResult = module.ModelValidationResult.AddError(propertyName, errorMessage); }
private static void CreateDelegate(NancyModule mod, Type modelType) { Func<NancyModule, Type, object> func = (module, type) => Convert.ChangeType(BindMethodInfo.MakeGenericMethod(modelType).Invoke(BindMethodInfo.DeclaringType, new object[] { module }), type); _bindingMappings.Add(modelType, func); }
private static void AddModuleMetaToApplication(INancyApplication application, NancyModule mod, string action) { IList<ModuleMeta> postList = GetModuleMetaList(application, action); postList.Add(new ModuleMeta(mod.GetType(), mod.GetRouteDescription(action))); }
public FakeNancyModuleConfigurator(NancyModule module) { this.module = module; }
public static void RequiresAnyPermissions(this NancyModule module, params string[] permissions) { module.RequiresAnyClaim(claim => claim.Type == Permissions.Claim && permissions.Contains(claim.Value)); }
public string CreateRoutePath(NancyModule module) { var path = module.ModulePath.StartsWith("/") ? module.ModulePath : "/" + module.ModulePath; return(path); }
public CustomRouteBuilder(string method, NancyModule parentModule) : base(method, parentModule) { }
public NancyModuleFixture() { this.module = new FakeNancyModuleNoRoutes(); }
public static dynamic CreatedResponse(this NancyModule module, string resourcePath, int id) { return(module.Negotiate.WithStatusCode(HttpStatusCode.Created).WithHeader("Location", module.Request.Url.SiteBase + resourcePath + "/" + id)); }
public NancyV1RouteBuilder(NancyModule module, HttpMethod httpMethod) { _module = module; _httpMethod = httpMethod; }
private Response getFileStatic(string ext, string content_type, string url, int level, dynamic parameters, NancyModule page) { string[] a = url.Split(new string[] { "//" }, StringSplitOptions.None); string path_file = hostServer.pathModule + "\\" + a[a.Length - 1].Replace("/", "\\"); var o = Response.AsText(url); if (ext == "css" || ext == "js" || ext == "html") { ObjectCache cache = MemoryCache.Default; string data = cache[path_file] as string; o = (Response)data; o.StatusCode = HttpStatusCode.OK; o.ContentType = content_type; } else { o = Response.AsFile(path_file).WithContentType(content_type); } return(o); //return Response.AsFile(path_file).WithContentType(content_type); }
public AmendmentBuilder(NancyModule module) { this.module = module; }
public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { return(nancyModule.View["AuthenticateCallback", model]); }
public dynamic OnRedirectToAuthenticationProviderError(NancyModule nancyModule, string errorMessage) { throw new NotImplementedException(); }
public static void RequiresAuthentication2(this NancyModule module) { module.Before.AddItemToEndOfPipeline(ModuleExtensions.AuthenticateSession); }
public dynamic OnRedirectToAuthenticationProviderError(NancyModule nancyModule, string errorMessage) { throw new System.NotImplementedException(); // Provider canceled auth or it failed for some reason e. g. user canceled it }
public static void MakeSecure(this NancyModule module) { module.Before.AddItemToEndOfPipeline(MakeSecure); }
/// <summary> /// Bind the incoming request to a model /// </summary> /// <typeparam name="TModel">Model type</typeparam> /// <param name="module">Current module</param> /// <param name="blacklistedProperties">Property names to blacklist from binding</param> /// <returns>Bound model instance</returns> public static TModel Bind <TModel>(this NancyModule module, params string[] blacklistedProperties) { return(module.Bind(blacklistedProperties)); }
public ValidatorRouteBuilder(string method, NancyModule parentModule) : base(method, parentModule) { }
public static Negotiator ValidationFailure(this NancyModule module, string error) { return(module.Negotiate .WithModel(new ValidationErrorResponse(error)) .WithStatusCode(HttpStatusCode.BadRequest)); }
/// <summary> /// This module requires authentication and certain claims to be present. /// </summary> /// <param name="module">Module to enable</param> /// <param name="requiredClaims">Claim(s) required</param> public static void RequiresClaims(this NancyModule module, IEnumerable <string> requiredClaims) { module.Before.AddItemToStartOfPipeline(RequiresClaims(requiredClaims)); module.Before.AddItemToStartOfPipeline(RequiresAuthentication); }
/// <summary> /// Ensures 404 status codes for null model or model matching given predicate. /// </summary> public static void ReturnNotFoundWhenModelIsNullOr(this NancyModule module, Func <dynamic, bool> modelIsInvalid) { module.After += Ensure404When(model => model == null || modelIsInvalid(model)); }
/// <summary> /// This module requires claims to be validated /// </summary> /// <param name="module">Module to enable</param> /// <param name="isValid">Claims validator</param> public static void RequiresValidatedClaims(this NancyModule module, Func <IEnumerable <string>, bool> isValid) { module.Before.AddItemToStartOfPipeline(RequiresValidatedClaims(isValid)); module.Before.AddItemToStartOfPipeline(RequiresAuthentication); }
/// <summary> /// Module requires basic authentication /// </summary> /// <param name="module">Module to enable</param> /// <param name="configuration">Basic authentication configuration</param> public static void EnableBasicAuthentication(this NancyModule module, BasicAuthenticationConfiguration configuration) { BasicAuthentication.Enable(module, configuration); }
public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { return(nancyModule.Negotiate.WithView("AuthenticateCallback").WithModel(model)); }
public static T BindAndValidateModel <T>(this NancyModule module) where T : class { return(module.BindAndValidateModel <T>(null)); }
public WsRouteBuilder(NancyModule parentModule) { _parentModule = parentModule; _rb = new RouteBuilder("GET", parentModule); }
public static void RequiresOwnershipAndClaims <T>(this NancyModule module, bool doesClientOwnItem, string grain, string securableItem, params Predicate <Claim>[] requiredClaims) { module.RequiresClaims(requiredClaims); module.AddBeforeHookOrExecute(RequiresOwnership <T>(doesClientOwnItem, grain, securableItem)); }
public static void RequiresPermissionsAndClaims <T>(this NancyModule module, string name, IEnumerable <string> permissions, string grain, string securableItem, params Predicate <Claim>[] requiredClaims) { module.RequiresClaims(requiredClaims); module.AddBeforeHookOrExecute(RequiresPermissions <T>(name, permissions, grain, securableItem)); }