Http Range request property?

2019-03-05 11:22发布

I'm stucked writing some java code pieces trying to download by range a target file from a remote host. I'm expecting to download remote file by some chunks relying on some multithreaded process.

According following debug trace all is running well :

Task 2 --> REQUEST PROPERTIES =  {Range=[bytes=59124782-88687172]}
Task 3 --> REQUEST PROPERTIES =  {Range=[bytes=88687173-118249563]}
Task 0 --> REQUEST PROPERTIES =  {Range=[bytes=0-29562390]}
Task 1 --> REQUEST PROPERTIES =  {Range=[bytes=29562391-59124781]}
Task 2 --> RETURN_CODE = 206 ==> OK :-) !
Task 2 --> Start Byte = 59124782 / End Byte = 88687172 --> Total bytes = 29562391
Task 0 --> RETURN_CODE = 206 ==> OK :-) !
Task 0 --> Start Byte = 0 / End Byte = 29562390 --> Total bytes = 29562391
Task 3 --> RETURN_CODE = 206 ==> OK :-) !
Task 3 --> Start Byte = 88687173 / End Byte = 118249563 --> Total bytes = 29562391
Task 1 --> RETURN_CODE = 206 ==> OK :-) !
Task 1 --> Start Byte = 29562391 / End Byte = 59124781 --> Total bytes = 29562391

According following debug trace all is NOT running well :

Task 0 --> REQUEST PROPERTIES =  {Range=[bytes=0-15308169]}
Task 3 --> REQUEST PROPERTIES =  {Range=[bytes=45924510-61232679]}
Task 1 --> REQUEST PROPERTIES =  {Range=[bytes=15308170-30616339]}
Task 2 --> REQUEST PROPERTIES =  {Range=[bytes=30616340-45924509]}
Task 0 --> RETURN_CODE = 206 ==> OK :-) !
Task 0 --> Start Byte = 0 / End Byte = 15308169 --> Total bytes = 15308170
Task 3 --> RETURN_CODE = 200 ==> NOK :-( !
Task 2 --> RETURN_CODE = 200 ==> NOK :-( !
Task 1 --> RETURN_CODE = 200 ==> NOK :-( !

Trick is between 2 runs code is same, remote host is same ... only target file is not same (second is half smaller than first and both are .zip files). As requesting for some defined range I'm expecting for each task 206 return code what's acknowledging partial data will be received. But relying on second trace I'm getting for 3 latest task 200 code what's means server response is ok but will send full file :-( ....

What king of black magic may lead to such ????. Googling a bit I found some http server may introduce some range request coalesce process ... is such may happen here ?

When multiple ranges are requested, a server MAY coalesce any of the ranges that overlap, or that are separated by a gap that is smaller than the overhead of sending multiple parts, regardless of the order in which the corresponding byte-range-spec appeared in the received Range header field. Since the typical overhead between parts of a multipart/byteranges payload is around 80 bytes, depending on the selected representation's media type and the chosen boundary parameter length, it can be less efficient to transfer many small disjoint parts than it is to transfer the entire selected representation.

Just for info here is my piece of java code :

HttpURLConnection urlConn = null;
try {
    urlConn = task.openConnection(task.getSourceFileUrl());
    urlConn.setRequestMethod("GET");
    urlConn.setRequestProperty("Range", "bytes=" + currentByte + "-" + endByte + "");

    System.out.println(String.format("Task %s --> REQUEST PROPERTIES =  %s", id, urlConn.getRequestProperties().toString()));

    if (urlConn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) {
        System.out.println(String.format("Task %s --> RETURN_CODE = %s ==> NOK :-( !", id, urlConn.getResponseCode()));
        return;
    } else {
        System.out.println(String.format("Task %s --> RETURN_CODE = %s ==> OK :-) !", id, urlConn.getResponseCode()));
    }

    System.out.println(String.format("Task %s --> Start Byte = %s / End Byte = %s --> Total bytes = %s", id, currentByte, endByte, urlConn.getContentLengthLong()));

Thks for help !!!!!

0条回答
登录 后发表回答