Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have to code a mod_perl handler for apache 1.x that supports partial file downloads. Apache::Archive doesn't bother with partial downloads at all. Apache::PAR::Static claims it supports them, but it looks it doesn't - it always returns 200 status code. Here is how it handles serving content:
#$contents is content of entire file we are asked for, not a p +art of it! $r->set_content_length(length($contents)); if((my $status = $r->meets_conditions) eq OK()) { $r->send_http_header if ($mod_perl::VERSION < 1.99); } else { return $status; } return OK() if $r->header_only; my $range_request = ($mod_perl::VERSION < 1.99) ? $r->set_byte +range : 0; if($range_request) { while(my($offset, $length) = $r->each_byterange) { $r->print(substr($contents, $offset, $length)) +; } } #I've also tried adding "return 206;" here. Apache still retur +ns status code 200 else { $r->print($contents); } return OK();
Here are headers I'm getting when fetching document served by handler doing the same:
HTTP/1.1 200 OK Date: Fri, 08 Jan 2010 22:06:46 GMT Server: Apache/1.3.33 (Unix) mod_perl/1.29 PHP/5.2.10 Accept-Ranges: bytes Last-Modified: Fri, 08 Jan 2010 20:35:02 GMT Connection: close Content-Type: text/html
Here what headers same Apache returns when serving a part of some static file:
HTTP/1.1 206 Partial Content Date: Fri, 08 Jan 2010 21:25:23 GMT Server: Apache/1.3.33 (Unix) mod_perl/1.29 PHP/5.2.10 Last-Modified: Thu, 14 Feb 2002 17:33:24 GMT ETag: "1ea-23908b-3c6bf4e4" Accept-Ranges: bytes Content-Length: 147241 Content-Range: bytes 2183522-2330762/2330763 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html
So the questions - is it possible to serve partial http requests in mod_perl handlers? If yes, can you give a hint on what module does it (in order to see how it implements it)? The biggest problems I see is forcing apache to return proper status code and accept my Content-Range headers. Thanks in advance for your answers!

Replies are listed 'Best First'.
Re: mod_perl handler inside apache 1.x serving partial content - is this possible? How?
by Anonymous Monk on Jan 12, 2010 at 11:48 UTC
    So the questions - is it possible to serve partial http requests in mod_perl handlers?

    Absolutely, there is nothing special about partial requests

    If yes, can you give a hint on what module does it (in order to see how it implements it)?

    Read the RFC. A client requests a range of bytes, the server (your handler) sends the bytes requested, or returns an error, all with the required headers (refer to the RFC for those).

    The biggest problems I see is forcing apache to return proper status code and accept my Content-Range headers.

    Why? Just try it.

Re: mod_perl handler inside apache 1.x serving partial content - is this possible? How?
by Anonymous Monk on Jan 12, 2010 at 11:49 UTC
    It turned out that setting status to 206 before call of $r->send_http_header solved the problem. Here is a fixed version of code:
    my $range_request = ($mod_perl::VERSION < 1.99) ? $r->set_byte +range : 0; if((my $status = $r->meets_conditions) eq OK()) { if ($mod_perl::VERSION < 1.99) { if ($range_request) { $r->status(206); }; $r->send_http_header; }; } else { return $status; } return OK() if $r->header_only; if($range_request) { while(my($offset, $length) = $r->each_byterange) { $r->print(substr($contents, $offset, $length)) +; } } else { $r->print($contents); } return OK();