Esempio n. 1
0
        public static AppDelegate App()
        {
            return call =>
            {
                var request = new Request(call);
                var response = new Response { Buffer = true, ContentType = "text/html" };
                var wilson = "left - right\r\n123456789012\r\nhello world!\r\n";

                var href = "?flip=left";
                if (request.Query["flip"] == "left")
                {
                    wilson = wilson.Split(new[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(line => new string(line.Reverse().ToArray()))
                        .Aggregate("", (agg, line) => agg + line + System.Environment.NewLine);
                    href = "?flip=right";
                }
                response.Write("<title>Wilson</title>");
                response.Write("<pre>");
                response.Write(wilson);
                response.Write("</pre>");
                if (request.Query["flip"] == "crash")
                {
                    throw new ApplicationException("Wilson crashed!");
                }
                response.Write("<p><a href='" + href + "'>flip!</a></p>");
                response.Write("<p><a href='?flip=crash'>crash!</a></p>");
                response.End();

                return response.GetResultAsync();
            };
        }
Esempio n. 2
0
        public static AppDelegate Middleware(AppDelegate app)
        {
            return (call, callback) =>
            {
                Action<Exception, Action<ArraySegment<byte>>> showErrorMessage =
                    (ex, write) =>
                        ErrorPage(call, ex, text =>
                        {
                            var data = Encoding.ASCII.GetBytes(text);
                            write(new ArraySegment<byte>(data));
                        });

                Action<Exception> showErrorPage = ex =>
                {
                    var response = new Response(callback) { Status = "500 Internal Server Error", ContentType = "text/html" };
                    response.Start(() =>
                    {
                        showErrorMessage(ex, data => response.Write(data));
                        response.End();
                    });
                };

                try
                {
                    app(call, (result, error) =>
                    {
                        if (error != null)
                        {
                            showErrorPage(error);
                        }
                        else
                        {
                            var body = result.Body;
                            result.Body = (write, end, cancel) =>
                            {
                                showErrorPage = ex =>
                                {
                                    if (ex != null)
                                    {
                                        showErrorMessage(ex, data => write(data, null));
                                    }
                                    end(null);
                                };
                                body.Invoke(write, showErrorPage, cancel);
                            };
                            callback(result, null);
                        }
                    });
                }
                catch (Exception exception)
                {
                    showErrorPage(exception);
                }
            };
        }
Esempio n. 3
0
        public static AppDelegate Middleware(AppDelegate app)
        {
            return (env, result, fault) =>
            {
                Action<Exception, Action<ArraySegment<byte>>> showErrorMessage =
                    (ex, write) =>
                        ErrorPage(env, ex, text =>
                        {
                            var data = Encoding.ASCII.GetBytes(text);
                            write(new ArraySegment<byte>(data));
                        });

                Action<Exception> showErrorPage = ex =>
                {
                    var response = new Response(result) { Status = "500 Internal Server Error", ContentType = "text/html" };
                    response.Start(() =>
                    {
                        showErrorMessage(ex, data => response.Write(data));
                        response.End();
                    });
                };

                try
                {
                    app(
                        env,
                        (status, headers, body) =>
                            result(
                                status,
                                headers,
                                (write, flush, end, token) =>
                                {
                                    showErrorPage = ex =>
                                    {
                                        if (ex != null)
                                        {
                                            showErrorMessage(ex, data => write(data));
                                        }
                                        end(null);
                                    };
                                    body(
                                        write,
                                        flush,
                                        showErrorPage,
                                        token);
                                }),
                        ex => showErrorPage(ex));
                }
                catch (Exception exception)
                {
                    showErrorPage(exception);
                }
            };
        }
Esempio n. 4
0
        public void Finish_will_call_result_delegate_with_current_status_and_headers()
        {
            var response = new Response(Result)
            {
                Status = "200 Blah",
                ContentType = "text/blah",
            };

            Assert.That(_status, Is.Null);
            response.End();
            Assert.That(_status, Is.EqualTo("200 Blah"));
            Assert.That(_headers.GetHeader("Content-Type"), Is.EqualTo("text/blah"));
        }
Esempio n. 5
0
 public static AppDelegate ShowEnvironment()
 {
     return (env, result, fault) =>
     {
         var response = new Response(result)
         {
             Status = "200 OK",
             ContentType = "text/xml"
         };
         response.Start(() =>
         {
             var detail = env.Select(kv => new XElement(kv.Key, kv.Value));
             var xml = new XElement("xml", detail.OfType<object>().ToArray());
             response.End(xml.ToString());
         });
     };
 }
Esempio n. 6
0
        public Task Invoke(IDictionary<string, object> env)
        {
            var request = new Request(env);
            var response = new Response(env)
            {
                ContentType = "text/html",
            };
            var wilson = "left - right\r\n123456789012\r\nhello world!\r\n";

            var href = "?flip=left";
            if (request.Query["flip"] == "left")
            {
                wilson = wilson.Split(new[] {System.Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(line => new string(line.Reverse().ToArray()))
                    .Aggregate("", (agg, line) => agg + line + System.Environment.NewLine);
                href = "?flip=right";
            }

            TimerLoop(350,
                () => response.Write("<title>Hutchtastic</title>"),
                () => response.Write("<pre>"),
                () => response.Write(wilson),
                () => response.Write("</pre>"),
                () =>
                {
                    if (request.Query["flip"] == "crash")
                    {
                        throw new ApplicationException("Wilson crashed!");
                    }
                },
                () => response.Write("<p><a href='" + href + "'>flip!</a></p>"),
                () => response.Write("<p><a href='?flip=crash'>crash!</a></p>"),
                () => response.End())
            .Catch(errorInfo =>
            {
                response.End(errorInfo.Exception);
                return errorInfo.Handled();
            });

            return response.Task;
        }
Esempio n. 7
0
        public void Write_calls_will_spool_until_finish_is_called()
        {
            var resp = new Response(Result) { Status = "200 Yep" };
            resp.Write("this");
            resp.Write("is");
            resp.Write("a");
            resp.Write("test");
            resp.End();

            Assert.That(_status, Is.EqualTo("200 Yep"));
            var data = Encoding.UTF8.GetString(Consume());
            Assert.That(data, Is.EqualTo("thisisatest"));
        }