Example #1
0
 public static HttpContent ShowLast(HttpRequest request)
 {
     if (lastException == null)
     {
         throw new ApplicationException("This is the first exception");
     }
     return(HttpContent.Read(request.Server, "sar.Http.Views.Error.Display.html", lastException));
 }
Example #2
0
        public static HttpContent Header(HttpRequest request)
        {
            var baseContent = new Dictionary <string, HttpContent>()
            {
            };

            baseContent.Add("Header", new HttpContent(request.Header));
            return(HttpContent.Read(request.Server, "sar.Http.Views.Debug.Header.html", baseContent));
        }
Example #3
0
        private byte[] Render(HttpCache cache, Dictionary <string, HttpContent> baseContent)
        {
            if (this.ContentType.Contains("text") || this.ContentType.Contains("xml"))
            {
                string text = Encoding.ASCII.GetString(this.content);

                // include linked externals
                MatchCollection matches = Regex.Matches(text, INCLUDE_RENDER_SYNTAX);
                if (matches.Count > 0)
                {
                    foreach (Match match in matches)
                    {
                        string key = match.Groups[1].Value.TrimWhiteSpace();
                        string replacmentContent = HttpContent.Read(cache, key, baseContent).RenderText(cache, baseContent);
                        text = Regex.Replace(text, match.Groups[0].Value, replacmentContent);
                    }
                }

                // include linked externals
                matches = Regex.Matches(text, CONTENT_RENDER_SYNTAX);
                if (matches.Count > 0)
                {
                    foreach (Match match in matches)
                    {
                        string key = match.Groups[1].Value.TrimWhiteSpace();
                        if (baseContent.ContainsKey(key))
                        {
                            HttpContent replacmentContent = baseContent[key];
                            text = Regex.Replace(text, match.Groups[0].Value, replacmentContent.RenderText(cache, baseContent));
                        }
                    }
                }

                return(Encoding.ASCII.GetBytes(text));
            }

            return(this.content);
        }
Example #4
0
        public static HttpContent Display(HttpRequest request, Exception ex, HttpStatusCode status)
        {
            Exception inner = ExceptionHelper.GetInner(ex);

            var baseContent = new Dictionary <string, HttpContent>()
            {
            };

            baseContent.Add("Title", new HttpContent(status.ToString()));
            baseContent.Add("ResponseCode", new HttpContent(((int)status).ToString()));
            baseContent.Add("ExceptionType", new HttpContent(inner.GetType().ToString()));
            baseContent.Add("RequestURL", new HttpContent(request.FullUrl));
            baseContent.Add("ExceptionMessage", new HttpContent(inner.Message));

            string stackTrace = "";

            if (inner != ex)
            {
                stackTrace += "<span><strong>Outer:</strong><br>";
                stackTrace += "<cite>" + ExceptionHelper.GetStackTrace(ex) + "</cite></span>";
                stackTrace += Environment.NewLine;
                stackTrace += "<span><strong>Inner:</strong><br>";
                stackTrace += "<cite>" + ExceptionHelper.GetStackTrace(inner) + "</cite></span>";
            }
            else
            {
                stackTrace += "<cite>" + ExceptionHelper.GetStackTrace(ex) + "</cite>";
            }

            stackTrace = stackTrace.Replace("\t", "");

            baseContent.Add("ExceptionStackTrace", new HttpContent(stackTrace.ToHTML()));
            lastException = baseContent;

            return(HttpContent.Read(request.Server, "sar.Http.Views.Error.Display.html", baseContent));
        }
Example #5
0
        public static HttpContent Info(HttpRequest request)
        {
            string data = "";

            if (request.Data != null)
            {
                data = StringHelper.GetString(request.Data);
            }

            var baseContent = new Dictionary <string, HttpContent>()
            {
            };

            // get connections
            lock (request.Server.Connections)
            {
                var totalConnections = request.Server.Connections.Count.ToString();
                var connections      = "";

                foreach (var connection in request.Server.Connections)
                {
                    if (!connection.Stopped)
                    {
                        var ip   = ((IPEndPoint)connection.Socket.Client.RemoteEndPoint).Address.ToString();
                        var port = ((IPEndPoint)connection.Socket.Client.RemoteEndPoint).Port.ToString();

                        connections += ip + ":" + port + "\n";
                    }
                    else
                    {
                        connections += "timed out\n";
                    }
                }

                baseContent.Add("TotalConnections", new HttpContent(totalConnections));
                baseContent.Add("Connections", new HttpContent(connections));
            }

            // get sessions
            // TODO: finish
            lock (request.Server.Sessions)
            {
                var totalSessions = request.Server.Sessions.Count.ToString();
                var sessions      = "";

                foreach (var session in request.Server.Sessions)
                {
                    sessions += session.Key + " - " + session.Value.CreationDate.ToString() + "\n";
                }

                baseContent.Add("TotalSessions", new HttpContent(totalSessions));
                baseContent.Add("Sessions", new HttpContent(sessions));
            }

            var currentSession = "";

            currentSession += "ID: " + request.Session.ID + "\n";
            currentSession += "Created: " + request.Session.CreationDate.ToString() + "\n";
            currentSession += "ExpiryDate: " + request.Session.ExpiryDate.ToString() + "\n";

            baseContent.Add("Session", new HttpContent(currentSession));

            return(HttpContent.Read(request.Server, "sar.Http.Views.Debug.Info.html", baseContent));
        }
Example #6
0
        public HttpResponse(HttpRequest request)
        {
            this.request = request;
            const string PDF_IDENT = "-pdf";

            try
            {
                if (this.request.Path == @"")
                {
                    if (HttpController.Primary == null)
                    {
                        throw new ApplicationException("Primary Controller Not Defined");
                    }
                    if (HttpController.Primary.PrimaryAction == null)
                    {
                        throw new ApplicationException("Primary Action Not Defined");
                    }

                    this.content = HttpController.RequestPrimary(this.request);
                }
                else if (this.request.Path.ToLower().EndsWith(PDF_IDENT, StringComparison.CurrentCulture))
                {
                    string url = "http://localhost:" + request.Server.Port.ToString() + this.request.FullUrl;

                    url = url.Replace(this.request.Path, StringHelper.TrimEnd(this.request.Path, PDF_IDENT.Length));

                    this.content = new HttpContent(HtmlToPdfHelper.ReadPDF(url), "application/pdf");
                }
                else if (this.request.IsWebSocket && HttpWebSocket.WebSocketControllerExists(this.request))
                {
                    var type = HttpWebSocket.GetWebSocketController(this.request);
                    this.request.WebSocket = (HttpWebSocket)Activator.CreateInstance(type, this.request);
                }
                else if (HttpController.ActionExists(this.request))
                {
                    this.content = HttpController.RequestAction(this.request);
                }
                else
                {
                    this.content = HttpContent.Read(this.request.Server, this.request.Path);
                }

                if (this.content is HttpErrorContent)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
                }
                else if (this.request.IsWebSocket)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SWITCHING_PROTOCOLS);
                }
                else if (this.content.ETag == this.request.ETag && !this.content.ParsingRequired)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.NOT_MODIFIED);
                }
                else
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.OK);
                }
            }
            catch (FileNotFoundException ex)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.NOTFOUND);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.SERVERERROR);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }