refactor: added 400 bad request responses

This commit is contained in:
jdevito
2023-02-01 12:22:52 -06:00
parent 1c1eabcef6
commit a3346d5ef4
7 changed files with 68 additions and 102 deletions

View File

@@ -54,5 +54,22 @@ namespace PepperDash.Essentials.Core.Web
JoinCapabilities = joinData.Value.Metadata.JoinCapabilities.ToString() JoinCapabilities = joinData.Value.Metadata.JoinCapabilities.ToString()
}; };
} }
public static object MapDeviceTypeToObject(string key, DeviceFactoryWrapper device)
{
var kp = new KeyValuePair<string, DeviceFactoryWrapper>(key, device);
return MapDeviceTypeToObject(kp);
}
public static object MapDeviceTypeToObject(KeyValuePair<string, DeviceFactoryWrapper> device)
{
return new
{
Type = device.Key,
Description = device.Value.Description,
CType = device.Value.CType == null ? "---": device.Value.CType.ToString()
};
}
} }
} }

View File

@@ -86,7 +86,14 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context) protected override void HandlePost(HttpCwsContext context)
{ {
if (context.Request.ContentLength < 0) return; if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength]; var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength); context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);

View File

@@ -80,7 +80,14 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context) protected override void HandlePost(HttpCwsContext context)
{ {
if (context.Request.ContentLength < 0) return; if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength]; var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength); context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);

View File

@@ -1,19 +1,12 @@
using System; using System.Linq;
using System.Linq;
using Crestron.SimplSharp.WebScripting; using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers; using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class GetTypesByFilterRequestHandler : WebApiBaseRequestHandler public class GetTypesByFilterRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "GetTypesByFilterRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>
@@ -52,14 +45,9 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
object filterObj; object filterObj;
if (!routeData.Values.TryGetValue("filter", out filterObj)) if (!routeData.Values.TryGetValue("filter", out filterObj))
{ {
Debug.Console(Verbose, "TryGetValue filter failed");
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request"; context.Response.StatusDescription = "Bad Request";
context.Response.End(); context.Response.End();
@@ -67,44 +55,25 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var types = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString()).Select(type => new var deviceFactory = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString());
if (deviceFactory == null)
{ {
Type = type.Key, context.Response.StatusCode = 404;
Description = type.Value.Description, context.Response.StatusDescription = "Not Found";
CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
}).Cast<object>().ToList();
if (types == null)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End(); context.Response.End();
return; return;
} }
try var deviceTypes = deviceFactory.Select(t => EssentialsWebApiHelpers.MapDeviceTypeToObject(t)).ToList();
{ var js = JsonConvert.SerializeObject(deviceTypes, Formatting.Indented);
var js = JsonConvert.SerializeObject(types, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK"; context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false); context.Response.Write(js, false);
context.Response.End(); context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
} }
/// <summary> /// <summary>

View File

@@ -1,19 +1,12 @@
using System; using System.Linq;
using System.Linq;
using Crestron.SimplSharp.WebScripting; using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers; using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class GetTypesRequestHandler : WebApiBaseRequestHandler public class GetTypesRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "GetTypesRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>
@@ -52,47 +45,25 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented); var deviceFactory = DeviceFactory.GetDeviceFactoryDictionary(null);
Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson); if (deviceFactory == null)
var types = DeviceFactory.GetDeviceFactoryDictionary(string.Empty).Select(type => new
{ {
Type = type.Key, context.Response.StatusCode = 404;
Description = type.Value.Description, context.Response.StatusDescription = "Not Found";
CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
}).Cast<object>().ToList();
if (types == null)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End(); context.Response.End();
return; return;
} }
try var deviceTypes = deviceFactory.Select(t => EssentialsWebApiHelpers.MapDeviceTypeToObject(t)).ToList();
{ var js = JsonConvert.SerializeObject(deviceTypes, Formatting.Indented);
var js = JsonConvert.SerializeObject(types, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK"; context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false); context.Response.Write(js, false);
context.Response.End(); context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
} }
/// <summary> /// <summary>

View File

@@ -2,18 +2,12 @@
using System.Text; using System.Text;
using Crestron.SimplSharp.WebScripting; using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers; using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class SetDeviceStreamDebugRequestHandler : WebApiBaseRequestHandler public class SetDeviceStreamDebugRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "SetDeviceStreamDebugRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>
@@ -86,12 +80,18 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context) protected override void HandlePost(HttpCwsContext context)
{ {
if (context.Request.ContentLength < 0) return; if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength]; var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength); context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length); var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
Debug.Console(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
var o = new var o = new
{ {

View File

@@ -7,11 +7,6 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class ShowConfigRequestHandler : WebApiBaseRequestHandler public class ShowConfigRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "ShowConfigRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>