作者:pfm4191006 | 来源:互联网 | 2022-12-20 16:24
ThecorequestionisabouttheuseoftheHTTPHeaders,includingRange,If-Range,Accept-Rangesand
The core question is about the use of the HTTP Headers, including Range, If-Range, Accept-Ranges and a user defined range specifier.
核心问题是关于HTTP头的使用,包括范围、If-Range、接受-范围和用户定义的范围说明符。
Here is a manufactured example to help illustrate my question. Assume I have a Web 2.0 style application that displays some sort of human readable documents. These documents are editorially broken up into pages (similar to articles you see on news websites). For this example, assume:
这里有一个人为的例子来说明我的问题。假设我有一个Web 2.0风格的应用程序,它显示了一些人类可读的文档。这些文档被编辑成页面(类似于你在新闻网站上看到的文章)。对于这个示例,假设:
- There is a document titled "HTTP Range Question" is broken up into three pages.
- 有一个名为“HTTP范围问题”的文档被分为三页。
- The shell page (
/document/shell/http-range-question
) knows the meta information about the document, including the number of pages.
- shell页面(/document/shell/http-range-question)知道关于文档的元信息,包括页面的数量。
- The first readable page of the document is loaded during the page onload event via an ajax GET and inserted onto the page.
- 文档的第一个可读页面是在页面onload事件期间通过ajax GET加载并插入到页面中。
- A UI control that looks like [ 1 2 3 All ] is at the bottom of the page, and clicking on a number will display that readable page (also loaded via ajax), and clicking "All" will display the entire document. Assume these URLS for the 1, 2, 3 and All use cases:
/document/content/http-range-question?page=1
- /文档/内容/ http-range-question ? = 1页
/document/content/http-range-question?page=2
- /文档/内容/ http-range-question ? = 2页
/document/content/http-range-question?page=3
- /文档/内容/ http-range-question ? = 3页
/document/content/http-range-question
- /文档/内容/ http-range-question
- 一个看起来像[1 2 3]的UI控件在页面的底部,单击一个数字将显示可读页面(也通过ajax加载),点击“All”将显示整个文档。假设这些url用于1,2,3和所有用例:/document/content/http-range-question?页面= 1 /文档/内容/ http-range-question吗?页面= 2 /文档/内容/ http-range-question吗?页面= 3 /文档/内容/ http-range-question
Now to the question. Can I use the HTTP Range headers instead part of the URL (e.g. a querystring parameter)? Maybe something like this on the GET /document/content/http-range-question
request:
现在的问题。我可以使用HTTP范围标头而不是URL的一部分(例如一个querystring参数)吗?可能在GET /document/content/http-range-question请求中有这样的内容:
Range: page=1
It looks like the spec only defines byte ranges as allowable, so even if I made my ajax calls work with my browser and server code, anything in the middle could break the contract (e.g. a caching proxy server).
看起来规范只定义了允许的字节范围,所以即使我让ajax调用与浏览器和服务器代码一起工作,中间的任何东西都可能破坏契约(例如缓存代理服务器)。
Range: bytes=0-499
Any opinions or real world examples of custom range specifiers?
对自定义范围说明符有什么看法或实际示例吗?
Update: I did find a similar question about the Range header (Paging in a Rest Collection) where they mention that Dojo's JsonRestStore uses a custom Range header value.
更新:我确实发现了一个类似的关于范围标头(在Rest集合中分页)的问题,在这个问题中,他们提到Dojo的JsonRestStore使用自定义范围标头值。
Range: items=0-24
4 个解决方案
32
Absolutely - you are free to specify any range units you like.
绝对-你可以自由指定任何你喜欢的范围单位。
From RFC 2616:
RFC 2616:
3.12 Range Units
3.12单位范围
HTTP/1.1 allows a client to request that only part (a range of) the
response entity be included within the response. HTTP/1.1 uses range units in the Range (section 14.35) and Content-Range (section 14.16)
header fields. An entity can be broken down into subranges according to various structural units.
HTTP/1.1允许客户端请求响应中只包含响应实体的一部分(范围)。HTTP/1.1使用范围单元(第14.35节)和内容范围(第14.16节)头字段。一个实体可以根据不同的结构单元分解成子程序。
range-unit = bytes-unit | other-range-unit
bytes-unit = "bytes"
other-range-unit = token
The only range unit defined by HTTP/1.1 is "bytes". HTTP/1.1
implementations MAY ignore ranges specified using other units.
HTTP/1.1定义的唯一范围单元是“字节”。HTTP/1.1实现可以忽略使用其他单元指定的范围。
The key piece is the last paragraph. Really what it's saying is that when they wrote the spec for HTTP/1.1, they only outlined the "bytes" token. But, as you can see from the 'other-range-unit' bit, you are free to come up with your own token specifiers.
关键是最后一段。它真正的意思是,当他们为HTTP/1.1编写规范时,他们只列出了“字节”标记。但是,正如您从“其他范围单元”位中看到的,您可以自由地提出自己的令牌说明符。
Coming up with your own Range specifiers does mean that you have to have control over the client and server code that uses that specifier. So, if you own the backend piece that exposes the "/document/content/http-range-question" URI, you are good to go; presumably you're using a modern web framework that lets you inspect the request headers coming in. You could then look at the Range values to perform the backing query correctly.
提供您自己的范围说明符意味着您必须控制使用该说明符的客户端和服务器代码。因此,如果您拥有公开“/document/content/http-range-question”URI的后端部分,那么您可以使用;假设您正在使用一个现代的web框架,它允许您检查传入的请求头。然后,您可以查看范围值,以正确执行支持查询。
Furthermore, if you control the AJAX code that makes requests to the backend, you should be able to set the Range header yourself.
此外,如果您控制了向后端发出请求的AJAX代码,您应该能够自己设置范围标题。
However, there is a potential downside which you anticipate in your question: the potential to break caching. If you are using a custom Range unit, any caches between your client and the origin servers "MAY ignore ranges specified using [units other than 'bytes']". So for example, if you had a Squid/Varnish cache between the front and backend, there's no guarantee that the results you're hoping for will be served from the cache!
但是,在您的问题中有一个潜在的缺点:可能破坏缓存。如果您正在使用自定义范围单元,那么您的客户端和源服务器之间的任何缓存“可能会忽略使用[除'bytes'之外的单元]指定的范围”。例如,如果你有一个鱿鱼/清漆缓存之间的前端和后端,没有保证你希望的结果将服务于缓存!
You might also consider an alternative implementation where, rather than using a query string, you make the page a "parameter" of the URI; e.g.: /document/content/http-range-question/page/1. This would likely be a little more work for you server-side, but it's HTTP/1.1 compliant and caches should treat it properly.
您还可以考虑另一种实现,即不使用查询字符串,而是将页面设置为URI的“参数”;例如:/文档/内容/ http-range-question / / 1页。对于服务器端来说,这可能需要做更多的工作,但是它是兼容HTTP/1.1的,缓存应该正确地对待它。
Hope this helps.
希望这个有帮助。