Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Propagate Form and ResponseCookie init options #675

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Microsoft.AspNetCore.Http/DefaultHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace Microsoft.AspNetCore.Http
{
public class DefaultHttpContext : HttpContext
{
private readonly Func<HttpRequest, FormFeature> _formFeatureFactory;
private readonly Func<IFeatureCollection, ResponseCookiesFeature> _responseCookiesFeatureFactory;

private FeatureReferences<FeatureInterfaces> _features;

private HttpRequest _request;
Expand All @@ -31,7 +34,14 @@ public DefaultHttpContext()
}

public DefaultHttpContext(IFeatureCollection features)
: this(features, null, null)
{
}

public DefaultHttpContext(IFeatureCollection features, Func<IFeatureCollection, ResponseCookiesFeature> responseCookiesFeatureFactory, Func<HttpRequest, FormFeature> formFeatureFactory)
{
_formFeatureFactory = formFeatureFactory;
_responseCookiesFeatureFactory = responseCookiesFeatureFactory;
Initialize(features);
}

Expand Down Expand Up @@ -172,10 +182,10 @@ public override void Abort()
}


protected virtual HttpRequest InitializeHttpRequest() => new DefaultHttpRequest(this);
protected virtual HttpRequest InitializeHttpRequest() => new DefaultHttpRequest(this, _formFeatureFactory);
protected virtual void UninitializeHttpRequest(HttpRequest instance) { }

protected virtual HttpResponse InitializeHttpResponse() => new DefaultHttpResponse(this);
protected virtual HttpResponse InitializeHttpResponse() => new DefaultHttpResponse(this, _responseCookiesFeatureFactory);
protected virtual void UninitializeHttpResponse(HttpResponse instance) { }

protected virtual ConnectionInfo InitializeConnectionInfo() => new DefaultConnectionInfo(Features);
Expand Down
6 changes: 1 addition & 5 deletions src/Microsoft.AspNetCore.Http/Features/FormFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ public FormFeature(HttpRequest request, FormOptions options)
{
throw new ArgumentNullException(nameof(request));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

_request = request;
_options = options;
_options = options ?? DefaultFormOptions;
}

private MediaTypeHeaderValue ContentType
Expand Down
22 changes: 10 additions & 12 deletions src/Microsoft.AspNetCore.Http/HttpContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
Expand All @@ -11,9 +10,9 @@ namespace Microsoft.AspNetCore.Http
{
public class HttpContextFactory : IHttpContextFactory
{
private readonly ObjectPool<StringBuilder> _builderPool;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly FormOptions _formOptions;
private readonly Func<IFeatureCollection, ResponseCookiesFeature> _responseCookiesFeatureFactory;
private readonly Func<HttpRequest, FormFeature> _formFeatureFactory;

public HttpContextFactory(ObjectPoolProvider poolProvider, IOptions<FormOptions> formOptions)
: this(poolProvider, formOptions, httpContextAccessor: null)
Expand All @@ -31,8 +30,13 @@ public HttpContextFactory(ObjectPoolProvider poolProvider, IOptions<FormOptions>
throw new ArgumentNullException(nameof(formOptions));
}

_builderPool = poolProvider.CreateStringBuilderPool();
_formOptions = formOptions.Value;

var options = formOptions.Value;
var builderPool = poolProvider.CreateStringBuilderPool();

_responseCookiesFeatureFactory = features => new ResponseCookiesFeature(features, builderPool);
_formFeatureFactory = request => new FormFeature(request, options);

_httpContextAccessor = httpContextAccessor;
}

Expand All @@ -43,18 +47,12 @@ public HttpContext Create(IFeatureCollection featureCollection)
throw new ArgumentNullException(nameof(featureCollection));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be missing it with that example? It will create its own ResponseCookiesFeature with the ???

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, but it looses the pooling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need DI in the feature collection ;-)

}

var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool);
featureCollection.Set<IResponseCookiesFeature>(responseCookiesFeature);

var httpContext = new DefaultHttpContext(featureCollection);
var httpContext = new DefaultHttpContext(featureCollection, _responseCookiesFeatureFactory, _formFeatureFactory);
if (_httpContextAccessor != null)
{
_httpContextAccessor.HttpContext = httpContext;
}

var formFeature = new FormFeature(httpContext.Request, _formOptions);
featureCollection.Set<IFormFeature>(formFeature);

return httpContext;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Microsoft.AspNetCore.Http/Internal/DefaultHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ namespace Microsoft.AspNetCore.Http.Internal
{
public class DefaultHttpRequest : HttpRequest
{
private static readonly Func<HttpRequest, FormFeature> _defaultFormFeatureFactory
= request => new FormFeature(request);

private readonly Func<HttpRequest, FormFeature> _formFeatureFactory;

private HttpContext _context;
private FeatureReferences<FeatureInterfaces> _features;

public DefaultHttpRequest(HttpContext context)
: this(context, _defaultFormFeatureFactory)
{
}

public DefaultHttpRequest(HttpContext context, Func<HttpRequest, FormFeature> formFeatureFactory)
{
_formFeatureFactory = formFeatureFactory ?? _defaultFormFeatureFactory;
Initialize(context);
}

Expand All @@ -41,7 +52,7 @@ public virtual void Uninitialize()
_features.Fetch(ref _features.Cache.Query, f => new QueryFeature(f));

private IFormFeature FormFeature =>
_features.Fetch(ref _features.Cache.Form, this, f => new FormFeature(f));
_features.Fetch(ref _features.Cache.Form, this, request => _formFeatureFactory(request));

private IRequestCookiesFeature RequestCookiesFeature =>
_features.Fetch(ref _features.Cache.Cookies, f => new RequestCookiesFeature(f));
Expand Down
13 changes: 12 additions & 1 deletion src/Microsoft.AspNetCore.Http/Internal/DefaultHttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ namespace Microsoft.AspNetCore.Http.Internal
{
public class DefaultHttpResponse : HttpResponse
{
private static readonly Func<IFeatureCollection, ResponseCookiesFeature> _defaultResponseCookiesFeatureFactory
= features => new ResponseCookiesFeature(features);

private readonly Func<IFeatureCollection, ResponseCookiesFeature> _responseCookiesFeatureFactory;

private HttpContext _context;
private FeatureReferences<FeatureInterfaces> _features;

public DefaultHttpResponse(HttpContext context)
: this(context, null)
{
}

public DefaultHttpResponse(HttpContext context, Func<IFeatureCollection, ResponseCookiesFeature> responseCookiesFeatureFactory)
{
_responseCookiesFeatureFactory = responseCookiesFeatureFactory ?? _defaultResponseCookiesFeatureFactory;
Initialize(context);
}

Expand All @@ -35,7 +46,7 @@ public virtual void Uninitialize()
_features.Fetch(ref _features.Cache.Response, f => null);

private IResponseCookiesFeature ResponseCookiesFeature =>
_features.Fetch(ref _features.Cache.Cookies, f => new ResponseCookiesFeature(f));
_features.Fetch(ref _features.Cache.Cookies, f => _responseCookiesFeatureFactory(f));


public override HttpContext HttpContext { get { return _context; } }
Expand Down