Hvordan gjøre fjerne noen httpresponse overskrifter på hvert svar som Server og ETag?

stemmer
1

Bruke IIS 8 vi kan fjerne ETag heading av conf , men hvordan du gjør det hvis vi bruker Kestrel eller en annen server gjennomføring?

med ASP.Net, 4 jeg brukte PreSendRequestHeaders ved Global.asax:

    protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
    {
        if (HttpContext.Current != null)
        {
            HttpContext.Current.Response.Headers.Remove(Server);
            HttpContext.Current.Response.Headers.Remove(ETag);
        }
    }

[Oppdater]

Jeg prøvde å bruke OnSendHeaders av IHttpResponseFeature men det fungerer ikke i det hele tatt tilfelle, especialy med statisk filer mellomvare

Publisert på 24/12/2014 klokken 13:41
kilden bruker
På andre språk...                            


1 svar

stemmer
1

Det jeg gjorde er en mellomvare for å overstyre httpcontext

public class RemoveResponseHeadersMiddleware
{
    readonly IEnumerable<string> _headersToRemove;
    readonly RequestDelegate _next;
    readonly ILoggerFactory _loggerFactory;
    /// <summary>
    /// Create an instance of <see cref="RemoveResponseHeadersMiddleware"/>
    /// </summary>
    /// <param name="next">the next <see cref="RequestDelegate"/> to call in the pipeline</param>
    /// <param name="optionsAccessor">the accessor to <see cref="CommaSeparatedListOptions"/> where headers to remove are configured</param>
    /// <param name="loggerFactory">the logger factory to create logger</param>
    public RemoveResponseHeadersMiddleware(RequestDelegate next, IOptions<CommaSeparatedListOptions> optionsAccessor, ILoggerFactory loggerFactory)
    {
        if (next == null)
            throw new ArgumentNullException("next");
        if (optionsAccessor == null || optionsAccessor.Options == null || optionsAccessor.Options.List == null)
            throw new ArgumentNullException("optionsAccessor");
        if (loggerFactory == null)
            throw new ArgumentNullException("loggerFactory");

        _next = next;
        _headersToRemove = optionsAccessor.Options.List;
        _loggerFactory = loggerFactory;
    }

    public async Task Invoke(HttpContext context)
    {
        await _next.Invoke(new RemoveHeaderHttpContext(context, _headersToRemove, _loggerFactory));            
    }
}

i påkalle metode I skape en httpcontext arkitekt med overstyring av httpresponse

public class RemoveHeaderHttpContext : HttpContext
{
    readonly HttpContext _parent;
    readonly HttpResponse _response;
    /// <summary>
    /// Create instance of <see cref="RemoveHeaderHttpContext"/>
    /// </summary>
    /// <param name="parent">the <see cref="HttpContext"/> to decorate/></param>
    /// <param name="headersToRemove">a list of unwanted header</param>
    /// <param name="loggerFactory">the logger factory to create logger</param>
    public RemoveHeaderHttpContext(HttpContext parent, IEnumerable<string> headersToRemove, ILoggerFactory loggerFactory)
    {
        if (parent == null)
            throw new ArgumentNullException("parent");
        if (headersToRemove == null)
            throw new ArgumentNullException("headersToRemove");
        if (loggerFactory == null)
            throw new ArgumentNullException("loggerFactory");

        _parent = parent;
        _response = new RemoveHeaderHttpResponse(this, parent.Response, headersToRemove, loggerFactory);
    }

    public override HttpResponse Response
    {
        get
        {
            return _response;
        }
    }

Dette httpresponse er en dekoratør av httpresponse heks overstyre overskrifter samling

public class RemoveHeaderHttpResponse : HttpResponse
{
    readonly HttpResponse _parent;
    readonly HttpContext _context;
    readonly IHeaderDictionary _headers;
    /// <summary>
    /// Create instance of <see cref="RemoveHeaderHttpResponse"/>
    /// </summary>
    /// <param name="context">the <see cref="HttpContext"/> associated to the HTTP respone</param>
    /// <param name="parent">the <see cref="HttpResponse"/> to decorate</param>
    /// <param name="headersToRemove">a list of unwanted header</param>
    /// <param name="loggerFactory">the logger factory to create logger</param>
    public RemoveHeaderHttpResponse(HttpContext context, HttpResponse parent, IEnumerable<string> headersToRemove, ILoggerFactory loggerFactory)
    {
        if (parent == null)
            throw new ArgumentNullException("parent");
        if (headersToRemove == null)
            throw new ArgumentNullException("headersToRemove");
        if (loggerFactory == null)
            throw new ArgumentNullException("loggerFactory");

        _parent = parent;
        _context = context;
        _headers = new RemoveHeaderHeaderDictionary(parent.Headers, headersToRemove, loggerFactory);
    }

    public override IHeaderDictionary Headers
    {
        get
        {
            return _headers;
        }
    }

og denne header ordbok filterhodene lagt ved annen mellomvare i rørledningen

public class RemoveHeaderHeaderDictionary : IHeaderDictionary
{
    readonly IHeaderDictionary _parent;
    readonly IEnumerable<string> _headersToRemove;
    /// <summary>
    /// Logger
    /// </summary>
    public ILogger Logger { get; private set; }
    /// <summary>
    /// Create an instance of <see cref="RemoveHeaderHeaderDictionary"/>
    /// </summary>
    /// <param name="parent">the <see cref="IHeaderDictionary"/> to decorate</param>
    /// <param name="headersToRemove">a list of unwanted header</param>
    /// <param name="loggerFactory">the logger factory to create logger</param>
    public RemoveHeaderHeaderDictionary(IHeaderDictionary parent, IEnumerable<string> headersToRemove, ILoggerFactory loggerFactory)
    {
        if (parent == null)
            throw new ArgumentNullException("parent");
        if (headersToRemove == null)
            throw new ArgumentNullException("headersToRemove");
        if (loggerFactory == null)
            throw new ArgumentNullException("loggerFactory");

        Logger = loggerFactory.Create<RemoveHeaderHeaderDictionary>();
        _parent = parent;
        _headersToRemove = headersToRemove;
        foreach (var header in headersToRemove)
            parent.Remove(header);
    }

    bool IsAllowedHeader(string header)
    {
        var allowed = !_headersToRemove.Any(h => h == header);
        Logger.WriteInformation(string.Format("{0} is {1}", header, allowed ? "allowed" : "not allowed"));
        return allowed;
    }

    public string this[string key]
    {
        get
        {
            return _parent[key];
        }

        set
        {
            if (IsAllowedHeader(key))
                _parent[key] = value;
        }
    }

men kanskje det er en mutch enklere måte?

Du kan finne hele koden på github, prosjekt ChatLe.HttpUtility

Svarte 24/12/2014 kl. 13:55
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more