Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Http客户端download时请求头Range #136

Open
Dmcz opened this issue Sep 17, 2021 · 0 comments
Open

关于Http客户端download时请求头Range #136

Dmcz opened this issue Sep 17, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@Dmcz
Copy link

Dmcz commented Sep 17, 2021

问题描述

在使用http客户端下载文件时, 对端返回的http status code 为 419. 测试后发现下载文件的请求头中包含了 Range 但是为空. 示例:

Host: 192.168.8.52
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Range: 

这并不符合: Range: bytes=start-end 的格式。因此服务端会判定为异常并返回code 419.
于是试图换个思路,通过传递headers来复写请求头中的header。
当我传递Range: bytes=1-后, 发现请求头中的Range消失了....,文件可以正常的下载

虽然目前解决了业务中的问题,但是感觉这应该属于不合理的地方。
应该修改withDownloadOffset方法当offset = 0时请求头不包含Range

或者有没有更合适的方法来解决这个问题。 请大佬们帮忙看看。

版本信息

composer info | grep saber
swlib/saber                        v1.x-dev 7b97f01   Swoole coroutine HTTP client
PHP 8.0.10 (cli) (built: Aug 27 2021 01:57:09) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.6.7
Built => Sep  6 2021 02:26:14
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1l  24 Aug 2021
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
async_redis => enabled

示例代码及结果

<?php

use Swlib\Saber;
use Swlib\SaberGM;
use Swlib\Http\Exception\RequestException;

require 'base.php';

\Swoole\Coroutine\run(function () {
    $url = 'http://192.168.8.52/group1/M00/00/00/wKgINGFEMNSAH4vNAAAsk_heX2w512.jpg?token=9ad2493d8d0c3ab2eab7d419d277992d&ts=1631953656';

    $client = Saber::create();

    // 请求头中存在Range, 但是Range为空
    $request = $client->download($url, './test', 0, [
        'timeout' => 60,
        'psr' => true
    ]);
    echo $request->getHeadersString() . PHP_EOL;
    echo '-----------' . PHP_EOL;

    // 请求头中存在Range, Range为  Range: bytes=5-
    $request = $client->download($url, './test', 5, [
        'timeout' => 60,
        'psr' => true
    ]);
    echo $request->getHeadersString() . PHP_EOL;
    echo '-----------' . PHP_EOL;
    
    // 请求头中不存在Range
    $request = $client->download($url, './test', 0, [
        'timeout' => 60,
        'psr' => true,
        'headers' =>[
            'Range' => 'bytes=1-',
            'Test' => 'bytes=1-',
        ]
    ]);
    echo $request->getHeadersString() . PHP_EOL;
    echo '-----------' . PHP_EOL;

    // 请求头中存在Range, Range为  Range: bytes=5- 
    $request = $client->download($url, './test', 5, [
        'timeout' => 60,
        'psr' => true,
        'headers' =>[
            'Range' => 'bytes=1-',
            'Test' => 'bytes=1-',
        ]
    ]);
    echo $request->getHeadersString() . PHP_EOL;
    echo '-----------' . PHP_EOL;
    
});
Host: 192.168.8.52
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Range:
-----------
Host: 192.168.8.52
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Range: bytes=5-
-----------
Host: 192.168.8.52
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Test: bytes=1-
-----------
Host: 192.168.8.52
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Test: bytes=1-
Range: bytes=5-
-----------
@twose twose added the bug Something isn't working label Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants