0

Recently we have decided to play some video in browser at my company. We want to support Safari, Firefox and Chrome. To stream video, Safari requires that we implement range http requests in servicestack. Our server supports range requests as indicated by the 'Accept-Ranges: bytes' header being returned in the response.

Looking at previous questions we would want to add a prerequest filter, but I don't understand the details of doing so. Adding this to our AppHost.cs's configure function does do something:

PreRequestFilters.Add((req, res) => {
    if (req.GetHeader(HttpHeaders.Range) != null) {
        var rangeHeader = req.GetHeader(HttpHeaders.Range);
        rangeHeader.ExtractHttpRanges(req.ContentLength, out var rangeStart, out var rangeEnd);
        if (rangeEnd > req.ContentLength - 1) {
            rangeEnd = req.ContentLength - 1;
        }
        res.AddHttpRangeResponseHeaders(rangeStart, rangeEnd, req.ContentLength);
    }
});

Setting a breakpoint I can see that this code is hit. However rangeEnd always equals -1, and ContentLength always equals 0. A rangeEnd of -1 is invalid as per spec, so something is wrong. Most importantly, adding this code breaks video playback in Chrome as well. I'm not sure I'm on the right track. It does not break the loading of pictures.

If you would like the details of the Response/Request headers via the network let me know.

  • ServiceStack already supports [HTTP Range requests](https://docs.servicestack.net/service-return-types#partial-content-support) for static files, stream, raw bytes and text responses. What are you trying to return the range request for? If you’re trying to take over the implementation you’ll need to return the partial response which I don’t see anywhere. – mythz Jun 21 '21 at 03:10
  • Thanks for the reply Mythz. So we're trying to make video streaming possible on Safari. We don't need to take over the implementation if its already supported which is good to know. SO: I set a breakpoint on the prefilter that would break on any request with a range header. And I am unable to see any variables that I would use to return the partial response. I'm under the impression I need a bytes range? Atleast a 'content-length' of non-zero? The request just has 0 for anything relevant but has a 'Range' in the headers. – matthewdelaney Jun 21 '21 at 04:28
  • Specifically variables in the request – matthewdelaney Jun 21 '21 at 04:35
  • The HTTP Range Request Header contains the range the client wants returned, (which [can be unbounded](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#syntax)). You need to specify the Content Length of the requested HTTP Resource, i.e. total bytes of the video, not the HTTP Request Body Length. – mythz Jun 21 '21 at 04:43
  • Ah of course, I can get the bytes that the range header wants (thankyou), however setting content length seems hard as it only has a 'get' and not a set. Assuming I could set the content length (I have been setting it to intmax when trying to add res.AddHttpRangeResponseHeaders(rangeStart, rangeEnd, ContentLength);). Do I actually need to add the range response headers like this? I guess assuming the content length is worked out, it seems I won't need to explicitly return a partial response as service stack should take care of that? – matthewdelaney Jun 21 '21 at 05:58
  • ContentLength should be the length of the requested Resource. I'm not clear on what you're trying to achieve? If you don't plan on handling the range request yourself, why are you messing with the headers? That should be determined by whoever is handling the request and writing the response. What's the issue with just [returning the requested resource](https://docs.servicestack.net/service-return-types#partial-content-support) in your Service and letting ServiceStack handle the response? – mythz Jun 21 '21 at 06:07
  • No of course you're right. All I want is for video to stream from Safari using the – matthewdelaney Jun 21 '21 at 06:58
  • What's led you to believe the issue is with range request responses? I'd start by looking at the HTTP Headers (e.g. using Fiddler) and identifying the invalid HTTP Responses that are causing the issue. – mythz Jun 21 '21 at 07:06
  • I ask because this is the first Google result for [HTML5 video doesn't work in Safari](https://stackoverflow.com/q/20347352/85785), going down the search results I see issues ranging from using different HTML attributes, to different video encoding. So I'm curious on what's led you look at range requests? If you've identified an invalid HTTP Response can you please update your question with the raw HTTP Request/Response Headers (i.e. w/o body/content). – mythz Jun 21 '21 at 07:19

0 Answers0