0

Can an http server respond with data bit by bit - without it being in response to a "Range" request?

For example, say I want to continually stream text data back to a client - an almost never ending http stream of textual data, which is received by the client as the result of a single http request.

Can I do this without the client sending me a "Range" request in response to me sending an "Accept-Ranges" header?

And I do see code examples of servers streaming data back to clients, but are those clients expected to have sent a range header?

Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336

1 Answers1

1

A range request asks for a specific range of bytes for a resource. It's safe to expect that the request ends in a reasonable time. However nothing stops you from pushing data from the server without closing/ending the connection in a regular request. It just means the request will take a long time and possibly waste resources on the server.

This is sometimes called "long polling", especially when the connection is idle (but still alive) for most of the time, sending data only when something happens on the server. An example would be a web based chat, although websockets would be more suitable than HTTP in these modern times.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Thanks for the feedback! So people can use a [StreamedResponse](http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/StreamingOutput.html) to stream data back from the server - but that's only a convenience on the server really, and isn't streamed by the client unless the client has requested a ranged response, and the server has responded with just that range? So even with a long polling request that the server is responding to with a StreamingOutput isn't really streaming from the clients perspective, if the client hasn't requested a range? – Brad Parks Oct 27 '16 at 18:12
  • 1
    No, just forget the whole range thing (or read about it if you want to know more). – Kayaman Oct 27 '16 at 18:23
  • Also I was talking about HTTP in general, but I noticed that you linked to an answer concerning Jersey. Are you using Jersey? – Kayaman Oct 27 '16 at 18:36
  • Yeah we are using Jersey... we've done large video files that are streamed using range headers, but have some requests to a 3rd party service that aren't closing after 5 minutes, and we're trying to decide if we should be closing them or not, after a much shorter time period, as they don't seem to be responding with any data, and are blocking threads. – Brad Parks Oct 27 '16 at 18:54
  • and to be 100% clear, the requests to the 3rd party service aren't ranged requests – Brad Parks Oct 27 '16 at 19:10
  • Well, that's an issue with the 3rd party service. Surely you have some kind of documentation about how they're supposed to function? I can't imagine it being advantageous for them to keep a connection open forever either, so it's either long polling (which would be documented), a serious bug with the 3rd party service or some kind of networking problem. – Kayaman Oct 27 '16 at 19:13
  • True - but no matter how they respond, I'll want to proactively protect against this in our client - killing their connection if it's "idle" for a max time interval. I'm just trying to make sure I understand what idle means in these cases - ie no response from their service in 5 min, or an incomplete response in 5 min, but they've been sending back data. – Brad Parks Oct 27 '16 at 19:21
  • It's not a good idea to do something like that unless you're dealing with potentially hostile parties. If your 3rd party service isn't hostile, then this is an issue you shouldn't even be wondering about. Contact them and ask why they're not closing connections. – Kayaman Oct 27 '16 at 19:25
  • Gotcha.. thanks for the info and suggestions... much appreciated! – Brad Parks Oct 27 '16 at 19:32