Skip to content

Commit

Permalink
Added "HttpStatusCode" file property to send specific HTTP status cod…
Browse files Browse the repository at this point in the history
…es. Updaed Markdig.
  • Loading branch information
jmalarcon committed Aug 17, 2018
1 parent bda283f commit d11b07d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
25 changes: 25 additions & 0 deletions src/IISHelpers/TypesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,30 @@ internal static T DoConvert<T>(object v)
return (T)Activator.CreateInstance(typeof(T));
}
}

/// <summary>
/// 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"
/// </summary>
/// <param name="val">The value to be checked</param>
/// <returns>true if is a truthy value according to the criteria</returns>
internal static bool IsTruthy(string val)
{
val = val.ToLower();
return (val == "1" || val == "true" || val == "yes");
}

/// <summary>
/// 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"
/// </summary>
/// <param name="val">The value to be checked</param>
/// <returns>true if is a truthy value according to the criteria</returns>
internal static bool IsFalsy(string val)
{
return (val == "0" || val == "false" || val == "no");
}
}
}
17 changes: 17 additions & 0 deletions src/MIISHandler/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/// <summary>
/// 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.
Expand Down
6 changes: 5 additions & 1 deletion src/MIISHandler/MIISHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/MIISHandler/MIISHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Markdig, Version=0.14.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Markdig.0.14.9\lib\net35\Markdig.dll</HintPath>
<Private>True</Private>
<Reference Include="Markdig, Version=0.15.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Markdig.0.15.1\lib\net35\Markdig.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Web" />
Expand Down Expand Up @@ -104,7 +103,7 @@
<Content Include="Templates\_common_fonts\fontawesome-webfont.eot" />
<Content Include="Templates\_common_fonts\fontawesome-webfont.ttf" />
<Content Include="Templates\_common_fonts\fontawesome-webfont.woff" />
<Content Include="packages.config" />
<None Include="packages.config" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand Down
25 changes: 19 additions & 6 deletions src/MIISHandler/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class MarkdownFile
private DateTime _dateCreated;
private DateTime _dateLastModified;
private SimpleYAMLParser _FrontMatter;
private bool _isPublished;
#endregion

#region Constructor
Expand Down Expand Up @@ -227,14 +226,28 @@ public DateTime DateLastModified {
internal List<string> 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
Expand All @@ -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
Expand Down Expand Up @@ -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, "");
}
Expand Down
4 changes: 2 additions & 2 deletions src/MIISHandler/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion src/MIISHandler/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Markdig" version="0.14.9" targetFramework="net35" />
<package id="Markdig" version="0.15.1" targetFramework="net35" />
</packages>
4 changes: 4 additions & 0 deletions src/MIISHandler/packages.config.old.20180817214207
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Markdig" version="0.14.9" targetFramework="net35" />
</packages>

0 comments on commit d11b07d

Please sign in to comment.