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