source: binary-improvements/MapRendering/Web/Handlers/SessionHandler.cs@ 382

Last change on this file since 382 was 367, checked in by alloc, 3 years ago

Web:

  • Added SSE (ServerSentEvents) subsystem
  • Added log endpoint to SSE. Less heavy weight and more responsive way of watching the server log
  • Bunch of refactoring
File size: 2.2 KB
RevLine 
[244]1using System.IO;
2using System.Net;
3using System.Text;
4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
[244]6 public class SessionHandler : PathHandler {
[325]7 private readonly string footer = "";
8 private readonly string header = "";
[244]9
[367]10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) {
[244]11 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {
12 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");
13 }
14
15 if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {
16 footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");
17 }
18 }
19
[351]20 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
21 int _permissionLevel) {
[367]22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length);
[244]23
24 StringBuilder result = new StringBuilder ();
25 result.Append (header);
26
27 if (subpath.StartsWith ("verify")) {
[351]28 if (_user != null) {
29 _resp.Redirect ("/static/index.html");
[244]30 return;
31 }
[325]32
33 result.Append (
34 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
[244]35 } else if (subpath.StartsWith ("logout")) {
[351]36 if (_user != null) {
37 parent.connectionHandler.LogOut (_user.SessionID);
[244]38 Cookie cookie = new Cookie ("sid", "", "/");
39 cookie.Expired = true;
[351]40 _resp.AppendCookie (cookie);
41 _resp.Redirect ("/static/index.html");
[244]42 return;
43 }
[325]44
45 result.Append (
46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
[244]47 } else if (subpath.StartsWith ("login")) {
[367]48 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;
[244]49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify");
[351]50 _resp.Redirect (url);
[244]51 return;
52 } else {
[325]53 result.Append (
54 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
[244]55 }
56
57 result.Append (footer);
58
[351]59 _resp.ContentType = MimeType.GetMimeType (".html");
60 _resp.ContentEncoding = Encoding.UTF8;
[244]61 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ());
[351]62 _resp.ContentLength64 = buf.Length;
63 _resp.OutputStream.Write (buf, 0, buf.Length);
[244]64 }
65 }
[325]66}
Note: See TracBrowser for help on using the repository browser.