Skip to content

Commit

Permalink
Fix GrpcWebHandler not always resetting HTTP version back to 2.0 (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Mar 9, 2020
1 parent 0b37fb3 commit 06fa6c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
18 changes: 8 additions & 10 deletions examples/Blazor/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@

#endregion

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Server
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<Startup>()
.Build();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
10 changes: 10 additions & 0 deletions examples/Blazor/Server/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Grpc": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions examples/Blazor/Server/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
18 changes: 11 additions & 7 deletions src/Grpc.Net.Client.Web/GrpcWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,27 @@ private async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request
request.Content = new GrpcWebRequestContent(request.Content, _mode);
if (_httpVersion != null)
{
// This doesn't guarantee that the specified version is used. Some handlers will ignore it.
// For example, version in the browser always negotiated by the browser and HttpClient
// uses what the browser has negotiated.
request.Version = _httpVersion;
}

var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (_httpVersion != null)
{
// If a HTTP version has been specified then we need to reset it back to 2.0.
// The gRPC client validates HTTP version 2.0.
response.Version = HttpVersion.Version20;
}

if (IsMatchingResponseContentType(_mode, response.Content.Headers.ContentType?.MediaType))
{
response.Content = new GrpcWebResponseContent(response.Content, _mode, response.TrailingHeaders);
}

// The gRPC client validates HTTP version 2.0 and will error if it isn't. Always set
// the version to 2.0, even for non-gRPC content type. The client maps HTTP status codes
// to gRPC statuses, e.g. HTTP 404 -> gRPC unimplemented.
//
// Note: Some handlers don't correctly set HttpResponseMessage.Version.
// We can't rely on it being set correctly. It is safest to always set it to 2.0.
response.Version = HttpVersion.Version20;

return response;
}

Expand Down

0 comments on commit 06fa6c2

Please sign in to comment.