简体   繁体   English

是否可以在 PHP 的内置 Web 服务器中启用 HTTP 范围请求支持?

[英]Is it possible to enable HTTP range request support in the PHP's built-in web server?

I'm using the PHP built-in server to serve static files.我正在使用 PHP 内置服务器来提供静态文件。

php -S localhost:8000

I have however noticed that the Range header gets ignored - instead of serving the requested range, the entire resource is served.然而,我注意到Range标头被忽略 - 而不是提供请求的范围,而是提供整个资源。

I am not that well versed in PHP, the reason I'm using this server is because I'm looking for an alternative to Python's built-in server ( python3 -m http.server ) which does not support range requests (either?) that is built-in into macOS.我不太精通 PHP,我使用这个服务器的原因是我正在寻找不支持范围请求的 Python 内置服务器( python3 -m http.server )的替代方案(要么?)这是内置在 macOS 中的。 It seems to me like Python and PHP servers are the two available options.在我看来,Python 和 PHP 服务器是两个可用的选项。 Knowing Python's doesn't support HTTP range requests, is there any way to use config.ini or some other mechanism to enable HTTP range requests in PHP's?知道 Python 不支持 HTTP 范围请求,有没有办法使用config.ini或其他一些机制来启用 PHP 中的 HTTP 范围请求?

I looked at php -h but it doesn't talk about the -S option much beyond the basic syntax.我查看了php -h但它并没有讨论超出基本语法的-S选项。 Not sure if there are more options that can be used with it so that's why I assume config.ini is the only possible way.不确定是否有更多选项可以与它一起使用,这就是为什么我认为config.ini是唯一可能的方法。

If you start the webserver with a router:如果您使用路由器启动网络服务器:

$ php -S 0.0.0.0:9999 router.php

All http requests will be passed through that script.所有 http 请求都将通过该脚本传递。 And in there you can handle the range request by reading and outputing chunks of files.在那里你可以通过读取和输出文件块来处理范围请求。

Using the script linked to here in this comment: https://stackoverflow.com/a/22398156/3392762使用此评论中链接到此处的脚本: https : //stackoverflow.com/a/22398156/3392762

You can try something like the following in router.php:您可以在 router.php 中尝试类似以下内容:

<?php
if($_SERVER['REQUEST_URI'] === '/foo.ogg')
    serveFilePartial('foo.ogg');

Be very careful mapping user submitted paths to files.将用户提交的路径映射到文件时要非常小心。

I can't attest to the quality of the linked code.我无法证明链接代码的质量。 But if you scan it, it looks for the $_SERVER['HTTP_RANGE'] header.但是如果你扫描它,它会查找$_SERVER['HTTP_RANGE']标头。 And tries to grab the byte range requested and serve a chunk appropriately.并尝试获取请求的字节范围并适当地提供块。

Although I don't have a definitive answer I'm pretty sure the documentation essentially says no.虽然我没有明确的答案,但我很确定文档基本上说不。

This web server was designed to aid application development.此 Web 服务器旨在帮助应用程序开发。 It may also be useful for testing purposes or for application demonstrations that are run in controlled environments.它也可用于测试目的或在受控环境中运行的应用程序演示。 It is not intended to be a full-featured web server.它不是一个功能齐全的网络服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM