diff --git a/src/IISHelpers/TypesHelper.cs b/src/IISHelpers/TypesHelper.cs index d13e75b..01e5a2a 100644 --- a/src/IISHelpers/TypesHelper.cs +++ b/src/IISHelpers/TypesHelper.cs @@ -19,5 +19,30 @@ internal static T DoConvert(object v) return (T)Activator.CreateInstance(typeof(T)); } } + + /// + /// Determines if a string value (normally got from params or Front-Matter) + /// representes a "true" value or not. + /// Valid "true" values are: "1", "true" and "yes" + /// + /// The value to be checked + /// true if is a truthy value according to the criteria + internal static bool IsTruthy(string val) + { + val = val.ToLower(); + return (val == "1" || val == "true" || val == "yes"); + } + + /// + /// Determines if a string value (normally got from params or Front-Matter) + /// representes a "false" value or not. + /// Valid "false" values are: "0", "false" and "no" + /// + /// The value to be checked + /// true if is a truthy value according to the criteria + internal static bool IsFalsy(string val) + { + return (val == "0" || val == "false" || val == "no"); + } } } diff --git a/src/MIISHandler/Common.cs b/src/MIISHandler/Common.cs index 1416852..6d37389 100644 --- a/src/MIISHandler/Common.cs +++ b/src/MIISHandler/Common.cs @@ -7,6 +7,23 @@ public static class Common { public const string WEB_CONFIG_PARAM_PREFIX = "MIIS:"; //THe prefix to use to search for parameters in web.config + public static string GetFieldValueFromFM(string name, MarkdownFile md, string defValue = "") + { + if (md != null) //If there's a file, possibly with a Front Matter + { + //Retrieve from the front matter... + string val = md.FrontMatter[name]; + if (!string.IsNullOrEmpty(val)) + return val; + else + return defValue; //Return defValue if field is not available + } + else + { + return defValue; //Return defValue if there's not MD file to process + } + } + /// /// Returns the value, if any, for a specified field name. It takes the value from the FrontMatter first, and if it's not there, tries to read it from the current Web.config. /// In web.config it first tries to read them prefixed with "MIIS_" to prevent collision with other products, and then without the prefix. diff --git a/src/MIISHandler/MIISHandler.cs b/src/MIISHandler/MIISHandler.cs index 324209e..65292a5 100644 --- a/src/MIISHandler/MIISHandler.cs +++ b/src/MIISHandler/MIISHandler.cs @@ -49,8 +49,12 @@ public void ProcessRequest(HttpContext ctx) else { //Check if the File is published - if (mdFile.isPublished) + if (mdFile.IsPublished) { + //Check if is a 404 page + if (mdFile.HttpStatusCode != 200) + ctx.Response.StatusCode = mdFile.HttpStatusCode; + //Send the rendered HTML for the file ctx.Response.ContentType = "text/html"; ctx.Response.Write(mdFile.HTML); diff --git a/src/MIISHandler/MIISHandler.csproj b/src/MIISHandler/MIISHandler.csproj index d0cbddf..6ec35e6 100644 --- a/src/MIISHandler/MIISHandler.csproj +++ b/src/MIISHandler/MIISHandler.csproj @@ -41,9 +41,8 @@ 4 - - ..\packages\Markdig.0.14.9\lib\net35\Markdig.dll - True + + ..\packages\Markdig.0.15.1\lib\net35\Markdig.dll @@ -104,7 +103,7 @@ - + Web.config diff --git a/src/MIISHandler/MarkdownFile.cs b/src/MIISHandler/MarkdownFile.cs index f7bb419..7a8211a 100644 --- a/src/MIISHandler/MarkdownFile.cs +++ b/src/MIISHandler/MarkdownFile.cs @@ -29,7 +29,6 @@ public class MarkdownFile private DateTime _dateCreated; private DateTime _dateLastModified; private SimpleYAMLParser _FrontMatter; - private bool _isPublished; #endregion #region Constructor @@ -227,14 +226,28 @@ public DateTime DateLastModified { internal List Dependencies { get; private set; } //If the file is published or is explicitly forbidden by the author using the "Published" field - internal bool isPublished + internal bool IsPublished { get { //Check if the File is published with the "Published" field string isPublished = Common.GetFieldValue("Published", this, "1").ToLower(); - //Check explicit negative values. Any other value means it's published (for the sake of security) - return !(isPublished == "0" || isPublished == "false" || isPublished == "no"); + //For the sake of security, if it's not explicitly a "falsy" value, then is assumed true + return !TypesHelper.IsFalsy(isPublished); + } + } + + //Determines a special status code to return for the file (default is 200, OK) + //Valid status codes: https://docs.microsoft.com/en-us/windows/desktop/WinHttp/http-status-codes + internal int HttpStatusCode + { + get + { + //Check if the File has a status code that is not a 200 (default OK status code) + string statusCode = Common.GetFieldValueFromFM("HttpStatusCode", this, "200").ToLower(); + bool isInt = int.TryParse(statusCode, out int nStatus); + if (!isInt) nStatus = 200; + return Math.Abs(nStatus); } } #endregion @@ -253,7 +266,7 @@ private void EnsureContentAndFrontMatter() { EnsureContent(); ProcessFrontMatter(); //Make sure the FM is processed - removeFrontMatter(); //This is a separate step because FM can be cached and it's only dependent of the current file + RemoveFrontMatter(); //This is a separate step because FM can be cached and it's only dependent of the current file } //Extracts Front-Matter from current file @@ -301,7 +314,7 @@ private void ProcessFrontMatter() } //Removes the front matter, if any, from the current contents - private void removeFrontMatter() + private void RemoveFrontMatter() { _content = FRONT_MATTER_RE.Replace(_content, ""); } diff --git a/src/MIISHandler/Properties/AssemblyInfo.cs b/src/MIISHandler/Properties/AssemblyInfo.cs index 6161177..eaf3f39 100644 --- a/src/MIISHandler/Properties/AssemblyInfo.cs +++ b/src/MIISHandler/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("2.1.0")] -[assembly: AssemblyFileVersion("2.1.0")] +[assembly: AssemblyVersion("2.2.0")] +[assembly: AssemblyFileVersion("2.2.0")] diff --git a/src/MIISHandler/packages.config b/src/MIISHandler/packages.config index 9de6293..f117397 100644 --- a/src/MIISHandler/packages.config +++ b/src/MIISHandler/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/src/MIISHandler/packages.config.old.20180817214207 b/src/MIISHandler/packages.config.old.20180817214207 new file mode 100644 index 0000000..9de6293 --- /dev/null +++ b/src/MIISHandler/packages.config.old.20180817214207 @@ -0,0 +1,4 @@ + + + + \ No newline at end of file