You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Victor Danilchenko <vi...@askonline.net> on 2008/02/22 15:25:28 UTC

A question about wiring up filehandles for direct dump to HTTP pipe

	Is it possible?

	Hi all,

	Here is my situation. I would like a mod-perl script (a Mason script 
actually, but from the mod_perl API POV it's the same thing) to act as a 
gatekeeper for file access -- regulate access by application-specific 
ACLs, do the accounting on download stats, etc; but then, once the 
housekeeping is done, I would like it to take the file (quite likely a 
very large video file) and dump it directly to the HTTP pipe.

	The obvious solution -- a sysread/syswrite loop -- will probably be 
quite a bit slower than simply having Apache read the filehandle 
directly. Given that I expect these files to be large and many, I would 
really prefer to avoid the sysread/syswrite loop solution.

	Is there any way, to, uhhh, tell Apache programatically to simply slurp 
up the file from an open filehandle, or to grab a given file from disk? 
Doing a redirect to the real location is less than ideal precisely 
because that will  open a door to the circumvention of this accounting 
script, a door which will be rather hard to close.

	Any ideas or alternative solutions are welcome.

	Thanks in advance.

-- 
	Victor Danilchenko
	Senior Software Engineer, AskOnline.net
	victor@askonline.net - 617-273-0119

Re: A question about wiring up filehandles for direct dump to HTTP pipe

Posted by Victor Danilchenko <vi...@askonline.net>.
Michael Peters wrote:
> Victor Danilchenko wrote:
> 
>>     Is there any way, to, uhhh, tell Apache programatically to simply
>> slurp up the file from an open filehandle
> 
> I could be wrong, but I doubt you can pass a Perl file handle to a C program
> that is not Perl aware (like Apache).

	I am hoping you are wrong, but I am not expecting it. To be honest, I 
kinda figured that there would be no easy and perfect way around it.

>> or to grab a given file from
>> disk? Doing a redirect to the real location is less than ideal precisely
>> because that will  open a door to the circumvention of this accounting
>> script, a door which will be rather hard to close.
> 
> What about an internal redirect? It won't expose where the door is and you can
> make it such that access to that URL is forbidden to any request not coming from
> your machine.

	Huh, I didn't even realize this was possible. $r->internal-redirect, 
eh? That sounds just like what I need -- coupled with some form of 
hotlink protection, it should do the job. Thanks a lot.

> But the best approach seems to convert your code into an Apache Authen or Authz
> handler to do all the book keeping. Then simply let Apache continue with the
> processing after that. You'd probably have to ditch Mason for that piece of the
> code, but it's pretty straight forward.

	If you are familiar with Apache internals, I am sure it is. i am not. 
Unfortunately, this happens to be one of those "the roof is on fire" 
deals -- I have to not only solve this problem, but solve it *fast*.

	Many thanks for your immensely helpful advice. I think the internal 
redirect is close enough to what I need to be useful. once again, thanks.

-- 
	Victor Danilchenko
	Senior Software Engineer, AskOnline.net
	victor@askonline.net - 617-273-0119

Re: A question about wiring up filehandles for direct dump to HTTP pipe

Posted by Michael Peters <mp...@plusthree.com>.
Victor Danilchenko wrote:

>     Is there any way, to, uhhh, tell Apache programatically to simply
> slurp up the file from an open filehandle

I could be wrong, but I doubt you can pass a Perl file handle to a C program
that is not Perl aware (like Apache).

> or to grab a given file from
> disk? Doing a redirect to the real location is less than ideal precisely
> because that will  open a door to the circumvention of this accounting
> script, a door which will be rather hard to close.

What about an internal redirect? It won't expose where the door is and you can
make it such that access to that URL is forbidden to any request not coming from
your machine.

But the best approach seems to convert your code into an Apache Authen or Authz
handler to do all the book keeping. Then simply let Apache continue with the
processing after that. You'd probably have to ditch Mason for that piece of the
code, but it's pretty straight forward.

-- 
Michael Peters
Plus Three, LP


Re: A question about wiring up filehandles for direct dump to HTTP pipe

Posted by Victor Danilchenko <vi...@askonline.net>.
Torsten Foertsch wrote:
> On Fri 22 Feb 2008, Victor Danilchenko wrote:
>> or to grab a given file from disk?
> 
> Perhaps $r->sendfile($filename)? Defined in Apache2::RequestIO.

	There is precious little documentation on it, so I will have to 
experiment with it a little, but this sounds like it might do the trick. 
Thanks.

-- 
	Victor Danilchenko
	Senior Software Engineer, AskOnline.net
	victor@askonline.net - 617-273-0119

Re: A question about wiring up filehandles for direct dump to HTTP pipe

Posted by Victor Danilchenko <vi...@askonline.net>.
	This solution ended up being perfect, and SIMPLE. I just had to 
sprinkle a little Mime::FileInfo::Magic over the request, to get the 
content-type to work right, and POOF! it worked. Just like that. My 
entire wrapper code (without the accounting, ACL, and somesuch stuff) is 
under 10 lines, and half of it is constructing the 404 responder.

Torsten Foertsch wrote:
> On Fri 22 Feb 2008, Victor Danilchenko wrote:
>> or to grab a given file from disk?
> 
> Perhaps $r->sendfile($filename)? Defined in Apache2::RequestIO.



-- 
	Victor Danilchenko
	Senior Software Engineer, AskOnline.net
	victor@askonline.net - 617-273-0119

Re: A question about wiring up filehandles for direct dump to HTTP pipe

Posted by Torsten Foertsch <to...@gmx.net>.
On Fri 22 Feb 2008, Victor Danilchenko wrote:
> or to grab a given file from disk?

Perhaps $r->sendfile($filename)? Defined in Apache2::RequestIO.

Torsten