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

Last change on this file since 364 was 351, checked in by alloc, 6 years ago

Fixed game version compatibility of GamePrefs
Code style cleanup (mostly argument names)

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.