Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BCP config, error details and middleware improvements (#2483)
- Loading branch information
Showing
43 changed files
with
525 additions
and
176 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
47 changes: 47 additions & 0 deletions
47
src/HotChocolate/AspNetCore/src/AspNetCore/Extensions/HttpRequestExtensions.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace HotChocolate.AspNetCore | ||
{ | ||
internal static class HttpRequestExtensions | ||
{ | ||
internal static bool AcceptHeaderContainsHtml(this HttpRequest request) | ||
{ | ||
return request.Headers.TryGetValue(HeaderNames.Accept, out StringValues values) && | ||
values.Count > 0 && values[0].Contains("text/html"); | ||
} | ||
|
||
internal static bool IsGetOrHeadMethod(this HttpRequest request) | ||
{ | ||
return HttpMethods.IsGet(request.Method) || HttpMethods.IsHead(request.Method); | ||
} | ||
|
||
internal static bool PathEndsInSlash(this HttpRequest request) | ||
{ | ||
return request.Path.Value?.EndsWith("/", StringComparison.Ordinal) ?? false; | ||
} | ||
|
||
internal static bool TryMatchPath( | ||
this HttpRequest request, | ||
PathString matchUrl, | ||
bool forDirectory, | ||
out PathString subpath) | ||
{ | ||
var path = request.Path; | ||
|
||
if (forDirectory && !request.PathEndsInSlash()) | ||
{ | ||
path += new PathString("/"); | ||
} | ||
|
||
if (path.StartsWithSegments(matchUrl, out subpath)) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/HotChocolate/AspNetCore/src/AspNetCore/Extensions/HttpResponseExtensions.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HotChocolate.AspNetCore.Utilities; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace HotChocolate.AspNetCore | ||
{ | ||
internal static class HttpResponseExtensions | ||
{ | ||
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions | ||
{ | ||
IgnoreNullValues = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase | ||
}; | ||
|
||
internal static Task WriteAsJsonAsync<TValue>( | ||
this HttpResponse response, | ||
TValue value, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
response.ContentType = ContentType.Json; | ||
response.StatusCode = 200; | ||
|
||
return JsonSerializer.SerializeAsync( | ||
response.Body, | ||
value, | ||
_serializerOptions, | ||
cancellationToken); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...colate/AspNetCore/src/AspNetCore/Extensions/RoutingEndpointConventionBuilderExtensions.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using HotChocolate.AspNetCore; | ||
|
||
namespace Microsoft.AspNetCore.Builder | ||
{ | ||
public static class RoutingEndpointConventionBuilderExtensions | ||
{ | ||
public static TBuilder WithToolOptions<TBuilder>(this TBuilder builder, ToolOptions options) | ||
where TBuilder : IEndpointConventionBuilder | ||
{ | ||
return builder.WithMetadata(options); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.