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