0

I'm using the PHP built-in server to serve static files.

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.

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. It seems to me like Python and PHP servers are the two available options. 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?

I looked at php -h but it doesn't talk about the -S option much beyond the basic syntax. 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.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
  • For me I can access the request range header via $_SERVER. See: https://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file – Progrock Aug 27 '20 at 17:25
  • However what are you trying to request and serve? The built in webserver is blocking and has limitations. – Progrock Aug 27 '20 at 17:27
  • The OP is looking to serve static files, so I would take that to mean that they want to basically support automatic file resumption. – Chris Haas Aug 27 '20 at 17:31
  • Could you not use Apache, isn't that another OSX built in? – Progrock Aug 28 '20 at 16:44
  • @Progrock could you give an example command line on how to serve static files using Apache so that I can test if it does range requests? – Tomáš Hübelbauer Aug 29 '20 at 08:47
  • I can't go into the intricacies of setting up and configuring apache. But usually if installed you'll be able to see a sample page by visiting http://localhost. Identify the document root for your install, and place some sample static files there. – Progrock Aug 29 '20 at 11:43

2 Answers2

2

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. 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

You can try something like the following in 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. And tries to grab the byte range requested and serve a chunk appropriately.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Thanks for the good snippet. Looks like it won't do it by itself, which is a shame, but good to know. – Tomáš Hübelbauer Aug 29 '20 at 08:49
  • May not be default built in behaviour, but you can easily use 3rd party code to implement a one file solution. I took the function serveFilePartial above, and cut and paste it into a file. Tested it with a jpeg, including null as the second argument, and the third argument (correct content header for jpegs), and it worked for me. – Progrock Aug 29 '20 at 11:47
1

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. 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.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • The docs suggest that you have to handle unsupported mime types yourself. So I'd have thought you could read the headers and chunk a file accordingly, if you really wanted to. – Progrock Aug 27 '20 at 22:16
  • 1
    @Progrock, I agree and upvoted your answer. I'm assuming that the OP isn't running on the public internet (hopefully) and they further have only specific devices connecting, so the security concerns about path traversal, script download, etc. shouldn't be an issue, either. – Chris Haas Aug 28 '20 at 13:19
  • Indeed I am only using this for testing on a local machine. – Tomáš Hübelbauer Aug 29 '20 at 08:48