public string BuildMenu(WWWUser user, Dictionary <string, string> items) { string itemsString = ""; if (user.UserType != "public") { itemsString += _templates["menuitem"].Replace("{path}", "/").Replace("{title}", "Home"); } foreach (KeyValuePair <string, string> menuItem in items) { if (menuItem.Value != "" && Servers.HTTP.EndPointsList._endPoints [menuItem.Value] != null) { WWWEndpointProvider endpointProvider = Servers.HTTP.EndPointsList._endPoints [menuItem.Value]; bool allowed = false; if (user.UserType == "admin") { allowed = true; } else { if (endpointProvider.acl != null) { foreach (string acl in endpointProvider.acl) { if (acl == user.UserType || acl == "public") { allowed = true; break; } } } } if (allowed) { itemsString += _templates["menuitem"].Replace("{path}", menuItem.Value).Replace("{title}", menuItem.Key); } } } if (user.UserType != "public") { if (user.UserType == "admin") { itemsString += _templates["menuitem"].Replace("{path}", "/settings/").Replace("{title}", "Settings"); } itemsString += _templates["menuitem"].Replace("{path}", "/logout/").Replace("{title}", "Logout"); } return(_templates["menu"].Replace("{items}", itemsString)); }
public void Run() { ThreadPool.QueueUserWorkItem((o) => { try { while (_listener.IsListening) { ThreadPool.QueueUserWorkItem((c) => { //bool redirected = false; var ctx = c as HttpListenerContext; try { if (ctx.Request.Url.LocalPath == "/favicon.ico") { ctx.Response.Close(); return; } string localPath = ctx.Request.Url.LocalPath; string fileName = Path.GetFileName(localPath); if (fileName != "" && fileName.IndexOf(".") > 0) { localPath = localPath.Replace(Path.GetFileName(localPath), ""); } else { if (localPath.Substring(localPath.Length - 1, 1) != "/") { localPath += "/"; } } WWWRequest request = new WWWRequest(ctx.Request); WWWUser requestUser = null; string sessionId = ""; if (ctx.Request.Cookies["sessionid"] != null) { sessionId = ctx.Request.Cookies["sessionid"].Value; } if (sessionId != "" && _sessions.ContainsKey(sessionId)) { requestUser = _sessions[sessionId]; } else { requestUser = new WWWUser(); } if (localPath == "/logout/") { _sessions.Remove(sessionId); ctx.Response.Redirect("/login/"); ctx.Response.OutputStream.Close(); return; } ctx.Response.Cookies.Add(new Cookie("sessionid", requestUser.SessionId + "; path=/")); if (!Servers.HTTP.EndPointsList._endPoints.ContainsKey(localPath)) { if (Path.GetExtension(localPath) != "") { localPath = Path.GetDirectoryName(localPath); } } if (Servers.HTTP.EndPointsList._endPoints.ContainsKey(localPath)) { WWWEndpointProvider endpointProvider = (WWWEndpointProvider)Servers.HTTP.EndPointsList._endPoints[localPath]; bool allowed = false; if (requestUser.UserType == "admin") { allowed = true; } else { if (endpointProvider.acl != null) { foreach (string acl in endpointProvider.acl) { if (acl == requestUser.UserType || acl == "public") { allowed = true; break; } } } } if (!allowed) { ctx.Response.Redirect("/login/"); ctx.Response.OutputStream.Close(); return; } else { string postData = GetRequestPostData(ctx.Request); Dictionary <string, string> FormValues = new Dictionary <string, string>(); if (postData != string.Empty && postData.Length > 0) { FormValues = GetFormParameters(postData); } request.Form = FormValues; request.Cookies = ctx.Request.Cookies; request.User = requestUser; WWWResponse userResponse = endpointProvider.ProcessRequest(request); //update session user _sessions[request.User.SessionId] = request.User; foreach (Cookie ck in userResponse.Cookies) { if (ck.Name != "sessionid") { ctx.Response.Cookies.Add(ck); } } if (userResponse.StatusCode == 302) { ctx.Response.Redirect(userResponse.Content); //redirected=true; } else { if (userResponse.StatusCode == -1) //file download { using (FileStream fs = File.OpenRead(userResponse.Content)) { string filename = Path.GetFileName(userResponse.Content); //response is HttpListenerContext.Response... ctx.Response.ContentLength64 = fs.Length; ctx.Response.SendChunked = false; ctx.Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet; ctx.Response.AddHeader("Content-disposition", "attachment; filename=" + filename); byte[] buffer = new byte[64 * 1024]; int read; using (BinaryWriter bw = new BinaryWriter(ctx.Response.OutputStream)) { while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { bw.Write(buffer, 0, read); bw.Flush(); //seems to have no effect } bw.Close(); } ctx.Response.StatusCode = (int)HttpStatusCode.OK; ctx.Response.StatusDescription = "OK"; ctx.Response.OutputStream.Close(); return; } } else { if (userResponse.StatusCode == -2) // XML Output, no template { byte[] buf = Encoding.UTF8.GetBytes(userResponse.Content); ctx.Response.StatusCode = 200; ctx.Response.ContentType = "text/xml"; ctx.Response.ContentLength64 = buf.Length; ctx.Response.OutputStream.Write(buf, 0, buf.Length); } else { if (userResponse.StatusCode == -4) //JSON output, no template { byte[] buf = Encoding.UTF8.GetBytes(userResponse.Content); ctx.Response.StatusCode = 200; ctx.Response.ContentType = "application/json"; ctx.Response.ContentLength64 = buf.Length; ctx.Response.OutputStream.Write(buf, 0, buf.Length); } else { if (userResponse.StatusCode == -5) { using (FileStream fs = File.OpenRead(userResponse.Content)) { string filename = Path.GetFileName(userResponse.Content); //response is HttpListenerContext.Response... ctx.Response.ContentLength64 = fs.Length; ctx.Response.SendChunked = false; ctx.Response.ContentType = "image/png"; ctx.Response.AddHeader("Content-disposition", "attachment; filename=" + filename); byte[] buffer = new byte[64 * 1024]; int read; using (BinaryWriter bw = new BinaryWriter(ctx.Response.OutputStream)) { while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { bw.Write(buffer, 0, read); bw.Flush(); //seems to have no effect } bw.Close(); } ctx.Response.StatusCode = (int)HttpStatusCode.OK; ctx.Response.StatusDescription = "OK"; ctx.Response.OutputStream.Close(); return; } } else { string menuString = BuildMenu(request.User, EndPointsList._menu); string rstr = "TEMPLATE NOT FOUND: " + ctx.Request.Url.LocalPath; string[] parts = ctx.Request.Url.LocalPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 0 || parts[0] != "settings") { rstr = _templates["main"]; } else { rstr = _templates["settingsmain"]; string settingsMenuStr = BuildSettingsMenu(request.User, EndPointsList._adminMenu); rstr = rstr.Replace("{settings_menu}", settingsMenuStr); } rstr = rstr.Replace("{content}", userResponse.Content).Replace("{title}", userResponse.Title).Replace("{menu}", menuString); byte[] buf = Encoding.UTF8.GetBytes(rstr); ctx.Response.StatusCode = userResponse.StatusCode; ctx.Response.ContentType = "text/html"; ctx.Response.ContentLength64 = buf.Length; ctx.Response.OutputStream.Write(buf, 0, buf.Length); } } } } } } } else { string rstr = "Page not Found"; byte[] buf = Encoding.UTF8.GetBytes(rstr); ctx.Response.StatusCode = 404; ctx.Response.ContentLength64 = buf.Length; ctx.Response.OutputStream.Write(buf, 0, buf.Length); } } catch (Exception err) { Log.Out(err.Message); } // suppress any exceptions finally { // always close the stream ctx.Response.OutputStream.Close(); } }, _listener.GetContext()); } } catch { } // suppress any exceptions }); }