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"; } 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>"); return response.EndAsync(); }
public static AppFunc Middleware(AppFunc app) { return env => { Action<Exception, Action<byte[], int, int>> showErrorMessage = (ex, write) => ErrorPage(env, ex, text => { var data = Encoding.ASCII.GetBytes(text); write(data, 0, data.Length); }); Func<Exception, Task> showErrorPage = ex => { var response = new Response(env) { Status = "500 Internal Server Error", ContentType = "text/html" }; showErrorMessage(ex, response.Write); return response.EndAsync(); }; // Don't try to modify the headers after the first write has occurred. TriggerStream triggerStream = new TriggerStream(env.Get<Stream>(OwinConstants.ResponseBody)); env[OwinConstants.ResponseBody] = triggerStream; bool bodyHasStarted = false; triggerStream.OnFirstWrite = () => { bodyHasStarted = true; }; try { return app(env) .Catch(errorInfo => { if (!bodyHasStarted) { showErrorPage(errorInfo.Exception).Wait(); } else { showErrorMessage(errorInfo.Exception, triggerStream.Write); } return errorInfo.Handled(); }); } catch (Exception exception) { if (!bodyHasStarted) { return showErrorPage(exception); } else { showErrorMessage(exception, triggerStream.Write); return TaskHelpers.Completed(); } } }; }
public static AppDelegate Middleware(AppDelegate app) { return call => { Action<Exception, Action<byte[]>> showErrorMessage = (ex, write) => ErrorPage(call, ex, text => { var data = Encoding.ASCII.GetBytes(text); write(data); }); Func<Exception, Task<ResultParameters>> showErrorPage = ex => { var response = new Response() { Status = "500 Internal Server Error", ContentType = "text/html" }; showErrorMessage(ex, data => response.Write(data)); return response.EndAsync(); }; try { return app(call) .Then(result => { if (result.Body != null) { var nestedBody = result.Body; result.Body = stream => { try { return nestedBody(stream).Catch( errorInfo => { showErrorMessage(errorInfo.Exception, data => stream.Write(data, 0, data.Length)); return errorInfo.Handled(); }); } catch (Exception ex) { showErrorMessage(ex, data => stream.Write(data, 0, data.Length)); return TaskHelpers.Completed(); } }; } return result; }) .Catch(errorInfo => { return errorInfo.Handled(showErrorPage(errorInfo.Exception).Result); }); } catch (Exception exception) { return showErrorPage(exception); } }; }
public static Task Call(IDictionary<string, object> env) { Response response = new Response(env); response.StatusCode = 404; response.ReasonPhrase = "Not Found"; response.Headers.SetHeader("Content-Type", new[] {"text/html"}); response.OutputStream.Write(body, 0, body.Length); return response.EndAsync(); }
public void Finish_will_call_result_delegate_with_current_status_and_headers() { var response = new Response() { Status = "200 Blah", ContentType = "text/blah", }; ResultParameters result = response.EndAsync().Result; Assert.That(result.Status, Is.EqualTo(200)); Assert.That(result.Headers.GetHeader("Content-Type"), Is.EqualTo("text/blah")); }
public void Normal_request_should_pass_through_unchanged() { var stack = Build(b => b .UseShowExceptions() .UseFunc<AppDelegate>(_ => appCall => { Response appResult = new Response(200); appResult.Headers.SetHeader("Content-Type", "text/plain"); appResult.Headers.SetHeader("Content-Length", "5"); appResult.Write("Hello"); return appResult.EndAsync(); })); ResultParameters result = stack(new Request().Call).Result; Assert.That(result.Status, Is.EqualTo(200)); Assert.That(ReadBody(result.Body), Is.EqualTo("Hello")); }
public void Write_calls_will_spool_until_finish_is_called() { var resp = new Response() { Status = "200 Yep" }; resp.Write("this"); resp.Write("is"); resp.Write("a"); resp.Write("test"); ResultParameters result = resp.EndAsync().Result; Assert.That(result.Status, Is.EqualTo(200)); var data = Encoding.UTF8.GetString(Consume(result.Body)); Assert.That(data, Is.EqualTo("thisisatest")); }
private static AppFunc Fail(int status, string body, string headerName = null, string headerValue = null) { return env => { Response response = new Response(env); response.StatusCode = status; response.Headers .SetHeader("Content-Type", "text/plain") .SetHeader("Content-Length", body.Length.ToString(CultureInfo.InvariantCulture)) .SetHeader("X-Cascade", "pass"); if (headerName != null && headerValue != null) { response.Headers.SetHeader(headerName, headerValue); } response.Write(body); return response.EndAsync(); }; }