编程语言
首页 > 编程语言> > 使用PHP生成HTTP 206内容响应

使用PHP生成HTTP 206内容响应

作者:互联网

我正在使用PHP尝试发送HTTP 206内容.但是,即使我确认内容是在服务器上发送的,接收到标头后客户端仍会以某种方式立即报告已关闭的连接.

我认为这与标头或类似的设置有关.顶级代码是:

  $headers['content-length'] = $range['end']-$range['start']+1;

  header('HTTP/1.1 206 Partial Content');
  header("Pragma: {$headers['pragma']}");
  header("Cache-Control: {$headers['cache-control']}");
  header("Content-type: {$headers['content-type']}");
  header("Content-Disposition: {$headers['content-disposition']}; filename=\"{$headers['filename']}\"");
  header("Content-range: {$range['start']}-{$range['end']}/{$reqFile['size']}");
  header("Content-length: {$headers['content-length']}");

  outputFileRange($reqFile['handle'], $range['start'], $range['end']);

WGET输出

当我尝试通过wget获取时:

$wget -t 1 -d -SO /dev/null --header="X-Backend-Port: 80" --header="Range: bytes=50-" http://1.2.3.4/download/123.ogv

我得到以下内容:

Setting --server-response (serverresponse) to 1
Setting --output-document (outputdocument) to /dev/null
Setting --header (header) to X-Backend-Port: 80
Setting --header (header) to Range: bytes=50-
DEBUG output created by Wget 1.13.4 on linux-gnu.

URI encoding = `UTF-8'
--2013-10-18 11:05:36--  http://1.2.3.4/download/123.ogv
Connecting to 1.2.3.4:80... connected.
Created socket 4.
Releasing 0x0855da38 (new refcount 0).
Deleting unused 0x0855da38.

---request begin---
GET /download/123.ogv HTTP/1.1
User-Agent: Wget/1.13.4 (linux-gnu)
Accept: */*
Host: 1.2.3.4
Connection: Keep-Alive
X-Backend-Port: 80
Range: bytes=50-

---request end---
HTTP request sent, awaiting response... 
---response begin---
HTTP/1.1 206 Partial Content
Server: nginx/0.7.67
Date: Fri, 18 Oct 2013 07:05:36 GMT
Content-Type: application/octet-stream
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze17
Pragma: 
Cache-Control: 
Content-Disposition: attachment; filename="123.ogv"
Content-range: 50-6642903/6642903
Content-length: 6642854

---response end---

  HTTP/1.1 206 Partial Content
  Server: nginx/0.7.67
  Date: Fri, 18 Oct 2013 07:05:36 GMT
  Content-Type: application/octet-stream
  Connection: keep-alive
  X-Powered-By: PHP/5.3.3-7+squeeze17
  Pragma: 
  Cache-Control: 
  Content-Disposition: attachment; filename="123.ogv"
  Content-range: 50-6642903/6642903
  Content-length: 6642854
Registered socket 4 for persistent reuse.
Disabling further reuse of socket 4.
Closed fd 4


Giving up.

解决方法:

尝试设置内容范围:50-6642902 / 6642903
根据http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

标签:http-headers,php
来源: https://codeday.me/bug/20191122/2061208.html