|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 | 2 | using System.Globalization;
|
4 | 3 | using System.IO;
|
5 | 4 | using System.Threading.Tasks;
|
6 |
| -using Firefly.Http; |
7 | 5 | using Gate;
|
8 | 6 | using Pretzel.Logic.Extensions;
|
| 7 | +using Owin; |
| 8 | +using Microsoft.Owin.Hosting; |
9 | 9 |
|
10 | 10 | namespace Pretzel
|
11 | 11 | {
|
@@ -39,77 +39,92 @@ public bool Start()
|
39 | 39 | return false;
|
40 | 40 | }
|
41 | 41 |
|
42 |
| - var server = new ServerFactory(); |
43 |
| - host = server.Create(NewServerCallback, Port); |
| 42 | + var options = new StartOptions |
| 43 | + { |
| 44 | + ServerFactory = "Nowin", |
| 45 | + Port = Port |
| 46 | + }; |
| 47 | + |
| 48 | + Startup.Content = content; |
| 49 | + host = WebApp.Start<Startup>(options); |
44 | 50 |
|
45 | 51 | IsRunning = true;
|
46 | 52 |
|
47 | 53 | return true;
|
48 | 54 | }
|
49 | 55 |
|
50 |
| - public bool Stop() |
| 56 | + public class Startup |
51 | 57 | {
|
52 |
| - if (!IsRunning) |
53 |
| - { |
54 |
| - return false; |
55 |
| - } |
56 |
| - |
57 |
| - host.Dispose(); |
58 |
| - host = null; |
| 58 | + public static IWebContent Content { get; set; } |
59 | 59 |
|
60 |
| - return true; |
61 |
| - } |
62 |
| - |
63 |
| - Task NewServerCallback(IDictionary<string, object> env) |
64 |
| - { |
65 |
| - var path = (string)env[OwinConstants.RequestPath]; |
| 60 | + public void Configuration(IAppBuilder app) |
| 61 | + { |
| 62 | + app.Run(context => |
| 63 | + { |
| 64 | + var path = context.Request.Path.Value; |
| 65 | + var env = context.Request.Environment; |
66 | 66 |
|
67 |
| - Tracing.Info(path); |
| 67 | + Tracing.Info(path); |
68 | 68 |
|
69 |
| - if (!content.IsAvailable(path)) |
70 |
| - { |
71 |
| - var path404 = "/404.html"; |
72 |
| - var use404 = content.IsAvailable(path404); |
| 69 | + if (!Content.IsAvailable(path)) |
| 70 | + { |
| 71 | + var path404 = "/404.html"; |
| 72 | + var use404 = Content.IsAvailable(path404); |
| 73 | + |
| 74 | + var response = new Response(env) { ContentType = use404 ? path404.MimeType() : path.MimeType() }; |
| 75 | + using (var writer = new StreamWriter(response.OutputStream)) |
| 76 | + { |
| 77 | + if (use404) |
| 78 | + { |
| 79 | + writer.Write(Content.GetContent(path404)); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + writer.Write("Page not found: " + path); |
| 84 | + } |
| 85 | + } |
| 86 | + return TaskHelpers.Completed(); |
| 87 | + } |
73 | 88 |
|
74 |
| - var response = new Response(env) { ContentType = use404 ? path404.MimeType() : path.MimeType() }; |
75 |
| - using (var writer = new StreamWriter(response.OutputStream)) |
76 |
| - { |
77 |
| - if (use404) |
| 89 | + if (path.MimeType().IsBinaryMime()) |
| 90 | + { |
| 91 | + var fileContents = Content.GetBinaryContent(path); |
| 92 | + var response = new Response(env) { ContentType = path.MimeType() }; |
| 93 | + response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) }; |
| 94 | + response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) }; |
| 95 | + response.Write(new ArraySegment<byte>(fileContents)); |
| 96 | + } |
| 97 | + else if (Content.IsDirectory(path) && !path.EndsWith("/")) |
78 | 98 | {
|
79 |
| - writer.Write(content.GetContent(path404)); |
| 99 | + // if path is a directory without trailing slash, redirects to the same url with a trailing slash |
| 100 | + var response = new Response(env) { Status = "301 Moved Permanently" }; |
| 101 | + response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", context.Request.LocalPort, path) }; |
80 | 102 | }
|
81 | 103 | else
|
82 | 104 | {
|
83 |
| - writer.Write("Page not found: " + path); |
| 105 | + var response = new Response(env) { ContentType = path.MimeType() }; |
| 106 | + using (var writer = new StreamWriter(response.OutputStream)) |
| 107 | + { |
| 108 | + writer.Write(Content.GetContent(path)); |
| 109 | + } |
84 | 110 | }
|
85 |
| - } |
86 |
| - return TaskHelpers.Completed(); |
| 111 | + |
| 112 | + return Task.Delay(0); |
| 113 | + }); |
87 | 114 | }
|
| 115 | + } |
88 | 116 |
|
89 |
| - if (path.MimeType().IsBinaryMime()) |
90 |
| - { |
91 |
| - var fileContents = content.GetBinaryContent(path); |
92 |
| - var response = new Response(env) { ContentType = path.MimeType() }; |
93 |
| - response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) }; |
94 |
| - response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) }; |
95 |
| - response.Write(new ArraySegment<byte>(fileContents)); |
96 |
| - } |
97 |
| - else if (content.IsDirectory(path) && !path.EndsWith("/")) |
98 |
| - { |
99 |
| - //if path is a directory without trailing slash, redirects to the same url with a trailing slash |
100 |
| - var response = new Response(env) { Status = "301 Moved Permanently" }; |
101 |
| - response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", Port, path) }; |
102 |
| - } |
103 |
| - else |
| 117 | + public bool Stop() |
| 118 | + { |
| 119 | + if (!IsRunning) |
104 | 120 | {
|
105 |
| - var response = new Response(env) { ContentType = path.MimeType() }; |
106 |
| - using (var writer = new StreamWriter(response.OutputStream)) |
107 |
| - { |
108 |
| - writer.Write(content.GetContent(path)); |
109 |
| - } |
| 121 | + return false; |
110 | 122 | }
|
111 | 123 |
|
112 |
| - return TaskHelpers.Completed(); |
| 124 | + host.Dispose(); |
| 125 | + host = null; |
| 126 | + |
| 127 | + return true; |
113 | 128 | }
|
114 | 129 |
|
115 | 130 | #region IDisposable
|
|
0 commit comments