Skip to content

Commit

Permalink
rename HTTP->Request to HTTP->ServerRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jun 23, 2024
1 parent 3a9ffad commit c0f9343
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 56 deletions.
20 changes: 10 additions & 10 deletions src/http/interfaces/IHTTP.Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,70 @@ public interface Map
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void WebSocket(string path, Action<Request, WebSocket> callback);
void WebSocket(string path, Action<ServerRequest, WebSocket> callback);

/// <summary>
/// Handle All Http Method from Path
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void All(string path, Action<Request, ServerResponse> callback);
void All(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Get) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Get(string path, Action<Request, ServerResponse> callback);
void Get(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Put) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Put(string path, Action<Request, ServerResponse> callback);
void Put(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Head) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Head(string path, Action<Request, ServerResponse> callback);
void Head(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Post) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Post(string path, Action<Request, ServerResponse> callback);
void Post(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Patch) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Patch(string path, Action<Request, ServerResponse> callback);
void Patch(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Delete) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Delete(string path, Action<Request, ServerResponse> callback);
void Delete(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Trace) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Trace(string path, Action<Request, ServerResponse> callback);
void Trace(string path, Action<ServerRequest, ServerResponse> callback);

/// <summary>
/// Handle (Options) Http Method
/// </summary>
/// <param name="path">Request Path</param>
/// <param name="callback">Response Callback</param>
void Options(string path, Action<Request, ServerResponse> callback);
void Options(string path, Action<ServerRequest, ServerResponse> callback);
}
}
}
4 changes: 2 additions & 2 deletions src/http/interfaces/IHTTP.Middleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface Middleware
/// </summary>
/// <param name="middleware">Middleware handler</param>
/// <returns>true if callback added successful</returns>
bool Add(Func<Request, ServerResponse, bool> middleware);
bool Add(Func<ServerRequest, ServerResponse, bool> middleware);

/// <summary>
/// Add local middleware handler
Expand All @@ -25,7 +25,7 @@ public interface Middleware
/// <param name="middleware">Middleware handler</param>
/// <returns>true if callback added successful</returns>
/// <returns></returns>
bool Add(string path, Func<Request, ServerResponse, bool> middleware);
bool Add(string path, Func<ServerRequest, ServerResponse, bool> middleware);
}
}
}
2 changes: 1 addition & 1 deletion src/http/interfaces/IHTTP.MiddlewareDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface MiddlewareDescriptor
/// <summary>
/// Handler callback
/// </summary>
Func<Request, ServerResponse, bool> Callback { get; }
Func<ServerRequest, ServerResponse, bool> Callback { get; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Netly.Interfaces
{
public static partial class IHTTP
{
public interface Request
public interface ServerRequest
{
/// <summary>
/// Request encoding
Expand Down Expand Up @@ -78,6 +78,11 @@ public interface Request
/// Request Body
/// </summary>
Body Body { get; }

/// <summary>
/// Request Enctype
/// </summary>
HTTP.Enctype Enctype { get; }


/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/http/interfaces/IHTTP.WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface WebSocket
/// <summary>
/// Connection request
/// </summary>
Request Request { get; }
ServerRequest ServerRequest { get; }

/// <summary>
/// Connection Headers
Expand Down
4 changes: 2 additions & 2 deletions src/http/partials/Server/HTTP.Middleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public Middleware(Server server)

public IHTTP.MiddlewareDescriptor[] Middlewares => _middlewares.ToArray();

public bool Add(Func<IHTTP.Request, IHTTP.ServerResponse, bool> middleware)
public bool Add(Func<IHTTP.ServerRequest, IHTTP.ServerResponse, bool> middleware)
{
return Add(GlobalPath, middleware);
}

public bool Add(string path, Func<IHTTP.Request, IHTTP.ServerResponse, bool> middleware)
public bool Add(string path, Func<IHTTP.ServerRequest, IHTTP.ServerResponse, bool> middleware)
{
if (middleware == null) return false;

Expand Down
4 changes: 2 additions & 2 deletions src/http/partials/Server/HTTP.MiddlewareDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ internal struct MiddlewareDescriptor : IHTTP.MiddlewareDescriptor
{
public string Path { get; }
public bool UseParams { get; }
public Func<IHTTP.Request, IHTTP.ServerResponse, bool> Callback { get; }
public Func<IHTTP.ServerRequest, IHTTP.ServerResponse, bool> Callback { get; }

public MiddlewareDescriptor(string path, bool useParams,
Func<IHTTP.Request, IHTTP.ServerResponse, bool> callback)
Func<IHTTP.ServerRequest, IHTTP.ServerResponse, bool> callback)
{
Path = path;
UseParams = useParams;
Expand Down
54 changes: 42 additions & 12 deletions src/http/partials/Server/HTTP.Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Netly
{
public partial class HTTP
{
internal class ServerRequest : IHTTP.Request
internal class ServerRequest : IHTTP.ServerRequest
{
internal ServerRequest(HttpListenerRequest request)
{
Expand Down Expand Up @@ -61,8 +61,7 @@ internal ServerRequest(HttpListenerRequest request)

Encoding = request.ContentEncoding;

// TODO: detect enctype from Header
var enctype = Enctype.None;
var enctype = GetEnctypeFromHeader();

var buffer = new byte[request.ContentLength64];
_ = request.InputStream.Read(buffer, 0, buffer.Length);
Expand Down Expand Up @@ -103,7 +102,7 @@ internal ServerRequest(ClientWebSocket ws, Uri uri, Dictionary<string, string> h
Status = -1;

// Not applicable
Method = HttpMethod.Head;
Method = HttpMethod.Get;

Url = uri.AbsoluteUri;

Expand All @@ -121,11 +120,9 @@ internal ServerRequest(ClientWebSocket ws, Uri uri, Dictionary<string, string> h

IsEncrypted = uri.IsAbsoluteUri && uri.Scheme.ToUpper() == "WSS";

// Not applicable
Encoding = Encoding.UTF8;
Encoding = GetEncodingFromHeader();

// Not applicable
var enctype = Enctype.None;
var enctype = GetEnctypeFromHeader();

// Not applicable
var buffer = Array.Empty<byte>();
Expand Down Expand Up @@ -198,11 +195,9 @@ internal ServerRequest(HttpResponseMessage message)

IsEncrypted = uri.IsAbsoluteUri && uri.Scheme.ToUpper() == "HTTPS";

// TODO: detect encoding from Header
Encoding = Encoding.UTF8;
Encoding = GetEncodingFromHeader();

// TODO: detect enctype from Header
var enctype = Enctype.None;
var enctype = GetEnctypeFromHeader();

var buffer = message.Content.ReadAsByteArrayAsync().Result;
Body = new Body(buffer, enctype, Encoding);
Expand All @@ -223,6 +218,7 @@ internal ServerRequest(HttpResponseMessage message)
public bool IsLocalRequest { get; }
public bool IsEncrypted { get; }
public IHTTP.Body Body { get; }
public Enctype Enctype { get; }
public int Status { get; }

/// <summary>
Expand All @@ -236,6 +232,40 @@ private void SetQueriesFromUri(Uri uri)

foreach (var queryName in queryBuilder.AllKeys) Queries.Add(queryName, queryBuilder[queryName]);
}

private Encoding GetEncodingFromHeader()
{
var comparisonType = StringComparison.InvariantCultureIgnoreCase;
var value = Headers.FirstOrDefault(x => x.Key.Equals("Content-Type", comparisonType));
var key = (value.Value ?? string.Empty).ToUpper();

if (string.IsNullOrWhiteSpace(key)) return Encoding.UTF8;

// generic
if (key.Contains("UTF-8")) return Encoding.UTF8;
if (key.Contains("ISO-8859-1")) return Encoding.GetEncoding("ISO-8859-1");
if (key.Contains("ASCII")) return Encoding.ASCII;
if (key.Contains("UTF-16")) return Encoding.Unicode;
if (key.Contains("UTF-32")) return Encoding.UTF32;

// https://en.wikipedia.org/wiki/Character_encodings_in_HTML
return Encoding.UTF8;
}

private Enctype GetEnctypeFromHeader()
{
var comparisonType = StringComparison.InvariantCultureIgnoreCase;
var value = Headers.FirstOrDefault(x => x.Key.Equals("Content-Type", comparisonType));
var key = (value.Value ?? string.Empty).ToUpper();

if (string.IsNullOrWhiteSpace(key)) return Enctype.None;

if (key.Contains("application/x-www-form-urlencoded")) return Enctype.UrlEncoded;
if (key.Contains("multipart/form-data")) return Enctype.Multipart;
if (key.Contains("text/plain")) return Enctype.PlainText;

return Enctype.None;
}
}
}
}
32 changes: 16 additions & 16 deletions src/http/partials/Server/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Map(Server server)
m_mapList = new List<MapContainer>();
}

public void WebSocket(string path, Action<IHTTP.Request, IHTTP.WebSocket> callback)
public void WebSocket(string path, Action<IHTTP.ServerRequest, IHTTP.WebSocket> callback)
{
Add
(
Expand All @@ -33,7 +33,7 @@ public void WebSocket(string path, Action<IHTTP.Request, IHTTP.WebSocket> callba
);
}

public void All(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void All(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -45,7 +45,7 @@ public void All(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callbac
);
}

public void Get(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Get(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -57,7 +57,7 @@ public void Get(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callbac
);
}

public void Put(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Put(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -69,7 +69,7 @@ public void Put(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callbac
);
}

public void Head(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Head(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -81,7 +81,7 @@ public void Head(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callba
);
}

public void Post(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Post(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -93,7 +93,7 @@ public void Post(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callba
);
}

public void Patch(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Patch(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -106,7 +106,7 @@ public void Patch(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callb
);
}

public void Delete(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Delete(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -118,7 +118,7 @@ public void Delete(string path, Action<IHTTP.Request, IHTTP.ServerResponse> call
);
}

public void Trace(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Trace(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -130,7 +130,7 @@ public void Trace(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callb
);
}

public void Options(string path, Action<IHTTP.Request, IHTTP.ServerResponse> callback)
public void Options(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> callback)
{
Add
(
Expand All @@ -147,8 +147,8 @@ private void Add
string path,
string method,
bool isWebsocket,
Action<IHTTP.Request, IHTTP.ServerResponse> httpCallback,
Action<IHTTP.Request, WebSocket> websocketCallback
Action<IHTTP.ServerRequest, IHTTP.ServerResponse> httpCallback,
Action<IHTTP.ServerRequest, WebSocket> websocketCallback
)
{
path = (path ?? string.Empty).Trim();
Expand Down Expand Up @@ -178,17 +178,17 @@ internal struct MapContainer
public string Path { get; }
public string Method { get; }
public bool IsWebsocket { get; }
public Action<IHTTP.Request, IHTTP.ServerResponse> HttpCallback { get; }
public Action<IHTTP.Request, WebSocket> WebsocketCallback { get; }
public Action<IHTTP.ServerRequest, IHTTP.ServerResponse> HttpCallback { get; }
public Action<IHTTP.ServerRequest, WebSocket> WebsocketCallback { get; }

public MapContainer
(
string path,
bool useParams,
string method,
bool isWebsocket,
Action<IHTTP.Request, IHTTP.ServerResponse> httpCallback,
Action<IHTTP.Request, WebSocket> websocketCallback
Action<IHTTP.ServerRequest, IHTTP.ServerResponse> httpCallback,
Action<IHTTP.ServerRequest, WebSocket> websocketCallback
)
{
Path = path;
Expand Down
Loading

0 comments on commit c0f9343

Please sign in to comment.