使用HTTP范围头,射程比符字节以外? [英] Using the HTTP Range Header with a range specifier other than bytes?

查看:126
本文介绍了使用HTTP范围头,射程比符字节以外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

核心问题是关于使用HTTP标头,包括范围 如果-范围,的接受-的范围和用户定义的范围说明。

下面是一个制造的例子来帮助说明我的问题。假设我有一个显示某种人类可读的文件,一个Web 2.0风格的应用程序。这些文件的社论分为页(类似于你看到新闻网站的文章)。对于本示例,假设:

  • 有一个标题为HTTP范围问题被分成三页的文档。
  • 在外壳页( /文件/壳/ HTTP范围-问题)知道该文件的元数据信息,包括页数。
  • 在该文件的第一个可读的页面通过Ajax获取页面的onload事件过程中加载并插入到页面上。
  • 在UI控件,看起来像 [1 2 3全部] 是在页面的底部,并点击一个数字将显示可读的页面(通过AJAX还装载),并单击所有将显示整个文档。假定这些URL为1,2,3和所有用例:
    • /文件/内容/ HTTP范围-问题?页= 1
    • /文件/内容/ HTTP范围-问题?页= 2
    • /文件/内容/ HTTP范围-问题?页= 3
    • /文件/内容/ HTTP范围-问题

现在的问题。我可以使用HTTP范围头中的URL(如查询参数),而不是一部分?也许是这样的 GET /文件/内容/ HTTP范围-问题要求:

 范围:页面= 1
 

它看起来像规范中只定义了字节范围为允许的,所以即使我做了我的Ajax调用与我的浏览器和服务器code,中间可能会撕毁合同任何东西(如缓存代理服务器)工作。

 范围:字节= 0-499
 

自定义范围说明符的任何意见或真实世界的例子?

更新:我没有找到有关范围的头一个类似的问题(的分页在休息集合),他们提到,Dojo的 JsonRestStore 使用自定义的范围标头值。

 范围:项目= 0-24
 

解决方案

当然 - 你可以随意指定任何范围内的单位,你喜欢

从2616:

  

3.12范围单位

     

HTTP / 1.1允许客户端的请求   只有部分(的范围)的
  响应实体被包括在所述   响应。 HTTP / 1.1应用范围单位   在范围(第14.35)和   内容范围(第14.16)
  头字段。一个实体可破   分解成子范围,根据   各种结构单元

 范围单位=字节单位|其他范围单元
  字节单位=字节
  其他范围单元=令牌
 

     

通过定义的唯一范围单元   HTTP / 1.1字节。 HTTP / 1.1
  实现可以忽略范围   使用指定的其他单位。

的关键部分是最后一段。真的就是它的意思是,当他们写的规格为HTTP / 1.1,他们只列出了字节令牌。但是,你可以从其他范围单元位看,你可以自由地拿出自己的标记符。

未来与你自己的范围说明符并不意味着你必须有在使用该说明在客户端和服务器code控制。所以,如果你拥有一个公开的/文件/内容/ HTTP范围-问题后端片URI,你是好去;您使用的是现代的Web框架,可以让你检查请求头进来presumably。你可以再看看数值范围正确执行的支持查询。

此外,如果你控制了AJAX code,使请求到后端,你应该能够设置Range头吧。

但是,你在你的问题预见潜在的缺点:打破缓存的潜力。如果您使用的是自定义的范围单元,您的客户端和原始服务器之间的高速缓存可以忽略使用[单位不是'字节'等]指定的范围。因此,举例来说,如果你有前部和后端之间的鱿鱼/清漆缓存,也不能保证你希望的结果会从缓存中服了!

您也可以考虑另一种实现,其中,而不是使用一个查询字符串,使页面的URI的参数;例如:/文件/内容/ HTTP范围-问题/页/ 1。这很可能是你的服务器端多一点的工作,但它的HTTP / 1.1兼容和缓存应该正确地对待它。

希望这有助于。

The core question is about the use of the HTTP Headers, including Range, If-Range, Accept-Ranges and a user defined range specifier.

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:

  • There is a document titled "HTTP Range Question" is broken up into three pages.
  • The shell page (/document/shell/http-range-question) knows the meta information about the document, including the number of pages.
  • The first readable page of the document is loaded during the page onload event via an ajax GET and inserted onto the page.
  • 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
    • /document/content/http-range-question?page=2
    • /document/content/http-range-question?page=3
    • /document/content/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:

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).

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.

Range: items=0-24

解决方案

Absolutely - you are free to specify any range units you like.

From RFC 2616:

3.12 Range Units

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.

  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.

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.

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.

Furthermore, if you control the AJAX code that makes requests to the backend, you should be able to set the Range header yourself.

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!

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.

Hope this helps.

这篇关于使用HTTP范围头,射程比符字节以外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆