|
| 1 | +using GenHTTP.Adapters.WiredIO.Server; |
| 2 | +using GenHTTP.Adapters.WiredIO.Types; |
| 3 | + |
| 4 | +using GenHTTP.Api.Content; |
| 5 | +using GenHTTP.Api.Infrastructure; |
| 6 | +using GenHTTP.Api.Protocol; |
| 7 | + |
| 8 | +namespace GenHTTP.Adapters.WiredIO.Mapping; |
| 9 | + |
| 10 | +public static class Bridge |
| 11 | +{ |
| 12 | + |
| 13 | + public static async ValueTask MapAsync(HttpContext context, IHandler handler, IServer? server = null, IServerCompanion? companion = null, string? registeredPath = null) |
| 14 | + { |
| 15 | + var actualServer = server ?? new ImplicitServer(handler, companion); |
| 16 | + |
| 17 | + try |
| 18 | + { |
| 19 | + using var request = new Request(actualServer, context); |
| 20 | + |
| 21 | + if (registeredPath != null) |
| 22 | + { |
| 23 | + AdvanceTo(request, registeredPath); |
| 24 | + } |
| 25 | + |
| 26 | + using var response = await handler.HandleAsync(request); |
| 27 | + |
| 28 | + if (response == null) |
| 29 | + { |
| 30 | + context.Response.StatusCode = 404; |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + await WriteAsync(response, context); |
| 35 | + |
| 36 | + actualServer.Companion?.OnRequestHandled(request, response); |
| 37 | + } |
| 38 | + } |
| 39 | + catch (Exception e) |
| 40 | + { |
| 41 | + actualServer.Companion?.OnServerError(ServerErrorScope.ServerConnection, context.Connection.RemoteIpAddress, e); |
| 42 | + throw; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static async ValueTask WriteAsync(IResponse response, HttpContext context) |
| 47 | + { |
| 48 | + var target = context.Response; |
| 49 | + |
| 50 | + target.StatusCode = response.Status.RawStatus; |
| 51 | + |
| 52 | + foreach (var header in response.Headers) |
| 53 | + { |
| 54 | + target.Headers.Append(header.Key, header.Value); |
| 55 | + } |
| 56 | + |
| 57 | + if (response.Modified != null) |
| 58 | + { |
| 59 | + target.Headers.LastModified = response.Modified.Value.ToUniversalTime().ToString("r"); |
| 60 | + } |
| 61 | + |
| 62 | + if (response.Expires != null) |
| 63 | + { |
| 64 | + target.Headers.Expires = response.Expires.Value.ToUniversalTime().ToString("r"); |
| 65 | + } |
| 66 | + |
| 67 | + if (response.HasCookies) |
| 68 | + { |
| 69 | + foreach (var cookie in response.Cookies) |
| 70 | + { |
| 71 | + if (cookie.Value.MaxAge != null) |
| 72 | + { |
| 73 | + target.Cookies.Append(cookie.Key, cookie.Value.Value, new() |
| 74 | + { |
| 75 | + MaxAge = TimeSpan.FromSeconds(cookie.Value.MaxAge.Value) |
| 76 | + }); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + target.Cookies.Append(cookie.Key, cookie.Value.Value); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (response.Content != null) |
| 86 | + { |
| 87 | + target.ContentLength = (long?)response.ContentLength ?? (long?)response.Content.Length; |
| 88 | + |
| 89 | + target.ContentType = response.ContentType?.Charset != null ? $"{response.ContentType?.RawType}; charset={response.ContentType?.Charset}" : response.ContentType?.RawType; |
| 90 | + |
| 91 | + if (response.ContentEncoding != null) |
| 92 | + { |
| 93 | + target.Headers.ContentEncoding = response.ContentEncoding; |
| 94 | + } |
| 95 | + |
| 96 | + await response.Content.WriteAsync(target.Body, 65 * 1024); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void AdvanceTo(Request request, string registeredPath) |
| 101 | + { |
| 102 | + var parts = registeredPath.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| 103 | + |
| 104 | + foreach (var _ in parts) |
| 105 | + { |
| 106 | + request.Target.Advance(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments