.NET 4.6 HttpResponse.PushPromise methods to manage http/2 PUSH_PROMISE header

谁说胖子不能爱 提交于 2019-12-08 07:16:31

问题


I am a bit confused about PUSH PROMISE http/2 header handling in .NET4.6.

When I look HttpResponse.PushPromise there are two overloads:

One that accepts path to resource public void PushPromise(string path) - am assuming resource is then read and binary sent across to client.

Second public void PushPromise(string path, string method, NameValueCollection headers) that accepts sting method and NameValueCollection headers which I am failing to understand.

Why would I want to pass method (assuming HttpMethod like GET, POST, etc) and collection of headers inside PUSH PROMISE header?


回答1:


From reading the HTTP/2 spec (Section 8.2), here is what I gather:

Passing the method

PUSH_PROMISE frames are required to be cacheable and safe. You have the option of using GET and HEAD, as those are the only two http methods that are defined as both safe and cacheable.

Passing headers

Since PUSH_PROMISE frames are required to be cacheable, this could be used to add specific Cache-Control directives to the promise. Section 8.2.2 of the spec states that a client has the option to download the promised stream and can refuse it, which I imagine a client would do if it found that it had an up-to-date version of the resource in its cache.

Controlling caching is the most obvious reason I can see for why you might pass headers, but there may be other reasons as well. If you're writing a custom client, you may use certain X-Headers to provide other hints (that aren't related to caching) to the client so it can decide whether or not it wants to accept the promised stream.




回答2:


You'll want to pass headers for anything that will cause your response to vary (i.e. anything in your Vary response header). The biggest one I've found is compression.

Read those headers from the original client request and include them with your push promise, e.g.:

var headers = new NameValueCollection { { "accept-encoding", this.Request.Headers["accept-encoding"] } };
this.Response.PushPromise("~/Scripts/jquery.js", "GET", headers);`


来源:https://stackoverflow.com/questions/31567704/net-4-6-httpresponse-pushpromise-methods-to-manage-http-2-push-promise-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!