diff options
author | methodus <methodus@web.de> | 2012-10-21 22:32:39 +0200 |
---|---|---|
committer | methodus <methodus@web.de> | 2012-10-21 22:32:39 +0200 |
commit | 9709cd3325c3bc85013e3835324ce2bdeead3f51 (patch) | |
tree | 191f61de088e2f65d28a5b266fe128ff801d73af /httptnt | |
parent | 6ceb9fdba8785f791b790e0a987a4bfea5cf431d (diff) | |
download | vdr-plugin-upnp-9709cd3325c3bc85013e3835324ce2bdeead3f51.tar.gz vdr-plugin-upnp-9709cd3325c3bc85013e3835324ce2bdeead3f51.tar.bz2 |
Some minor improvements to the bytes position calculation.
Diffstat (limited to 'httptnt')
-rw-r--r-- | httptnt/resourceStreamer.ecpp | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/httptnt/resourceStreamer.ecpp b/httptnt/resourceStreamer.ecpp index 67aa69a..f064c49 100644 --- a/httptnt/resourceStreamer.ecpp +++ b/httptnt/resourceStreamer.ecpp @@ -43,51 +43,60 @@ int doRequest(tnt::HttpReply reply, tnt::HttpRequest request, std::string object reply.setHeader("contentFeatures.dlna.org ", streamer->GetContentFeatures()); reply.setHeader("transferMode.dlna.org ", streamer->GetTransferMode(reply.getHeader("transferMode.dlna.org"))); - if(streamer->GetContentLength() > 0){ - reply.setContentLengthHeader(streamer->GetContentLength()); - if(streamer->Seekable()) reply.setHeader("Accept-Ranges ", "bytes"); - } - - if(!request.isMethodHEAD()){ - - if(!streamer->Open()){ - code = HTTP_INTERNAL_SERVER_ERROR; - goto ret; - } + unsigned long from = 0, to = 0, contentLength = streamer->GetContentLength(), length = contentLength; + bool hasRange = false; - unsigned long offset = 0, length = streamer->GetContentLength(); - ssize_t bytesRead = 0; - bool hasRange = false; + if(contentLength > 0){ + reply.setContentLengthHeader(contentLength); + if(streamer->Seekable()) reply.setHeader("Accept-Ranges ", "bytes"); if(request.hasHeader("Range:")){ std::string rangeRequest = request.getHeader("Range:"); if(rangeRequest.find("bytes=",0) == 0){ unsigned int minus = rangeRequest.find_first_of('-',6); if(minus != std::string::npos){ - offset = atoi(rangeRequest.substr(6, minus - 6).c_str()); - length = atoi(rangeRequest.substr(minus + 1).c_str()) - offset; - if(length == 0){ - length = streamer->GetContentLength() - offset; - } else { - if(length > MB(1)) - length = MB(1); + from = atoi(rangeRequest.substr(6, minus - 6).c_str()); + to = atoi(rangeRequest.substr(minus + 1).c_str()); + if(from){ + // If range is off the road, reset it to correct values. + if(from <= 0 || from > contentLength) from = 0; + if(to <= 0 || to > contentLength) to = contentLength; + length = to - from; hasRange = true; + + if(request.isMethodHEAD()){ + std::stringstream contentRangeHeader; + contentRangeHeader << "bytes " << from << "-" << to << "/" << contentLength; + reply.setHeader("Content-Range ", contentRangeHeader.str()); + } } } } } + } - if(hasRange && streamer->Seekable() && streamer->GetContentLength() > 0){ + if(!request.isMethodHEAD()){ - if(!streamer->Seek(offset, SEEK_SET)){ + if(!streamer->Open()){ + code = HTTP_INTERNAL_SERVER_ERROR; + goto ret; + } + + ssize_t bytesRead = 0; + + if(hasRange && streamer->Seekable() && contentLength > 0){ + + if(!streamer->Seek(from, SEEK_SET)){ code = HTTP_INTERNAL_SERVER_ERROR; goto close; } + if(length > MB(1)) length = MB(1); + char buffer[length]; if((bytesRead = streamer->Read(buffer, length)) > 0){ std::stringstream ss; - ss << "bytes " << offset << "-" << offset + bytesRead << "/" << streamer->GetContentLength(); + ss << "bytes " << from << "-" << from + bytesRead << "/" << contentLength; reply.setHeader("Content-Range ", ss.str()); reply.setContentLengthHeader(bytesRead); code = HTTP_PARTIAL_CONTENT; |