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

Last change on this file since 410 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
Line 
1using System.IO;
2using System.Net;
3using System.Text;
4
5namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
6 public class SessionHandler : PathHandler {
7 private readonly string footer = "";
8 private readonly string header = "";
9
10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) {
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
20 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
21 int _permissionLevel) {
22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length);
23
24 StringBuilder result = new StringBuilder ();
25 result.Append (header);
26
27 if (subpath.StartsWith ("verify")) {
28 if (_user != null) {
29 _resp.Redirect ("/static/index.html");
30 return;
31 }
32
33 result.Append (
34 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
35 } else if (subpath.StartsWith ("logout")) {
36 if (_user != null) {
37 parent.connectionHandler.LogOut (_user.SessionID);
38 Cookie cookie = new Cookie ("sid", "", "/");
39 cookie.Expired = true;
40 _resp.AppendCookie (cookie);
41 _resp.Redirect ("/static/index.html");
42 return;
43 }
44
45 result.Append (
46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
47 } else if (subpath.StartsWith ("login")) {
48 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;
49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify");
50 _resp.Redirect (url);
51 return;
52 } else {
53 result.Append (
54 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
55 }
56
57 result.Append (footer);
58
59 _resp.ContentType = MimeType.GetMimeType (".html");
60 _resp.ContentEncoding = Encoding.UTF8;
61 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ());
62 _resp.ContentLength64 = buf.Length;
63 _resp.OutputStream.Write (buf, 0, buf.Length);
64 }
65 }
66}
Note: See TracBrowser for help on using the repository browser.