Fixed seek-related bugs in URIReader for HTTP sources

This commit is contained in:
Thulinma 2022-08-17 17:06:25 +02:00
parent 747438746c
commit b210b4f5af

View file

@ -255,20 +255,30 @@ namespace HTTP{
// seek to pos, return true if succeeded. // seek to pos, return true if succeeded.
bool URIReader::seek(const uint64_t pos){ bool URIReader::seek(const uint64_t pos){
if (isSeekable()){ //Seeking in a non-seekable source? No-op, always fails.
allData.truncate(0); if (!isSeekable()){return false;}
bufPos = 0;
if (stateType == HTTP::File){ //Reset internal buffers
curPos = pos; allData.truncate(0);
return true; bufPos = 0;
}else if (stateType == HTTP::HTTP && supportRangeRequest){
INFO_MSG("SEEK: RangeRequest to %" PRIu64, pos); //Files always succeed because we use memmap
if (!downer.getRangeNonBlocking(myURI.getUrl(), pos, 0)){ if (stateType == HTTP::File){
FAIL_MSG("error loading request"); curPos = pos;
} return true;
}
} }
//HTTP-based needs to do a range request
if (stateType == HTTP::HTTP && supportRangeRequest){
downer.getSocket().close();
downer.getSocket().Received().clear();
if (!downer.getRangeNonBlocking(myURI.getUrl(), pos, 0)){
FAIL_MSG("Error making range request");
return false;
}
curPos = pos;
return true;
}
return false; return false;
} }
@ -323,19 +333,13 @@ namespace HTTP{
}else if (stateType == HTTP::HTTP){ }else if (stateType == HTTP::HTTP){
downer.continueNonBlocking(cb); downer.continueNonBlocking(cb);
if (curPos == downer.const_data().size()){
Util::sleep(50);
}
curPos = downer.const_data().size();
}else{// streaming mode }else{// streaming mode
int s; int s;
static int totaal = 0;
if ((downer.getSocket() && downer.getSocket().spool())){// || downer.getSocket().Received().size() > 0){ if ((downer.getSocket() && downer.getSocket().spool())){// || downer.getSocket().Received().size() > 0){
s = downer.getSocket().Received().bytes(wantedLen); s = downer.getSocket().Received().bytes(wantedLen);
std::string buf = downer.getSocket().Received().remove(s); std::string buf = downer.getSocket().Received().remove(s);
cb.dataCallback(buf.data(), s); cb.dataCallback(buf.data(), s);
totaal += s;
}else{ }else{
Util::sleep(50); Util::sleep(50);
} }