You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Sam Carleton <sc...@miltonstreet.com> on 2007/04/09 21:57:30 UTC

server side includes

As I sit here on hold, I started thinking about my little apache
module again.  I recall reading in "Writing Apache Modules in Perl and
C" how with Perl it is quick and easy to create a server side include
handler, I think that is what it was called.  How would one go about
doing that in a C module?  Are there any examples?

Sam
-- 
The contents of this e-mail are intended for the named addressee only.
It contains information that may be confidential. Unless you are the
named addressee or an authorized designee, you may not copy or use it,
or disclose it to anyone else. If you received it in error please
notify us immediately and then destroy it.

Re: server side includes

Posted by Nick Kew <ni...@webthing.com>.
On Tue, 10 Apr 2007 22:26:09 +0300
Issac Goldstand <ma...@beamartyr.net> wrote:

> Good point.  Actually, with Perl, I've found that it's simpler and
> usually more convenient to dispatch buckets to HTML::Parser, which is
> better at catching that sort of thing, and can also work in a stream,
> chunk by chunk.

That's exactly the right approach to filtering markup.

> The downside is that it locks you in to Perl, which I intentionally

Actually what you describe works the same in C.  See for example
mod_proxy_html or mod_xmlns.

Looks like you're doing it right.  But that wasn't clear in your
earlier post, which hinted at a naive and unreliable filter.

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/

Re: server side includes

Posted by Issac Goldstand <ma...@beamartyr.net>.
Good point.  Actually, with Perl, I've found that it's simpler and
usually more convenient to dispatch buckets to HTML::Parser, which is
better at catching that sort of thing, and can also work in a stream,
chunk by chunk.  The stream-based interface provided by mod_perl really
makes this easy: while ($buf = $f->read) {$parser->parse($buf);}
$parser->eof;

You then have callbacks for opening tags, whitespace and closing tags,
which all default to $f->write($content), and you can add any custom
business logic above that.  I can provide some more fleshed out
pseudocode if anyone's interested.

The downside is that it locks you in to Perl, which I intentionally
wanted to avoid in this particular module.  And you still need to make
sure you don't get stuck with huge amounts of "leftover"s between
buckets (HTML::Parser will take care of remembering the leftover data as
a small bonus).

I don't see any perfect way to avoid the big common problems, though;
you always have to know what you're aiming the filters at and work
accordingly...

  Issac

Nick Kew wrote:
> On Tue, 10 Apr 2007 09:20:22 +0300
> Issac Goldstand <ma...@beamartyr.net> wrote:
> 
>> $buf = ${$f->ctx}{leftover}.$buf if defined(${$f->ctx}{leftover});
>> (prepend f->ctx->leftover onto buf)
>>
>> and anything leftover that doesn't include a full HTML tag goes to
>>
>> ${$f->ctx}{leftover} = $buf || undef;
> 
> Define "a full HTML tag".
> 
> As in, for instance
> 	<img
> 	src = "arrow.gif"
> 	alt = " --> "
> 	>
> 
> The point being, it's not a trivial task (and that's without
> putting things like the above in a comment or cdata section
> where its semantics are completely different, etc).
> 

Re: server side includes

Posted by Nick Kew <ni...@webthing.com>.
On Tue, 10 Apr 2007 09:20:22 +0300
Issac Goldstand <ma...@beamartyr.net> wrote:

> $buf = ${$f->ctx}{leftover}.$buf if defined(${$f->ctx}{leftover});
> (prepend f->ctx->leftover onto buf)
> 
> and anything leftover that doesn't include a full HTML tag goes to
> 
> ${$f->ctx}{leftover} = $buf || undef;

Define "a full HTML tag".

As in, for instance
	<img
	src = "arrow.gif"
	alt = " --> "
	>

The point being, it's not a trivial task (and that's without
putting things like the above in a comment or cdata section
where its semantics are completely different, etc).

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/

Re: server side includes

Posted by Issac Goldstand <ma...@beamartyr.net>.

Nick Kew wrote:
> On Mon, 09 Apr 2007 23:31:32 +0300
> Issac Goldstand <ma...@beamartyr.net> wrote:
> 
>> You'd probably just use output filters to parse the output stream and
>> add content according to what you find...  I don't have a C example,
>> but do have a Perl example (string parsing is just soooo much more
>> trivial with Perl than C) at
>> http://search.cpan.org/src/ISAAC/Apache-UploadMeter-0.9915/lib/Apache/UploadMeter.pm
>> (search for "### Output filters").  One replaces a "custom" tag, and
>> one simply includes HTML at the beginning of <head>
> 
> You can't just do that in any language unless you make a BIG performance
> sacrifice.  The basic problem is that "<head>", or any other pattern of
> more than one byte, might be split over more than one call to your
> filter function.  Techniques for dealing with that are of course
> described in my book.

I do consider that.  After reading the buckets (And before analyzing
them), I do

$buf = ${$f->ctx}{leftover}.$buf if defined(${$f->ctx}{leftover});
(prepend f->ctx->leftover onto buf)

and anything leftover that doesn't include a full HTML tag goes to

${$f->ctx}{leftover} = $buf || undef;

Of course, if we had a big resource with no HTML, we'd be in pretty big
trouble as everything would go into leftover, but this particular module
doesn't, in general, need to deal with that particular problem - at
least not where this filter might be injected.

> 
> 
>> Sam Carleton wrote:
>>> As I sit here on hold, I started thinking about my little apache
>>> module again.  I recall reading in "Writing Apache Modules in Perl
>>> and C" how with Perl it is quick and easy to create a server side
> 
> That book is very, very old.  Apache 1.x didn't have filters, so
> SSI was altogether more primitive.
> 
>>> include handler, I think that is what it was called.  How would one
>>> go about doing that in a C module?  Are there any examples?
> 
> ... and I don't know what it refers to as an SSI handler (I expect
> a primitive mod_include alternative).
> 

It is exactly that, IIRC.  It's been a few years since I read the eagle
book.

  Issac

Re: server side includes

Posted by Nick Kew <ni...@webthing.com>.
On Mon, 09 Apr 2007 23:31:32 +0300
Issac Goldstand <ma...@beamartyr.net> wrote:

> You'd probably just use output filters to parse the output stream and
> add content according to what you find...  I don't have a C example,
> but do have a Perl example (string parsing is just soooo much more
> trivial with Perl than C) at
> http://search.cpan.org/src/ISAAC/Apache-UploadMeter-0.9915/lib/Apache/UploadMeter.pm
> (search for "### Output filters").  One replaces a "custom" tag, and
> one simply includes HTML at the beginning of <head>

You can't just do that in any language unless you make a BIG performance
sacrifice.  The basic problem is that "<head>", or any other pattern of
more than one byte, might be split over more than one call to your
filter function.  Techniques for dealing with that are of course
described in my book.


> Sam Carleton wrote:
> > As I sit here on hold, I started thinking about my little apache
> > module again.  I recall reading in "Writing Apache Modules in Perl
> > and C" how with Perl it is quick and easy to create a server side

That book is very, very old.  Apache 1.x didn't have filters, so
SSI was altogether more primitive.

> > include handler, I think that is what it was called.  How would one
> > go about doing that in a C module?  Are there any examples?

... and I don't know what it refers to as an SSI handler (I expect
a primitive mod_include alternative).

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/

Re: server side includes

Posted by Issac Goldstand <ma...@beamartyr.net>.
You'd probably just use output filters to parse the output stream and
add content according to what you find...  I don't have a C example, but
do have a Perl example (string parsing is just soooo much more trivial
with Perl than C) at
http://search.cpan.org/src/ISAAC/Apache-UploadMeter-0.9915/lib/Apache/UploadMeter.pm
(search for "### Output filters").  One replaces a "custom" tag, and one
simply includes HTML at the beginning of <head>

  Issac

Sam Carleton wrote:
> As I sit here on hold, I started thinking about my little apache
> module again.  I recall reading in "Writing Apache Modules in Perl and
> C" how with Perl it is quick and easy to create a server side include
> handler, I think that is what it was called.  How would one go about
> doing that in a C module?  Are there any examples?
> 
> Sam