Exemple #1
0
        public bool Contains(HttpRequest request)
        {
            var requestPath = request.Path.TrimWhiteSpace();
            string filePath = server.Root + @"\" + requestPath.Replace(@"/", @"\");

            return Contains(filePath);
        }
        public static HttpContent GetIntArrayJSON(HttpRequest request)
        {
            var json = new Dictionary<string, object>();
            json.Add("data", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });

            return new HttpContent(json);
        }
        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);
        }
Exemple #4
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 (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.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)
            {
                Program.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.NOTFOUND);
                this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                Program.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.SERVERERROR);
                this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }
        public static HttpContent UpdateTable(HttpRequest request)
        {
            var json = new Dictionary<string, object>();
            /*
            string[] row1 = { "blabla", "passed", Guid.NewGuid().ToString(), "booo" };
            string[] row2 = { "blabla", "failed", Guid.NewGuid().ToString(), "booo" };
            string[] row3 = { "blabla", "whatever", Guid.NewGuid().ToString(), "booo" };

            string[][] table = { row1, row2, row3 };

            json.Add("testTabelData", table.ToHTML());*/
            return new HttpContent(json);
        }
        public static HttpContent json(HttpRequest request)
        {
            try
            {
                var result = new Dictionary<string, object>();
                result.Add("request", Encoding.ASCII.GetString(request.Data));
                result.Add("guid", Guid.NewGuid().ToString());
                result.Add("html", @"<h1 class=""page-header"">" + request.Data + @"</h1><br/>" );

                if (Encoding.ASCII.GetString(request.Data) == "error") throw new ApplicationException("error test");

                return new HttpContent(result.ToJSON());
            }
            catch (Exception ex)
            {
                return new HttpErrorContent(ex);
            }
        }
        public static HttpContent Data(HttpRequest request)
        {
            var json = new List<Dictionary<string, object>>();
            int alternate = 1;

            var rand = new Random();
            rand.Next(100,220);

            int offset = rand.Next(100,220);

            for (int columnNumber=1; columnNumber<=10; columnNumber++)
            {
                if (alternate > 2) alternate = 1;
                var labelJSON = new Dictionary<string, object>();
                labelJSON.Add("col1", columnNumber + offset);
                labelJSON.Add("col2", alternate++);
                json.Add(labelJSON);
            }

            return new HttpContent(json);
        }
Exemple #8
0
 public static HttpContent Read(HttpRequest request, string requestView)
 {
     return Read(request, requestView, new Dictionary<string, HttpContent>() {});
 }
 public static HttpContent html(HttpRequest request)
 {
     return new HttpContent(request.Data, "text/html");
 }
 public static HttpContent Show(HttpRequest request)
 {
     return HttpContent.Read(request, "test.html");
 }
 public static HttpContent Test(HttpRequest request)
 {
     return HttpController.RequestAction("Test", "Show", request);
 }
Exemple #12
0
 public static HttpContent Read(HttpRequest request, string requestView, Dictionary<string, HttpContent> baseContent)
 {
     return Read(request.Server, requestView, baseContent);
 }
 public static HttpContent Index(HttpRequest request)
 {
     return HttpContent.Read(request, "index.html");
 }
        private void ServiceRequests()
        {
            // Read and parse request
            while (this.Open)
            {
                try
                {
                    if (this.Socket.Connected && this.RequestReady())
                    {
                        // reset timeout
                        timeout.Stop();
                        timeout.Start();

                        // return initial header
                        lock (Socket)
                        {
                            const string INIT_HEADER = "HTTP/1.1";
                            var bytes = Encoding.ASCII.GetBytes(INIT_HEADER);
                            this.Stream.Write(bytes, 0, bytes.Length);
                        }

                        // process request and get responce
                        var request = new HttpRequest(this);
                        var response = request.Responce.bytes;

                        // send responce
                        lock (Socket)
                        {
                            try
                            {
                                const int MAX_LENGTH = 8192;
                                for (int b = 0; b <= response.Length; b += MAX_LENGTH)
                                {
                                    int length = Math.Min(response.Length - b, MAX_LENGTH);
                                    this.Stream.Write(response, b, length);
                                }

                                this.Stream.Flush();
                            }
                            catch
                            {
                                // TODO: close connection?
                            }
                        }
                    }

                    Thread.Sleep(1);
                    this.Open &= this.Socket.Connected;
                }
                catch (Exception ex)
                {
                    Program.Log(ex);
                }
            }

            // close connections
            try
            {
                this.Stream.Close();
                this.Socket.Close();
            }
            catch
            {

            }

            this.Stopped = true;
        }
        public static HttpContent RequestAction(HttpRequest request)
        {
            string[] urlSplit = request.Path.Split('/');
            string controllerName = urlSplit[0];
            string actionName = urlSplit[1];

            return RequestAction(controllerName, actionName, request);
        }
        public static bool ActionExists(HttpRequest request)
        {
            string[] urlSplit = request.Path.Split('/');

            if (urlSplit.Length != 2) return false;

            string controllerName = urlSplit[0];
            string actionName = urlSplit[1];
            if (!controllers.ContainsKey(controllerName)) return false;
            if (!controllers[controllerName].actions.ContainsKey(actionName)) return false;

            return true;
        }
 public static HttpContent RequestPrimary(HttpRequest request)
 {
     object contentObject = HttpController.primary.primaryAction.Invoke(null, new object[] { request });
     return (HttpContent)contentObject;
 }
        public static HttpContent RequestAction(string controllerName, string actionName, HttpRequest request)
        {
            if (!controllers.ContainsKey(controllerName)) throw new FileNotFoundException("controller " + @"""" + controllerName + @"""" + " not found");
            HttpController controller = controllers[controllerName];

            if (!controller.actions.ContainsKey(actionName)) throw new FileNotFoundException("action " + @"""" + actionName + @"""" + " not found in controller " + @"""" + controllerName + @"""");
            MethodInfo action = controller.actions[actionName];

            object contentObject = action.Invoke(null, new object[] { request });
            return (HttpContent)contentObject;
        }
 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);
 }
Exemple #20
0
        public HttpCachedFile Get(HttpRequest request)
        {
            var requestPath = request.Path.TrimWhiteSpace();

            string filePath = server.Root + @"\" + requestPath.Replace(@"/", @"\");
            return Get(filePath);
        }