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

Last change on this file since 326 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

File size: 2.3 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 private readonly Web parent;
10 private readonly string staticPart;
11
12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string moduleName = null) :
13 base (moduleName) {
14 staticPart = _staticPart;
15 parent = _parent;
16
17 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {
18 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");
19 }
20
21 if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {
22 footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");
23 }
24 }
25
26 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
27 int permissionLevel) {
28 string subpath = req.Url.AbsolutePath.Remove (0, staticPart.Length);
29
30 StringBuilder result = new StringBuilder ();
31 result.Append (header);
32
33 if (subpath.StartsWith ("verify")) {
34 if (user != null) {
35 resp.Redirect ("/static/index.html");
36 return;
37 }
38
39 result.Append (
40 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
41 } else if (subpath.StartsWith ("logout")) {
42 if (user != null) {
43 parent.connectionHandler.LogOut (user.SessionID);
44 Cookie cookie = new Cookie ("sid", "", "/");
45 cookie.Expired = true;
46 resp.AppendCookie (cookie);
47 resp.Redirect ("/static/index.html");
48 return;
49 }
50
51 result.Append (
52 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
53 } else if (subpath.StartsWith ("login")) {
54 string host = (Web.isSslRedirected (req) ? "https://" : "http://") + req.UserHostName;
55 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify");
56 resp.Redirect (url);
57 return;
58 } else {
59 result.Append (
60 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");
61 }
62
63 result.Append (footer);
64
65 resp.ContentType = MimeType.GetMimeType (".html");
66 resp.ContentEncoding = Encoding.UTF8;
67 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ());
68 resp.ContentLength64 = buf.Length;
69 resp.OutputStream.Write (buf, 0, buf.Length);
70 }
71 }
72}
Note: See TracBrowser for help on using the repository browser.