-
-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #874 from Cysharp/feature/JsonTranscodingCallContext
Add support for some members of JsonTranscodingServerCallContext.
- Loading branch information
Showing
3 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 61 additions & 8 deletions
69
src/MagicOnion.Server.JsonTranscoding/MagicOnionJsonTranscodingServerCallContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,77 @@ | ||
using System.Net.Sockets; | ||
using Grpc.AspNetCore.Server; | ||
using Grpc.Core; | ||
using MagicOnion.Server.Binder; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MagicOnion.Server.JsonTranscoding; | ||
|
||
public class MagicOnionJsonTranscodingServerCallContext(IMagicOnionGrpcMethod method) : ServerCallContext, IServerCallContextFeature | ||
public class MagicOnionJsonTranscodingServerCallContext(HttpContext httpContext, IMagicOnionGrpcMethod method) : ServerCallContext, IServerCallContextFeature | ||
{ | ||
protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders) => throw new NotImplementedException(); | ||
Metadata? requestHeaders; | ||
|
||
public ServerCallContext ServerCallContext => this; | ||
|
||
protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders) | ||
{ | ||
foreach (var header in responseHeaders) | ||
{ | ||
var key = header.IsBinary ? header.Key + "-bin" : header.Key; | ||
var value = header.IsBinary ? Convert.ToBase64String(header.ValueBytes) : header.Value; | ||
|
||
httpContext.Response.Headers.TryAdd(key, value); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions? options) => throw new NotImplementedException(); | ||
|
||
protected override string MethodCore { get; } = $"{method.ServiceName}/{method.MethodName}"; | ||
protected override string HostCore => throw new NotImplementedException(); | ||
protected override string PeerCore => throw new NotImplementedException(); | ||
protected override DateTime DeadlineCore => throw new NotImplementedException(); | ||
protected override Metadata RequestHeadersCore => throw new NotImplementedException(); | ||
protected override CancellationToken CancellationTokenCore => throw new NotImplementedException(); | ||
|
||
protected override string HostCore { get; } = httpContext.Request.Host.Value ?? string.Empty; | ||
|
||
protected override string PeerCore { get; } = httpContext.Connection.RemoteIpAddress switch | ||
{ | ||
{ AddressFamily: AddressFamily.InterNetwork } => $"ipv4:{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}", | ||
{ AddressFamily: AddressFamily.InterNetworkV6 } => $"ipv6:{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}", | ||
{ } => $"unknown:{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}", | ||
_ => "unknown" | ||
}; | ||
|
||
protected override DateTime DeadlineCore => DateTime.MaxValue; // No deadline | ||
|
||
protected override Metadata RequestHeadersCore | ||
{ | ||
get | ||
{ | ||
if (requestHeaders is null) | ||
{ | ||
requestHeaders = new Metadata(); | ||
foreach (var header in httpContext.Request.Headers) | ||
{ | ||
var key = header.Key; | ||
var value = header.Value; | ||
if (key.EndsWith("-bin")) | ||
{ | ||
key = key.Substring(0, key.Length - 4); | ||
requestHeaders.Add(key, Convert.FromBase64String(value.ToString())); | ||
} | ||
else | ||
{ | ||
requestHeaders.Add(key, value.ToString()); | ||
} | ||
} | ||
} | ||
|
||
return requestHeaders; | ||
} | ||
} | ||
|
||
protected override CancellationToken CancellationTokenCore => httpContext.RequestAborted; | ||
protected override Metadata ResponseTrailersCore => throw new NotImplementedException(); | ||
protected override Status StatusCore { get; set; } | ||
protected override WriteOptions? WriteOptionsCore { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
protected override AuthContext AuthContextCore => throw new NotImplementedException(); | ||
public ServerCallContext ServerCallContext => this; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters