Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Aug 26, 2022
1 parent 98d38e0 commit c09cb72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
34 changes: 26 additions & 8 deletions WebContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Dynamic;
using System.Security.Cryptography;
using System.Text.Json;

Expand All @@ -17,6 +18,7 @@ namespace Maussoft.Mvc
public string Controller;
public string Action;
public string View;
public string RedirectUrl;
public bool Sent;

public Dictionary<string, string> Post;
Expand All @@ -37,13 +39,14 @@ public WebContext(HttpListenerContext context, string sessionSavePath)
this.context = context;
this.sessionSavePath = sessionSavePath;

Method = this.context.Request.HttpMethod;
Url = this.context.Request.RawUrl;
Post = new Dictionary<string, string>();
ReadPostData();
this.Method = this.context.Request.HttpMethod;
this.Url = this.context.Request.RawUrl;
this.Post = new Dictionary<string, string>();
this.ReadPostData();

Data = new System.Dynamic.ExpandoObject();
Sent = false;
this.Data = new ViewData();
this.RedirectUrl = null;
this.Sent = false;
}

private String CreateSessionIdentifier()
Expand Down Expand Up @@ -157,6 +160,11 @@ FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileSh
return null;
}

public void Redirect(string url)
{
this.RedirectUrl = url;
}

private void ReadPostData()
{
HttpListenerRequest request = this.context.Request;
Expand All @@ -174,9 +182,19 @@ private void ReadPostData()

}

internal void SendString(string output, int StatusCode = 200)
internal void SendString(string output, string mimeType = "text/html", int StatusCode = 200)
{
if (!Sent && StatusCode != 200) this.context.Response.StatusCode = StatusCode;
if (Sent)
{
throw new Exception("Output has already been sent");
}
if (RedirectUrl != null)
{
this.context.Response.StatusCode = 302;
this.context.Response.AddHeader("Location", RedirectUrl);
return;
}
if (StatusCode != 200) this.context.Response.StatusCode = StatusCode;
byte[] buf = System.Text.Encoding.UTF8.GetBytes(output);
this.context.Response.ContentLength64 = buf.Length;
this.context.Response.OutputStream.Write(buf, 0, buf.Length);
Expand Down
17 changes: 12 additions & 5 deletions WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,23 @@ public void Run()
actionResult = (new ActionRouter<TSession>(this.controllerNamespace)).Route(webctx);
webctx.WriteSession();
if (!actionResult) webctx.View = "Error.NotFound";
viewResult = new ViewRouter<TSession>(this.viewNamespace).Route(webctx);
webctx.FinalizeSession();
if (viewResult == null) webctx.SendString("NotFound", 404);
else webctx.SendString(viewResult);
if (webctx.RedirectUrl == null)
{
viewResult = new ViewRouter<TSession>(this.viewNamespace).Route(webctx);
webctx.FinalizeSession();
if (viewResult == null) webctx.SendString("NotFound", "text/plain", 404);
else webctx.SendString(viewResult);
}
else
{
webctx.SendString(viewResult);
}
}
}
catch (Exception e) // application log
{
Console.WriteLine(e.ToString());
if (webctx != null) webctx.SendString("<pre>" + e.ToString() + "</pre>", 500);
if (webctx != null) webctx.SendString("<pre>" + e.ToString() + "</pre>", "text/html", 500);
}
finally
{
Expand Down

0 comments on commit c09cb72

Please sign in to comment.