Cleaning up code and added more documentation.

This commit is contained in:
2025-10-11 18:44:11 -07:00
parent 3da97d4fff
commit e8e8efc00d
9 changed files with 69 additions and 72 deletions

View File

@@ -49,27 +49,6 @@ namespace MontoyaTech.Rest.Net
return response;
}
/// <summary>
/// Sets the response content type to text encoded as utf16 and writes the given text to it.
/// </summary>
/// <param name="response"></param>
/// <param name="text"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithText16(this HttpListenerResponse response, string text)
{
response.ContentType = "text/plain; charset=utf-16";
var bytes = Encoding.Unicode.GetBytes(text);
response.ContentLength64 = bytes.Length;
response.OutputStream.Write(bytes, 0, bytes.Length);
response.OutputStream.Dispose();
return response;
}
/// <summary>
/// Sets the response content type to text and writes the given text compressed to it.
/// </summary>
@@ -122,27 +101,6 @@ namespace MontoyaTech.Rest.Net
return response;
}
/// <summary>
/// Sets the response content type to json encoded as utf16 and serializes the object as json and writes it.
/// </summary>
/// <param name="response"></param>
/// <param name="obj"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithJson16(this HttpListenerResponse response, object obj)
{
response.ContentType = "application/json; charset=utf-16";
var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj));
response.ContentLength64 = bytes.Length;
response.OutputStream.Write(bytes, 0, bytes.Length);
response.OutputStream.Dispose();
return response;
}
/// <summary>
/// Sets the response content type to json and writes the given json compressed to it.
/// </summary>
@@ -355,25 +313,6 @@ namespace MontoyaTech.Rest.Net
return response;
}
/// <summary>
/// Sets the response content type to html encoded in utf 16 and writes the given html to it.
/// </summary>
/// <param name="response"></param>
/// <param name="html"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithHtml16(this HttpListenerResponse response, string html)
{
response.ContentType = "text/html; charset=utf-16";
var bytes = Encoding.Unicode.GetBytes(html);
response.ContentLength64 = bytes.Length;
response.OutputStream.Write(bytes, 0, bytes.Length);
return response;
}
/// <summary>
/// Sets the response to include the given stream and sets the length and content type if possible.
/// </summary>
@@ -695,7 +634,7 @@ namespace MontoyaTech.Rest.Net
if (requestComponents.Count == 0)
return false;
var absolutePath = Path.Combine(basePath, requestComponents.Separate(Path.DirectorySeparatorChar));
var absolutePath = Path.Combine(basePath, requestComponents.Concat(Path.DirectorySeparatorChar));
if (File.Exists(absolutePath))
{
@@ -842,7 +781,7 @@ namespace MontoyaTech.Rest.Net
requestComponents.Add(indexFile);
//Combine the path into an absolute path
var absolutePath = Path.Combine(basePath, requestComponents.Separate(Path.DirectorySeparatorChar));
var absolutePath = Path.Combine(basePath, requestComponents.Concat(Path.DirectorySeparatorChar));
//If a file exists, return true
if (File.Exists(absolutePath))

View File

@@ -14,13 +14,21 @@ namespace MontoyaTech.Rest.Net
public enum HttpRequestMethod
{
Get,
Head,
Post,
Put,
Delete,
Connect,
Options,
Trace,
Patch
}
}

View File

@@ -8,15 +8,23 @@ namespace MontoyaTech.Rest.Net
{
internal static class MimeTypeExtensions
{
/// <summary>
/// Returns the mime type if possible for known extensions.
/// </summary>
/// <param name="extension">The extension to get the mime type for. Example valid values: ".txt", "txt"</param>
/// <returns></returns>
public static string GetMimeType(this string extension)
{
//If the extension is null or empty use the default mime type.
if (string.IsNullOrWhiteSpace(extension))
return "application/octet-stream";
//Remove the leading . if needed
if (extension.StartsWith("."))
extension = extension.Substring(1);
switch (extension.ToLower())
//Check the extension against known types.
switch (extension.ToLower().Trim())
{
case "323": return "text/h323";
case "3g2": return "video/3gpp2";
@@ -584,7 +592,6 @@ namespace MontoyaTech.Rest.Net
case "xwd": return "image/x-xwindowdump";
case "z": return "application/x-compress";
case "zip": return "application/x-zip-compressed";
default: return "application/octet-stream";
}
}

View File

@@ -162,11 +162,14 @@ namespace MontoyaTech.Rest.Net
{
foreach (var include in routeIncludes)
{
var types = this.FindTypeDependencies(include.Type);
if (include.Type != null)
{
var types = this.FindTypeDependencies(include.Type);
foreach (var type in types)
if (!dependencies.Contains(type))
dependencies.Add(type);
foreach (var type in types)
if (!dependencies.Contains(type))
dependencies.Add(type);
}
}
}
}

View File

@@ -28,6 +28,9 @@ namespace MontoyaTech.Rest.Net
/// <param name="type"></param>
public RouteInclude(Type type)
{
if (type == null)
throw new ArgumentNullException($"{nameof(type)} cannot be null.");
this.Type = type;
}
}

View File

@@ -38,6 +38,9 @@ namespace MontoyaTech.Rest.Net
/// <param name="requestType"></param>
public RouteRequest(Type requestType)
{
if (requestType == null)
throw new ArgumentNullException($"{nameof(requestType)} cannot be null.");
this.RequestType = requestType;
}
}

View File

@@ -43,6 +43,9 @@ namespace MontoyaTech.Rest.Net
/// <param name="responseType"></param>
public RouteResponse(Type responseType)
{
if (responseType == null)
throw new ArgumentNullException($"{nameof(responseType)} cannot be null.");
this.ResponseType = responseType;
}
}

View File

@@ -17,8 +17,15 @@ namespace MontoyaTech.Rest.Net
/// </summary>
public string Name;
/// <summary>
/// Creates a new RouteTypeName with the new name to use.
/// </summary>
/// <param name="name"></param>
public RouteTypeName(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException($"{nameof(name)} cannot be null or empty.");
this.Name = name;
}
}

View File

@@ -6,8 +6,17 @@ using System.Threading.Tasks;
namespace MontoyaTech.Rest.Net
{
/// <summary>
/// A list of helper extensions for when working with strings.
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Counts the occurance of a character in a string and returns it.
/// </summary>
/// <param name="input"></param>
/// <param name="c"></param>
/// <returns></returns>
public static int Count(this string input, char c)
{
int count = 0;
@@ -19,7 +28,13 @@ namespace MontoyaTech.Rest.Net
return count;
}
public static string Separate(this IList<string> input, char separator)
/// <summary>
/// Concats a list of strings and separates them by a given separator character.
/// </summary>
/// <param name="input"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string Concat(this IList<string> input, char separator)
{
if (input == null || input.Count == 0)
return "";
@@ -36,7 +51,13 @@ namespace MontoyaTech.Rest.Net
return builder.ToString();
}
public static string Separate(this IList<string> input, string separator)
/// <summary>
/// Concats a list of strings and separates them by a given separator string.
/// </summary>
/// <param name="input"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string Concat(this IList<string> input, string separator)
{
if (input == null || input.Count == 0)
return "";
@@ -48,7 +69,10 @@ namespace MontoyaTech.Rest.Net
builder.Append(input[0]);
for (int i = 1; i < input.Count; i++)
builder.Append(separator).Append(input[i]);
if (separator != null)
builder.Append(separator).Append(input[i]);
else
builder.Append(input[i]);
return builder.ToString();
}