Esempio n. 1
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. 2
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. 3
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. 4
0
        public static AppDelegate AsyncApp()
        {
            return call =>
            {
                var request = new Request(call);
                var response = new Response
                {
                    ContentType = "text/html",
                };
                var wilson = "left - right\r\n123456789012\r\nhello world!\r\n";

                response.StartAsync()
                    .Then(()=>
                    {
                        Delay
                    });

                return response.GetResultAsync();

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try
                    {
                        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.Start(() => TimerLoop(350, response.Error,
                            () => 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 (Exception ex)
                    {
                        callback(default(ResultParameters), ex);
                    }
                });
            };
        }
Esempio n. 5
0
        public void Sync_Exception_in_response_body_stream_should_be_formatted_as_it_passes_through()
        {
            var stack = Build(b => b
                .UseShowExceptions()
                .UseFunc<AppDelegate>(_ => appCall =>
                {
                    ResultParameters rawResult = new ResultParameters();
                    rawResult.Body = stream =>
                        {
                            byte[] bodyBytes = Encoding.ASCII.GetBytes("<p>so far so good</p>");
                            stream.Write(bodyBytes, 0, bodyBytes.Length);
                            throw new ApplicationException("failed sending body sync");
                        };
                    rawResult.Status = 200;
                    Response appResult = new Response(rawResult);
                    appResult.Headers.SetHeader("Content-Type", "text/html");
                    appResult.Start();
                    return appResult.ResultTask;
                }));

            ResultParameters result = stack(new Request().Call).Result;

            Assert.That(result.Status, Is.EqualTo(200));
            Assert.That(result.Headers.GetHeader("Content-Type"), Is.EqualTo("text/html"));
            String bodyText = ReadBody(result.Body);
            Assert.That(bodyText, Is.StringContaining("<p>so far so good</p>"));
            Assert.That(bodyText, Is.StringContaining("failed sending body sync"));
        }